Results 1 to 17 of 17
- 10-03-2008, 05:46 PM #1
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
- 10-03-2008, 06:50 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 12
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.
- 10-03-2008, 07:21 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
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
- 10-03-2008, 07:39 PM #4
Senior Member
- Join Date
- Sep 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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(); } }
- 10-03-2008, 08:10 PM #5
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
**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.
- 10-03-2008, 08:56 PM #6
Senior Member
- Join Date
- Sep 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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?
- 10-03-2008, 11:26 PM #7
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
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); } }
- 10-04-2008, 01:19 AM #8
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?
- 10-04-2008, 02:04 AM #9
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
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); } }
- 10-04-2008, 02:15 AM #10
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
never mind it doesn't work
- 10-04-2008, 02:19 AM #11
Member
- Join Date
- Oct 2008
- Location
- New York
- Posts
- 14
- Rep Power
- 0
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.
- 10-04-2008, 03:20 AM #12it doesn't work.
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?
-
cross-posted in the Sun java forums.
New To Java - Java program HELPPPP!!!!!
- 10-04-2008, 06:16 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
- 10-04-2008, 06:17 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
- 01-25-2011, 11:55 PM #16
Member
- Join Date
- Jan 2011
- Location
- philippines
- Posts
- 7
- Rep Power
- 0
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();
- 01-26-2011, 12:10 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 06:50 PM -
Formatting isbn number with Math.random()
By dns77x7 in forum New To JavaReplies: 11Last Post: 09-21-2008, 07:02 PM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 03:09 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 04:44 AM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 04:31 AM
Bookmarks