PDA

View Full Version : I have no idea what I'm doing anymore(Java)



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);
}
}

Flying Mullet
04-02-2009, 11:13 PM
In your Juggler constructor, you pass in an int for ballOne, ballTwo and ballThree. Then you try to assign a new ball to these int's you passed in. When you're accessing the private Ball objects of the Juggler class, use "this" to specify you want the ballOne in this class, otherwise it thinks you're referring to the parameter.

You can remove updateBallsDiameter() as the diameter of each ball will be set by the constructor, making the function call redundant. And I would recommend renaming your parameters to ballOneDiameter or some such so they are more descriptive. Right now it appears that you are passing in a Ball, when in fact you're passing in the diameter of a ball.

I would suggest something like this:


public Juggler(int ballOneDiameter, int ballTwoDiameter, int ballThreeDiameter){
this.ballOne = new Ball(ballOneDiameter);
this.ballTwo = new Ball(ballTwoDiameter);
this.ballThree = new Ball(ballThreeDiameter);
}


Also, it is always good practice to call constructors with more parameters from constructors with less parameters in the same object. This results in less code management and makes maintenance much easier.

Also, if someone calls your single int constructor they will receive null pointer exceptions if they try to access the string member objects because you never initialize the color, material, name and name piece, and all java objects initialize to a null value when they aren't initialized. I would recommend something like this:




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;

private final String defaultColor = "Red";
private final String defaultMaterial = "Rubber";
private final String defaultBallName = "Spot";

/**
* Constructors
*/
public Ball(String anyColor, int anyDiameter, String anyMaterial, String anyName){
color=anyColor;
diameter=anyDiameter;
material=anyMaterial;
ballName=anyName;
bounces = 0;
namePiece = new String();
}

public Ball(int anyDiameter){
this(defaultColor, anyDiameter, defaultMaterial, defaultBallName);
}

// more happy code...


Let me know if you have any more questions.

NeoTifa
04-02-2009, 11:53 PM
I have a ball class. Wanna look?



/**
*
* @author Erica Boyer
* @datecreated 31 Mar 2009
*
* This class handles the ball.
*/

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class ball {

private final int BALL_DIAMETER = 10;
private final int INIT_X = 400 - 5; //Just so its centered
private final int INIT_Y = 300 - 5;

private int xCoord;
private int yCoord;
private int height;
private int width;
private int rise;
private int run;
private Color color;
private int panelHeight,panelWidth;

public ball() {

setXCoord();
setYCoord();
setRise();
setRun();
setColor();

}//End constructor

/** This method sets the initial x-coordinate */
public void setXCoord() {

int max = 51;
Random rand = new Random();
int tempX = rand.nextInt(max);
xCoord = tempX;

}//End setXCoord method

public int getXCoord() {

return(xCoord);

}//End getXCoord method

/** This method sets the initial y-coordinate */
public void setYCoord() {

int max = 51;
Random rand = new Random();
int tempY = rand.nextInt(max);
yCoord = tempY;

}//End setYCoord method

public int getYCoord() {

return(yCoord);

}//End getYCoord method


/** This method sets the initial rise */
public void setRise() {

int max = 11;
Random rand = new Random();
int tempR = rand.nextInt(max);
rise = tempR;

}//End setRise method

public int getRise() {

return(rise);

}//End getRise method

/** This method sets the initial run */
public void setRun() {

int max = 11;
Random rand = new Random();
int tempR = rand.nextInt(max);
run = tempR;

}//End setRise method

public int getRun() {

return(run);

}//End getRun method

/** This method sets a random color to color */
public void setColor() {

int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color randomColor = new Color(red, green, blue);
color = randomColor;

}//End setColor method

public Color getColor() {

return(color);

}//End getColor method

public void paintComponent (Graphics g) {

g.setColor(color);
g.fillOval(xCoord, yCoord, width, height);

} // end method paintComponent

public void move() {


// If ball is approaching a wall, reverse direction
if (xCoord < (0 - run) || xCoord > (panelWidth - width)) {
run = -run;
}

if (yCoord < (0 - rise) || yCoord > (panelHeight - height)) {
rise = -rise;
}

// "Move" ball according to values in rise and run
xCoord += run;
yCoord += rise;

}//End move method

}//End ball class


I like sharing my codez! This is just for a pong game I'm writing. <3 I hope this helps. I used graphics.

Balzac
04-03-2009, 12:14 AM
If your name is really Erica Boyer, I feel sorry for you.

NeoTifa
04-03-2009, 02:05 AM
Why?

qwertysaur
04-03-2009, 02:34 AM
Woah, I'm not trying to be rude, but use Java doc comments for all your methods NeoTifa, that way if other people call your class they know what each method does :p

Also I figured out the issue, I forgot to set the parameters for the constructor, whoops. :p

NeoTifa
04-03-2009, 03:56 AM
... XD Man, I'm usually pretty good about that. I even yell at people for not doing it myself. I even wrote my first tutorial on it!!!! XD Sorry. It was something I whipped up in like a minute or two. Quick notes, you know...