Results 1 to 11 of 11
  1. #1
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Red face Comparing values within an Object

    I have am having a problem with my assignment and of course the teacher is no help at all.

    Specifications:
    Include the following data members (others may be necessary):
    feet:Integer
    inches:Integer
    Create both a default and a parameterized constructor
    Implement the following methods (you may want others also):
    add(d: Distance): Distance
    sub(d: Distance): Distance
    equals()
    toString()
    Create a test application to demonstrate the capabilities of the Distance class

    I seem to be having a problem trying to compare the objects (Distance) to one another. Any help would be appreciated, and yes I am a Java noob :o

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Posting an assignment does little to let us know what you have done, where you are, what the problem is, etc...post code (use the code tags), a defined question, compile errors, exceptions, misbehavior, etc...suggested reading: How To Ask Questions The Smart Way

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    What do you mean by "compare"? Using an equals method or a compareTo method. In both cases you would need to have a Distance parameter. Then inside the method write code to make a comparison between the 2 objects.

  4. #4
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class Distance
    {
        private int feet;
        private int inches;
    
        public Distance(int ft, int in)
        {
            feet = ft;
    		  inches = in;
        }
    	
    	public void setInches(int dInches) 
    	{
    		this.inches = dInches;
    	}
    
    	public int getInches() 
    	{
    		return inches;
    	}
    
    	public void setFeet(int dFeet) 
    	{
    		this.feet = dFeet;
    	}
    
    	public int getFeet() 
    	{
    		return feet;
    	}
    		
    	@Override 
    	
    		public String toString()
    		{
    			return String.format("%-23d %-26s %-15s %3.1f \n",
    			inches, feet);
    		}
    }
    Java Code:
    import java.util.Scanner; 
    
    public class DistanceOptions
    {
    	Distance d1 = new Distance( 4, 6);
    	Distance d2 = new Distance( 4, 6);
    
    	public static void distanceMenu()
    	 {
            System.out.println(
    		  "\n Please choose from the following menu:\n"
    		 	+"---------------------------------------"
                    +"1. Add Distances\n"
                    +"2. Subtract Distances\n"
                    +"3. Convert Distances\n"
                    +"4. Display Distances\n"
                    +"5. Exit"
    		+"\n--------------------------------------\n" );
                System.out.print("Your choice is: ");
    	 }
    	 
    	 public static Distance addDistance()
    	{
    		
       }
    	
    	public boolean equals( d1, d2 )
    	{
    		
    		if (d2.inches == d1.inches &&
    		    d2.feet == d1.feet)
    		{
    			return true;
    		}
    	
    		else
    		{ 
    			return false;
    		}
    	}
    }
    Im trying to figure out how to compare the values (inches and feet) of both objects to one another. I apologize for the lack of information before. I am new to forums. Thanks

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why do you have equals in another class? I think the idea is to override the Objects .equals() method.

    You can have the menu for choices printed in a main method and only use one class with one main method. You also need to create a default(no-arg) constructor.

    You can create equals in a second class but it's a sloppy way to approach this. However you reference a private variable from outside the class.

    Should be:
    Java Code:
    public boolean equals( d1, d2 )
    	{
    		
    		if (d2.getInches() == d1.getInches() &&
    		    d2.getFeet() == d1.getFeet())
    		{
    			return true;
    		}
    	
    		else
    		{ 
    			return false;
    		}
    	}
    This would work but it's not the correct way to do it.
    Last edited by sunde887; 02-09-2011 at 01:34 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The add (not addDistance) method, sub method and equals method should all be in the Distance class. They all take another Distance object as a parameter. Now you have 2 Distance objects. The one passed as a parameter and one that the parameter was passed to. Once again inside the methods you then write code that adds, subtract or compares the objects. How you do that is upto you as it is your assignment not ours.

  7. #7
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    So all my methods(add, sub, and equals) should be in the Distance class. So it should look more like this?

    Java Code:
    import java.util.Scanner;
    
    public class Distance
    {
        private int feet;
        private int inches;
    
        public Distance(int ft, int in)
        {
            feet = ft;
    		  inches = in;
        }
    	
    	public void setInches(int dInches) 
    	{
    		this.inches = dInches;
    	}
    
    	public int getInches() 
    	{
    		return inches;
    	}
    
    	public void setFeet(int dFeet) 
    	{
    		this.feet = dFeet;
    	}
    
    	public int getFeet() 
    	{
    		return feet;
    	}
    		
    	@Override 
    	
    		public String toString()
    		{
    			return String.format("%-23d %-26s %-15s %3.1f \n",
    			inches, feet);
    		}
    	
    	public boolean equals( Distance d )
    	{
    		
    		if (this.inches == d.inches &&
    		    this.feet == d.feet)
    		{
    			return true;
    		}
    	
    		else
    		{ 
    			return false;
    		}
    	}
    }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Read your instructions!

    The teacher is not trying to trick you. They usually give precise clear instructions that all you need to do is follow step by step.

    "Create both a default and a parameterized constructor
    Implement the following methods (you may want others also):
    add(d: Distance): Distance
    sub(d: Distance): Distance"

    Have you done these?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That sounds like it's correct, however, the only way for you to be entirely sure is to add a main method, create a few distance objects, and call all possible methods on them. Also, like junky said, do not forget a default(no-arg) constructor. Make it create a distance object with a default feet and inches(maybe 1 foot, 3 inches), test methods on it as well in main. Then also test your setter and getter methods.

  10. #10
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Since the methods aren't static, won't I have a problem when I try to call them?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Whats your thought process on this? Try writing the main how you think regardless of with static methods or not and post that. Also, can you post your newly designed add, sub, etc code?

    static methods don't require you to have an instance of some object created, these methods assume you have a distance object created, you apply the method to the created object.

Similar Threads

  1. Comparing values in the table with the value the user keyed in
    By pancakeleh in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-06-2011, 01:31 AM
  2. Comparing values problems
    By mari.beka in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 09:15 PM
  3. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  4. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM
  5. Replies: 0
    Last Post: 11-21-2008, 08:17 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
  •