Results 1 to 12 of 12
- 09-19-2008, 07:48 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
- 09-19-2008, 10:04 PM #2
I didn't know that Math.random() did formatting.
What is it you are trying to do? Create 4 four digit numbers that you can string together separated by -.
- 09-19-2008, 10:27 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
Formatting isbn number with Math.random()
Our assignment is to create a program that randomly generates an isbn number in either 9999-9999-9999-9999 or 0000-0-000-0000 format using Math.random(). Also, using Math.random() we're suppose to generate a random Year between 1900-2008, together with a random book price (<=100) and random copies in stock (<=100).
- 09-19-2008, 10:54 PM #4
What's your question?
Have you designed anything yet?
Start small and work up from there.
For example, create a random 4 digit number. Can the number have leading 0s?
- 09-19-2008, 11:21 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
Formatting isbn number with Math.random()
Maybe this will help.
Here is the code we were given to adapt.
FROM the above code, we are suppose to write a class called Book that has the following attributes: copies in stock, year of publication, isbn, price, title, publisher and author. This class will later be used in a test method to compile a listing of 10 Book objects of the same listed above.Java Code:public class Video { private String title; private int stock_num; Video(String in_title, int in_stock_num) { title = in_title; stock_num=in_stock_num; } public String get_title() { return title; } public int get_stock_num() { return stock_num; } } PLUS import javax.swing.*; public class VideoStore { public static void main(String[] args) { String display1 = "This program will ask you to enter the names of five videos then ask you whether or not you would like to view the records sorted or unsorted."; String display2 = "Thank you. Click OK to display the unsorted records."; String display3 = "Click OK to display the records sorted by stock number."; String display4 = "Click OK to display the records sorted by title."; String videoName = ""; String ques1= "Please enter the name of the video."; int counter; int rand_num; Video[] someVideos = new Video[5]; JOptionPane.showMessageDialog(null, display1, "Program Notification", JOptionPane.INFORMATION_MESSAGE); for(counter=0; counter<5;++counter) { videoName=JOptionPane.showInputDialog(null,ques1); rand_num=(int)(Math.random() * 100000); someVideos[counter] = new Video(videoName,rand_num); } JOptionPane.showMessageDialog(null, display2, "Program Notification", JOptionPane.INFORMATION_MESSAGE); for(counter = 0; counter<5; ++counter) { System.out.println(someVideos[counter].get_stock_num() + "\t\t" + someVideos[counter].get_title()); } JOptionPane.showMessageDialog(null,display3, "Program Notification", JOptionPane.INFORMATION_MESSAGE); System.out.println("\n\n"); bubbleSort(someVideos, someVideos.length); for(counter = 0; counter<5;++counter) { System.out.println(someVideos[counter].get_stock_num() + "\t\t" + someVideos[counter].get_title()); } JOptionPane.showMessageDialog(null,display4, "Program Notification",JOptionPane.INFORMATION_MESSAGE); System.out.println("\n\n"); bubbleSortString(someVideos, someVideos.length); for(counter = 0; counter<5;++counter) { System.out.println(someVideos[counter].get_stock_num() + "\t\t" + someVideos[counter].get_title()); } System.exit(0); } public static void bubbleSort(Video[] array, int len) { int a,b; Video temp; int highSubscript = len - 1; for(a = 0; a<highSubscript; ++a); for(b = 0; b<highSubscript; ++b) if(array[b].get_stock_num()>array[b+1].get_stock_num()) { temp = array[b]; array[b] = array[b+1]; array[b+1] = temp; } } public static void bubbleSortString(Video[] array, int len) { int a,b; Video temp; int highSubscript = len -1; for(a = 0; a<highSubscript; ++a); for(b = 0; b<highSubscript; ++b) if((array[b].get_title()).compareTo(array[b+1].get_title())>0) { temp = array[b]; array[b] = array[b+1]; array[b+1] = temp; } } }
Write proper gets and sets to affect the values.
Set the year of publication by default to 1900.
Allow the user the option to enter values for ISBN, price and copiesInStock. If the user chooses to generate these valuse use Math.random to randomly generate values for the ISBN to be a format 9999-9999-9999-9999 (an acceptable isbn might also appear as 2465-4-123-6548), copiesInStock nto to exceed 100, price of book not to exceed $100.00, and year of publication to be not less than the year 1900, but no greater than the current year (1900-2008).
HERE IS MY CODE SO FAR
Java Code:public class Book { private String title; private String publisher; private String author; private int copies; private int dpublication = 1900; private int book_num; private int price; Book(String in_title, String in_publisher, String in_author, int in_copies, int in_dpublication, int in_book_num, int in_price) { title = in_title; publisher=in_publisher; author=in_author; copies=in_copies; dpublication=in_dpublication; book_num=in_book_num; price=in_price; } public String get_title() { return title; } public String get_publisher() { return publisher; } public String get_author() { return author; } public void setCopies(int copies) { copies = 100; } public int get_copies() { return copies; } public void setYear(int dpublication) { dpublication = 1900; } public int getdpublication() { return dpublication; } public int get_book_num() { return book_num; } public void setPrice(double price) { price = 100; } public int get_price() { return price; } } PLUS import javax.swing.JOptionPane; public class BookInventory { public static void main(String[] args) { String display1 = "This program will ask you to enter the isbn numbers, price and number of copies in stock for ten Books then ask you whether or not you would like to view the records sorted or unsorted."; String display2 = "Thank you. Click OK to display the unsorted records."; String display3 = "Click OK to display the records sorted by stock number."; String display4 = "Click OK to display the records sorted by title."; String ques1= "Please enter the isbn number of the Book."; String ques2= "Please enter the price of the Book."; String ques3= "Please enter the number of copies in stock."; String ques4= "Please enter the year of publication."; String ques5= "Please enter the title of the book."; String ques6= "Please enter the author of the book."; String bookName; String title= ""; String publisher= ""; String author= ""; int counter = 0; int rand_num; int rand_year; int copies; double rand_price; Book[] someBooks = new Book[10]; JOptionPane.showMessageDialog(null, display1, "Program Notification", JOptionPane.INFORMATION_MESSAGE); for(counter=0; counter<10;++counter) { bookName=JOptionPane.showInputDialog(null,ques1); rand_num=(int)(Math.random() * 9999); bookName=JOptionPane.showInputDialog(null,ques2); rand_price=(Math.random() * 100); bookName=JOptionPane.showInputDialog(null,ques3); copies=(int)(Math.random() * 100); bookName=JOptionPane.showInputDialog(null,ques4); rand_year=(int)(Math.random() * 1900); title=JOptionPane.showInputDialog(null,ques5); author=JOptionPane.showInputDialog(null,ques6); someBooks[counter] = new Book(bookName, rand_num, rand_price, copies, rand_year, title, author); //CONSTRUCTOR IS UNDEFINED(String, int, double, int, int, String, String) } JOptionPane.showMessageDialog(null, display2, "Program Notification", JOptionPane.INFORMATION_MESSAGE); for(counter = 0; counter<10; ++counter) { System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title()); } JOptionPane.showMessageDialog(null,display3, "Program Notification", JOptionPane.INFORMATION_MESSAGE); System.out.println("\n\n"); for(counter = 0; counter<10;++counter) { System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title()); } JOptionPane.showMessageDialog(null,display4, "Program Notification",JOptionPane.INFORMATION_MESSAGE); System.out.println("\n\n"); for(counter = 0; counter<10;++counter) { System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title()); } System.exit(0); } }
- 09-20-2008, 12:15 AM #6
Didn't see your question?
Does it compile?
Does it execute without errors?
Does it give the correct results?
- 09-20-2008, 12:29 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
My question is: Can you format a number such as 9999-9999-9999-9999 using Math.random()? If so, can you explain how that works. If not, how would such a format be displayed from a randomly generated number, using Math.random()?
The code provided does compile and executes. My code is a work in progress. Book.java compiles, but BookInventory.java does not compile. It is a work in progress.
- 09-20-2008, 02:57 AM #8
You can build a string of random digits using random. You need 16 digits. In a loop, keep calling random, concatenate the results until you get 16 digits. Then insert the - where needed.
According to the API doc random returns a double in the range of 0.0 to 1.0. Multiply that by a larger number to map the results to a different range, say 0 to 9999. Convert it to an int and then to a String. That would give you from 1 to 4 digits. Save by concatenating to a String. Continue until you get 16 digits. If you get more than 16, throw away the excess.
Write a small test program that only generates the string of random digits. When you get that to work, you can make it a method and copy it into your program.Last edited by Norm; 09-20-2008 at 03:00 AM.
- 09-20-2008, 04:28 AM #9
-
I'm guessing that the original poster is misusing the word "formatting" that he really means can we provide code to him that will create a random ISBN number that will work with the code given to him by his instructor.
If so, the answer should be no. You should create your own code (it is your assignment after all). If you have a question regarding your attempt, then feel free to ask it, but don't ask anyone here to provide you with your homework answer.
- 09-21-2008, 04:21 AM #11
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
- 09-21-2008, 06:02 PM #12
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Random number
By jithan in forum Advanced JavaReplies: 1Last Post: 06-13-2008, 01:42 PM -
Formatting a number to currency
By Java Tip in forum java.textReplies: 0Last Post: 04-16-2008, 10:59 PM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM -
Correct Number formatting
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:59 AM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks