this is a homework problem, and I am having trouble understanding how I can create a .add() method in a class Distance.
class Distance has two integer instance variables:
private int feet;
private int inches;
It has a no argument constructor that initializes a Distance with zero feet and zero inches.
It has a two argument constructor that accepts two positive integers for feet and inches, and throws and exception if they are negative or if inches is greater than 11.
It also has get/set methods for the instance variables. My get/set methods:
public void setFeet(int f){
feet = f;
}
public void setInches(int in){
inches = in;
}
public int getFeet(){
return feet;
}
public int getInches(){
return inches;
}
My first question to anyone willing to answer: Is this how I am supposed to set up these get/set methods? I am unsure of myself.
My second problem lies with creating a method add() that adds another Distance object to itself. That is,
w1 = new Distance(4,9);
w2 = new Distance(3,6);
w1.add(w2); //w1 becomes 8 feet, 3 inches.
So far, I have this:
public int add(Distance d) {
int df = d.getFeet();
int di = d.getInches();
int a = this.feet;
int b = this.inches;
int c = a.getFeet();
int d = b.getInches();
int sumInches, sumFeet, temp;
sumInches =di + d;
sumFeet = df + c;
if (sumInches>11) {
temp = sumInches-11;
sumFeet = sumFeet+1;
sumInches = temp;
}
this.feet = sumFeet;
this.inches = sumInches;
}
But I am unsure if this would even compile (I don't have access to a computer where I can install a compiler right now). Can someone examine this and explain to me why this might be wrong?