lab-assignment/code/src/polymor/Polymor.java lab-assignment/code/src/polymor/Polymor.java /* * To change this license header, choose License Headers in Project Properties....

explanation attached in the pdf file in the zip


lab-assignment/code/src/polymor/Polymor.java lab-assignment/code/src/polymor/Polymor.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package polymor; import shape.Shape; import shape.polygon.Rectangle; import shape.polygon.Triangle; import shape.circle.Circle; import java.util.Scanner; /**  *  * @author walsanie  */ public class Polymor {     public static Shape makeShape() {                  Scanner sc = new Scanner(System.in);                  System.out.print("Enter 1 for Rectangle, 2 for Triangle "                 + "and 3 for Circle: ");         int sTy = sc.nextInt();         // Skip the end of line that was entered in the previous step.         String line = sc.nextLine();                  Shape sh = null;                  switch (sTy) {             case 1:                 System.out.print("Enter rectangle sides (real numbers) "                         + "separated by comma: ");                 line = sc.nextLine();                 String[] strSides = line.split(",");                 double[] doubleSides = {Double.parseDouble(strSides[0]),                                          Double.parseDouble(strSides[1]),                                         Double.parseDouble(strSides[2]),                                         Double.parseDouble(strSides[3])};                 // Create a rectangle instance:                 sh = new Rectangle(doubleSides[0], doubleSides[1],                                    doubleSides[2], doubleSides[3]);                 break;                              case 2:                 System.out.print("Enter triangle sides (real numbers) "                         + "separated by comma, with the base being "                         + "the last one: ");                 line = sc.nextLine();                 strSides = line.split(",");                 doubleSides = new double[3];                 doubleSides[0] = Double.parseDouble(strSides[0]);                  doubleSides[1] = Double.parseDouble(strSides[1]);                 doubleSides[2] = Double.parseDouble(strSides[2]);                 // Create a triangle instance:                 sh = new Triangle(doubleSides[0], doubleSides[1], doubleSides[2]);                 break;                          case 3:                 System.out.print("Enter the radius of the circle (real number): ");                 line = sc.nextLine();                 double r = Double.parseDouble(line);                 // Create a rectangle instance:                 sh = new Circle(r);         }                  return sh;     }          /**      * @param args the command line arguments      */     public static void main(String[] args) {         // TODO code application logic here                  Shape myShape = makeShape();                  System.out.println("##########################################");         System.out.println("The area of the " + myShape.getName() + " is: " +                  myShape.computeArea());         System.out.println("The perimeter of the " + myShape.getName() +                  " is: " + myShape.computePerimeter());                  System.out.println("##########################################");         System.out.println("More Information: \n" + myShape);         System.out.println("##########################################");     }      } lab-assignment/code/src/shape/Shape.java lab-assignment/code/src/shape/Shape.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package shape; /**  *  * @author Waleed Alsanie  */ public abstract class Shape {          public abstract double computeArea();     public abstract double computePerimeter();     public abstract String getName();      } lab-assignment/code/src/shape/circle/Circle.java lab-assignment/code/src/shape/circle/Circle.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package shape.circle; import shape.Shape; /**  *  * @author walsanie  */ public class Circle extends Shape {          private double radius;          public Circle() {         this(1);     }          public Circle(double radius) {                                   if ( radius <= 0 ) {><= 0");         }=""         this.radius =" radius;"     }=""     =""     @override=""     public double computearea() { =""         =""         return math.pi * math.pow(radius, 2); =""     }=""     =""     @override=""     public double computeperimeter() { =""         =""         return 2 * math.pi * radius; =""     }=""     =""     @override=""     public string tostring() {=""         return "name: " + getclass().getcanonicalname() + =""                 "\nradius is: " + radius + "\narea: " +=""                 computearea() + "\nperimeter: " + computeperimeter();=""     }=""     =""     @override=""     public string getname() { return "circle"; }=""     ="" }="" lab-assignment/code/src/shape/polygon/polygon.java="" lab-assignment/code/src/shape/polygon/polygon.java="" *=""  * to change this license header, choose license headers in project properties.=""  * to change this template file, choose tools | templates=""  * and open the template in the editor.=""  */="" package shape.polygon;="" import shape.shape;="" **=""  *=""  * @author walsanie=""  */="" public abstract class polygon extends shape {=""     =""     protected double[] sides;=""     =""     @override=""     public string tostring() {=""         string obj =" "Name: " + getClass().getCanonicalName() + "\n";"         for (int i ="">< sides.length; i++) {             obj =" obj.concat("Side " + (i + 1) + ": " + sides[i] + ", ");"         }=""         obj =" obj.concat("\nArea: " + computeArea() + "\nPerimeter: " + "                 computeperimeter());=""         return obj;=""     }=""     ="" }="" lab-assignment/code/src/shape/polygon/rectangle.java="" lab-assignment/code/src/shape/polygon/rectangle.java="" *=""  * to change this license header, choose license headers in project properties.=""  * to change this template file, choose tools | templates=""  * and open the template in the editor.=""  */="" package shape.polygon;="" **=""  *=""  * @author walsanie=""  */="" public class rectangle extends polygon {=""     =""     public rectangle() {=""         this(1, 1, 1, 1);=""     }=""     =""     public rectangle(double side1, double side2, double side3, double side4) {=""         =""><><= 0) || ><><= 0) ) {><= 0!");         }=""         =""         if ( (side1 !=" side3) || (side2 != side4) ) {"             throw new illegalargumentexception("this is not a rectangle");=""         }=""         sides =" new double[4];"         sides[0] =" side1;  "         sides[1] =" side2; "         sides[2] =" side3; "         sides[3] =" side4;"     }=""     =""     @override=""     public double computearea() { =""         =""         return sides[0] * sides[3]; =""     }=""     =""     @override=""     public double computeperimeter() { =""         =""         return 2 * (sides[0] + sides[3]); =""     }=""     =""     @override=""     public string getname() { return "rectangle"; }=""     ="" }="" lab-assignment/code/src/shape/polygon/triangle.java="" lab-assignment/code/src/shape/polygon/triangle.java="" *=""  * to change this license header, choose license headers in project properties.=""  * to change this template file, choose tools | templates=""  * and open the template in the editor.=""  */="" package shape.polygon;="" **=""  *=""  * @author walsanie=""  */="" public class triangle extends polygon {=""     =""     private double height;=""     =""     public triangle() {=""         this(1, 1, 1.5);=""     }=""     =""     public triangle(double side1, double side2, double base) {=""         =""><><><= 0) ) {><= 0!");         }=""         =""         // check the triangle inequality theorem which states that:=""         // the sum of the lengths of any two sides of a triangle is =""         // greater than the length of the third side =""><= base ) || ><= side2 ) ||><= side1 ) ) {             throw new illegalargumentexception("this is not a triangle! " +                     "it does not satisfy the triangle inequality theorem.");         }         sides = new double[3];         sides[0] = side1;           sides[1] = side2;          sides[2] = base;                  // heron's formula:         height = (0.5 * math.sqrt( (side1 + side2 + base) *                                    (-side1 + side2 + base) *                                    (side1 - side2 + base) *                                    (side1 + side2 - base) )) / base;     }          public double getheight() { return height; }          @override     public double computearea() {                   return 0.5 * height * sides[2];      }          @override     public double computeperimeter() {                   return sides[0] + sides[1] + sides[2];      }          @override     public string getname() { return "triangle"; } } lab-assignment/code/shapes.txt circle 4 & circle 3 & triangle 5,6,8 & regular hexagon 6 & regular hexagon 4 & triangle 3,2,4 & rectangle 6,3,6,3 & regular hexagon 9 & rectangle 5,2,5,2 & triangle 8,2,4 & rectangle 1.5,5.6,1.5,5.6 & circle 11 & regular hexagon 8.23 & circle 10.23598 & triangle 11.5,4.3,8.9 lab-assignment/polymorphism_interface.pdf cs102 – advanced programming ii objectives: in this lab, the following topic will be covered: 1. polymorphism and interface exercise 1: recommended : draw the class diagram to understand the class hierarchy. - read the classes ‘shape’, ‘circle’, ‘polygon’, ‘rectangle’ and ‘triangle’ and understand them. - add a class ‘reghexagon’ in the package ‘shape.polygon’ defining a regular hexagon. this class inherits ‘polygon’ and includes the following:  a constructor which takes one argument representing the length of the sides of the regular hexagon. the constructor initlaises the array attribute ‘sides’ to an array of 1 element having the value of the argument.  a no-argument constructor which sets the sides of the regular hexagon to ‘1’.  a method: public double computearea() that overrides ‘computearea()’ in ‘shape’ such that it returns the area using the following formula: area=3∗√3∗ side2/2  a method: public double computeperimeter() that overrides ‘computeperimeter()’ in ‘shape’ such that it returns the perimeter using the following formula: perimeter=side∗6 - modify the method ‘makeshape()’ in class ‘polymor’ in package ‘polymor’ to allow the user to enter the length of the sides of a regular hexagon and create an instance of a regular hexagon accordingly. - do you need to modify anything in the main method to compute the area and the perimeter of a regular hexagon if asked by the user? why? ------------------------------------------------------------------------------------------------------------------------ the folder ‘code’ contains code which includes two packages ‘shape’ and ‘polymor’. package ‘shape’ contains a class named ‘shape’ and two packages ‘polygon’ and ‘circle’. package ‘polygon’ contains three classes ‘rectangle’, ‘triangle’ and ‘polygon’. ‘polygon’ inherits class ‘shape’ and ‘rectangle’ and ‘triangle’ inherit ‘polygon’. package ‘circle’ contains a class ‘circle’ which inherits class ‘shape’. package ‘polymor’ contains a class ‘polymor’ which includes the main application. exercise 2: - modify class ‘shape’ in package ‘shape’ to make it implement interface ‘comparable’. override the method ‘compareto(shape)’such that it compares shapes based on their areas. - create a new package in the project and name it ‘shapescomp’ in the same level of packages ‘shape’ and ‘ploymor’. - create a class ‘shapecomparator’ in package ‘shapescomp’ that contains:  a static method named ‘loadshapes’ that reads the shapes in the file ‘shapes.txt’ (included), add their information in an array of shapes and returns this array.  a main method that calls the method named ‘loadshapes’ to return an array of shapes, sorts the returned array in an ascending order and then prints out the information of the shape with the minimum area and the shape with the maximum area.             throw new illegalargumentexception("this is not a triangle! " +=""                     "it does not satisfy the triangle inequality theorem.");=""         }=""         sides =" new double[3];"         sides[0] =" side1;  "         sides[1] =" side2; "         sides[2] =" base;"         =""         // heron's formula:=""         height =" (0.5 * Math.sqrt( (side1 + side2 + base) * "                                   (-side1 + side2 + base) * =""                                   (side1 - side2 + base) * =""                                   (side1 + side2 - base) )) / base;=""     }=""     =""     public double getheight() { return height; }=""     =""     @override=""     public double computearea() { =""         =""         return 0.5 * height * sides[2]; =""     }=""     =""     @override=""     public double computeperimeter() { =""         =""         return sides[0] + sides[1] + sides[2]; =""     }=""     =""     @override=""     public string getname() { return "triangle"; }="" }="" lab-assignment/code/shapes.txt="" circle="" 4="" &="" circle="" 3="" &="" triangle="" 5,6,8="" &="" regular="" hexagon="" 6="" &="" regular="" hexagon="" 4="" &="" triangle="" 3,2,4="" &="" rectangle="" 6,3,6,3="" &="" regular="" hexagon="" 9="" &="" rectangle="" 5,2,5,2="" &="" triangle="" 8,2,4="" &="" rectangle="" 1.5,5.6,1.5,5.6="" &="" circle="" 11="" &="" regular="" hexagon="" 8.23="" &="" circle="" 10.23598="" &="" triangle="" 11.5,4.3,8.9="" lab-assignment/polymorphism_interface.pdf="" cs102="" –="" advanced="" programming="" ii="" objectives:="" in="" this="" lab,="" the="" following="" topic="" will="" be="" covered:="" 1.="" polymorphism="" and="" interface="" exercise="" 1:="" recommended="" :="" draw="" the="" class="" diagram="" to="" understand="" the="" class="" hierarchy.="" -="" read="" the="" classes="" ‘shape’,="" ‘circle’,="" ‘polygon’,="" ‘rectangle’="" and="" ‘triangle’="" and="" understand="" them.="" -="" add="" a="" class="" ‘reghexagon’="" in="" the="" package="" ‘shape.polygon’="" defining="" a="" regular="" hexagon.="" this="" class="" inherits="" ‘polygon’="" and="" includes="" the="" following:="" ="" a="" constructor="" which="" takes="" one="" argument="" representing="" the="" length="" of="" the="" sides="" of="" the="" regular="" hexagon.="" the="" constructor="" initlaises="" the="" array="" attribute="" ‘sides’="" to="" an="" array="" of="" 1="" element="" having="" the="" value="" of="" the="" argument.="" ="" a="" no-argument="" constructor="" which="" sets="" the="" sides="" of="" the="" regular="" hexagon="" to="" ‘1’.="" ="" a="" method:="" public="" double="" computearea()="" that="" overrides="" ‘computearea()’="" in="" ‘shape’="" such="" that="" it="" returns="" the="" area="" using="" the="" following="" formula:="" area="3∗√3∗" side2/2="" ="" a="" method:="" public="" double="" computeperimeter()="" that="" overrides="" ‘computeperimeter()’="" in="" ‘shape’="" such="" that="" it="" returns="" the="" perimeter="" using="" the="" following="" formula:="" perimeter="side∗6" -="" modify="" the="" method="" ‘makeshape()’="" in="" class="" ‘polymor’="" in="" package="" ‘polymor’="" to="" allow="" the="" user="" to="" enter="" the="" length="" of="" the="" sides="" of="" a="" regular="" hexagon="" and="" create="" an="" instance="" of="" a="" regular="" hexagon="" accordingly.="" -="" do="" you="" need="" to="" modify="" anything="" in="" the="" main="" method="" to="" compute="" the="" area="" and="" the="" perimeter="" of="" a="" regular="" hexagon="" if="" asked="" by="" the="" user?="" why?="" ------------------------------------------------------------------------------------------------------------------------="" the="" folder="" ‘code’="" contains="" code="" which="" includes="" two="" packages="" ‘shape’="" and="" ‘polymor’.="" package="" ‘shape’="" contains="" a="" class="" named="" ‘shape’="" and="" two="" packages="" ‘polygon’="" and="" ‘circle’.="" package="" ‘polygon’="" contains="" three="" classes="" ‘rectangle’,="" ‘triangle’="" and="" ‘polygon’.="" ‘polygon’="" inherits="" class="" ‘shape’="" and="" ‘rectangle’="" and="" ‘triangle’="" inherit="" ‘polygon’.="" package="" ‘circle’="" contains="" a="" class="" ‘circle’="" which="" inherits="" class="" ‘shape’.="" package="" ‘polymor’="" contains="" a="" class="" ‘polymor’="" which="" includes="" the="" main="" application.="" exercise="" 2:="" -="" modify="" class="" ‘shape’="" in="" package="" ‘shape’="" to="" make="" it="" implement="" interface="" ‘comparable’.="" override="" the="" method="" ‘compareto(shape)’such="" that="" it="" compares="" shapes="" based="" on="" their="" areas.="" -="" create="" a="" new="" package="" in="" the="" project="" and="" name="" it="" ‘shapescomp’="" in="" the="" same="" level="" of="" packages="" ‘shape’="" and="" ‘ploymor’.="" -="" create="" a="" class="" ‘shapecomparator’="" in="" package="" ‘shapescomp’="" that="" contains:="" ="" a="" static="" method="" named="" ‘loadshapes’="" that="" reads="" the="" shapes="" in="" the="" file="" ‘shapes.txt’="" (included),="" add="" their="" information="" in="" an="" array="" of="" shapes="" and="" returns="" this="" array.="" ="" a="" main="" method="" that="" calls="" the="" method="" named="" ‘loadshapes’="" to="" return="" an="" array="" of="" shapes,="" sorts="" the="" returned="" array="" in="" an="" ascending="" order="" and="" then="" prints="" out="" the="" information="" of="" the="" shape="" with="" the="" minimum="" area="" and="" the="" shape="" with="" the="" maximum="">
Nov 03, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here