Originally Posted by Hallowed
Why to override equals.
Because the Object class has a method called equals and since Object is the parent class, directly or indirectly, of all other java classes, your class already has by default a method called equals, but it looks at memory location only. You need to override it...otherwise it'll probably return false all the time.
The method in Object has the format
public boolean equals (Object obj)
Object also has a method called toString() that needs to be overridden.
I cannot explain what toString() does as it can do many things.
It returns a String. I know that is true every time.
Distance add(Distance d)
// No, that is calling a method within itself. While that might work sometimes in other circumstances, it's not
// a good idea here.
// as the variable feet is private...you can't get to variable feet in other Distance object. At least
// I'm 99.9% sure you can't.
int x = this.feet + d.getFeet();
int y = this.inches + d.getInches();
Distance temp = new Distance(x,y);
Also, I'd recommend creating two different Distance objects instead of having to foot and two inches variables.
Also, have your main ask for all the inputs.
If you try to run your class without the main method..it'll say
"Exception in class Distance
NoSuchMethodError main "
or something like that.
You get them in main and pass them as parameters to methods in your Distance class. You don't get them directly in distance class.
I would have a Distance constructor like this:
public Distance (int ft, int in)
Then in main you might do this to add two distances
System.out.println("Enter feet for distance 1");
int f1 = console.nextInt();
System.out.println("Enter inches for distance 1");
int in1 = console.nextInt();
System.out.println("Enter feet for distance 2");
int f2 = console.nextInt();
System.out.println("Enter inches for distance 2");
int in2 = console.nextInt();
Distance d1 = new Distance(f1, in1);
Distance d2 = new Distance(f2,in2);
Distance d3 = d1.add(d2);