• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random number and Nano Time  RSS feed

 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created the a Time class, a Random number class, and Node class. The thing is when i run the Node class its suppose to spit out a random number and tell me the time it spit it out in nano time. The thing is i cant get the nano time to appear at the time the number was generated. This is my assignment
1) A “MyNum” class that will generate a random integer number from 1 to 200.
2) A “Time” class that will return the system time with sufficient resolution to show the time between insertions. I suggest System.nanoTime().
3) A “Node” class that will encapsulate the integer number and the Time it was generated.
4) And a “Storage class” class that will contain the all the SORTED Nodes using an insertion sort.
a. This class should have a “add” method to add in each node.
b. And method(s) so the driver class may display the sorted list of nodes
5) The “main” or driver class.


but what i want for now is to get the node class to run for now.


This is the code so far:



The Number class

The node class that suppose to print out the number with the time it was generated
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john vandeven wrote:but what i want for now is to get the node class to run for now.

Classes don't run. Methods run. If a method is static, you can call it without creating an object. If it isn't static, you can't.

So...what do you really want to do here? What method of the node class do you want to run?
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay got sorry very new to java lol
Okay I have to complete the whole thing in order to check if it runs right because all the program has to do it get a Radom number from (0-200) and then know the time it was generated using nano.Time and then I have
To create another class that stores those numbers and uses an insertion sort to sort 50 numbers. Then the "main" is going to compile all of that. Only thing is I'm not sure if I'm doing it right so far
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john vandeven wrote:Okay I have to complete the whole thing in order to check if it runs right

That is actually a phenomenally BAD idea - albiet a common one.

When I code, I NEVER write more than 2-3 lines before I compile and test to see if it works. the smaller the chunks, the easier it is to understand/debug/fix/re-test. What you need is to get the code working that generates a random number from 0-200 (is that inclusive?). Make sure that is bulletproof before you work on anything else.

THEN write code that gets the current time. Get that to be bulletproof.

THEN combine those two pieces. Get that to be bulletpoof.

THEN write something that will store ONE of those. THEN write something that will store 50. etc.
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Thanks for the tip
i wrote the random number class it compiles and gives me a number from 0 to 200 so thats all good.
I wrote the Time in nano.Time(); i gave me nano times every time i wrote it so thats all clear. The problem i ran into is HOW encapsulating it into the class called NODE here is the Assignment i have

1) A “MyNum” class that will generate a random integer number from 1 to 200.
2) A “Time” class that will return the system time with sufficient resolution to show the time between insertions. I suggest System.nanoTime().
3) A Node class that will encapsulate the integer number and the Time it was generated.
4) And a “Storage class” class that will contain the all the SORTED Nodes using an insertion sort.
a. This class should have a “add” method to add in each node.
b. And method(s) so the driver class may display the sorted list of nodes
5) The “main” or driver class.

Number 3 is the problem im not quite sure how to combine the together. Sorry if imm asking to many questions
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Has somebody told you to implement your application in a particular way?
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

No this is what my teacher told us to do
For this lab you will write:

1) A “MyNum” class that will generate a random integer number from 1 to 200.
2) A “Time” class that will return the system time with sufficient resolution to show the time between insertions. I suggest System.nanoTime().
3) A “Node” class that will encapsulate the integer number and the Time it was generated.
4) And a “Storage class” class that will contain the all the SORTED Nodes using an insertion sort.
a. This class should have a “add” method to add in each node.
b. And method(s) so the driver class may display the sorted list of nodes
5) The “main” or driver class.



The driver class should exercise the other classes, such that the MyNum class assigns a number and the Time class a time to the Node class. The node is added to the Storage class which maintains a sorted list of the Nodes added from Low (position 0) to High( position 49). The driver class will complete this process 50 times and after completion display all the nodes in sorted order and in insertion order with the time, and the lowest and highest node times.



Im just stuck on the 3rd one i got the Mynum and Time class im not sure how to encapsulate these 2 into the "Node" class idk if the Node class i wrote is correct?
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you will see in this discussion, nanoTime will tell you the differnce between insertion times, but is not reliable as an index for unique identification.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class which has the insertion time and random number as two fields?
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well all i wanted it to do is sort the random numbers along with is time it took to produce this is the example my teacher gave me

Number 109 Time 019183883
Number 4 Time 094883332
Number 57 Time 019938338

and then it will be sorted using an array

Number 4 Time 094883332
Number 57 Time 019938338
Number 109 Time 019183883

Thanks and sorry for all these questions lol
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right: you appear to have a
class which has the insertion time and random number as two fields
… as I said yesterday. You obviously need to provide a toString method to get that output.

As for sorting, you have to start by providing an order for the classes, and there is a really good section about that in the Java Tutorials.
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ill post the code when im done
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome and look forward to seeing the result
 
john vandeven
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay i got it to work
Main


Node


Time


Number


Quick question if i want to store the numbers and time it was outputted using and arrange 50 of those numbers at the same time does the "Storage" class have to contain the an array and the algorithm along with it and also if i want it to loop 50 times that has to be put in MyNum class right?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the design of this still seems off to me. Why create a class that has nothing but a static method that does nothing but call a static method of another class?

I would have your Time class have a member variable that stores the time. Have the constructor automatically set the variable. Then create a getTime() method that returns the time stored in that object.

Do the same for your MyNum class.

Then, your Node class should have a reference to a Time and MyNum object. It would then have methods to getTime and getNum, which would access the underlying objects methods.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tested those classes? What happens if you pass negative numbers to their constructors? What is the output of the toString method? How are you sorting or ordering Node instances? There is no point in asking questions about arrays until you have got all those things worked out. Get one problem sorted out before you even start on the next one.

What is the point of a Time class which consists entirely of a System.nanoTime call?
What is the point of the myNum class? Have you really got a loop which does nothing once?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is the point of a Time class which consists entirely of a System.nanoTime call?


Looks like the point is to satisfy requirement #2 of the assignment (see the OP).
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . Looks like the point is to satisfy requirement #2 . . .
Aaaaaaaaaaaaaaaaaaaaaah. Yes, I see it now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!