2016-10-30 8 views
0

Also schreibe ich eine Konsole-basierte Shape Creator, die Formen mit Benutzereingaben erstellt. Wenn ein Benutzer 5-Platz schaffen will, erstellen das Programm ein ArrayList und Daten von Square-Klasse erhalten, die ShapeID, Gebiet hat usw.Eine Spalte aus einer ArrayList holen

import java.util.ArrayList; 
import javax.swing.JOptionPane; 

public class ShapeMaker { 


    public ShapeMaker(){ 


     create(); 

    } 

    public void create(){ 
     //creates list of arrays within shapes. 
     ArrayList<Circle>circles=new ArrayList<Circle>(); 
     ArrayList<Square>squares=new ArrayList<Square>(); 
     ArrayList<Rectangle>rectangles=new ArrayList<Rectangle>(); 

     //this loop is dangerously infinity but I had to open the program for choices. 
     while(true){ 
      String[] choices={"Create Random Shapes","Circles","Squares","Rectangles", 
        "Print All Shapes","Print Rectangles","Print Squares", 
        "Print Circles","Exit"}; 
      String optionMenu = (String)JOptionPane.showInputDialog(
        null, 
        "Choose what you desire", 
        "Customized Dialog", 
        JOptionPane.PLAIN_MESSAGE, 
        null, 
        choices, 
        choices[0]); 


      if (optionMenu==choices[0]){ 
       //    String input4=(String)JOptionPane.showInputDialog(null,"How Many Shape ?","Question", 
       //      JOptionPane.QUESTION_MESSAGE); 
       //    int inputLength4=Integer.parseInt(input4); 
       //    for (int i=0;i<inputLength4;i++){ 
       // 
       //     circles2[i]=new Circle(); 
       //     circles.add(circles2[i]); 
       //     squares2[i]=new Square(); 
       //     squares.add(squares2[i]); 
       //     rectangles2[i]=new Rectangle(); 
       //     rectangles.add(rectangles2[i]); 
       // 
       //    } 
       //    JOptionPane.showMessageDialog(null,inputLength4+" shapes have been created successfully."); 
      } 
      else if (optionMenu==choices[1]) { 
       String input=(String)JOptionPane.showInputDialog(null,"How Many Circle ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength=Integer.parseInt(input); 
       Circle[]circles2=new Circle[inputLength]; 
       if (circles.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are circles in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         circles.remove(circles2); 
        } 
        else{ 
         for (int i=0;i<inputLength;i++){ 
          circles2[i]=new Circle(); 
          circles.add(circles2[i]); 
         } 
        } 

       } 
       else{ 


        for (int i=0;i<circles2.length;i++){ 
         circles2[i]=new Circle(); 
         circles.add(circles2[i]); 
        } 
        JOptionPane.showMessageDialog(null,inputLength+" circle have been created successfully."); 
       } 

      } 
      else if (optionMenu==choices[2]) { 
       String input3=(String)JOptionPane.showInputDialog(null,"How Many Squares ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength3=Integer.parseInt(input3); 
       Square[]squares2=new Square[inputLength3]; 
       if (squares.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are squares in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         squares.remove(squares2); 
        } 
        else{ 
         for (int i=0;i<inputLength3;i++){ 
          squares2[i]=new Square(); 
          squares.add(squares2[i]); 
         } 
        } 

       } 
       else{ 

        for (int i=0;i<inputLength3;i++){ 
         squares2[i]=new Square(); 
         squares.add(squares2[i]); 
        } 
       } 
       JOptionPane.showMessageDialog(null,inputLength3+" square have been created successfully."); 


      } 
      else if (optionMenu==choices[3]) { 
       String input2=(String)JOptionPane.showInputDialog(null,"How Many Rectangles ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength2=Integer.parseInt(input2); 
       Rectangle[]rectangles2=new Rectangle[inputLength2]; 
       if (rectangles.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are rectangles in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         rectangles.remove(rectangles2); 
        } 
        else{ 
         for (int i=0;i<inputLength2;i++){ 
          rectangles2[i]=new Rectangle(); 
          rectangles.add(rectangles2[i]); 
         } 
        } 

       } 
       else{ 
        for (int i=0;i<inputLength2;i++){ 
         rectangles2[i]=new Rectangle(); 
         rectangles.add(rectangles2[i]); 

        } 
        JOptionPane.showMessageDialog(null,inputLength2+" rectangle have been created successfully."); 
       } 
      } 
      else if (optionMenu==choices[4]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<rectangles.size();i++){ 
        System.out.println(rectangles.get(i)); 
       } 
       for (int i=0;i<squares.size();i++){ 
        System.out.println(squares.get(i)); 
       } 
       for (int i=0;i<circles.size();i++){ 
        System.out.println(circles.get(i));     
       } 


      } 
      else if (optionMenu==choices[5]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<rectangles.size();i++){ 
        System.out.println(rectangles.get(i)); 

       } 
       System.out.print("There are "+rectangles.size()+" rectangle with total area of "); 
      } 
      else if (optionMenu==choices[6]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<squares.size();i++){ 
        System.out.println(squares.get(i)); 

       } 
       System.out.print("There are "+squares.size()+" square with total area of "); 

      } 
      else if (optionMenu==choices[7]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<circles.size();i++){ 
        System.out.println(circles.get(i)); 

       } 
       System.out.print("There are "+circles.size()+" circle with total area of "); 
      } 
      else if(optionMenu==choices[8]) { 
       System.exit(1); 

      } 
     } 

    } 

    public static void main(String [] args){ 
     new ShapeMaker(); 
     //  System.out.println(shape); 


    } 
} 

Die Frage ist, muss ich Werte aus dem Bereich Reihe bekommen und Gesamtfläche berechnen von diesen Formen. Output

Also, wie bekomme ich alle Bereiche der erstellten ArrayList?

+0

Können Sie Ihren vollständigen Code veröffentlichen? Bitte geben Sie den Kontext von else an. –

+0

Sehen Sie sich den Code an, der alle Quadrate druckt. Sie können den 'Bereich' sehen, der gedruckt wird. Kannst du die Gesamtfläche aus diesem Screenshot berechnen? Ich wette du kannst. Jetzt denken Sie, wie Sie das in Code – borowis

+0

@ CarlosVázquezLosada den vollständigen Code hinzufügen würde. – Egrimo

Antwort

1

Greatings, Egrimo.

Wie Sie gesagt, Sie haben eine Klasse Square, die die Attribute alle hat: ID, Shape, Area, Full Shape, Distance und Properties.

Wenn Sie das tun:

System.out.println(squares.get(i)); 

Und Sie wollen Area Attribut zuzugreifen, müssen Sie tun:

  1. Erstellen Sie eine Methode getArea() in Ihrem Square-Klasse. Diese Methode muss öffentlich sein und die Fläche des Quadrats zurückgeben.
  2. Anruf getArea() -Methode von Ihrem System.out.printin die quadratische Fläche drucken:

System.out.println(squares.get(i).getArea()); Wird Ihre quadratische Fläche drucken.

EDIT:

Wenn Sie nicht wissen, wie ein getArea() -Methode erstellen, in diesem Beispiel aussehen:

public class Square{ 
    int area; 
    int id; 
    // other attributes 

    // constructor 

    public int getArea(){ // This method will return your square area 
     return this.area; 
    } 
} 

einen guten Tag!

+0

Vielen Dank für die ausführliche Antwort. Meine Bereichsmethode wird aus der IShape-Klasse implementiert und ist ein öffentlicher Doppelbereich() { \t \t // Berechnet den Bereich des Rechtecks. \t \t Rücksprunggröße * Größe; \t} – Egrimo

Verwandte Themen