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

    Default this looks chinese to me

    Create an array of references (initially null) to Washer object instances (say 20 elements).
    You then want to populate the array with "random" Washer instances.
    Using Math.random() generate values for the inner diameter (id), outer diameter (od), and thickness (thick). Also generate a random value to determine the material (either brass or steel) using some percentage (e.g., 60% brass and 40% steel). Based on these random values construct a new Washer (actually either a BrassWasher or SteelWasher) with the random constructor argument values for id, od, and thick.
    This will produce a collection of random type and size washers which are stored in the array.
    Pass the array to a method which computes the weight of the collection of washers and report that value.

    how would this look like?

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

    Default

    Hopefully you'll get specific help here but until then, and as has been suggested to you in the Sun Java forums, you may benefit most by setting up a conference with your teacher asap.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    You'll have to ask your instructor. Did (s)he talk about programming concepts in class?

    We'll Take it one step at a time.
    Do you know how to create an array?

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

    Yes at the time try to create an array and populate with some values, if you don't know how to do it. You have to do something first, before asking help here. No one here write codes for you.

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

    Default

    yes, he told us how to make a class, constructors, mutators, accessors. And yes I do know how to make arrays, but not this particular array. In my previous Java course, the teacher just gave her lectures with powerpoint slides, and told us to copy code from the book, but never taught us the "why should the code look like this" or "How to do it". So now that this teacher tell us to make such an advance program well, it's really hard, 'cause I'm new at this and well I'm looking at the book reading and all, it gives me an idea but then it doesn't work. So it's hard, I'm going to talk to my instructor about it, tell him if I can have more time.

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

    Default

    what those it mean when it says "populate the array with "random" Washer instances"??

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

    Ok, show me what you have done so far? First thing you have to do is define an array of type Washer of 20 elements.

  8. #8
    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 xgi1008 View Post
    what those it mean when it says "populate the array with "random" Washer instances"??
    Your Washer class have a constructor with some values. What saying there is generate those values randomly and create a washer object.

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

    Default

    Java Code:
    import java.util.Random;
    
    public class TestWashers
    {
    	public static void main(String[] args)
    	{
    		Washer[] Outer = new Washer[20];
    		Washer[] Inner = new Washer[20];
    		Washer[] Thick = new Washer[20];
    		
    		Random random = new Random();
    this is what I've got so far

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

    Default

    Your Washer class have a constructor with some values
    Java Code:
    public Washer(double od, double id, double thick)
    	{
    		this.Outer = od;
    		this.Inner = id;
    		this.Thick = thick;
    	}
    like these??

  11. #11
    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 xgi1008 View Post
    Java Code:
    import java.util.Random;
    
    public class TestWashers
    {
    	public static void main(String[] args)
    	{
    		Washer[] Outer = new Washer[20];
    		Washer[] Inner = new Washer[20];
    		Washer[] Thick = new Washer[20];
    		
    		Random random = new Random();
    this is what I've got so far
    Here you have create three arrays, each have 20 elements. You need only one, not three.

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

    Try something like this in simple way.

    Java Code:
            Washer[] washArr = new Washer[20];
            
            for(int i = 0; i < washArr.length; i++) {
                washArr[i] = new Washer(Math.random(), Math.random(), Math.random());
            }

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

    Default

    I still need the
    Java Code:
    Random random = new Random()
    right?

    and when it says "Also generate a random value to determine the material (either brass or steel) using some percentage (e.g., 60% brass and 40% steel)."

    is it the same thing?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Given an instance of the Random class, now you need to call one of its methods to get a random number. There are a bunch of next... methods that return random values.
    Deside what kind you need and use that method.
    For example if want a 60% 40% division of replies, use nextDouble to get a number between 0 and 1. If the number is < .6 than put choose the 60% option, if > .6 the the 40%

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

    Default

    like this??

    Java Code:
    import java.util.Random;
    
    public class TestWashers
    {
    	public static void main(String[] args)
    	{
    		Random random = new Random();
    		double brass = random.nextDouble();
    		double steel = random.nextDouble();
    		
    		Washer[] washers = new Washer[20];
    		
    		for (int = 0; i < washers.lenght; i++)
    		{
    			washArr[i] = new Washer(Math.random(), Math.random(), Math.random());
          }		
    				
    		if (brass > .6)
    		{
    			System.out.println("This has 60% of Brass");
    		}
    		else if (steel < .6)
    		{
    			System.out.println("This has 40% of Steel");
    		}
    	}
    }

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

    Default

    1. Have you tried the code? What's the output?
    2. Do you think it looks right? If not, what do you think is wrong with it?
    3. I suppose the brass and steel variables are supposed to be used to create random values for a particular Washer object? That's not what happens right now. And if that's the idea, how do you get the values from the Washer class?
    4. What does the Washer class look like? Does its constructor take the arguments you provide in your for loop? Do you have any getter methods?
    5. And: The if-else statement. Aren't they supposed to compare the values of a particular Washer object's variables? Not two numbers you just randomized in main()? And what happens if brass < .6 and steel is > .6?


    There are several things I could bring up, but try to answer these first.

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

    Default

    well, it's an inheritance assignment, I kind of have the parent class code. The output should display the list of washers and then the total weight.

    Java Code:
    public class Washer
    {
    	private double Outer;
    	private double Inner;
    	private double Thick;
    	
    	public Washer()
    	{
    		Outer = 0;
    		Inner = 0;
    		Thick = 0;
    	}
    				
    	public Washer(double od, double id, double thick)
    	{
    		if (od < id || od < 0)
    		{
    			System.out.println("The outer diameter must be greater than the inner diameter and zero");
    			System.exit(0);
    		}
    		
    		Outer = od;
    		Inner = id;
    		Thick = thick;
    	}
    	
    	public double getOuter()
    	{
    		return this.Outer;
    	}
    	
    	public double getInner()
    	{
    		return this.Inner;
    	}
    	
    	public double getThick()
    	{
    		return this.Thick;
    	}
    			
    	
    }
    this is the Washer class, you know the parent class and here is one of the derived classes BrassWasher:

    Java Code:
    public class BrassWasher extends Washer
    {
    	private double density;
    	private double volume;
    	private double weight;
    	
    	public BrassWasher()
    	{
    		super();
    		density = 8.4;
    		volume = 0;
    		weight = 0;
    	}
    	
    	public BrassWasher(double od, double id, double thick, String washer, double weight)
    	{
    		super(od, id, thick);
    	}
    
    	public double getVolume()
    	{
    		return volume = Math.PI * ((od*od)-(id*id))*thick;
    	}
    	
    	public double getWeight()
    	{
    		return weight = volume * density;
    	}
    	
    }
    this is what I have so far, and well the other derived class is SteelWasher, but it looks like the BrassWasher.

    it needs a lot of work, but I'm trying so I need some feedback, I'll fix the if statements

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    What values do the variables: brass and steel hold?
    they can have values from 0 to 1.
    For example brass could be .95 and steel could be .87

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

    Default

    Instead of
    Java Code:
    for (int = 0; i < washers.lenght; i++)
    		{
    			washArr[i] = new Washer(Math.random(), Math.random(), Math.random());
          }
    you should use
    Java Code:
    for (Washer w : washArr)
    			w = new Washer(Math.random(), Math.random(), Math.random());
    Much easier to read. May be faster as well, not sure about that.

    Oh, and you should follow the Java code conventions. So outer instead of Outer.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Replies: 1
    Last Post: 08-21-2009, 08:07 PM
  2. Displaying Chinese characters on Swing components
    By vaskarbasak in forum AWT / Swing
    Replies: 3
    Last Post: 06-26-2008, 08:27 AM
  3. drawString with Chinese Characters
    By vaskarbasak in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2008, 07:49 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
  •