-1

I'm learning object and i'm trying diffrent things to learn that. I've writen 2 method one of them return's an int and it works as i want. But other one is tuffer bc i'm doing something wrong it gives strange numbers. Could you please help me how can i write Box calculateArea2?(I looked at getter and setter but we did not learn those things yet). Here is my code;

 public static void main(String[] args) {
    Box box1 = new Box();
    Box box2 = new Box();
    String str = JOptionPane.showInputDialog("Enter a length and width");
    Scanner input = new Scanner(str);

    box1.length = input.nextInt();
    box1.width = input.nextInt();

    int BoxsArea = calculateArea(box1); // calculate box1
    JOptionPane.showMessageDialog(null, " First Box's area is: "+BoxsArea );

    String str2 = JOptionPane.showInputDialog("Enter a length and width");
    input = new Scanner(str2);

    box2.length = input.nextInt();
    box2.width = input.nextInt();

    Box box3 = new Box();
    calculateArea2(box2); // Calculate box 2

    JOptionPane.showMessageDialog(null, " Second Box's area is: "+box3 );

}

public static int calculateArea(Box k){
    return k.length* k.width;
}


public static Box calculateArea2(Box k){
    Box c = new Box();
    c.area = c.length*c.area;
    return c;
}

}

class Box{
int length;
int width;
int area;

}

  • Why do you have a string as a parameter in the constructor of the scanner (`new Scanner(str)`)? Anyways, your question is not clear at all. – RaminS Jun 19 '16 at 00:56
  • 1
    Override the Box class's `public String toString()` method so that when you pass a Box object into the println method, it will display text that makes sense, that tells you the Box objects state (its length, width and area). – Hovercraft Full Of Eels Jun 19 '16 at 00:59
  • @Gendarme Bc i want to repeat what i know and it helps to remmember old lessons, – Lena Monika Marshall Jun 19 '16 at 01:05
  • @Hovercraft Full Of Eels Could you please explain why do i need to Ovirride toString method? Why default toString methods is not good enough to write my object, why does that method need something like this ; public String toString(){ return ""+area;} – Lena Monika Marshall Jun 19 '16 at 16:25

1 Answers1

0

c.area = c.length*c.area; should be c.area = k.length*k.area;

And, to write the object in your console logs, you should implement toString() method for Box like this:

@Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Box [length=");
            builder.append(length);
            builder.append(", width=");
            builder.append(width);
            builder.append(", area=");
            builder.append(area);
            builder.append("]");
            return builder.toString();
        }

You will be able to write the object in console using box.toString() or even with box then. Different IDEs provide the capability to generate toString() implementation looking at the class.

ritesh.garg
  • 3,725
  • 1
  • 15
  • 14