2016-04-12 17 views
0
class TestShapes { 

    public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); // creates the scanner class 
    System.out.print("Enter the numer of shapes: "); // asks user for input 
    int N = input.nextInt(); // stores the user input as N 
    Shape [] myShape = new Shape[N]; // will create as many shapes (N) in an array 
    for(int i=0;i<N;i++) 
    { 
     System.out.println("Enter the choice (Square, Rectangle, Circle):"); 
     int select = input.nextInt(); 
     if(select == 1) 
     { 
      //user wanted a Square 
      System.out.print("Enter the color: "); 
      String c = input.next(); 
      System.out.print("Enter the side length of the square: "); 
      double s = input.nextDouble(); 
      myShape[i] = new Square(c,s); 

     } 
     else if(select == 2) 
     { 
      //user wanted a Rectangle 
      System.out.print("Enter the color: "); 
      String c = input.next(); 
      System.out.print("Enter the length of the rectangle: "); 
      double l = input.nextDouble(); 
      System.out.print("Enter the width of the rectangle: "); 
      double w = input.nextDouble(); 
      myShape[i] = new Rectangle(c,l,w); 

     } 
     else if(select == 3) 
     { 
      //user wanted a Circle 
      System.out.print("Enter the color: "); 
      String c = input.next(); 
      System.out.print("Enter the radius of the circle: "); 
      double r = input.nextDouble(); 
      myShape[i] = new Circle(c,r); 

     } 
} 
    for(int i=0;i<N;i++) //this will print the details 
    { 
     System.out.println("\nShape "+ (i+1)+ ":"); 
     myShape[i].print(); 
    } 
} 

} 

class Shape { 

String color; 
double area; 

public Shape(){ // default constructor 
    color = "red"; 
} 

public Shape(String c){ // constructor 
    color =c; 
} 

public String getColor(){ //accessors 
    return color; 
} 

public void setColor(String c){//mutators 
    color=c; 
} 
//print method 
public void print() 
{ 
    System.out.println("Color: "+ getColor()); 
} 
public double area(){ 
    return area; 
} 
} 

class Square extends Shape{ // inherits from the shape class 

double sideLength; 

public Square(){//default constructor 
    super(); 
    sideLength = 1; 
} 

public Square(String c, double s){ //constructor 
    super(c); 
    sideLength = s; 
} 

public double getSideLength(){//accessor 
    return sideLength; 
} 

public void setSideLength(double s){//mutator 
    sideLength = s; 
} 

public double area(){ 
    return sideLength * sideLength; //calculates the area of the square 
} 

public void print() 
{ 
    super.print(); 
    System.out.println("Side length: " + getSideLength() 
     + "\nArea: " + area); 

} 
} 

class Rectangle extends Shape{// inherits from the shape class 
    double length; 
    double width; 

public Rectangle(){//default constructor 
    super(); 
    length = 1; 
    width = 1; 
} 

public Rectangle(String c, double l, double w){ //constructor 
    super(c); 
    length = l; 
    width = w; 
} 

public double getLength(){//accessor 
    return length; 
} 

public double getWidth(){//accessor 
    return width; 
} 

public void setLength(double l){//mutator 
    length = l; 
} 
public void setSideLength(double w){//mutator 
    width = w; 
} 

public double area(){ 
    return length * width; //calculates thea area of the rectangle 
} 

public void print() 
{ // prints out the information 
    super.print(); 
    System.out.println("Length: " + getLength() + "\nWidth:"+ getWidth() + "\nArea: "+ area); 

} 

} 

class Circle extends Shape{// inherits from the shape class 

    double radius; 
public Circle(String c, double r){//default constructor 
    super(c); 
    radius = 1; 
} 

public Circle(double r){ //constructor 
    super(); 
    radius = r; 
} 

public double getRadius(){//accessor 
    return radius; 
} 

public void setRadius(double r){//mutator 
    radius = r; 
} 

public void print() 
{ // prints out the information 
    super.print(); 
    System.out.println("Radius: " + getRadius() + "\nArea:"+ area); 

} 
public double area(){ 
return 3.14159 * (radius * radius); //calculates the area of the circle 
} 
} 

Bölgeyi geri almak için her türlü yolu denedim ve denediğimde denemeyeceğim. Her şey iyi çalışıyor, her şeyi doğru şekilde yazdırıyor ama bölgeyi değil. herhangi bir tavsiye? Bu neden bölgeye geri dönmeyecek?

şekillerin sayısını girin: 2

Şekli 1: Renk kırmızı karenin kenar uzunluğunu giriniz: renk girin: 1 seçim (Kare, Dikdörtgen, Daire) girin : kırmızı kenar uzunluğu: 2.0 Alan: 0.0

cevap

1

sen alanın hesaplanması değil, yazdırma statment alanını() kullanmalıdır

System.out.println("Side length: " + getSideLength() + "\nArea: " + area()); 
+0

alan, şekli uzatan sınıfların (kare, dikdörtgen, daire) her birinde hesaplanır. Her sınıfın altındalar. hesaplandıktan sonra iade edilir. Yukarıdaki koddan 'public double area() { return sideLength * sideLength; // kare } –

+0

@ 154 karakterinin alanını hesaplar. doğru, ancak yazdırma alanınızdaki değişken alanı çağırıyorsunuz, işlev alanı() –

0

area() işlevini çağırmak yerine başlatılmamış değişken area'u yazdırıyorsunuz.

System.out.println("Side length: " + getSideLength() + "\nArea: " + area()); 

Bu işe yarayacak, ancak aynı ada sahip işlev ve değişkenleri kullanmaktan kaçınmalısınız. getArea(), daha iyi bir işlev adı olurdu.

+0

çalıştı, teşekkürler. Hasta, gelecekteki bir geri dönüş için kullanacaktır –

+0

Sorununuzu çözmenize yardımcı olsaydı, cevaplardan birini kabul etmelisiniz. –