Public Class Airplan Data Fieldsensing Engrudder Ru
Public Class Airplan Data Fieldsensing Engrudder Ru
Public class Airplan { / Data Fields / Enging eng; Rudder rud, Wing[] wings = new wing [2]; .... } ---------------------------------------------- public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() == obj.getClass()) { Employee other = (Employee) obj; return name.equals(other.name) && address.equals(other.address); } else { return false; } } AbstrtactShape.java AbstrtactShape.java // / Listing 1.6 @author Koffman and Wolfgang / package KW . CH01 . Shapes ; / Abstract class for a geometric shape. / public abstract class AbstrtactShape implements Shape { / The name of the shape / private final String shapeName ; / Initializes the shapeName. @param shapeName the kind of shape / public AbstrtactShape ( String shapeName ) { this . shapeName = shapeName ; } / Get the kind of shape. @return the shapeName / public String getShapeName () { return shapeName ; } @ Override public String toString () { return "Shape is a " + shapeName ; } } // ComputeAreaAndPerim.java ComputeAreaAndPerim.java // / Listing 1.8 @author Koffman and Wolfgang / package KW . CH01 . Shapes ; import java . util . Scanner ; / Computes the area and perimeter of selected figures. / public class ComputeAreaAndPerim { / The main program performs the following steps. 1. It asks the user for the type of figure. 2. It asks the user for the characteristics of that figure. 3.
It computes the perimeter. 4. It computes the area. 5. It displays the result. @param args The command line arguments -- not used / public static void main ( String args []) { AbstrtactShape myShape ; double perimeter ; double area ; myShape = getShape (); // Ask for figure type myShape . readShapeData (); // Read the shape data perimeter = myShape . computePerimeter (); // Compute perimeter area = myShape . computeArea (); // Compute the area displayResult ( area , perimeter ); // Display the result System . exit ( 0 ); // Exit the program } / Ask the user for the type of figure. @return An instance of the selected shape / public static AbstrtactShape getShape () { Scanner in = new Scanner ( System . in ); System . out . println ( "Enter C for circle" ); System . out . println ( "Enter R for Rectangle" ); System . out . println ( "Enter T for Right Triangle" ); String figType = in . next (); switch ( figType ) { case "C" : case "c" : return new Circle (); case "R" : case "r" : return new Rectangle (); case "T" : case "t" : return new RtTriangle (); default : return null ; } } / Display the result of the computation. @param area The area of the figure @param perim The perimeter of the figures / private static void displayResult ( double area , double perim ) { System . out . printf ( "The area is %.2f%nThe perimeter is %.2f%n" , area , perim ); } } // lab001.png lab01.png lab1.png Rectangle.java Rectangle.java /
/ / Listing 1.7 @author Koffman and Wolfgang / package KW . CH01 . Shapes ; import java . util . Scanner ; / Represents a rectangle. Extends AbstrtactShape. / public class Rectangle extends AbstrtactShape { // Data Fields / The width of the rectangle. / private double width = 0 ; / The height of the rectangle. / private double height = 0 ; // Constructors / Constructs a default Rectangle / public Rectangle () { super ( "Rectangle" ); } / Constructs a rectangle of the specified size. @param width the width @param height the height / public Rectangle ( double width , double height ) { super ( "Rectangle" ); this . width = width ; this . height = height ; } // Methods / Get the width. @return The width / public double getWidth () { return width ; } / Get the height. @return The height / public double getHeight () { return height ; } / Set the width @param width The new width / public void setWidth ( double width ) { this . width = width ; } / Set the height @param height The new height / public void setHeight ( double height ) { this . height = height ; } / Compute the area. @return The area of the rectangle / @ Override public double computeArea () { return height width ; } / Compute the perimeter. @return The perimeter of the rectangle / @ Override public double computePerimeter () { return 2 ( height + width ); } / Read the attributes of the rectangle. / @ Override public void readShapeData () { Scanner in = new Scanner ( System . in ); System . out . println ( "Enter the width of the Rectangle" ); width = in . nextDouble (); System . out . println ( "Enter the height of the Rectangle" ); height = in . nextDouble (); } / Create a string representation of the rectangle. @return A string representation of the rectangle / @ Override public String toString () { return super . toString () + ": width is " + width + ", height is " + height ; } } // Shape.java Shape.java package KW . CH01 . Shapes ; / An interface for a geometric shape
@author Koffman and Wolfgang / public interface Shape { // abstract methods /
Compute the area of the shape @return The area of the shape
/ double computeArea (); / Compute the perimeter of the shape
@return The perimeter of the shape / double computePerimeter (); /
Prompt the user for the parameters of the shape and read them.
/ void readShapeData (); / Get the width of the shape
@return The width of the shape / double getWidth (); /
Get the height of the shape @return The height of the shape */ double getHeight (); }