Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: newb help..

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default newb help..

    First off thanks so much to anyone that helps!

    So i received this assignment except I don't understand the difference between a default constructor and a parametrized constructor? and I have to use certain methods but I don't understand what my teacher means. He says I have to use these methods:
    1. add(distance)istance
    2. sub(distance)istance
    3. equals()
    4. toString()
    I know that a toString is used to help format a String so it can print certain variables or whatever in a certain way/format. But i don't understand what to do for 1 and 2 in regards to parametrized constructors and and default.

    I'm basically just suppose to take distances and add them subtract them etc. I just don't know how to create the methods exactly.. in different classes or what?
    __________________________________________________ __________

    * Include the following: (or more)
    1. feet:Integer
    2. inches:Integer
    * Create both a default and a parameterized constructor
    * Implement the following methods (or more):
    1. add(distance)istance // <-- the smiley face is suppose to be (d : Distance) without the space. =S
    2. sub(distance)istance
    3. equals()
    4. toString()
    * application to demonstrate the capabilities of the Distance class

    __________________________________________________ __________


    I also took these notes, and I know I'm suppose to use them somehow or my program will be wrong, but trying to make sense of it was hard so i wrote down what I could. They are just kind in random order =/

    I also think that I have to use that @override thingy listed below so that when I compare the distances that I'm guessing are suppose to be stored in different classes (or methods?) somehow, so that it doesn't look at the reference id for the object? it looks at the actual integers somehow? I dunno I'm so confused @_@'

    ___________________________________________
    Distance d1 = new Distance (4' 6")

    Distance d2 = new Distance (4' 6")

    d1 == d2 <=== reference to objects. returns false.

    if d1.get inch = d2.getinch() &



    d1.equals(d2)


    ...println(d1);
    4' 6" <--- appears via a toString method. || ⌂



    __________________________________________________ _________

    @override
    public boolean equals (Distance d)

    if (obj(<--refrence variable) instanceof Distance (<--className)
    {
    Distance d = (Distance) obj;
    if (this.inches == d.inches &&
    this.feet == d.feet)
    return true;
    }

    else return false;

    ________________________________________
    distance d3 = d1.add(d2); <--with right return type. <-returns reference to a distance obj.
    //How to return a reference to a distance object.
    Distance d4 = d2 sub(d1)
    Last edited by Hallowed; February 5th, 2011 at 07:03 PM.


  2. #2
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: newb help..

    I'm not completely useless though! I did make a program that actually works, and hopefully has some algorithms that can still be used once I figure out how to make the methods and parametrized stuffs!


    import java.util.*;
     
    public class distance
    {
    	public static int inch1;
    	public static int foot1;
    	public static int inch2;
    	public static int foot2;
     
    	public static void main(String[] args)
    	{
    		int i1 = 0; // variable for computing.
    		int i2 = 0; // variable for computing.
    		int f1 = 0; // variable for computing.
    		int f2 = 0; // variable for computing.
    		int x = 0; // variable for computing.
    		int y = 0; // variable for computing.
    		int z = 0; // variable for computing.
     
    		Scanner input = new Scanner(System.in); // ask for user response.
     
    		System.out
    				.println("This program will compute the distance of two distances for you.");
     
    		System.out.println("How many feet is the first distance?");
    		foot1 = input.nextInt(); // get first distance foot
     
    		System.out.println("How many inches is the first distance?");
    		inch1 = input.nextInt(); // get first distance inch
     
    		if (inch1 > 11)
    		{
    			i1 = inch1 / 12;
    			foot1 = foot1 + i1;
    			inch1 = inch1 - i1 * 12;
     
    		}
     
    		System.out.println("How many feet is the second distance?");
    		foot2 = input.nextInt(); // get second distance foot
     
    		System.out.println("How many inches is the second distance?");
    		inch2 = input.nextInt(); // get second distance inch
     
    		if (inch2 > 11)
    		{
    			i1 = inch2 / 12;
    			foot2 = foot2 + i1;
    			inch2 = inch2 - i1 * 12;
     
    		}
     
    		System.out.println("Which computation would you like to perform?");
    		System.out
    				.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    						+ "Add the distances. \n4. Subtract the distances. "
    						+ "\n5. Quit. \n0. View this menu."); // prints
    																// user
    																// options.
     
    		int ur1;
    		ur1 = 0;
     
    		while (ur1 != 6)
    		{
    			ur1 = input.nextInt(); // get user response for computation.
     
    			if (ur1 == 1)
    			{
    				if (foot1 == foot2 && inch1 == inch2)
    				{
    					System.out.println("They are the same!");
    				}
    				else
    				{
    					System.out.println("of course their not stupid! :)");
    				}
    			}
    			if (ur1 == 2)
    			{
    				System.out.println("The first distance was:");
    				System.out.print(foot1 + "'feet ");
    				System.out.println(inch1 + "''inches");
    				System.out.println("The second distance was:");
    				System.out.print(foot2 + "'feet ");
    				System.out.println(inch2 + "''inches");
    			}
    			if (ur1 == 3)
    			{
    				f1 = foot1 + foot2;
    				i1 = inch1 + inch2;
     
    				if (i1 > 11)
    				{
    					i2 = i1 / 12;
    					f1 = f1 + i2;
    					i1 = i1 - i2 * 12;
     
    				}
    				System.out
    						.println("The total length of these two distances is: \n"
    								+ f1 + "'feet " + i1 + "''inches");
     
    			}
    			if (ur1 == 4)
    			{
    				System.out
    						.println("1. subtract the second distance from the first distance.");
    				System.out
    						.println("2. subtract the first distance from the second distance.");
     
    				i1 = input.nextInt(); // get user response for desired
    										// computation.
     
    				if (i1 == 1)
    				{
    					f1 = foot1;
    					i1 = inch1;
    					i2 = inch2;
    					f2 = foot2;
     
    					f1 = f1 - f2;
    					i1 = i1 - i2;
     
    					while (i1 < 0 && f1 > 0)
    					{
    						f1 = f1 - 1;
    						i1 = i1 + 12;
     
    					}
     
    					if (f1 > 0 || i1 > 0)
    					{
     
    						System.out.println("The subtracted distance is: " + f1
    								+ "'feet " + i1 + "''inches.");
    					}
    					else
    					{
    						System.out
    								.println("Impossible, the distance would be negative.");
    					}
    				}
    				if (i1 == 2)
    				{
    					f1 = foot1;
    					i1 = inch1;
    					i2 = inch2;
    					f2 = foot2;
     
    					f1 = f2 - f1;
    					i1 = i2 - i1;
     
    					while (i1 < 0 && f1 > 0)
    					{
    						f1 = f1 - 1;
    						i1 = i1 + 12;
     
    					}
     
    					if (f1 > 0 || i1 > 0)
    					{
     
    						System.out.println("The subtracted distance is: " + f1
    								+ "'feet " + i1 + "''inches.");
    					}
    					else
    					{
    						System.out
    								.println("Impossible, the distance would be negative.");
    					}
    				}
    			}
     
    			if (ur1 == 5)
    			{
    				System.out.println("Good Bye!");
    				System.exit(0);
    				System.out.println("Good Bye!!!");
    			}
    			if (ur1 == 0)
    			{
    				System.out.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    						+ "Add the distances. \n4. Subtract the distances. "
    						+ "\n5. Quit. \n0. View this menu.");
    			}
    			System.out.println("Now what? Enter 0 to view the menu.");
    		}
     
    	}
    }

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: newb help..

    A default constructor usually is made automatically I think.

    It usually is used to set it to default values whereas the parameter one sets it to the values of the parameters.

     
    public boolean equals(Object obj)
    {
    if (obj instanceof Distance)
    {
    Distance d = (Distance) obj;
     
    // you will net to make get and set methods instead of calling variables as it'll make a compiler error 
    if (this.getInches() == d.getInches() &&
    this.getFeet() == d.getFeet())
    return true;
    }
    return false;
    }

    // use similar way to create get and set methods for feet
    // remember to create variables inches and feet (not in main...before constructor)
     
    private int inches;
    private int feet;
    // default constructor 
    public Distance()
    {
    setInches(defaultValue);
    setFeet(defaultValue);
    }
    // parameterized  constructor 
    public Distance(int inches, int feet)
    {
    setInches(inches);
    setFeet(feet);
    }
     
    public void setInches(int inches)
    {
    this.inches = inches;
    }
     
    public int getInches()
    {
    return inches;
    }

     
     
    public String toString()
    {
    // what toString() does varies by class but it usually  involves
    // calling a bunch of get methods and put them together as a String then return the String
     
    // for example
    String str = "Inches are: " + getInches() + ". Feet are: " + getFeet() + ".";
    return str;
    }

    That one overrides Object's toString() which returns hash code and some other weird stuff.

    You can also overload that method instead of overriding it(above) but passing it a String as a parameter and stuff like that.

    I'm kind of confused on what you're doing with inches and feet.
    Last edited by javapenguin; February 7th, 2011 at 03:37 PM.

  4. The Following User Says Thank You to javapenguin For This Useful Post:

    Hallowed (February 7th, 2011)

  5. #4
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: newb help..

    Ok... so I made this, but im confused about where I could use a to string.
    and why or where i would need to override equals( )
    and is there a way that I can store my parametrized constructors into a single object? I think that's what I need to do to make it so I have to compare d1 to d2. cause I guess I'm suppose to compare methods, not integers??

    I know I'm suppose to make it look like this somehow.. cause I got these instructions in class today.
    How to add two methods:

    Distance add(Distance Obj)
    {
    d1.add(d2)

    int x = this.feet + d.feet
    int y = this.inches

    }


    Here is what I've made so far. =/

    import java.util.*;
     
    public class Distance
    {
    	Scanner input = new Scanner(System.in); // user input
     
    	// stores user input from parameterized constructors.
    	private int inch1;
    	private int foot1;
    	private int inch2;
    	private int foot2;
     
    	// variables for computing.
    	private int i1 = 0;
    	int i2 = 0;
    	int f1 = 0;
    	int f2 = 0;
     
    	// setter methods / parameterized constructors.
    	public void setd1(int f, int i)
    	{
    		foot1 = f; // store feet
    		inch1 = i; // store inches
     
    		// if more than 11 inches is input by the use this formats it correctly.
    		if (inch1 > 11)
    		{
    			i1 = inch1 / 12;
    			foot1 = foot1 + i1;
    			inch1 = inch1 - i1 * 12;
     
    		}
    	}
     
    	public void setd2(int f2, int i2)
    	{
    		foot2 = f2; // store feet
    		inch2 = i2; // store inches
     
    		// if more than 11 inches is input by the use this formats it correctly.
    		if (inch2 > 11)
    		{
    			i1 = inch2 / 12;
    			foot2 = foot2 + i1;
    			inch2 = inch2 - i1 * 12;
     
    		}
    	}
     
    	// getter methods if needed.
    	public int getfoot1()
    	{
    		return foot1;
    	}
     
    	public int getinch1()
    	{
    		return inch1;
    	}
     
    	public int getfoot2()
    	{
    		return foot2;
    	}
     
    	public int getinch2()
    	{
    		return inch2;
    	}
     
    	// adds distances.
    	public void addDistances()
    	{
    		f1 = foot1 + foot2;
    		i1 = inch1 + inch2;
     
    		// in case inches or feet aren't formated correctly.
    		if (i1 > 11)
    		{
    			i2 = i1 / 12;
    			f1 = f1 + i2;
    			i1 = i1 - i2 * 12;
     
    		}
     
    		System.out.println("The total length of these two distances is: \n"
    				+ f1 + "'feet " + i1 + "''inches");
    	}
     
    	// displays distances.
    	public void viewDistances()
    	{
    		System.out.println("The first distance was:");
    		System.out.print(foot1 + "'feet ");
    		System.out.println(inch1 + "''inches");
    		System.out.println("The second distance was:");
    		System.out.print(foot2 + "'feet ");
    		System.out.println(inch2 + "''inches");
    	}
     
    	// compares distances.
    	public void compareDistances()
    	{
    		if (foot1 == foot2 && inch1 == inch2)
    		{
    			System.out.println("They are the same!");
    		}
    		else
    		{
    			System.out.println("of course their not! :)");
    		}
    	}
     
    	// subtracts distances.
    	public void subtractDistances()
    	{
    		System.out
    				.println("1. subtract the second distance from the first distance.");
    		System.out
    				.println("2. subtract the first distance from the second distance.");
     
    		i1 = input.nextInt(); // get user response for desired
    								// computation.
     
    		// if user decides to subtract the second distance from the first
    		// distance.
    		if (i1 == 1)
    		{
    			// stores user input into other variables.
    			f1 = foot1;
    			i1 = inch1;
    			i2 = inch2;
    			f2 = foot2;
     
    			// subtracts the distance
    			f1 = f1 - f2;
    			i1 = i1 - i2;
     
    			// in case inches or feet aren't formated correctly.
    			while (i1 < 0 && f1 > 0)
    			{
    				f1 = f1 - 1;
    				i1 = i1 + 12;
     
    			}
     
    			// Displays results
    			if (f1 >= 0 && i1 >= 0)
    			{
     
    				System.out.println("The subtracted distance is: " + f1
    						+ "'feet " + i1 + "''inches.");
    			}
     
    			// In case subtracted distance results in a negative distance.
    			else
    			{
    				System.out
    						.println("Impossible, the distance would be negative.");
    			}
    		}
     
    		// same thing as above except used if user decides to subtract the first
    		// distance from the second distance.
    		if (i1 == 2)
    		{
    			f1 = foot1;
    			i1 = inch1;
    			i2 = inch2;
    			f2 = foot2;
     
    			f1 = f2 - f1;
    			i1 = i2 - i1;
     
    			while (i1 < 0 && f1 > 0)
    			{
    				f1 = f1 - 1;
    				i1 = i1 + 12;
     
    			}
     
    			if (f1 >= 0 && i1 >= 0)
    			{
     
    				System.out.println("The subtracted distance is: " + f1
    						+ "'feet " + i1 + "''inches.");
    			}
    			else
    			{
    				System.out
    						.println("Impossible, the distance would be negative.");
    			}
    		}
    	}
     
    }

    import java.util.*;
     
    public class DistanceTest
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in); // user input
     
    		Distance theDistance = new Distance();// creates an object out of class
    												// Distance.
     
    		System.out
    				.println("This program will compute the distance of two distances for you.");
     
    		// get user input for distance 1.
    		System.out.println("How many feet are in the first distance?");
    		int userF = input.nextInt();
    		System.out.println("How many inches are in the first distance?");
    		int userI = input.nextInt();
     
    		// set d1 variables
    		theDistance.setd1(userF, userI);
     
    		// get user input for distance 2.
    		System.out.println("How many feet are in the second distance?");
    		int userF2 = input.nextInt();
    		System.out.println("How many inches are in the second distance?");
    		int userI2 = input.nextInt();
     
    		// set d2 variables
    		theDistance.setd2(userF2, userI2);
     
    		// Menu for user
    		System.out.println("Which computation would you like to perform?");
    		System.out
    				.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    						+ "Add the distances. \n4. Subtract the distances. "
    						+ "\n5. Quit. \n0. View this menu."); // prints
    																// user
    																// options.
     
    		int ur1;
    		ur1 = 0;
     
    		while (ur1 != 5)
    		{
    			ur1 = input.nextInt(); // get user response for computation.
     
    			if (ur1 == 1)
    			{
     
    				theDistance.compareDistances(); // calls method from the
    												// Distance class that compares
    												// if the two distances are the
    												// same.
    			}
    			if (ur1 == 2)
    			{
    				theDistance.viewDistances(); // calls a method from the distance
    												// class that displays the
    												// distances input
    												// by the user.
    			}
    			if (ur1 == 3)
    			{
     
    				theDistance.addDistances(); // calls a method from the Distance
    											// class that adds the two distances
    											// input by the user.
     
    			}
    			if (ur1 == 4)
    			{
    				theDistance.subtractDistances(); // calls a method from the
    													// Distance class that
    													// subtracts the two
    													// distances input by the
    													// user in the users desired
    													// order.
    			}
     
    			if (ur1 == 5)
    			{
    				System.out.println("Good Bye!");
    				System.exit(0);
    				System.out.println("Good Bye!!!");
    			}
    			if (ur1 == 0)
    			{
    				System.out
    						.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    								+ "Add the distances. \n4. Subtract the distances. "
    								+ "\n5. Quit. \n0. View this menu.");
    			}
    			System.out.println("Now what? Enter 0 to view the menu.");
    		}
    	}
    }

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: newb help..

    Quote Originally Posted by Hallowed View Post
    Ok... so I made this, but im confused about where I could use a to string.
    and why or where i would need to override equals( )
    and is there a way that I can store my parametrized constructors into a single object? I think that's what I need to do to make it so I have to compare d1 to d2. cause I guess I'm suppose to compare methods, not integers??

    I know I'm suppose to make it look like this somehow.. cause I got these instructions in class today.
    How to add two methods:

    Distance add(Distance Obj)
    {
    d1.add(d2)

    int x = this.feet + d.feet
    int y = this.inches

    }


    Here is what I've made so far. =/

    import java.util.*;
     
    public class Distance
    {
    	Scanner input = new Scanner(System.in); // user input
     
    	// stores user input from parameterized constructors.
    	private int inch1;
    	private int foot1;
    	private int inch2;
    	private int foot2;
     
    	// variables for computing.
    	private int i1 = 0;
    	int i2 = 0;
    	int f1 = 0;
    	int f2 = 0;
     
    	// setter methods / parameterized constructors.
    	public void setd1(int f, int i)
    	{
    		foot1 = f; // store feet
    		inch1 = i; // store inches
     
    		// if more than 11 inches is input by the use this formats it correctly.
    		if (inch1 > 11)
    		{
    			i1 = inch1 / 12;
    			foot1 = foot1 + i1;
    			inch1 = inch1 - i1 * 12;
     
    		}
    	}
     
    	public void setd2(int f2, int i2)
    	{
    		foot2 = f2; // store feet
    		inch2 = i2; // store inches
     
    		// if more than 11 inches is input by the use this formats it correctly.
    		if (inch2 > 11)
    		{
    			i1 = inch2 / 12;
    			foot2 = foot2 + i1;
    			inch2 = inch2 - i1 * 12;
     
    		}
    	}
     
    	// getter methods if needed.
    	public int getfoot1()
    	{
    		return foot1;
    	}
     
    	public int getinch1()
    	{
    		return inch1;
    	}
     
    	public int getfoot2()
    	{
    		return foot2;
    	}
     
    	public int getinch2()
    	{
    		return inch2;
    	}
     
    	// adds distances.
    	public void addDistances()
    	{
    		f1 = foot1 + foot2;
    		i1 = inch1 + inch2;
     
    		// in case inches or feet aren't formated correctly.
    		if (i1 > 11)
    		{
    			i2 = i1 / 12;
    			f1 = f1 + i2;
    			i1 = i1 - i2 * 12;
     
    		}
     
    		System.out.println("The total length of these two distances is: \n"
    				+ f1 + "'feet " + i1 + "''inches");
    	}
     
    	// displays distances.
    	public void viewDistances()
    	{
    		System.out.println("The first distance was:");
    		System.out.print(foot1 + "'feet ");
    		System.out.println(inch1 + "''inches");
    		System.out.println("The second distance was:");
    		System.out.print(foot2 + "'feet ");
    		System.out.println(inch2 + "''inches");
    	}
     
    	// compares distances.
    	public void compareDistances()
    	{
    		if (foot1 == foot2 && inch1 == inch2)
    		{
    			System.out.println("They are the same!");
    		}
    		else
    		{
    			System.out.println("of course their not! :)");
    		}
    	}
     
    	// subtracts distances.
    	public void subtractDistances()
    	{
    		System.out
    				.println("1. subtract the second distance from the first distance.");
    		System.out
    				.println("2. subtract the first distance from the second distance.");
     
    		i1 = input.nextInt(); // get user response for desired
    								// computation.
     
    		// if user decides to subtract the second distance from the first
    		// distance.
    		if (i1 == 1)
    		{
    			// stores user input into other variables.
    			f1 = foot1;
    			i1 = inch1;
    			i2 = inch2;
    			f2 = foot2;
     
    			// subtracts the distance
    			f1 = f1 - f2;
    			i1 = i1 - i2;
     
    			// in case inches or feet aren't formated correctly.
    			while (i1 < 0 && f1 > 0)
    			{
    				f1 = f1 - 1;
    				i1 = i1 + 12;
     
    			}
     
    			// Displays results
    			if (f1 >= 0 && i1 >= 0)
    			{
     
    				System.out.println("The subtracted distance is: " + f1
    						+ "'feet " + i1 + "''inches.");
    			}
     
    			// In case subtracted distance results in a negative distance.
    			else
    			{
    				System.out
    						.println("Impossible, the distance would be negative.");
    			}
    		}
     
    		// same thing as above except used if user decides to subtract the first
    		// distance from the second distance.
    		if (i1 == 2)
    		{
    			f1 = foot1;
    			i1 = inch1;
    			i2 = inch2;
    			f2 = foot2;
     
    			f1 = f2 - f1;
    			i1 = i2 - i1;
     
    			while (i1 < 0 && f1 > 0)
    			{
    				f1 = f1 - 1;
    				i1 = i1 + 12;
     
    			}
     
    			if (f1 >= 0 && i1 >= 0)
    			{
     
    				System.out.println("The subtracted distance is: " + f1
    						+ "'feet " + i1 + "''inches.");
    			}
    			else
    			{
    				System.out
    						.println("Impossible, the distance would be negative.");
    			}
    		}
    	}
     
    }

    import java.util.*;
     
    public class DistanceTest
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in); // user input
     
    		Distance theDistance = new Distance();// creates an object out of class
    												// Distance.
     
    		System.out
    				.println("This program will compute the distance of two distances for you.");
     
    		// get user input for distance 1.
    		System.out.println("How many feet are in the first distance?");
    		int userF = input.nextInt();
    		System.out.println("How many inches are in the first distance?");
    		int userI = input.nextInt();
     
    		// set d1 variables
    		theDistance.setd1(userF, userI);
     
    		// get user input for distance 2.
    		System.out.println("How many feet are in the second distance?");
    		int userF2 = input.nextInt();
    		System.out.println("How many inches are in the second distance?");
    		int userI2 = input.nextInt();
     
    		// set d2 variables
    		theDistance.setd2(userF2, userI2);
     
    		// Menu for user
    		System.out.println("Which computation would you like to perform?");
    		System.out
    				.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    						+ "Add the distances. \n4. Subtract the distances. "
    						+ "\n5. Quit. \n0. View this menu."); // prints
    																// user
    																// options.
     
    		int ur1;
    		ur1 = 0;
     
    		while (ur1 != 5)
    		{
    			ur1 = input.nextInt(); // get user response for computation.
     
    			if (ur1 == 1)
    			{
     
    				theDistance.compareDistances(); // calls method from the
    												// Distance class that compares
    												// if the two distances are the
    												// same.
    			}
    			if (ur1 == 2)
    			{
    				theDistance.viewDistances(); // calls a method from the distance
    												// class that displays the
    												// distances input
    												// by the user.
    			}
    			if (ur1 == 3)
    			{
     
    				theDistance.addDistances(); // calls a method from the Distance
    											// class that adds the two distances
    											// input by the user.
     
    			}
    			if (ur1 == 4)
    			{
    				theDistance.subtractDistances(); // calls a method from the
    													// Distance class that
    													// subtracts the two
    													// distances input by the
    													// user in the users desired
    													// order.
    			}
     
    			if (ur1 == 5)
    			{
    				System.out.println("Good Bye!");
    				System.exit(0);
    				System.out.println("Good Bye!!!");
    			}
    			if (ur1 == 0)
    			{
    				System.out
    						.println("1. Check to see if the distances are the same. \n2. View the first and second distance. \n3. "
    								+ "Add the distances. \n4. Subtract the distances. "
    								+ "\n5. Quit. \n0. View this menu.");
    			}
    			System.out.println("Now what? Enter 0 to view the menu.");
    		}
    	}
    }
    Why to override equals.

    Because the Object class has a method called equals and since Object is the parent class, directly or indirectly, of all other java classes, your class already has by default a method called equals, but it looks at memory location only. You need to override it...otherwise it'll probably return false all the time.

    The method in Object has the format
     
    public boolean equals (Object obj)
    {
     
    }


    Object also has a method called toString() that needs to be overridden.

    I cannot explain what toString() does as it can do many things.

    It returns a String. I know that is true every time.


     
    Distance add(Distance d)
    {
    // d1.add(d2)
    // No, that is calling a method within itself.  While that might work sometimes in other circumstances, it's not
    // a good idea here.
     
    // as the variable feet is private...you can't get to variable feet in other Distance object.  At least 
    // I'm 99.9% sure you can't.  
     
    int x = this.feet + d.getFeet();
    int y = this.inches + d.getInches();
     
    Distance temp = new Distance(x,y);
    return temp;
     
    }

    Also, I'd recommend creating two different Distance objects instead of having to foot and two inches variables.

    Also, have your main ask for all the inputs.

    If you try to run your class without the main method..it'll say

    "Exception in class Distance
    NoSuchMethodError main "

    or something like that.

    You get them in main and pass them as parameters to methods in your Distance class. You don't get them directly in distance class.

    I would have a Distance constructor like this:

     
    public Distance (int ft, int in)
    {
    setFeet(ft);
    setInches(in);
    }

    Then in main you might do this to add two distances

     
    System.out.println("Enter feet for distance 1");
    int f1 = console.nextInt();
     
    System.out.println("Enter inches for distance 1");
    int in1 = console.nextInt();
     
    System.out.println("Enter feet for distance 2");
    int f2 = console.nextInt();
     
    System.out.println("Enter inches for distance 2");
     
    int in2 = console.nextInt();
     
    Distance d1 = new Distance(f1, in1);
    Distance d2 = new Distance(f2,in2);
     
    Distance d3 = d1.add(d2);
    Last edited by javapenguin; February 8th, 2011 at 04:31 PM.

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    Hallowed (February 8th, 2011)

Similar Threads

  1. Replies: 2
    Last Post: January 7th, 2011, 08:10 PM
  2. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  3. help a newb
    By WahLao in forum Java Theory & Questions
    Replies: 1
    Last Post: January 4th, 2010, 01:13 PM