File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Storing Cut off Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing Cut off" Watch "Storing Cut off" New topic
Author

Storing Cut off

Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Hello everyone, I'm new here and to Java so decided to join hoping to get help with Java. I have an assignment that task me to...well I'll show the instructions and then show my current coding. I am having troubles trying to store the cutoff command the a new variable.

Write, compile, and run a Java program that: (1) uses a single line comment to
document your source code with your name and section number. This should be the
very first line of your program, (2) uses a println statement to print your name and
section number as the first line of your output, (3) using a random number generator
to produce floating point(double) number, use this number to create 4 floating point
numbers: the number cutoff at the 3rd, 4th, 5th, and 6th decimal place. (4) adjust the
random number to round at the 3rd, 4th, 5th, and 6th decimal place. (5) use an
accumulator to capture each number after it has been calculated. (6) use an
accumulator to capture the actual sum of the random numbers. (7) repeat steps 3
through 6 for 10, 100, 1000, and 10000 repetitions. (8) upon completion of each
loop, print the real sum, each of the 8 calculated/truncated.(10 Points)


Now before I show my code I just need to know. Is this going to somewhat like a main loop from 1 to 10,000 and then a nested loop inside that from 1 to 1,000 and so on?


But here is my code:



I said testing as a comment because I actually initialized the command but im not sure if Im suppose to display it. I just use testing to print on the screen to see if the commands will be displayed. Thanks if someone can help me with this.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

Tarrell Fletcher wrote:
Now before I show my code I just need to know. Is this going to somewhat like a main loop from 1 to 10,000 and then a nested loop inside that from 1 to 1,000 and so on?

I wouldn't think so. The way I read it, you can have one loop of 10,000. Then i'd put in an if or a case statement to 'catch' the sums. For example:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Hmmm, but I have to cut off and then round those same values. It calls for a lot of reps so would an if statement really be good?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Welcome to the Ranch

I suspect you are going the wrong way about this assignment. You need to write down with a pencil and paper (and a large eraser) what you are going to do.
Get the Random object to print numbers, and see what format they print in. Read about that random method in the API and see exactly what it returns. You obviously know how to truncate a number. Find methods which round numbers (or add 0.5 and truncate). I don’t think NumberFormat will truncate or round your number at all.
Make up your application from small parts.
Don’t try to get everything into the main method. Why are you using /.001 rather than * 1000? Use 0.001 rather than .001. Leave one space on each side of every binary operator.
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Yea I actually did that first. I was thinking about making all separate functions/modules or whatever you call them. I did it on paper but at the same time I went on the actual program because I don't exactly know the coding/syntax/objects/symbols or whatever you call them. I know the NumberFormat cuts off at a desired position after looking it up. Im just confused because I think he wants us to return, which I guess means to accumulate, the random number used but have it returned as cutoff number and another as the rounded value. I just can't figure out how to return those values because I can't set a new variable equal to that random number at the cutoff or rounded. But then its the whole have 8 calculated sums thing that confuses me the most which changed the entire way I did the program. I was doing it step by step then stopped after getting to the last step.
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Also I divided it by .001 because I was just changing it to a whole number at that particular decimal spot so I could store it as an integer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Dividing by 0.001 is the same as multiplying by 1000. But it is easier to read * 1000 than / .001. You are not changing it to a whole number by that division; you change it to a whole number with the cast.
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Yea I could have done that lol. I'm just all messed up right now all I'm seeing are numbers everywhere. I'm taking a cal2 course and its ridiculous so I gotta ask. Is all this math I have to take even necessary to be a programmer? I mainly want to get into the field of like game programming and etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
You need to understand the maths to program something. But if you are given an algorithm, you can copy and implement it. So you will be surprised how little Maths you can get away with.
Beware: you can use lots of Maths in games, eg for trajectories, movement, and affine transforms (rotation and shearing of objects, etc).

If you are taking a Maths course, you had better complete and pass it. Taking a course you don’ really want is far less serious than taking it and failing.
 
jQuery in Action, 2nd edition
 
subject: Storing Cut off
 
Similar Threads
Random-Format Number? Pls advise
Problem with printf output
Truncation
Formatting display
Random Number Generator