Re: Binary Operator Methods
Could you clean up your code with code tags please?
I think what your prof. is saying is this: If I were to input a distance of 9 feet, what I should instead store is 3 yards. Another example would be if I stored 1 mile, 13 feet, what I would instead store would be 1 mile, 4 yards, 1 foot. You can use / and % to both determine how many increments you need to use, and to get the remainder after. Try this example, and see if you can make it work with your app:
Input:
13 feet
0 yards
0 miles
Convert this to yards AND feet instead of just feet using / and %.
Re: Binary Operator Methods
Re: Binary Operator Methods
I am not sure how to clean up your code with code tags. I am new to this forum.
Also I don't think I am understanding where I am supposed to normalize these values and increment as necessary. I think I understand what / and % mean but, am not sure how to use them in the program.
Re: Binary Operator Methods
I've added the code tags: you put [code] at the start of the code and [/code] at the end.
Re: Binary Operator Methods
[code][/code] -- wrap your code in those, pbrockway2 gave you a link the a reference for tags here. It will formate your code nicely for us to see.
So if you know what / and % do, then the answer should be obvious, but let me try to push you along a little.
If we want to find out how many yards are contained in a certain number of feet, we could divide right?
37 / 3 = 12
Great, its 12 yards. But what about the remainder? The remainder would be written in feet, but how can we figure that out?
Enter the modulus operator.
37 % 3 = 1
What do we have? 12 yards and 1 foot. You could do this successively starting with mile, and save the values as you go into the corresponding fields. My old CS prof gave me the same problem in my first programming class years ago, but he used a change drawer as an example - You want to give proper change to the customer using the largest bills/coins possible. If the price was $737.31, how many hundreds, fifties, twenties, tens, fives, ones, .25, .1, and 1 cent coins does the person get back? This is the same idea.
As far as where to do it -- it should probably happen in the constructor, but the logic doesn't have to be there. You could write a method to do the work, and call the method from the constructor.
Re: Binary Operator Methods
Code:
public class Distance
{
int feet;
int yards;
int miles;
// Constructors
public Distance()
{
feet = 0;
yards = 0;
miles = 0;
}
public Distance(int inFeet, int inYards, int inMiles)
{
feet = inFeet % 3;
yards = inYards / 3;
miles = inMiles;
}
public int getFeet() { return feet; }
public int getYards() { return yards; }
public int getMiles() { return miles; }
}
Okay so this is what I input and I was able to compile this. Could you please tell me if this is what I am looking for?
Re: Binary Operator Methods
Ok, without a computer, using only paper and pen, does this do what you think it should?
Code:
public Distance(int inFeet, int inYards, int inMiles)
{
feet = inFeet % 3;
yards = inYards / 3;
miles = inMiles;
}
For instance, if you input numbers into the params, what would you end up with?
Re: Binary Operator Methods
Quote:
Could you please tell me if this is what I am looking for?
The way to tell is to add a small main() method that can exercise (test) the class. In particular we want to see if the new form of the constructor is doing the right thing:
Code:
// examples assume that my memory of the antedeluvian units doesn't fail me...
public static void main(String args) {
Distance toTest = new Distance(1, 2, 3);
// expected output "feet=1 yards=2 miles=3"
System.out.println("feet=" + toTest.getFeet() + " yards="
+ toTest.getYards() + " miles=" + toTest.getMiles());
toTest = new Distance(3, 41, 0);
// expected output "feet=0 yards=42 miles=0"
System.out.println("feet=" + toTest.getFeet() + " yards="
+ toTest.getYards() + " miles=" + toTest.getMiles());
toTest = new Distance(0, 1761, 0);
// expected output "feet=0 yards=1 miles=1"
System.out.println("feet=" + toTest.getFeet() + " yards="
+ toTest.getYards() + " miles=" + toTest.getMiles());
toTest = new Distance(5280, 42, 0);
// expected output "feet=0 yards=42 miles=1"
System.out.println("feet=" + toTest.getFeet() + " yards="
+ toTest.getYards() + " miles=" + toTest.getMiles());
}
As you update the code, run the resulting program and check that you're getting the right answer.
-----
One thing that does strike me is that if "big" numbers are given as arguments to the constructor I would expect some change to be made. (that's the whole point of the question). But I don't see any if statements anywhere...