DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    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)
    {
       Distance d2=d2.add (new Distance(param5, 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;
    	}
    	public void add (Distance distance)
    	{
    		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
    																//add method
    	{
    	 	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 + "\"";
    	}
    }
    please help.

  2. #2
    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.

Similar Threads

  1. soft drink Vending machine program
    By divagoddess in forum C++
    Replies: 5
    Last Post: 08-14-2009, 03:12 PM
  2. Help with Server program
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 08:18 AM
  3. Connecting to a Server Program HELP
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 07:58 AM
  4. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  5. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links