Originally Posted by
Hallowed
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);