Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help with my java homework CSC 255- Programming III CSC255-Assignment 1 MyShape Project (Total: 100 pts) Using inheritance, we can factor out the common features

help with my java homework

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

CSC 255- Programming III CSC255-Assignment 1 MyShape Project (Total: 100 pts) Using inheritance, we can "factor out" the common features from classes MyLine, MyRectangle and MyCircle and place them in a single MyShape superclass. Then, using variables of the superclass type, we can manipulate shape objects polymorphically This hierarchy begins with superclass MyShape, which is extended by subclass MyBoundedShape -is a MyShape. MyBoundedShape is a Comparable (interface) object. The third level of this hierarchy contains specific types of MyCircle, MySquare and MyRectangle. All shape has points. MyShape class has-a (composition) MyPoint class. As in the below figure, we can follow the arrows from the bottom of the diagram to the topmost superclass in this class hierarchy to identify several is-a relationships. For example, a MyCircle is a MyBoundedShape object and is a MyShape as well as is a Comparable object. Also, MyCircle has two points. MyPoint MyShape MyLine MyBoundedShape MyCircle le MySareMyRectangle Requirements Need to create the following eight (8) classes and each class MUST include the code documentations and all requirements. (See more information under Course Information button) Zip all eight-source files onto one single folder and name the folder with your initials. For example, syA1 for my assignment 1 folder. Name the filename same as the class name for the following classes. Specify the instance variables and methods for each class. 1. MyPoint class-all shapes have points. (5 pts) two instance variable for x coordinate and y coordinate double x,y Yeung 1 CSC 255- Programming III constructor: MyPoint (x.y) getX0 and getYO to get x any coordinate toString() to display x, y coordinate with one decimal place as (x, y) for example: (2.4, 10.0) 2. MyShape class- abstract class (15 pts) In class MyShape in the above figure must be abstract. Since MyShape represents any shape in general, you cannot implement a getName method without knowing specifically what shape it is. The data representing the coordinates and color of the shapes in the hierarchy should be declared Need In addition to the common data, class MyShape should declare the following methods: as private instance variables of class MyShape. to import the Color class. i a. A no-argument constructor that sets all the coordinates of the shape to 0 and the b. A constructor that initializes the coordinates to the values of the arguments supplied c. Set methods for the color that allow the programmer to set any piece of data d. Get methods for the point1, point2 and color that allow the programmer to retrieve e. The abstract method public abstract String getName0: which the program will call import java.awt.Color stroke color to Color BLACK and the stroke color to Color.BLACK or to the value of the color argument supplied. independently for a shape in the hierarchy. any piece of data independently for a shape in the hierarchy to tell a MyShape to return the appropriate shape name (i.e., Line, Circle, and Rectangle). getCoordinatesAsString() method will format two points objects as (12.0, 16.0), (34.0, 45.0) Override the method toString() invoke getCoordinatesAsString method and getName method to display the message as the following example: Coordinates of Circle are: (12.0, 16.0), (34.0, 45.0) f. g. Variable x1: double -x2: double yl: double -y2: double -point1: MyPoint -point2: MyPoint color: Color class Constructor/method +MyShape(double x1, double yl, double x2,double y2) +MyShape(double x1, double y1, double x2,double y2, Color +setColor(Color): void getColor0: Color getPoint10: MyPoint getPoint20:MyPoint +getName(): abstract String Yeung 2 CSC 255- Programming III +toString(: String getCoordinatesAsString():String 3. MyLine class (S pts) Class MyLine should provide a no-argument constructor and a constructor with arguments for the A no-argument constructor that invoke su perelass's default .Need to implement getName0 method from the abstract super class MyShape. It will display "Line" for its name. class Variable Constructor/method MyLine +MyLine) +MyLine(double x1, double yl, double x2, double y2) +MyLine(double x1, double yl, double x2, double y2, Color color) getName():String return "Line" string for MyLine shape 4. MyBoundedShape class -abstract class (20 pts) MyBoundedShape is-a MyShape as well as MyBoundedShape is-kind-a comparable (see the hierarchy in the above figure) Class MyBoundedShape should declare three constructors that mimic those of class MyShape, but with an added parameter specifying whether the shape is filled Also declare get and set methods for manipulating the filled flag and fill color, and methods that calculate the upper-left xcoordinate, upper-left y-coordinate, width and height by invoking super class's getPoint10 and getPoint2) method . if you have the same four coordinate values (x1, y1, x2 and y2) for circles, squares and rectangles. Each requires an upper-left x-coordinate value (the smaller of the two x- coordinate values), an upper-left y-coordinate value (the smaller of the two y- coordinate values), a width (the absolute value of the difference between the two x- coordinate values) and a height (the absolute value of the difference between the two y-coordinate values). The abstract method public abstract double getArea(): which the program will call to The abstract method public abstract double getPerimeter): which the program will call The static max method to compare two MyBoundedShape objects and return the bigger The class will not implement getName() method. All subclasses will implement four tell a MyBoundedShape to return the area value. to tell a MyBoundedShape to return the Perimeter value. object. (see below) abstract methods: getName), getArea() and getPerimenter). So, the class must declare as an abstract class too. Yeung 3 CSC 255- Programming III If designed properly, the new MyCircle and MyRectangle classes should each have only two constructors and implement getName, getArea and getPerimeter methods from MyShape and MyBoundedShape superclasses. .Class MyRectangle should provide three constructors and invoke super class's constructor Invoke superclass's getWidth) and getHeight() method to calculate its area and perimeter Area width height Perimeter width2+height *2 getName() method will return "Rectangle" string. class Variable Constructor/method +MyRectangle() +MyRectangle(double x1, double yl, double x2, double y2) +MyRectangle(double x1, double y1, double x2, double y2, MyRectangle Color color, boolean filled) getArea):double +getPerimeter(: double +getName):String "Rectangle" 6. MySquare class (8 point) .Class MySquare should provide three constructors and invoke super class's constructor Invoke superclass's getWidth) and getHeight() method to calculate its area and perimeter Area width width Perimeter-width4 getName() method will return "Square" string. class Variable Constructor/method +MySquare) +MyRectangle(double x1, double yl, double x2, double y2) +MyRectangle(double x1, double y1, double x2, double y2, MySquare Color color, boolean filled) Yeung 5 CSC 255- Programming III getArea():double +getPerimeter): double +getName():String "Square" 7. MyCircle class (12 pts) o Classes MyCircle should provide a no-argument constructor and a constructor with arguments for the coordinates as well as a constructor with arguments of the coordinates specify whether the shape is filled with the color All constructor invoke super class's constructors. The getRadius() method will return (double)getWidth()/2.0 value. Implements getArea, getPerimeter, and getName methods. o . Area radius radius Perimeter diameter class Variable Constructor/method MyCircle radius: double +MyCircle) +MyCircle(double x1, double y1, double x2, double y2) +MyCircle(double x1, double y1, double x2, double y2 Color color, boolean filled) +getRadius(): double +getArea):double +getPerimeter(): double +getName(: StringCirdle" 8. MyShapeTest class (25 pts) for a driver class to test all classes you have created from the above. It must use ArrayList to create eight objects as the following from this hierarchy. Use the instanceof operator to determine whether a particular shape object's type // instantiate all the objects to be tested MyLine line1 new MyLine(12, 13, 20, 25); MyCircle circle1 new MyCircle(12, 16, 34, 45); MyCircle circle2- new MyCircle(10, 15, 20, 25); MyRectangle rect1 new MyRectangle(23, 35, 25, 13); MyRectangle rect2new MyRectangle(12, 20, 15, 30); MySquare squ1 new MySquare(20, 25, 30, 35); MySquare squ2 new MySquare(12, 20, 20, 28) It will also display the maximum of circle, square and rectangle by invoking their max() method. All result MUST display with two decimal places and all points MUST display with one decimal place. The result is similar to the following figure Yeung 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

2. Always guess when you can eliminate some of the alternatives.

Answered: 1 week ago