Java se program for color having trouble

In summary, the code creates a Ball class with attributes for color, hardness, and size and instantiates three Ball objects: golf, baseball, and soccer. It also contains a display method to print the current state of the attributes for all objects. The program then changes the attribute values and prints them again with appropriate messages. The output shows the default values and the changed values for each object. The comments in the code are a mix of single line and multi-line comments.
  • #1
toppcon
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.
 
Physics news on Phys.org
  • #2
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
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:
 

1. What is Java SE program for color?

Java SE (Standard Edition) is a programming language that allows developers to create applications for a wide range of platforms. It is commonly used for creating desktop applications, web applications, and mobile applications. The program for color refers to the ability to manipulate and display colors in Java applications.

2. Why am I having trouble with colors in my Java SE program?

There could be several reasons for trouble with colors in your Java SE program. It could be due to incorrect color coding, issues with the graphics library, or a bug in your code. It is important to carefully review your code and make sure the colors are defined correctly and used consistently throughout the program.

3. How can I fix color issues in my Java SE program?

The first step to fixing color issues in your Java SE program is to carefully review your code and make sure the colors are defined correctly. You can also try debugging your code to identify any errors that may be causing the issue. Additionally, make sure you are using the correct graphics library and check for any updates that may have fixed color-related bugs.

4. Can Java SE programs display all colors?

Yes, Java SE programs can display all colors. However, the colors may appear differently depending on the platform and the graphics library being used. It is important to test your program on different devices and platforms to ensure that the colors are displayed correctly.

5. Are there any resources for learning about colors in Java SE programs?

Yes, there are many online resources available for learning about colors in Java SE programs. You can refer to the official Java documentation, online tutorials, and forums for specific questions and issues. There are also books and courses available for a more comprehensive understanding of colors in Java SE programming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top