Java se program for color having trouble

  • Comp Sci
  • Thread starter toppcon
  • Start date
  • #1
1
0
1.Write a Java program that does the following:

a. Creates a Ball class with attributes for color, hardness, and size and instantiates the following Ball objects: golf, baseball, soccer.

b. Creates a display method which can print the current state of the attributes for all objects.

c. Creates attributes that have default values (your choice) which will display to the screen when the program is run and then each of the attributes for each object will be changed (your choice) during the program run and printed again to the screen with the appropriate message so that I can tell what the original values were and how they were changed during the program run.



2. Code:
import java.awt.Color;
import java.lang.*;

public class Ball
{
private Color color;
private String hardness;
private String size;

Ball(Color thecolor, String hardness, String size)
{
this.color = thecolor;
this.hardness = hardness;
this.size = size;
}

void displayInfor()
{
System.out.println(" color = " + color.toString() + " hardness = " + hardness + " and size = " + size);
System.out.println(color);
}



//method used to set the new color*/
public void setColor(Color newcolor)
{
color = newcolor;
}

//method used to set the new hardness*/
public void setHardness(String newHardness)
{
hardness = newHardness;
}

//method used to set the new size*/
public void setSize(String newSize)
{
size = newSize;
}

}

class driver
{
public static void main(String args[])
{

Ball golf, baseball, soccer;
golf = new Ball(Color.WHITE, "hard", "small");
baseball = new Ball(Color.RED, "soft", "median");
soccer = new Ball(Color.BLACK, "median", "big");

//print out the default value*/
System.out.println("The golf ball ");
golf.displayInfor();

System.out.println("The baseball ");
baseball.displayInfor();

System.out.println("The soccer ball ");
soccer.displayInfor();

//now change the attribute values and print again. Since there are no requirements on how to change the value, I'll just use an example to show how it work. And of course you can make this part fancier by using a for/while loop to change the attribute values randomly...*/
golf.setColor(Color.RED);
golf.setHardness("soft");
golf.setSize("big");
System.out.println("The golf ball is now ");
golf.displayInfor();

baseball .setColor(Color.GREEN);
baseball .setHardness("median");
baseball .setSize("median");
System.out.println("The baseball is now ");
baseball.displayInfor();

soccer .setColor(Color.YELLOW);
soccer .setHardness("hard");
soccer .setSize("small");
System.out.println("The soccer ball is now ");
soccer.displayInfor();

}
}


3. Output after run

The golf ball
color = java.awt.Color[r=255,g=255,b=255] hardness = hard and size = small
java.awt.Color[r=255,g=255,b=255]
The baseball
color = java.awt.Color[r=255,g=0,b=0] hardness = soft and size = median
java.awt.Color[r=255,g=0,b=0]
The soccer ball
color = java.awt.Color[r=0,g=0,b=0] hardness = median and size = big
java.awt.Color[r=0,g=0,b=0]
The golf ball is now
color = java.awt.Color[r=255,g=0,b=0] hardness = soft and size = big
java.awt.Color[r=255,g=0,b=0]
The baseball is now
color = java.awt.Color[r=0,g=255,b=0] hardness = median and size = median
java.awt.Color[r=0,g=255,b=0]
The soccer ball is now
color = java.awt.Color[r=255,g=255,b=0] hardness = hard and size = small
java.awt.Color[r=255,g=255,b=0]


The problem is: where the output is color = java.awt.Color[r,g,b], I need it to show the color "red" or "green" etc... Is that possible? no matter what I do, the java.awt.Color statement is still being printed.
 

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,369
281
This output comes from Color.toString() and it is supposed to be like that for Color.

If you only are going to refer to the color by name, then perhaps you should think about if Color really is an appropriate type for your color property. Perhaps another type would better fit your purpose?
 
  • #3
297
2
//method used to set the new color*/

//now change the attribute values and print again. Since there are no requirements on how to change the value, I'll just use an example to show how it work. And of course you can make this part fancier by using a for/while loop to change the attribute values randomly...*/

What's with your comments? You're mixing the two comment types. You should use one or the other at one time.

For a single line comment:
Code:
// A single line comment (like this one) starts with double-slashes and is only this line

Or a multi-line comment:
Code:
/* Everything from the starting slash-asterisk to the end asterisk-slash
 * is part of the comment.  It will go on until you end it like this:
 */
Note that I fancied up that last multi-line comment to illustrate, but suffice to say it goes from /* to */.

You don't want to try and close a single line comment, as it just ends at the end of the line.

Thought it would help if I cleared that up for you. :biggrin:
 

Related Threads on Java se program for color having trouble

  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
12
Views
8K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
1K
Top