Click to See Complete Forum and Search --> : absolute beginner
twinks
June 16th, 2001, 03:32 AM
Can anyone help me with this? Once I have the correct code, I am hoping to be able to work backwards from that to try and understand it.
Write a program that generates a number randomly. This number should be a float between 0 and 1000. Show the truncated value as well as the rounded value. Convert this number to an integer and show the hex and octal representation of the value. Generate another random number, an integer between 0 and 1000. Display the larger of the two numbers as well as their sum. Multiply the largest value by 2. You cannot use the " * " operator (use a shift operator).
When answering this question, you should use:
The Random class
The appropriate wrapper classes
You do not need to use different methods to write this program but you will be credited for doing so.
much appreciated
Norm
June 16th, 2001, 10:38 AM
Generally, it's better for students to write their own code and learn from their own mistakes. Also it's unfair to other students in your course if someone else writes your code. So, write your code and we'll help you get it to work.
Norm
dlorde
June 16th, 2001, 04:34 PM
Norm is right, if you try to learn to write code by working backwards from other people's code, you may teach yourself to understand the code, but it doesn't mean you understand how to write your own. Also, it's not necessarily going to be good code you're reading, and you won't always know it.
Dave
To email me remove '_spamjam' from my email address
twinks
June 17th, 2001, 02:31 AM
Hi there,
Thanks for taking the time to respond. It's much appreciated. What I've done so far seems ok but I can't seem to find a reference to how I represent the value in octal and hex. I think it either involves the "exp" or I read something about pre-pending a "0" or a 0x". I don't know how or where to add this in.
class MyRandom2 {
public static void main(String[] arguments) {
float randomNumber;
randomNumber=(float)(Math.random()*1000)+1;
System.out.println("Random number is "+ randomNumber);
short s = (short) randomNumber;
System.out.println("The short value is " + s);
int i = (int) randomNumber;
System.out.println("The integer value is " + i);
double d = (double) randomNumber;
System.out.println("The floating point literal is " + d);
}
}
any pointers in the right direction much appreciated.
regards,
Lisa
Norm
June 17th, 2001, 10:30 AM
The Integer class has methods that returns String representations in Hex and Octal: toHexString() and toOctalString(). So get the value into an Integer class object and then use the above mentioned methods to display it. Something like this:
Integer iV = new Integer(theValue);
System.out.println(iV.toHexString());
The leading 0x is for when you want to specify a hex value (vs decimal) when initializing a variable:
int iX = 0xFF0B;
Norm
Norm
June 17th, 2001, 12:31 PM
As Dave said, not all the code that is posted is good. My last posted code was wrong. I didn't read the doc for the toHexString() method. The compiler would have told me quickly enough if I'd tried to compile it. Often I let the compiler find syntax errors of that sort. I code what I think is correct and move on. Otherwise I read the doc and correct my errors.
So the toHexString() method is static, which means that it is callable without an instance of the Integer class. static methods are the same as functions in older languages. You use them to do a particular function where they do NOT need to keep a state or remember anything between calls. They are called by coding the Classname.methodname();
Also the toHexString(int) method takes an int arg.
So the correct call would be:
String hexStr = Integer.toHexString(255);
// value of hexStr is "FF"
Read the other Integer methods: decode() and valueOf(). They can be used to convert Strings of decimal, hex or octal characters to Integer objects.
Integer iX = Integer.decode("0xFF");
// value of iX is 255
Again, this code is off the top of my head and may not work. Type it in and try it.
Norm
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.