Getting the sum of two double parameters - Java - java

Having a hard time figuring out the code neccessary to get the sum of the parameter.
public class SumOfTwo {
public static void main(String[] args) {
double number = sumCalc(5.2, 4.2);
System.out.println("Returned value: " + number);
}
// Method here
}
Returned value:
Any help would be greatly appreciated thank you :)
BTW. I have been trying to work on it for more than an hour now. Tried many different things and I would've continued to try but I needed to complete this by a certain time. It's not like I'm taking the help and running, I'm saving it so I can get better so next time I won't have to ask you guys because apparently you're not friendly to people trying to learn Java. Thank you Codetector.

I would recommend a close look at some intro Java docs...
But here is your functions
static double sumCalc(double a, double b) {
return a + b;
}

Related

Can't Find Symbol in my method instance?

This a very quick and i feel obvious mistake but i keep getting the CANNOT FIND SYMBOL
symbol : method print(int,int)
this would lead me to believe that i'm not giving the method the right data type parameters, however..
public class Test
{
public static void main(String[] args)
{
TestSrv srvObj = new TestSrv();
srvObj.print(0, 0);
srvObj.print(1, 1);
srvObj.print(2, 10);
}
}
and this method, what it's meant to do aside, i keep getting errors from the above code for all 3 calls to the print method? I am passing it integers on all 3 occasions?
public class TestSrv
{
public void print(int num, int count)
{
for (int i = 0; i <= count; ++i)
{
System.out.print(num + ". " + "*");
}
}
}
Your code should compile. Make sure that you declare both classes in the same package or that you import TestSrv in Test.java.
You almost certainly didn't compile TestSrv after making changes. Using an IDE such as Eclipse or IDEA will take care of much of that detail for you.
while renaming my method and class and such so that it wasn't what i originally named it(as to not confuse anyone) i actually fixed the problem that i had.. that is why this compiled for everyone xD i feel stupid! thanks again

Java Storing sequence of functions run

i have a program in java and what i want to do is somehow store all the functions that have been run whilst the program ran. but i can not seem to find anything on this matter. Also then have to find out which of the function has been ran the most amount out time.
my thought was that i could make an array, assign each function to have a variable with a name of the function and then everytime it is run return that char into the array, and print out the array at the end. But i dont know how to go about storing them in different arr[i] i's everytime same function is ran, also im not sure how i would then find the one that was ran most, any help is much appreciated.
What I'd recommend is creating a boolean for each method, and at the beginning of the method, set the boolean to true. Then create a save method using the java.io class and save the boolean name and value to the file.
EDIT
I just realized i put boolean instead of integer. Have an integer for each method and do integer++ for each method run.
AOP is great for something like this.
See https://dzone.com/articles/monitoring-performance-spring for an example that uses the Spring AOP library.
i tried a program that may help you or not. i created a Interceptor class which will print out the details you may need. You didnot mention about any frameworks, so i just gave you an example program with plain old java.
This approach also offers flexibility to print all the details at the end of program execution.
public class Test {
public void demoMethod() throws InterruptedException{
Intercept.printMethodStartTime(System.currentTimeMillis());
Intercept.printMethodName(Thread.currentThread().getStackTrace()[1].getMethodName());
Thread.sleep(5000);
Intercept.printMethodEndTime(System.currentTimeMillis());
}
public static void main(String[] args) throws InterruptedException {
new Test().demoMethod();
}
}
class Intercept{
private static Long startTime;
private static Long endTime;
public static void printMethodName(String methodName){
System.out.println("Current method name: "+methodName);
}
public static void printMethodStartTime(Long time){
startTime = time;
System.out.println("Method started at "+startTime);
}
public static void printMethodEndTime(Long time){
endTime = time;
System.out.println("Method ended at "+endTime);
printMethodRunTime();
}
public static void printMethodRunTime(){
System.out.println("Method ran for "+((endTime-startTime)/1000)+" seconds");
}
}

very simple main where I am stuck adding two numbers

I just want to write a program that adds two numbers so I wrote this
public class Mainclass {
public static void main(String[] args) {
addTwoNumbers(5, 3);
}
public static int addTwoNumbers(int a, int b){
int c;
c = a + b;
return c;}}
What is my problem?
I know in java it excepts always a main and I think that is the point at which the program executes so I wrote the other function above it so it can read from that function.
Thanks a lot.
That actually works. You are just not seeing it because you are not printing anything.
You should use System.out.println(addTwoNumbers(5,3));
Also for future reference, please use proper indentation.

Testing Fuzzy Logic [closed]

I have a problem to testing my fuzzy logic on Java programming either right or wrong.
Do you have any simple source code to test it.
May you share to me, please.
Kindly need your help.
Thank you so much.
Best Regards,
Deni Y.
The codez:
public class FuzzyLogicTester
{
public static void main(String[] args)
{
int a = 1 + 1; // \u000a\u0061\u002b\u002b;
System.out.println(a);
}
}

How to print answer of 'external' recursive function in GUI?

This is my 2nd recursive function ever (I hope!) only this time I need it to print out in a textField. It prints out "5 x 4 x 3 x 2 x 1" nothing too fancy. I have a feeling my attempt is terribly wrong since in the program its underlined a very noticeable ugly shade of red.
I'm trying to understand by researching it (not working too well) and I've yet to master the whole 'theoretical thinking' side of things, so any tips or hints would be greatly appreciated!
public class Main {
public static String fact(int n) {
if(n == 1){
return "1";
}
return n + " x " + (fact(n-1));
}
public static void main(String[] args) {
System.out.println(fact(5));
}
private void itsAButtonActionPerformed(java.awt.event.ActionEvent evt) {//button on GUI
//some other code that has no significant value to question
itsATextField.setText("" + return); //only line underlined
}
The only thing it says when hovered over it is 'illegal start of expression'
itsATextField.setText(fact(5));
would be syntactically correct.
However, it will not be a complete program with a GUI, of course.

Resources