Results 1 to 12 of 12
  1. #1
    dns77x7 is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default Formatting isbn number with Math.random()

    Does anyone know how an isbn number would be formatted (i.e. 9999-9999-9999-9999) using Math.random()?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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 -.

  3. #3
    dns77x7 is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default 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).

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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?

  5. #5
    dns77x7 is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default Formatting isbn number with Math.random()

    Maybe this will help.

    Here is the code we were given to adapt.

    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;
    			}
    	}
    }
    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.

    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);
    	}
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Didn't see your question?
    Does it compile?
    Does it execute without errors?
    Does it give the correct results?

  7. #7
    dns77x7 is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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 04:00 AM.

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by dns77x7 View Post
    Can you format a number such as 9999-9999-9999-9999 using Math.random()?
    No, you can't. Use a formatting function, not Math.random().

    If someone told you to do this, ask them why they think it will work.

    The question makes zero sense.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    your question is so confusing. could you say it more clearly?

  12. #12
    dns77x7 is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Thank you Norm and everyone else for your suggestions and advice. I'd forgotten completely about using 'toString".

Similar Threads

  1. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 02:42 PM
  2. Formatting a number to currency
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-16-2008, 11:59 PM
  3. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  4. Correct Number formatting
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:59 AM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •