Problem: I am trying to output different attributes of balls - golf, soccer, baseball.
I am trying the output to look like the following:
color = red and hardness = median and size = big
but the output is as follows no matter what I do:
color = java.awt.Color[r=0,g=0,b=0] hardness = median and size = big
Can anyone point me in the right direction?
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()+ " and " + " 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();
}
}