Java se program for color having trouble

  • Context: Comp Sci 
  • Thread starter Thread starter toppcon
  • Start date Start date
  • Tags Tags
    Color Java Program
Click For Summary
SUMMARY

The forum discussion focuses on a Java program designed to create a Ball class with attributes for color, hardness, and size. Users instantiate three Ball objects: golf, baseball, and soccer, and implement a display method to print their attributes. The issue raised is that the output displays color in the format "java.awt.Color[r,g,b]" instead of the desired color names like "red" or "green". The discussion suggests reconsidering the use of the Color class for the color attribute to achieve more user-friendly output.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with object-oriented programming concepts
  • Knowledge of Java AWT Color class
  • Experience with Java methods and constructors
NEXT STEPS
  • Explore alternatives to the Java AWT Color class for color representation
  • Learn about overriding the toString() method in Java classes
  • Investigate best practices for Java comments and code documentation
  • Research Java collections for managing multiple Ball objects efficiently
USEFUL FOR

Java developers, programming students, and anyone interested in improving their object-oriented programming skills in Java.

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 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K