Results 1 to 17 of 17
  1. #1
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Question Array of instances using Math.random()

    Hello, I'm very confuse, I don't know how to create an array using random numbers. Can anyone help me please???

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    what is your problem? an array using random numbers?

    int[] array = new int[(int)(Math.random()*MAX)];
    I die a little on the inside...
    Every time I get shot.

  3. #3
    spider is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Use java.util.Random. The below link will help you in familiarizing with Random class
    javaadda.blogspot.com/2008/10/random-numbers-in-java.html

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    You mean something like
    PHP Code:
    import java.util.*;
    
    public class RandomNumbers {
    	private static final int ARRAY_SIZE = 100;
    	private static final int RAND_MAX = 1000;
    	private Random random = new Random();
    	private int[] randomNumbers = new int[ARRAY_SIZE];
    	private int randNum;
    	
    	public void go() {
    		for (int i = 0; i < ARRAY_SIZE; i++) {
    			randNum = random.nextInt(RAND_MAX)+1;
    			randomNumbers[i] = randNum;
    		}
    	}
    	public static void main(String[] args) {
    		new Tmp().go();
    	}
    }
    This will create an array with 100 ints ranging randomly from 1 to 1000.

  5. #5
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Default

    **well, the thing is I have to an assignment and here is what I have to do:

    All classes developed must be complete including:

    appropriate symbolic constants
    appropriate data fields
    constructors (default, complete, and, if appropriate, partial)
    accessor methods
    mutator methods
    standard methods inherited and overridden from Object:
    boolean equals(Object other)
    String toString()
    problem specific methods
    The project involves working with washers. A Washer is a circular metallic disc with its circular center removed. As such, the relevant data fields for a washer are:
    the outer diameter (OD) of the larger circle
    the inner diameter (ID) of the smaller circle
    the thickness of the disc
    When constructing or mutating these data fields remember that they need to be validated such that all fields are greater than zero and the outer diameter must be larger than the inner diameter (use helper methods for validation so that the code only appears once).
    In addition to the Washer class you will also need to define two derived classes based on the type of metal used for the washer. These will be BrassWasher and SteelWasher. Brass has a density of 8.4 g/cm3 while steel has a density of 7.8 g/cm3 (both of these numbers are approximate since both metals are alloys and their densities depend on the composition of the alloys). The densities should be defined as symbolic constants in the appropriate derived classes.

    The problem specific methods needed are:

    double volume()
    double weight()
    These should be placed in the appropriate classes.
    The main program (in class TestWashers) will create an array of Washer instances (generate reasonable values for the inner and outer diameters and thickness of the washers as well as the material types by using random numbers (see Math.random())) (discussed in class and look up the documentation). The program will then compute the total weight of the collection of washers as stored in the array. The program will display the list of washers (i.e., by using toString() for each array element) and then the total weight of the whole collection of washers (properly annotated).

    **and I already have some code, but I'm not sure what to really put in the parent class, and derived classes, and main program.

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    And how far have you gotten so far? What does your class structure look like? Any UML?

    And where does the Random class fit it, that you originally asked about?

  7. #7
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    public class Washer
    {
    	private String washer;
    	private double Outer;
    	private double Inner;
    	private double Thick;
    	
    	public double weight;
    	
    	public Washer()
    	{
    		washer = "No washer";
    		weight = 0;
    	}
    		
    	public Washer(double o, double i, double t)
    	{
    		this.Outer = o;
    		this.Inner = i;
    		this.Thick = t;
    	}
    	
    	public double getOuter()
    	{
    		return this.Outer;
    	}
    	
    	public double getInner()
    	{
    		return this.Inner;
    	}
    	
    	public double getThick()
    	{
    		return this.Thick;
    	}
    	
    	public void setOuter(double o)
    	{
    		if(o < 0)
    		{
    			System.out.println("Invalid");
    			System.exit(0);
    		}
    		else if (o < Inner)
    		{
    			System.out.println("Invalid");
    			System.exit(0);
    		}
    		else
    		{
    			Outer = o;
    		}
    	}
    	
    	public void setInner(double i)
    	{
    		if(i < 0)
    		{
    			System.out.println("Invalid");
    			System.exit(0);
    		}
    		else
    		{
    			Inner = i;
    		}
    	}
    	
    	public void setThick(double t)
    	{
    		if(t < 0)
    		{
    			System.out.println("Invalid");
    			System.exit(0);
    		}
    		else
    		{
    			Thick = t;
    		}
    	}
    		
    	public boolean equals(Washer otherWasher)
    	{
    		return(washer.equals(otherWasher.washer));
    	}
    	
    	public String toString()
    	{
    		return(washer + " has a total weight of " + weight);
    	}
    	
    	
    }
    this is the parent class.

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

    Default

    A couple of comments:
    Don't use System.exit() for invalid input. That immediately ends the whole program. If you want to notify the user of bad data, throw an exception.

    You need comments in the program to answer the following:
    Why is weight only set in one place?
    What is the String washer used for?

    How are two washers equal? If they have the same dimensions & weight or if they have the same value in washer?

  9. #9
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    public class Washer
    {
    	private String washer;
    	private double Outer;
    	private double Inner;
    	private double Thick;
    	
    	public double weight;
    	
    	public Washer()
    	{
    		washer = "No washer";
    		weight = 0;
    	}
    		
    	public Washer(double o, double i, double t)
    	{
    		this.Outer = o;
    		this.Inner = i;
    		this.Thick = t;
    	}
    	
    	public double getOuter()
    	{
    		return this.Outer;
    	}
    	
    	public double getInner()
    	{
    		return this.Inner;
    	}
    	
    	public double getThick()
    	{
    		return this.Thick;
    	}
    	
    	public void setOuter(double o)
    	{
    		if(o > 0 && o > i)
    		{
    			this.o = o;
    		}
    		else
    		{
    			throw new IllegalArgumentException("Invalid Outer Diameter");
    		}
    
    
    	public void setInner(double i)
    	{
    		if(i > 0)
    		{
    			this.i = i;
    		}
    		else
    		{
    			throw new IllegalArgumentException("Invalid Inner Diameter");
    		}
    	}
    	
    	public void setThick(double t)
    	{
    		if(t > 0)
    		{
    			this.t = t;
    		}
    		else
    		{
    			throw new IllegalArgumentException("Invalid Thickness");
    		}
    	}
    		
    	public boolean equals(Washer otherOuter)
    	{
    		return(washer.equals(otherWasher.washer));
    	}
    	
    	public String toString()
    	{
    		return(washer + " has a total weight of " + weight);
    	}
    	
    	
    }
    how about now? I still need to do a little work on the boolean equals and toString().

  10. #10
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Default

    never mind it doesn't work

  11. #11
    xgi1008 is offline Member
    Join Date
    Oct 2008
    Location
    New York
    Posts
    14
    Rep Power
    0

    Default

    can somebody tell me how to really do this? Because it's really confusing....and I'm not sure what else to put. I have some derived classes code. And trust me I have tried, but it doesn't work.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,575
    Rep Power
    25

    Default

    it doesn't work.
    Please explain. Can't help if you don't describe what is not working.

    Did you read my previous post?
    You need comments in the program to answer the following:
    Why is weight only set in one place?
    What is the String washer used for?

    How are two washers equal? If they have the same dimensions & weight or if they have the same value in washer?

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

    Default

    cross-posted in the Sun java forums.
    New To Java - Java program HELPPPP!!!!!

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    How are two washers equal? If they have the same dimensions & weight or if they have the same value in washer?
    Before coding make your complete design on your application. It's really helpful to you as well as for others who are going to help you. And in that sense you can explain what's your requirement is.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    jhon123 is offline Member
    Join Date
    Jan 2011
    Location
    philippines
    Posts
    7
    Rep Power
    0

    Default hi guys

    guys can u help me this!!!..


    int n=137;

    and i want to display like this!!!...

    173,371,317,731,713

    how to use a math.random();

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This thread is over 3 years old, and you probably need to define a number permutation instead of using random numbers

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Formatting isbn number with Math.random()
    By dns77x7 in forum New To Java
    Replies: 11
    Last Post: 09-21-2008, 06:02 PM
  3. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  4. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03: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
  •