2013-09-18 2 views
0

Я пытаюсь напечатать самую маленькую область и периметр, но все, что выходит из выхода по периметру и области для круга. Вот мой основной метод (для петель все пути в нижней части):Программа не печатает наименьшую площадь и периметр на рисунке

public static void main (String [] args)//print Figure(Figure[]) 
{ 
    System.out.println("TESTING FIGURES"); 
    System.out.println("===============\n\n\n"); 

    // form an array of figures 
    System.out.println("We form an array of 4 figures"); 
    Figure[] set1 = new Figure[4]; 
    set1[0] = new Circle(10); 
    set1[1] = new Triangle(10, 6, 8); 
    try 
    { 
     set1[2] = new Triangle(5, 12, 7); 
    }  
    catch (IllegalArgumentException e) 
    { 
     System.out.println("We try to form an illegal triangle"); 
    }  

    set1[2] = new Parallelogram(10, 20, Math.PI/3); 
    set1[3] = new Square(6); 

    System.out.println("The array is "); 
    printArray(set1); 

    // find the figures with the largest area, largest perimeter, 
    // smallest area, smallest perimeter 
    Figure smallArea = getSmallestArea(set1); 
    Figure bigArea = getLargestArea(set1); 
    Figure smallPerimeter = getSmallestPerimeter(set1); 
    Figure bigPerimeter = getLargestPerimeter(set1); 

    // print these figures 
    System.out.print("\nThe figure with a largest perimeter is "); 
    printFig(bigPerimeter); 
    System.out.print("\nThe figure with a smallest perimeter is "); 
    printFig(smallPerimeter); 
    System.out.print("\nThe figure with a largest area is "); 
    printFig(bigArea); 
    System.out.print("\nThe figure with a smallest area is "); 
    printFig(smallArea); 
} 

// print an array of figures 
// if the array is null or empty print the message "The array is empty" 
// otherwise print 2 lines 
// that displays the shape, the fields, the perimeter and the area 
// of each item in the array 
public static void printArray(Figure[] figs) 
{ 
    if(figs == null || figs.length == 0) 
    { 
    System.out.println("The array is empty"); 
    } 
    else 
     for(int i = 0; i < figs.length; i++) 
     { 
      printFig(figs[i]); 
     } 

}   

// print the shape, the fields, the perimeter and the area 
// of fig 
// if fig = null, write null 
public static void printFig(Figure fig) 
{ 
    if(fig == null) 
     System.out.println("null"); 
    else 
    { 
     if(fig instanceof Circle) 
     { 
        System.out.print("a circle of "); 
      System.out.println("radius = " + ((Circle)fig).getRadius()); 
     } 

     else if (fig instanceof Triangle) 
     { 
      System.out.print("a triangle with "); 
      System.out.println("sides " + ((Triangle)fig).getSide1() + ", " + ((Triangle)fig).getSide2() + ", " + ((Triangle)fig).getSide3()); 
     } 

      else if (fig instanceof Parallelogram) 
     { 
      System.out.print("a parallelogram with "); 
      System.out.println("sides " + ((Parallelogram)fig).getSide1() + " and " + ((Parallelogram)fig).getSide2() + " and angle of " + 
        ((Parallelogram)fig).getAngle()); 
     } 

      else if (fig instanceof Square) 
     { 
      System.out.print("a square with "); 
      System.out.println("side= " + ((Square)fig).getSide()); 
     } 


     System.out.println("The perimeter is " + fig.getPerimeter() + " and the area is " + fig.getArea()); 
    } 
}   

// return a reference to a figure with the largest perimeter 
// among all figures of arr 
// if arr is null or empty return null 
public static Figure getLargestPerimeter(Figure[] arr) 
{ 
    if(arr == null || arr.length == 0) 
     return null; 
    Figure bigPerimeter = arr[0]; 
    for(int i = 0; i < arr.length; i++) 
    { 
     if(arr[i] != null && arr[i].getPerimeter() > bigPerimeter.getPerimeter()) 
      bigPerimeter = arr[i]; 
    } 
    return bigPerimeter; 
}   

// return a reference to a figure with the smallest perimeter 
// among all figures of arr 
// if arr is null or empty return null 
public static Figure getSmallestPerimeter(Figure[] arr) 
{ 
    if(arr == null || arr.length == 0) 
    return null; 
    Figure smallPerimeter = arr[0]; 
    for(int i = 0; i > arr.length; i++) 
    { 
     if(arr[i] != null && arr[i].getPerimeter() < smallPerimeter.getPerimeter())    
      smallPerimeter = arr[i]; 
    } 

    return smallPerimeter; 
}   

// return a reference to a figure with the largest area 
// among all figures of arr 
// if arr is null or empty return null 
public static Figure getLargestArea(Figure[] arr) 
{ 
    if(arr == null || arr.length == 0) 
     return null; 
    Figure bigArea = arr[0]; 
    for(int i = 0; i < arr.length; i++) 
    { 
     if(arr[i] != null && arr[i].getArea() > bigArea.getArea()) 
     bigArea = arr[i]; 
     } 
    return bigArea; 
}   

// return a reference to a figure with the smallest area 
// among all figures of arr 
// if arr is null or empty return null 
public static Figure getSmallestArea(Figure[] arr) 
{ 
    if(arr == null || arr.length == 0) 
     return null; 
    Figure smallArea = arr[0]; 
    for(int i = 0; i > arr.length; i++) 
    { 
     if(arr[i] != null && arr[i].getArea() < smallArea.getArea()) 
     smallArea = arr[i]; 
    } 
    return smallArea; 
} 

Кроме того, мой класс треугольника не будет печатать стороны и я попытался ввести в IllegalArgumentException но сторона до сих пор не будет печатать. Вот код:

public class Triangle implements Figure 
{ 
    // the fields 
    private double a, b, c; // the 3 fields 

    // the constructor 
    // form a triangle with sides s1,s2,s3 
    // if s1,s2,s3 do not form a triangle, throw an 
    // IllegalArgumentException 
    public Triangle(double s1, double s2, double s3) //throws IllegalArgumentException 
    { 
     s1= a;//hypotenuse 
     s2= b;//base 
     s3= c;//height 
     if(a + b < c || a + c < b || c + b < a) 
     { 
      throw new IllegalArgumentException 
       ("We try to form an illegal triangle"); 
     } 
    } 

    // methods that return the 3 sides 
    public double getSide1() 
    { 
     return a; 
    } 

    public double getSide2() 
    { 
     return b; 
    } 

    public double getSide3() 
    { 
     return c; 
    } 

    // @ return the perimeter 
    @Override 
    public double getPerimeter() 
    { 
     return a + b + c; 
    } 

    // @return the area 
    @Override 
    public double getArea() 
    { 
     return (0.5) * b * c; 
    } 
} 

ответ

6

Второе выражение в for цикле означает «держать выполнение цикла, пока условие истинно» - не «держать выполнение, пока это не правда». Это означает, что если ваш цикл выглядит следующим образом:

for(int i = 0; i > arr.length; i++) 

вы никогда не будете ничего выполнять в цикле, так как i начинается как 0 и 0 > arr.length является false и поэтому цикл останавливается сразу. Изменить > на <.

+0

Большое вам спасибо за помощь, а также объяснение и разъяснение моей ошибки. если у вас есть что-то для класса треугольников, что также будет полезно. – user2792215

+0

Если вы пытаетесь сохранить параметр 's1' в поле' a', возможно, 'a = s1' вместо' s1 = a'? – ajb

Смежные вопросы