Results 1 to 9 of 9
Thread: Binary Operator Methods
- 01-09-2012, 01:54 AM #1
Member
- 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; 01-09-2012 at 02:36 AM. Reason: code tags added
- 01-09-2012, 02: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 %.
- 01-09-2012, 02:16 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Binary Operator Methods
BB Code List - Java Programming Forum for the code tags.
- 01-09-2012, 02:31 AM #4
Member
- 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.
- 01-09-2012, 02:39 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Binary Operator Methods
I've added the code tags: you put [code] at the start of the code and [/code] at the end.
- 01-09-2012, 02: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.
- 01-09-2012, 04:42 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Re: Binary Operator Methods
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?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; } }
- 01-09-2012, 04:54 AM #8
Re: Binary Operator Methods
Ok, without a computer, using only paper and pen, does this do what you think it should?
For instance, if you input numbers into the params, what would you end up with?Java Code:public Distance(int inFeet, int inYards, int inMiles) { feet = inFeet % 3; yards = inYards / 3; miles = inMiles; }
- 01-09-2012, 04:59 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Binary Operator Methods
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:Could you please tell me if this is what I am looking for?
As you update the code, run the resulting program and check that you're getting the right answer.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: 04-16-2012, 11:00 PM -
Convert integer to binary then each binary character to boolean
By davidfla90 in forum New To JavaReplies: 1Last Post: 11-01-2011, 05:35 PM -
What Type is a Binary Operator?
By TheGame in forum New To JavaReplies: 5Last Post: 10-13-2010, 04:04 AM -
"Best" implementation of math operator methods?
By rgrant222 in forum New To JavaReplies: 2Last Post: 09-01-2010, 07:47 AM -
Binary Tree Deletion & Balance Methods
By sev51 in forum New To JavaReplies: 1Last Post: 01-27-2009, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks