Results 1 to 9 of 9
Thread: Binary Operator Methods
 01092012, 02:54 AM #1Member
 Join Date
 Jan 2012
 Posts
 8
 Rep Power
 0
Binary Operator Methods
Java Code:// This class represents linear distance 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; yards = inYards; miles = inMiles; } public int getFeet() { return feet; } public int getYards() { return yards; } public int getMiles() { return miles; } }
I am having a hard time and am probably over thinking this but, even the explaination my teacher provided does not make sense.
"The paramerterized constructor does not check to see if the input arguments are normalized. That is, it does not check to see if the value for feet is between 0 and 3 (not inclusive) or the value for yards is between 0 and 1760 (not inclusive). Normalize these values and increment as necessary. (Hint: use / and %)
Any help would be appreciated. ThanksLast edited by pbrockway2; 01092012 at 03:36 AM. Reason: code tags added
 01092012, 03:11 AM #2
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 %.
 01092012, 03:16 AM #3Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,705
 Rep Power
 13
Re: Binary Operator Methods
BB Code List  Java Programming Forum for the code tags.
 01092012, 03:31 AM #4Member
 Join Date
 Jan 2012
 Posts
 8
 Rep Power
 0
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.
 01092012, 03:39 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,705
 Rep Power
 13
Re: Binary Operator Methods
I've added the code tags: you put [code] at the start of the code and [/code] at the end.
 01092012, 03:41 AM #6
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.
 01092012, 05:42 AM #7Member
 Join Date
 Jan 2012
 Posts
 8
 Rep Power
 0
Re: Binary Operator Methods
Java 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; } }
 01092012, 05:54 AM #8
Re: Binary Operator Methods
Ok, without a computer, using only paper and pen, does this do what you think it should?
Java Code:public Distance(int inFeet, int inYards, int inMiles) { feet = inFeet % 3; yards = inYards / 3; miles = inMiles; }
 01092012, 05:59 AM #9Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,705
 Rep Power
 13
Re: Binary Operator Methods
Could you please tell me if this is what I am looking for?
Java 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()); }

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...
Similar Threads

Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04162012, 11:00 PM 
Convert integer to binary then each binary character to boolean
By davidfla90 in forum New To JavaReplies: 1Last Post: 11012011, 06:35 PM 
What Type is a Binary Operator?
By TheGame in forum New To JavaReplies: 5Last Post: 10132010, 04:04 AM 
"Best" implementation of math operator methods?
By rgrant222 in forum New To JavaReplies: 2Last Post: 09012010, 07:47 AM 
Binary Tree Deletion & Balance Methods
By sev51 in forum New To JavaReplies: 1Last Post: 01272009, 05:26 PM
Bookmarks