0

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?

Ishmael
  • 235
  • 3
  • 8
  • 17
  • You can initilise your variables to 0 while declaring. and your setInches(int in) method is wrong. you have to set it to the inches value and not the feet value, public void setInches(int in){ inches = in; } – Abi Mar 13 '13 at 03:01
  • @Abi That was a major typo, I was copying this from paper. Sorry about that. I made the change. Thank you for catching that. Also, what values and I initializing to 0 and where am I doing that? – Ishmael Mar 13 '13 at 03:06
  • 1
    "I don't have access to a computer where I can install a compiler right now" - I recommend you get to one. Surely it's not the best use of SO to have someone copy/paste your code into a compiler for you, much less do the rest of the debugging you haven't even gotten around to yet. – millimoose Mar 13 '13 at 03:07
  • 4
    Also: http://ideone.com – millimoose Mar 13 '13 at 03:09
  • 1
    Work through some examples with your addition algorithm. I also think you are making a mistake taking nice, meaningful expressions like d.getFeet() and using arbitrary letters instead. It may make your code shorter, but it is less readable and harder to check. – Patricia Shanahan Mar 13 '13 at 03:09
  • a.getFeet() won't compile. In this case, "a" is an int, not an instance of the Distance class. You don't need c and d anyway. – Darius X. Mar 13 '13 at 03:11

7 Answers7

1
int a = this.feet;
int b = this.inches;

Is a bit verbose...

int c = a.getFeet();
int d = b.getInches();

Won't compile, recall that primitive values (int, in our case) does not have methods.


Here's a solution, (added support for varargs, allowing infinite Distances):

public void add(Distance... distance) {
    for(Distance d : distance) {
        feet += d.getFeet();
        if(inches + d.getInches >= 12)
            feet++;
        inches += d.getInches() % 12;
    }
}

With this, you can easily add like this:

d1.add(d2, d3, d4, d5);
Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • Can you explain what the first two lines are doing? I've never seen a method accept an argument like "distance..."; and I know what a for loop does, but I've never seen a for loop with that form. What is it doing exactly? – Ishmael Mar 13 '13 at 03:20
  • 1
    _Varargs_ were added to the language in release 1.5, allowing to add infinite objects as arguments, then they are converted into an array and passed as actual argument. The _enhanced for-loop_, or commonly referred to as _for-each loop_ was also added to the language at release 1.5, giving an easy way to iterate over arrays and collections. – Mordechai Mar 13 '13 at 03:23
  • for(Distance d : distance) is read as for d in distance? Why not just have feet += distance.getFeet()? – Ishmael Mar 13 '13 at 03:34
  • 1
    *for __each__ `Distance` d in __array__ distance*. Recall, varargs are converted to array of `Distance` – Mordechai Mar 13 '13 at 03:38
  • So having distance... allows the method add to accept an indeterminate amount of arguments/paramets, for each d of type Distance in the array distance (so I can have something like w1.add(w2, w3, w4)), I add it's feet and inches to w1. I apologize if I am frustrating you, but is that what you are doing? – Ishmael Mar 13 '13 at 03:49
  • Thank you for helping me understand this step by step, and for pointing out why c=a.getFeet(); is wrong (it's obvious to me now). – Ishmael Mar 13 '13 at 03:58
  • 1
    You're welcome, I love to help anyone on SO. – Mordechai Mar 13 '13 at 03:59
  • why would the add method returns an int ? where is the return in this method. This won't compile – Joe2013 Mar 13 '13 at 04:24
1

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.

Your getters/setter look correct; the point of them is to encapsulate the member variables so that they are not "modified" accidentally (restricting access). You probably want to initialize your member variables to some "default" value - in this case, I initialized them to 0 below. Note: By default, the compiler will initialize them to 0.

private int feet = 0;
private int inches = 0;

public void setFeet(int f){
    feet = f;
}

public void setInches(int in){
    feet = in;
}

public int getFeet(){
    return feet;
}

public int getInches(){
    return inches;
}

My second problem lies with creating a method add().

About your "add" method, you probably want to change your local variables to be something more "descriptive", it will help "readability" and "maintainability" in the long run.

Since your "Distance" class already contains the member variables "feet" and "inches", there is no point of setting these values to a local variable. You can just access them directly -- along with any of the class's getters/setters.

   public int add(Distance newDistance) {

    int newDistanceFeet = newDistance.getFeet();
    int newDistanceInches = newDistance.getInches();

    int sumInches = newDistanceInches + this.getInches();
    int sumFeet = newDistanceFeet + this.getFeet();

    // Now we can check to see if any conversion are needed.. Note: 12 inches = 1 foot
    //   
    sumInches += (sumInches % 12);
    sumFeet += (sumInches / 12);

   // Now set the newly added inches/feet to the class's member variables
   //
   this.setFeet(sumFeet);
   this.setInches(sumInches);
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
1
public void setInches(int in){
    feet = in;
}

I assume that's a typo because you are assigning the in value to feet instead of inches.

public int getInches(){
    return Inches;
}

Another typo. Java is case-sensitive so "Inches" is not the same as "inches". Other than those, the getters/setters look fine.

As for the add() method... There's no reason to create those "a", "b", "c" and "d" variables

int a = this.feet;
int b = this.inches;
int c = a.getFeet();
int d = b.getInches();

You can just use this.feet and this.inches directly. It's not wrong per-se, just unnecessary and clutters the code. Also, I would highly recommend always creating meaningful variable names. "a", "b", "c" code is difficult to read. Variable names should be expressive. Also, in your algorithm, you would need to subtract 12 from the summed inches. So the add method could be written more succinctly like this:

public void add(Distance d)
{
  int newFeet = this.feet + d.feet;
  int newInches = this.inches + d.inches;
  if (newInches > 11)
  {
     newFeet++;
     newInches = newInches - 12;
  }
  this.feet = newFeet;
  this.inches = newInches;
} 

Personally, I would implement that logic by converting everything to inches and then using division and the modulus operator (%) to determine the feet and inches but I'll leave that as an exercise for you if you care.

Blake
  • 581
  • 4
  • 14
0

The basic idea is correct but you were doing some things wrong, consider instead:

public int add( Distance d ){
  feet += d.feet;
  inches += d.inches;
  if( inches >= 12 ){
    inches = inches - 12;
    feet++;
  }
}

The biggest issue i saw was

int a = this.feet;
int d = a.getFeet();

here a is an int not a Distance object hence you cant call getFeet()

Adrian
  • 495
  • 2
  • 10
0

Update your add() to :-

public int add(Distance d) {
    int df = d.getFeet();
    int di = d.getInches();
    int a = this.feet;
    int b = this.inches;

ERROR : c and d are integers and not Distance objects. You don't require c and d integers.

    int sumInches, sumFeet, temp;
    sumInches =di + b;
    sumFeet = df + a;
    if (sumInches>11) {
        temp = sumInches-11;
        sumFeet = sumFeet+1;
        sumInches = temp;
    }
    this.feet = sumFeet;
    this.inches = sumInches;
DarknessBeginsHere
  • 592
  • 1
  • 5
  • 20
0

Your problem is here,

int a = this.feet;
int b = this.inches;
int c = a.getFeet();
int d = b.getInches();

Since a and b are int variable. They do not have getFeet() and getInches() method. Change your add() method to

public int add(Distance d) {
    int df = d.getFeet();
    int di = d.getInches();
    int sumInches, sumFeet, temp;
    sumInches =di + this.inches;
    sumFeet = df + this.feet;
    if (sumInches>11) {
        temp = sumInches-11;
        sumFeet = sumFeet+1;
        sumInches = temp;
    }
    this.feet = sumFeet;
    this.inches = sumInches;
}
swemon
  • 5,840
  • 4
  • 32
  • 54
0

Since it is a homework problem, I am not providing the complete solution. Since you have attempted the add method, I will optimize it further and provide the same here with comments

public void add(Distance distance) {

        int extraFeet=0;  /*Intializing the extra Feet*/
        extraFeet = (this.inches+distance.getInches())/11; /*Calculating the additional feet*/
        if(extraFeet >0)  
            this.inches = (this.inches+distance.getInches())%11;  /*if extra feet is greater than zero thn the remainder is the inches*/
        else
            this.inches +=distance.getInches(); /*else teh remainder is the sum of the inches*/
        this.feet += (distance.getFeet()+extraFeet);


    }
Joe2013
  • 1,007
  • 1
  • 9
  • 24