CS2 Assignment A1S The Simple Shapes Package Overview In this project you will create a simple shapes package consisting of three interfaces and three classes. In abstract terms, you will establish classes for circles, squares, and rectangles. I will provide an interface, a class which implements the interface, and a test program for circles. For squares and rectangles, I will provide the interfaces and you will provide the classes and the test programs. The three interfaces and the three classes which implement them will be placed in a source package called shapes, which we will make good use of later. The three test programs will be placed in a source package called testers. This project presents you with a nice opportunity employ the problem solving strategy of working by analogy to help create a nice little package. Demos SCircle demo... Testing the functionality of the SCircle class... Create a circle of radius 10 and display it: <Circle: radius=10.0> Display the radius, diameter, area, perimeter. radius = 10.0 diameter = 20.0 area = 314.1592653589793 perimeter = 62.83185307179586 Expand the circle by 5 and display it: <Circle: radius=15.0> Shrink the circle by 10 and display it: <Circle: radius=5.0> SSquare demo... Testing the functionality of the SSquare class... Create a square of side 100 and display it: <Square: side=100.0> Display the side, diagonal, area, and perimeter. side = 100.0 diagonal = 141.4213562373095 area = 10000.0 perimeter = 400.0 Expand the square by 5 and display it: <Square: side=105.0> Shrink the side by 10 and display it: <Square: side=95.0> SRectangle demo... Testing the functionality of the SRectangle class... Create a rectangle of height 40 width 50 and display it: <Rectangle: height=40.0 width=50.0> Display the sides, diagonal, area, and perimeter. height = 40.0 width = 50.0 diagonal = 64.03124237432849 area = 2000.0 perimeter = 180.0 Expand the height by 11 and width by 12 and display: <Rectangle: height=51.0 width=62.0> Shrink the height by 9 and width by 8 and display: <Rectangle: height=42.0 width=54.0>
Code SCircleADT.java... / Abstract Data Type for a simple Circle class. public interface SCircleADT { public double radius(); public double diameter(); public double area(); public double perimeter(); public void expand(double amount); public void shrink(double amount); public String tostring(); SCircle.java... / Simple Circle class. public class SCircle implements SCircleADT { private double radius; Creates a new SCircle instance. @param r the radius of the circle public SCircle(double r) { this.radius = r; Computes the radius of the circle. @return the radius of the circle public double radius() { return radius; Computes the area of the circle. @return the area of the circle public double area() { return Math.PI Math.pow(radius,2);
Computes the perimeter of the circle. @return the perimeter of the circle public double perimeter() { return Math.PI diameter(); Computes the diameter of the circle. @return the diameter of the circle public double diameter() { return radius2; Expand the circle by a given amount. @param a is the amount by which the radius is increased public void expand(double a) { radius = radius + a; Shrink the circle by a given amount. @param a is the amount by which the radius is decreased public void shrink(double a) { radius = radius - a; Computes a textual representation of the circle. @return a textual representation of the circle public String tostring() { return "<Circle: radius=" + radius + ">"; SCircleTester.java... / SCircleTester is a program to test the SCircle functionality. package testers; import shapes.scircle; public class SCircleTester { Test the functionality of the SCircle class. @param args the command line arguments public static void main(string[] args) { System.out.println( "Testing the functionality of the SCircle class..."); System.out.print("Create a circle of radius 10 and display it: "); SCircle c = new SCircle(10); System.out.println(c.toString());
System.out.println( "Display the radius, diameter, area, perimeter."); System.out.println("radius = " + c.radius()); System.out.println("diameter = " + c.diameter()); System.out.println("area = " + c.area()); System.out.println("perimeter = " + c.perimeter()); System.out.print("Expand the circle by 5 and display it: "); c.expand(5); System.out.println(c.toString()); System.out.print("Shrink the circle by 10 and display it: "); c.shrink(10); System.out.println(c.toString()); SSquareADT.java... / Abstract Data Type for a simple Square Class public interface SSquareADT { public double side(); public double area(); public double perimeter(); public double diagonal(); public void expand(double amount); public void shrink(double amount); public String tostring(); SRectangleADT.java... / Abstract Data Type for a simple Rectangle class. public interface SRectangleADT { public double height(); public double width(); public double area(); public double perimeter(); public double diagonal(); public void expand(double h, double w); public void shrink(double h, double w); public String tostring(); Tasks
1. Get into Netbeans. 2. Choose File New Project. 3. Establish a new project by stepping through the New Project wizard. (a) On the Choose Project form: i. Select Catategories = Java and Projects = Java Class Application and then hit Next. i. Type Shapes into the Project Name Field. 4. Notice that there is no Main.java class, as this is not an application. 5. Choose File New File. 6. Establish the SCircleADT interface by stepping through the New File wizard. (a) On the Choose File Type form: i. Select Catategories = Java and File Type = Java Interface ii. Hit Next. i. Make Class Name = SCircleADT and Package = shapes. 7. Modify the SCircleADT.java interface template so that, except for the author information, it matches that given above. 8. Choose File New File. 9. Establish the SCircle class by stepping through the New File wizard. (a) On the Choose File Type form: i. Select Catategories = Java and File Type = Java Class ii. Hit Next. i. Make Class Name = SCircle and Package = shapes. 10. Modify the SCircle.java interface template so that, except for the author information, it matches that given above. 11. Choose File New File. 12. Establish the SCircleTester class by stepping through the New File wizard. (a) On the Choose File Type form: i. Select Catategories = Java and File Type = Java Main Class ii. Hit Next. i. Make Class Name = SCircleTester and Package = testers. 13. Modify the SCircleTester.java interface template so that, except for the author information, it matches that given above.
14. Run the test program. Compare the output with that required. Fix as necessary. 15. Working by analogy, establish SSquareADT.java, SSquare.java, and SSquareTester.java in the appropriate packages. Note that SSquareADT.java must be used as provided, and that it is up to you to write the other two programs. Run the test program. Compare the output with that required. Fix as necessary. 16. Working by analogy, establish SRectangleADT.java, SRectangle.java, and SRectangleTester.java in the appropriate packages. Note that SRectangleADT.java must be used as provided, and that it is up to you to write the other two programs. Run the test program. Compare the output with that required. Fix as necessary. 17. Run the javadoc on the project in order to generate the HTML documentation for this package. (To generate the documentation for a package using the Javadoc tool you can click on the Generate Javadoc item associated with the name of the package!) Questions 1. What is an interface? 2. What does it mean for a class to implement an interface? 3. What does it mean in number 15 to work by analogy. 4. What does it mean in number 16 to work by analogy. 5. What does the Javadoc tool do? 6. How many packages did you establish in this project? 7. How many interfaces did you establish in this project? 8. How many classes did you establish in this project? 9. How many objects does each test program create when run?