qwertysaur
04-02-2009, 10:45 PM
This has been my life for two hours. I have to make a juggler class that creates three ball objects, and then can change their diameters. but I can't get the constructor to work, and I'm going crazy over it.
Here is the code for the two classes. Please help!
The error message says I am giving the wrong data type, but I'm just so confused about the project I can't find where it went wrong. :(
Edit: I tried to change the data type in the constructor to ball, which worked but I can't make balls now. I'm being destroyed by this. :(
Ball
/**
* Ball
* @author me
* @version 1.0.2
*/
/**
* Public Class for the Ball
*/
public class Ball{
/**
* Fields for the Ball Class
*/
private String color;
private int diameter;
private String material;
private String ballName;
private String namePiece;
private int bounces;
/**
* Constructors
*/
public Ball(String anyColor, int anyDiameter, String anyMaterial, String anyName){
color=anyColor;
diameter=anyDiameter;
material=anyMaterial;
ballName=anyName;
bounces = 0;
}
public Ball(int anyDiameter){
diameter=anyDiameter;
bounces = 0;
}
//--------------------------------------Accessor Methods-------------------------------------------//
/**
* Method to retrieve the color of the ball
*/
public String getColor(){
return color;
}
/**
* Method to retrieve the Diameter of the ball
*/
public int getDiameter(){
return diameter;
}
/**
* Method to retrieve the material of the ball
*/
public String getMaterial(){
return material;
}
/**
* Method to display the number of bounces
*/
public int getBounces(){
return bounces;
}
//--------------------------Mutator Methods----------------------------------//
/**
* Method to inflate/deflate the ball
* Note that the max diameter is 256
*/
public void setnewDiameter(int anyDiameterInflate){
int diameterBackup;
diameterBackup = diameter;
diameter += anyDiameterInflate;
if (diameter <= 0 || diameter > 256){
System.out.println("BOOM!!!! YOU HAVE MADE THE BALL GO BYE BYE. \nGood thing that was a simulation. New ball created.");
diameter = diameterBackup;
}else if(diameter == 256){
System.out.println("You should stop inflating the ball. It's kinda full.");
}else if(diameter == 1){
System.out.println("You should put some diameter in this ball. Or it will dissapear... :(");
}else{
System.out.println("Success! The new Diameter is now " + diameter);
}
}
/**
* Method to change the ball material
*/
public void setMaterial(String anyMaterial){
material = anyMaterial;
}
/**
* Method to change the Diameter of the ball
*/
public void setDiameter(int anyDiameter){
diameter=anyDiameter;
}
/**
* Method to change the Color of the ball
*/
public void setColor(String anyColor){
color=anyColor;
ballName = color + namePiece;
}
/**
* Method to print the ball info
*/
public void printBallInfo(){
System.out.println("The ball called " + ballName + " has the following properties!" + "\nDiameter: " + diameter + "\nMaterial: " + material);
}
/**
* Method to change all the fields at once
*/
public void setAll(String anyColor, int anyDiameter, String anyMaterial, String anyName){
color= anyColor;
diameter= anyDiameter;
material= anyMaterial;
ballName= color + anyName;
}
/**
* Method to name the ball
*/
public void setBallName(String anyName){
namePiece = anyName;
ballName = color + namePiece;
}
/**
* Method to increase the the number of bounces by one
*/
public void setBounceNumber(){
bounces++;
}
}
Juggler
/**
* the juggler class is able to create 3 ball objects to use
* It can set the diameter of each ball
*
* @author me
* @version 1.0.0
*/
public class Juggler{
private Ball ballOne;
private Ball ballTwo;
private Ball ballThree;
/**
* Constructor for objects of class Juggler
* Creates three distinct ball objects
*/
public Juggler(int ballOne, int ballTwo, int ballThree){
ballOne = new Ball(1);
ballTwo = new Ball(2);
ballThree = new Ball(3);
updateBallsDiameter();
}
/**
* method that updates the three balls
*/
public void updateBallsDiameter(int diameterOne, int diameterTwo, int diameterThree){
ballOne.setnewDiameter(diameterOne);
ballTwo.setnewDiameter(diameterTwo);
ballThree.setnewDiameter(diameterThree);
}
}
Here is the code for the two classes. Please help!
The error message says I am giving the wrong data type, but I'm just so confused about the project I can't find where it went wrong. :(
Edit: I tried to change the data type in the constructor to ball, which worked but I can't make balls now. I'm being destroyed by this. :(
Ball
/**
* Ball
* @author me
* @version 1.0.2
*/
/**
* Public Class for the Ball
*/
public class Ball{
/**
* Fields for the Ball Class
*/
private String color;
private int diameter;
private String material;
private String ballName;
private String namePiece;
private int bounces;
/**
* Constructors
*/
public Ball(String anyColor, int anyDiameter, String anyMaterial, String anyName){
color=anyColor;
diameter=anyDiameter;
material=anyMaterial;
ballName=anyName;
bounces = 0;
}
public Ball(int anyDiameter){
diameter=anyDiameter;
bounces = 0;
}
//--------------------------------------Accessor Methods-------------------------------------------//
/**
* Method to retrieve the color of the ball
*/
public String getColor(){
return color;
}
/**
* Method to retrieve the Diameter of the ball
*/
public int getDiameter(){
return diameter;
}
/**
* Method to retrieve the material of the ball
*/
public String getMaterial(){
return material;
}
/**
* Method to display the number of bounces
*/
public int getBounces(){
return bounces;
}
//--------------------------Mutator Methods----------------------------------//
/**
* Method to inflate/deflate the ball
* Note that the max diameter is 256
*/
public void setnewDiameter(int anyDiameterInflate){
int diameterBackup;
diameterBackup = diameter;
diameter += anyDiameterInflate;
if (diameter <= 0 || diameter > 256){
System.out.println("BOOM!!!! YOU HAVE MADE THE BALL GO BYE BYE. \nGood thing that was a simulation. New ball created.");
diameter = diameterBackup;
}else if(diameter == 256){
System.out.println("You should stop inflating the ball. It's kinda full.");
}else if(diameter == 1){
System.out.println("You should put some diameter in this ball. Or it will dissapear... :(");
}else{
System.out.println("Success! The new Diameter is now " + diameter);
}
}
/**
* Method to change the ball material
*/
public void setMaterial(String anyMaterial){
material = anyMaterial;
}
/**
* Method to change the Diameter of the ball
*/
public void setDiameter(int anyDiameter){
diameter=anyDiameter;
}
/**
* Method to change the Color of the ball
*/
public void setColor(String anyColor){
color=anyColor;
ballName = color + namePiece;
}
/**
* Method to print the ball info
*/
public void printBallInfo(){
System.out.println("The ball called " + ballName + " has the following properties!" + "\nDiameter: " + diameter + "\nMaterial: " + material);
}
/**
* Method to change all the fields at once
*/
public void setAll(String anyColor, int anyDiameter, String anyMaterial, String anyName){
color= anyColor;
diameter= anyDiameter;
material= anyMaterial;
ballName= color + anyName;
}
/**
* Method to name the ball
*/
public void setBallName(String anyName){
namePiece = anyName;
ballName = color + namePiece;
}
/**
* Method to increase the the number of bounces by one
*/
public void setBounceNumber(){
bounces++;
}
}
Juggler
/**
* the juggler class is able to create 3 ball objects to use
* It can set the diameter of each ball
*
* @author me
* @version 1.0.0
*/
public class Juggler{
private Ball ballOne;
private Ball ballTwo;
private Ball ballThree;
/**
* Constructor for objects of class Juggler
* Creates three distinct ball objects
*/
public Juggler(int ballOne, int ballTwo, int ballThree){
ballOne = new Ball(1);
ballTwo = new Ball(2);
ballThree = new Ball(3);
updateBallsDiameter();
}
/**
* method that updates the three balls
*/
public void updateBallsDiameter(int diameterOne, int diameterTwo, int diameterThree){
ballOne.setnewDiameter(diameterOne);
ballTwo.setnewDiameter(diameterTwo);
ballThree.setnewDiameter(diameterThree);
}
}