I want to create a method called plus that adds the variable i1 to i2. However I don't understand how I call on my method plus with the variabel i1 according to the given code below (code snippet 2). My initial thought was to do it like this (but can't because of the instructions):
public class Int{
public static voind main(String[] args){
Int i1 = new Int(5);
Int i2 = new Int(2);
Int sum = plus(i1, i2);
}
public static int plus(int i1, int i2) {
int sum = i1 + i2;
return sum;
}
System.out.prinln("Sum of i1 and i2 is " + sum);
}
I tried creating a function plus(int i1, int i2) in the same way as the code above but then I get the following error: The method plus(int, int) in the type Int is not applicable for the arguments (Int)
I then tried this:
public class Int {
public Int(int i) {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Int i1 = new Int(5); //cannot be changed to due assignment instructions
Int i2 = new Int(2); //cannot be changed to due assignment instructions
Int sum = i1.plus(i2); //cannot be changed to due assignment instructions
}
public static int plus(int i2) {
int sum = i1 + i2;
return sum;
}
}
But get the error In method plus: i1 cannot be resolved to a variable
Desired output: Sum of i1 and i2 is Int(7)