-1

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)

Norruas
  • 61
  • 1
  • 9
  • You are mixing your type `Int` a lot with the primitive type `int`. Maybe it would help to clarify things if you renamed your class to something more obviously different? – vlumi Jun 24 '19 at 13:17
  • The class name have to be Int according to the instructions unfournently – Norruas Jun 24 '19 at 13:20

3 Answers3

1

I assume your Int class has some member i holding the value of the respective instances of Int. This member can be accessed as this.i in any class method of Int.

Additionally in your main method you call plus with i2 as argument. i2 is an instance of Int but your method signature asks for int (the primitive).

And last but not least you want to access instance information of Int that is you probably don’t want to make your method static but want an instance method.

Given all the above your method could look like this:

public int plus(Int i2) {
  int sum = this.i + i2.i;
  return sum;
}
dpr
  • 10,591
  • 3
  • 41
  • 71
1

i1 and i2 are objects of the class Int, so you cant sum them like this. I suggest you to add an attribute "value" in the Int class, assign the value in the constructor and then sum this value in your plus method

Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
1

To achieve the syntax of Int sum = i1.plus(i2), you need a method that:

  • is an instance (ie. non-static) method
  • takes a Int as a parameter
  • returns a Int

Specifically, plus should compute the sum of the 2 Ints and returns a new Int.

Also, you would have to override the toString method of Int so that it returns the string representation of the wrapped int when printed.

Speaking of the wrapped int, your Int class doesn't seem to have an underlying int field that stores the int that's passed into the constructor. You are currently just ignoring whatever is passed into the constructor, which doesn't seem like a good idea.

Based on the above, Int should look something like this:

class Int {
    private int wrapped;

    public Int(int wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public String toString() {
        return Integer.toString(wrapped);
    }

    public Int plus(Int other) {
        int sum = this.wrapped + other.wrapped;
        return new Int(sum);
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313