Reklama
Pokazuje wyniki od 1 do 3 z 3

Temat: Java (podstawy) problem z programem

  1. #1
    Avatar Absynt
    Data rejestracji
    2009
    Wiek
    30
    Posty
    1,266
    Siła reputacji
    15

    Domyślny Java (podstawy) problem z programem

    siema, mam zadanie domowe na jutro ale mam problem z 1 rzecza.

    Otoz mam stworzyc 3 klasy, 1 z kwadratem ktory sie porusza, 2 ktora stworzy 5 kwadratow na raz poruszajacych sie razem i 3 ktora stworzy kilka takich "zestawow" kwadratow.

    pierwsze 2 mam zrobione, ale 3 mi nie smiga w ogole.

    Prosze pomozcie bo stracilem nadzieje


    Kwadrat (dziala)
    Kod:
    package assignment1;
    
    /*
        * Square class
        * Author:  Tony Cooke
        * Date:    2009
        */
    
    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
    Grupa kwadratow (dziala):

    Kod:
    package assignment1;
    
    import java.awt.*;
    
    public class FleetOfShips
    {
        private Square[] fleetMembers; // declare fleetMembers as an array of Square
        private int total = 5; // Max number of squares in fleet
        
        FleetOfShips() // Constructor
        {
            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); 
            }
        }    
        
    }

    Grupa grup (nie dziala):

    Kod:
    package assignment1;
    
    import java.awt.*;
    
    public class FleetOfFleets
    {
        private FleetOfShips[] fleetMembers; // declare fleetMembers as an array of FleetOfShips
        private int total = 5; // Max number of squares in fleet
     
        FleetOfFleets() // Constructor
        {
            fleetMembers = new FleetOfShips[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 FleetOfShips();  // create a FleetOfShips 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); 
            }
        }    
        
    }

  2. #2
    Avatar bercik
    Data rejestracji
    2005
    Położenie
    Rojca
    Wiek
    35
    Posty
    406
    Siła reputacji
    20

    Domyślny

    Kod:
    private FleetOfShips[] fleetMembers; // declare fleetMembers as an array of FleetOfShips
    Masz tu tablicę tablic, więc powinieneś iterować po każdej tablicy statków, a następnie po każdym statku z osobna:
    Kod:
    FleetOfFleets() // Constructor
    {
    	fleetMembers = new FleetOfShips[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
    	{
    		for (int j = 0; j < total; j++) 
    		{	
    			fleetMembers[i].fleetMembers[j] = new FleetOfShips();  // create a FleetOfShips object for location [i] in the array
    			fleetMembers[i].fleetMembers[j].setX (i * 10); // multiply i by 10, and pass this to the setX method
    			fleetMembers[i].fleetMembers[j].setY (i * 10); // multiply i by 10, and pass this to the setY method
    		}
    	}
    }
    To samo w draw i gosouth

  3. Reklama
  4. #3
    Avatar Bazan
    Data rejestracji
    2008
    Wiek
    31
    Posty
    1,909
    Siła reputacji
    18

    Domyślny

    A tak na prawde to powinien zrobic metode w fleetofships, ktora iteruje wsszystkie swoje statki,
    a w fleetoffleets zrobic metode ktora iteruje wszystkie swoje zestawy i wykonuje dla nich swoja metode.

    Nie powinien w metodzie iterujacej po zestawie bezposrednio iterowac statkow, tym powinien sie zajac zestaw a nie wlasnie zestaw zestawow.

    @edit
    Do przeniesienia do działu sprzęt i oprogramowanie -> programowanie
    Ostatnio zmieniony przez Bazan : 21-02-2014, 15:33
    Dreaming by drumming. ˆˆ™

Reklama

Informacje o temacie

Użytkownicy przeglądający temat

Aktualnie 1 użytkowników przegląda ten temat. (0 użytkowników i 1 gości)

Podobne tematy

  1. Problem z programem?
    Przez Perkun w dziale Sprzęt i oprogramowanie
    Odpowiedzi: 0
    Ostatni post: 26-01-2015, 13:56
  2. Problem z programem(Gcody)
    Przez Halunek w dziale Szkoła i nauka
    Odpowiedzi: 0
    Ostatni post: 25-12-2014, 16:57
  3. Problem z programem AEGISUB
    Przez Krychu99 w dziale Sprzęt i oprogramowanie
    Odpowiedzi: 10
    Ostatni post: 03-04-2013, 14:08
  4. problem z programem w c++
    Przez Criss26 w dziale Sprzęt i oprogramowanie
    Odpowiedzi: 7
    Ostatni post: 14-01-2013, 22:28
  5. [Podstawy] Podstawy związane z bazą danych MySQL dla OpenTibii*
    Przez Killavus w dziale Artykuły developerskie
    Odpowiedzi: 24
    Ostatni post: 27-06-2009, 22:32

Zakładki

Zakładki

Zasady postowania

  • Nie możesz pisać nowych tematów
  • Nie możesz pisać postów
  • Nie możesz używać załączników
  • Nie możesz edytować swoich postów
  •