Results 1 to 7 of 7

Thread: I have no idea what I'm doing anymore(Java)

  1. #1
    GO! use leech seed! qwertysaur's Avatar
    Join Date
    Dec 2006
    Location
    Kanto
    Posts
    11,627
    Contributions
    • Former Site Staff

    Default I have no idea what I'm doing anymore(Java)

    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
    Code:
    /**
     * 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
    Code:
    /**
     * 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);
        }
    }
    Last edited by qwertysaur; 04-02-2009 at 10:57 PM.

  2. #2
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    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:
    Code:
        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:

    Code:
    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.
    Figaro Castle

  3. #3
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    I have a ball class. Wanna look?

    Code:
    /**
     *
     * @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.
    Oh gods, why? ಥ_ಥ


  4. #4
    The Anti Mosher Balzac's Avatar
    Join Date
    Feb 2008
    Location
    Hangcaster, England
    Posts
    1,534
    Blog Entries
    1

    Default

    If your name is really Erica Boyer, I feel sorry for you.
    You hold my heart in your manly hands I wanna feel the throb of your handsome gland. I wanna hold you tight like a newborn kitten, against my flesh like a cashmere mitten. Tickly tick, I'm makin' skin bump heaven and all the way down it's lookin' cleanly shaven. Prickety pricks, it's stubble on stubble I better slow down or I'm in real trouble. Want you, touch you, feel you, taste you! Knick knack whacky whack 'till I see the man stew. spin you around let me see that hole! I'm a tunnelin' in a like a short hair mole. Once I'm inside I'm gonna leave a trace, half in there and half on that face! One finger, two finger, there fingers gone! Mano a mano I love you John!

  5. #5
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    Why?
    Oh gods, why? ಥ_ಥ


  6. #6
    GO! use leech seed! qwertysaur's Avatar
    Join Date
    Dec 2006
    Location
    Kanto
    Posts
    11,627
    Contributions
    • Former Site Staff

    Default

    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

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

  7. #7
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    ... 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...
    Oh gods, why? ಥ_ಥ


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •