Comp Sci Java se program for color having trouble

AI Thread Summary
The Java program creates a Ball class with attributes for color, hardness, and size, instantiating golf, baseball, and soccer ball objects. A display method prints the attributes, but the output shows color in the format "java.awt.Color[r,g,b]" instead of color names like "red" or "green." The discussion suggests that using a different data type for color might be more appropriate if the intention is to display color names. Additionally, there are comments about improving code clarity by standardizing comment styles. The main issue revolves around the representation of color in the output.
toppcon
Messages
1
Reaction score
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.
 
Physics news on Phys.org
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?
 
toppcon said:
//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:
 

Similar threads

Replies
7
Views
3K
Replies
12
Views
2K
Replies
3
Views
2K
Replies
16
Views
13K
Replies
15
Views
7K
Replies
5
Views
3K
Back
Top