Distance Program

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Aug 2007
Posts
11

## Distance Program

Ok, the instructor has give explicit instructions to work with this code and make it work. I have most of it working, but I am hung up on the

Code:
```public void add1 (int param5, int param6)
{
}```
portion of the code. I am attaching the class below.

Code:
```//*********************************************************************************
//Distance.java
//
//Chapter 4 project
//*********************************************************************************
public class Distance
{
private int feet;
private int inches;

public Distance()
{
feet = 0;
inches = 0;
}
public Distance(int param1, int param2)
{
feet = param1;
inches = param2;
}
public Distance(Distance d)
{
feet = d.getFeet();
inches = d.getInches();
}
public int getFeet()
{
return feet;
}
public int getInches()
{
return inches;
}
public void setFeet(int param3)
{
feet = param3;
}
public void setInches(int param4)
{
inches = param4;
}
private int toInches()
{
return feet * 12 + inches;
}
{
int totalInches = toInches() + distance.toInches();
feet = totalInches / 12;
inches = totalInches % 12;
}

//--------------------------------------------------------------------------
//method takes 2 integer parameters representing a distance in feet and
//inches.

//--------------------------------------------------------------------------
public void add1 (int param5, int param6)					//creates local Distance object & passes it to the other
{
Distance d2 = d2.add new Distance(param5, param6);	//adds parameter's distance to the current
//distance ensuring that the result is
//correct & that inches is between 0-11
}
public String toString()
{
return feet + "\'" + inches + "\"";
}
}```

2. Registered User
Join Date
Oct 2007
Posts
5
Hi,

Method add should be defined with the return type Distance.
and return the Distance object within the add method definition.
Make the following changes in ur code.

Code:
```public Distance  add (Distance distance)
{
int totalInches = toInches() + distance.toInches();
Distance d=new Distance();
d.feet = totalInches / 12;
d.inches = totalInches % 12;
return d;

}```
Last edited by aksnathan; 10-25-2007 at 07:09 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise