Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: My fleet of squares doesnt work (simple high school work)

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default My fleet of squares doesnt work (simple high school work)

    Hi guys I wasted my entire day today trying to figure out and I just can't. I lost all my hope.

    Square:
    package assignment1;
     
     
    import java.awt.*;
     
    public class Square  
    {   
       private double x,y;      // Current position of the square.
     
       private Color color;     // The color of the square.
     
       private int side;   // The side of the square
     
       public Square() // constructor
       {
          x = 100;
          y = 100;
          side = 5;
    	  color = Color.orange;
       }
     
     
       public void draw(Graphics g) 
       {
          // Draw the square in the graphics context g.  Note: The drawing color
          // in g is changed to the color of the square.
          g.setColor(color);
          g.fillRect( (int)(x), (int)(y), (int)(side), (int)(side) );
       }
     
       public void setSide(int r)
       {
           side = r;
       }
     
       public int getSide()
       {
           return side;
       }
     
       public int getX()
       {
            return (int) x;
       }
     
       public int getY()
       {
            return (int) y;
       }
     
       public void setX(int xIn)
       {
           x = xIn;
       }
     
       public void setY(int setY)
       {
           y = setY;
       }
    }  // end class Square

    This is the square class, it draws an orange square that moves around in the applet viewer, works fine and as it supposed to do.

    Ship:
    package assignment1;
     
    import java.awt.*;
     
    public class Ship
    {
        private Square[] fleetMembers; // declare fleetMembers as an array of Square
        private int total = 5; // Max number of squares in fleet
        private double x,y; 
        Ship() // Constructor
        {
            x = 100;
            y = 100;
            fleetMembers = new Square[total]; // create an empty array with 5 "locations"
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                fleetMembers[i] = new Square();  // create a Square object for location [i] in the array
                fleetMembers[i].setX (i * 10); // multiply i by 10, and pass this to the setX method
                fleetMembers[i].setY (i * 10); // multiply i by 10, and pass this to the setY method
            }
        }
     
        public void draw(Graphics g)
        {
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                fleetMembers[i].draw(g);    // ask fleetMember[i] to draw itself
            }
        }
     
        public void goSouth(int howFar)
        {
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                // ask fleetMember[i] for its current Y position
                // then add howFar (howFar to go south) to it
                // then pass this to the setY method 
                fleetMembers[i].setY (fleetMembers[i].getY() + howFar); 
            }
        }    
     
       public int getX()
       {
            return (int) x;
       }
     
       public int getY()
       {
            return (int) y;
       }
     
       public void setX(int xIn)
       {
           x = xIn;
       }
     
       public void setY(int setY)
       {
           y = setY;
       }
     
     
    }

    This is my ship class, it is basically a set of 5 squares that move around together, works fine as well.

    The problem begins here:

    package assignment1;
     
    import java.awt.*;
     
    public class FleetOfShips
    {
        private Ship[] fleetMembers; // declare fleetMembers as an array of Ship
        private int total = 5; // Max number of Ships in fleet
        private double x,y; 
        FleetOfShips() // Constructor
        {
            x = 100;
            y = 100;
            fleetMembers = new Ship[total]; // create an empty array with 5 "locations"
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                fleetMembers[i] = new Ship();  // create a Ship object for location [i] in the array
                fleetMembers[i].setX (i * 50); // multiply i by 10, and pass this to the setX method
            }
        }
     
        public void draw(Graphics g)
        {
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                fleetMembers[i].draw(g);    // ask fleetMember[i] to draw itself
            }
        }
     
        public void goSouth(int howFar)
        {
            for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle
            {
                // ask fleetMember[i] for its current Y position
                // then add howFar (howFar to go south) to it
                // then pass this to the setY method 
                fleetMembers[i].setY (fleetMembers[i].getY() + howFar); 
            }
        }    
     
       public int getX()
       {
            return (int) x;
       }
     
       public int getY()
       {
            return (int) y;
       }
     
       public void setX(int xIn)
       {
           x = xIn;
       }
     
       public void setY(int setY)
       {
           y = setY;
       }
     
     
    }

    This is my FleetOfShips code. It is supposed to be a set of ships moving around together, but for unknown to me reasons it doesn't work. It does compile without any problems but when I run the applet it doesn't do anything.

    Guys please help me, the deadline is for tomorrow and I spent last 7 hours trying to figure this out. I lost all my hope.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    What do you mean when you say it doesn't do anything? What output is in your console when you run this? Does the JApplet display? Do the shapes show up but not move? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    It shows just a single fleet of the squares (5 squares) in a shape defined in the "Ship" class and it doesn't move when I click it.

    I got UML as an example of how to do it that I can paste here but it doesn't really help me. :/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    Where is the code that displays and controls your fleet?

    For better help sooner, I suggest boiling your problem down to an SSCCE. You've posted a few classes here, but instead you should remove any code that doesn't actually contribute to the problem. Can you create a standalone program that displays a rectangle or something that moves around the screen? Then can you add to that code until it shows the same behavior your full program shows? Work away from a working program instead of starting out with broken code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    macko939 (February 20th, 2014)

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    Sorry man I am really new to java, we started it recently in school and I am not the best at it. Sorry for all the problems.

    I decided to start over because my previous project was a massive mess, but it displays an error:

    http://puu.sh/73C9h.png

    this is the actual code for Square and Ship classes:

    package assignment1;
     
    import java.awt.*;
     
    public class Square  
    {   
       private double x,y;    
     
       private Color color;    
     
       private int side;
     
       public Square()
       {
          x = 100;
          y = 100;
          side = 5;
    	  color = Color.orange;
       }
     
     
       public void draw(Graphics g) 
       {
          g.setColor(color);
          g.fillRect( (int)(x), (int)(y), (int)(2*side), (int)(2*side) );
       }
     
       public void setSide(int s)
       {
           side = s;
       }
     
       public int getSide()
       {
           return side;
       }
     
       public int getX()
       {
            return (int) x;
       }
     
       public int getY()
       {
            return (int) y;
       }
     
       public void setX(int xIn)
       {
           x = xIn;
       }
     
       public void setY(int setY)
       {
           y = setY;
       }
    }

    package assignment1;
     
     
    public class Ship extends Square  
    {   
     
       public Ship()
       {
          Square();
       }
     
       public void draw() 
       {
          int side = SuperClass.getSide();
          int x = SuperClass.getX();
          int y = SuperClass.getY();
     
     
          g.setColor(color);
          g.fillRect( (int)(x), (int)(y), (int)(2*side), (int)(2*side) );
     
          g.setColor(color);
          g.fillRect( (int)(x), (int)(y - side), (int)(2*side), (int)(2*side) );
     
          g.setColor(color);
          g.fillRect( (int)(x - side), (int)(y - side), (int)(2*side), (int)(2*side) );
     
          g.setColor(color);
          g.fillRect( (int)(x + side), (int)(y - side), (int)(2*side), (int)(2*side) );
     
          g.setColor(color);
          g.fillRect( (int)(x), (int)(y - (2*side)), (int)(2*side), (int)(2*side) );
       }
     
    }

    Basically I need to have a square class that draws a square in the applet, then a ship class that draws 5 squares in a cross shape.

    I really appreciate your help

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    I'm just guessing because you haven't posted the full text of the error (I'm behind a firewall that blocks unknown urls so posting a screenshot won't work), but I would bet that your problem is here:

    public Ship()
    {
    Square();
    }

    That's incorrect syntax. You can either do this:

    public Ship()
    {
    super();
    }

    Or just leave it out completely, because calls to the no-args constructor are automatically implied.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    macko939 (February 20th, 2014)

  9. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My fleet of squares doesnt work (simple high school work)

    Thanks man, I managed to make it work, i wouldn't be able to do it without your help. Really, thanks <3

Similar Threads

  1. Instanceof/cast doesnt work?
    By sci4me in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2013, 08:53 PM
  2. no errors on japplet but it doesnt work!!!!
    By game06 in forum Java Theory & Questions
    Replies: 16
    Last Post: May 2nd, 2013, 09:20 AM
  3. Why doesnt my constructor work?
    By hairLess in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 25th, 2012, 11:38 AM
  4. [SOLVED] (Beginner) Program doesnt work
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 15th, 2012, 04:28 AM
  5. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 01:37 AM