- #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.
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.