-3
package com.company;

import java.util.Scanner;

public class javaCalculator {


public static void main(String[] args) {
    setNumbers(x,y);//---->>>>Problem
    addNumbers(x,y);//---->>>Problem
    multiplyNumbers(x,y);//---->>>Problem

}



public static double[] setNumbers(double x, double y) {

    Scanner set = new Scanner(System.in);

    double[] inputs = new double[2];
    System.out.println("Enter First Number");

    x = inputs [0] = set.nextDouble();
    System.out.println("Enter Second Number");
    y =  inputs [1] = set.nextDouble();
    return inputs;

}


 public static double addNumbers(double x, double y){

   double addition = x+y;
   System.out.println(addition);
    return addition;
 }

public static double multiplyNumbers(double x, double y){

    double multiply = x+y;
    System.out.println(multiply);
    return multiply;
}

}

The problem is that it says x and y are not initialised.

I don't know how to do that, i declared them in the setNumbers and want to use those for addition and multiplication.

If i declare them again in main then x and y will have values 0 and that wont help me.

THANK YOU.

Also i am new to java. Can someone explain why my software intelliJ also says to use

   static//--->>in all the name of the methods

in English please, i wont understand if you make it sound complicated.

once again thank you.

ElecLearn
  • 27
  • 9

2 Answers2

0

The program starts execution in the main method. The first statement that Java executes is

setNumbers(x,y);

at that point you didn't define any x or y variable. Thus the error.

Note that method(x, y) means that you want to call a method and give it additional information, two variables, on the way. So you need to provide such variables like

double x = 1;
double y = 2;
setNumbers(x, y);

then the method will be called with those parameters. However, your setNumbers method doesn't seem to use the variables in any way, you just try to overwrite them:

x = inputs [0] = set.nextDouble();
y =  inputs [1] = set.nextDouble();

Which would have no effect on the program since x and y inside the method are not connected anymore to the scope outside of the world, Java is pass by value.

If you want the method to assign two variables and then use them later you should call it without arguments and then return the values:

// In main method
double[] numbers = setNumbers();
double x = numbers[0];
double y = numbers[1];

// setNumbers method
public static double[] setNumbers() {
    double[] input = new double[2];
    input[0] = ...
    input[1] = ...

    return input;
}

Next, your addNumbers and multipleNumbers method have a return type, why? You don't use it, then you don't need it. If you want to use it you should not throw the result away like

double resultAdd = addNumbers(x, y);
double resultMult = multiplyNumbers(x, y);

System.out.println("Result of x + y = " + resultAdd);
System.out.println("Result of x * y = " + resultMult);

Can someone explain why my software intelliJ also says to use static in all the name of the methods?

Your program execution starts in the main method, a static method. At that point no objects are created. static means something is not bound to a specific object instance. If you want to be able to call non-static methods you would first need to create object instances. Imagine a class like

public class Human() {
    public void drink() { ... }
    public void eat() { ... }
}

If you want to be able to call drink or eat you first need to create some Human instance like

Human john = new Human();
Human abigail = new Human();

john.eat();
abigail.drink();

The methods are specific to the instance. So john may still be thirsty since we called drink only on abigail. A static method is not specific to an instance. If we add the following method

public class Human {
    public static void getIQ() { ... }
}

It may only be able to return the average IQ of a human, not the IQ of john. The method does not know anything about instances like john and we could call it even if we didn't yet create instances like john. That is also why we should indicate calling of a static method like

Human.getIQ();

and not as

john.getIQ();

Unfortunately, the second variant is possible and does compile. But it confuses since the call is not executed on john.

In your code example, since you didn't created any instances of classes, you need static methods to call them.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • `You can only assign one variable at a time.` This is incorrect. `int a = 3; int b = a = 4;` is completely valid, and will set both `a` and `b` to 4. – Matt Clark Mar 04 '18 at 15:44
0

In main method you are calling some methods and passing x and y, You have not declared what x and why means.

You should declare the variables first then use them other wise for compiler 'x' and 'y' doesn't mean anything until you declare their type.

In programming we declare variable and assign default values to them e.g

double x = 0.0;
double y = 0.0;

In your code just assign default values as mentioned above, then scanner input would then be assigned to them afterwards.

Hope you got it.

fatimasajjad
  • 605
  • 8
  • 16
  • if x and y are 0 the setNumbers method changes then but these values dont get passed onto the addition method or multiplication method and gives output of 0 which is wrong. this was my problem. – ElecLearn Mar 04 '18 at 17:28
  • buti think someone has answered my question now. – ElecLearn Mar 04 '18 at 17:28