Results 1 to 7 of 7
  1. #1
    Gligum is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Method Repetition

    Hello everyone.

    I am working on an assignment for class, where there will be a cube, and each side will be painted one of four possible colors. There will be additional aspects required, but the issue I am having is that each side is getting set to the same color, and I am just not sure why. I will post what I have below, and it will include the methods I use to print out the result.


    Thanks for any possible help






    import java.awt.Color;
    import java.util.Random;

    public class Cube
    {
    private Color top;
    private Color bottom;
    private Color left;
    private Color right;
    private Color front;
    private Color back;

    private Color painter;

    public Cube()
    {
    top = AssignColor();
    bottom = AssignColor();
    left = AssignColor();
    right = AssignColor();
    front = AssignColor();
    back = AssignColor();
    }

    private Color AssignColor()
    {
    int colorVal;
    Random generator;

    generator = new Random();
    colorVal = generator.nextInt(4) + 1;

    if(colorVal == 1)
    painter = Color.blue;
    else if(colorVal == 2)
    painter = Color.red;
    else if(colorVal == 3)
    painter = Color.green;
    else if(colorVal == 4)
    painter = Color.white;

    return painter;
    }

    public String colorConv()
    {
    String tempFront = "red";
    if(front.getRGB() == -16776961)
    tempFront = "Blue";
    else if(front.getRGB() == -16711936)
    tempFront = "Green";
    else if(front.getRGB() == -65536)
    tempFront = "Red";
    else if(front.getRGB() == -1)
    tempFront = "White";

    return tempFront;
    }

    public void print()
    {
    System.out.println("The top is " + colorConv());
    System.out.println("The bottom is " + colorConv());
    System.out.println("The left side is " + colorConv());
    System.out.println("The right side is " + colorConv());
    System.out.println("The front is " + colorConv());
    System.out.println("The back is " + colorConv());
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    When posting code, make sure you use the code tags and use standard naming conventions (methods start with a lower case letter, for example).

    Is it the same color each time? Or a different color, but each side gets the same color?

    You'd be better off posting a short piece of runnable code and the actual output you get.

  3. #3
    Gligum is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    It doesn't matter if it is the same color, but it is supposed to be random, so it might be, it might not be, but each side is supposed to be determined differently. No matter how many times I run it, each side is always the same color as the rest in that instance.


    The top is Red
    The bottom is Red
    The left side is Red
    The right side is Red
    The front is Red
    The back is Red


    That is an example of what it looks like, and that is ok, just not every time I run the code.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    When you convert a color that method only checks the color of 'front'.

    kind regards,

    Jos

  5. #5
    Gligum is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Are you referring to where I return the painter value, which is then associated with the side?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Gligum View Post
    Are you referring to where I return the painter value, which is then associated with the side?
    He is referring to your colorConv() method. Trace through every time you call it and see what it returns each time.

  7. #7
    Gligum is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Ha... oh wow.... I feel bad about wasting both of your guys' time on this, I can't believe I missed that. Thanks a lot for your help both of you.

Similar Threads

  1. Nested Loop help -- concerning image repetition
    By Submerged in forum Java Applets
    Replies: 0
    Last Post: 03-05-2010, 03:57 AM
  2. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 09:37 AM
  3. Matrix variations with repetition
    By jnovice in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:42 AM
  4. while Loop (Repetition) structure.
    By gapeach97 in forum New To Java
    Replies: 9
    Last Post: 12-03-2008, 11:52 PM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 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
  •