Вы находитесь на странице: 1из 5

/** * */ package GEOFIGURES; import java.util.

*; /** * @author Stephanie Little * */ public class FigureCalculator { /** * @param args */ public static void main(String[] args) { // Declare a TwoDFigure object, using the default constructor // IMPORTANT NOTES: only declare the variable, but NOT YET allocate spac e for it TwoDFigure a2DFigure; // Create an object of class Scanner to read inputs Scanner inReader = new Scanner(System.in); // Read the figure code: 1 for circle, 2 for square, 3 for rectangle System.out.println("Enter figure code - 1 for Circle, 2 for Square, or 3 for Rectangle: "); int figureCode = inReader.nextInt(); // Verify to be sure figure code is correct: 1, 2, or 3. It cannot be an y other value while (figureCode != 1 && figureCode != 2 && figureCode != 3) { System.out.println("Figure code must be 1, 2, or 3!!!"); System.out.println("Enter figure code - 1 for Circle, 2 for Square, or 3 for Rectangle: "); figureCode = inReader.nextInt(); } // At this point, figure code is correct: 1, 2, or 3 if (figureCode == 1) { // // // assign it to the variable // a TwoDFigure variable // Handle the case of a circle IMPORTANT NOTES: Figure is a circle --> create a new Circle object and a2DFigure Polymorphism is used here: assign a Circle object to Circle is a subclass (child) of TwoDFigure a2DFigure = new Circle(); handleCircleObject(inReader, (Circle) a2DFigure) ; } else if (figureCode == 2) { // Handle the case of a square // IMPORTANT NOTES: // Figure is a square--> create a new Square obj

ect and assign it to the variable a2DFigure // Polymorphism is used here: assign a Square ob ject to a TwoDFigure variable // Square is a subclass (child) of TwoDFigure a2DFigure = new Square(); handleSquareObject(inReader, (Square) a2DFigure) ; } else { // Handle the case of a rectangle // IMPORTANT NOTES: // Figure is a rectangle--> create a new Rectang le object and assign it to the variable a2DFigure // Polymorphism is used here: assign a Rectangle object to a TwoDFigure variable // Rectangle is a subclass (child) of TwoDFigure a2DFigure = new Rectangle(); handleRectangleObject(inReader, (Rectangle) a2DF igure); } // Display figure data + perimeter, area double perimeter = a2DFigure.calculatePerimeter(); // Polymorphism is used here double area = a2DFigure.calculateArea(); // Polymorphism is used here String unit = a2DFigure.getLengthUnit(); a2DFigure.displayFigureData(); // Polymorphism is used here System.out.printf("; perimeter = %5.2f %-2s; area = %5.2 f %-2s \n", perimeter, unit, area, "square " + unit); } // End of main // This private method handles the case of a circle private static void handleCircleObject(Scanner inReader, Circle aCircle) { // Read measurement of radius System.out.println("Enter radius: "); double inRadius = inReader.nextDouble(); // Check to be sure inRadius>= 0 if (inRadius < 0) { System.out.println("Radius cannot be negative!") ; System.out.println("Enter radius: "); inRadius = inReader.nextDouble(); } // MUST add this extra statement to bring Scanner to the start of the next line String temp = inReader.nextLine();

System.out.println("Enter measurement unit - in, ft, cm, or m:"); String unit = inReader.nextLine(); if (!unit.equals("in") && !unit.equals("ft") && !unit.eq uals("cm") && !unit.equals("m")) { System.out.println("Unit cannot be recgonized!") ; System.out.println("Enter measurement unit - in, ft, cm, or m:"); unit = inReader.next(); } // Set the length unit aCircle.setLengthUnit(unit); // Set the value of the radius of the circle aCircle.setRadius(inRadius); }// End of handleCircleObject // This private method handles the case of a square private static void handleSquareObject(Scanner inReader, Square aSquare) { // Read measurement of side length System.out.println("Enter sideLength: "); double inSideLength = inReader.nextDouble(); // Check to be sure sideLength>= 0 if (inSideLength < 0) { System.out.println("SideLength cannot be negativ e!"); System.out.println("Enter sideLength: "); inSideLength = inReader.nextDouble(); } // MUST add this extra statement to bring Scanner to the start of the next line String temp = inReader.nextLine(); System.out.println("Enter measurement unit - in, ft, cm, or m:"); String unit = inReader.nextLine(); if (!unit.equals("in") && !unit.equals("ft") && !unit.eq uals("cm") && !unit.equals("m")) { System.out.println("Unit cannot be recgonized!") ; System.out.println("Enter measurement unit - in, ft, cm, or m:"); unit = inReader.next(); } // Set the length unit aSquare.setLengthUnit(unit);

// Set the value of the side length aSquare.setSideLength(inSideLength); } // End of handleSquareObject // This private method handles the case of a rectangle private static void handleRectangleObject(Scanner inReader, Rect angle aRectangle) { // Read measurement of length System.out.println("Enter Length: "); double Length = inReader.nextDouble(); // Check to be sure Length>= 0 if (Length < 0) { System.out.println("Length cannot be negative!") ; System.out.println("Enter Length: "); Length = inReader.nextDouble(); } // MUST add this extra statement to bring Scanner to the start of the next line String temp = inReader.nextLine(); // Read measurement of width System.out.println("Enter width: "); double Width = inReader.nextDouble(); // Check to be sure Width>= 0 if (Width < 0) { System.out.println("Width cannot be negative!"); System.out.println("Enter Width: "); Width = inReader.nextDouble(); } // MUST add this extra statement to bring Scanner to the start of the next line temp = inReader.nextLine(); System.out.println("Enter measurement unit - in, ft, cm, or m:"); String unit = inReader.nextLine(); if (!unit.equals("in") && !unit.equals("ft") && !unit.eq uals("cm") && !unit.equals("m")) { System.out.println("Unit cannot be recgonized!") ; System.out.println("Enter measurement unit - in, ft, cm, or m:"); unit = inReader.next(); } // Set the length unit aRectangle.setLengthUnit(unit);

// Set the value of the length aRectangle.setLength(Length); // Set the value of the width aRectangle.setWidth(Width); } // End of handleRectangleObject } // End of class FigureCalculator

Вам также может понравиться