Question
Please help me with this assignment, it's easy but it looks long because I included the code of the classes. It's Java and about design
Please help me with this assignment, it's easy but it looks long because I included the code of the classes. It's Java and about design patterns, exceptions, catch and try. This assignment is on Exceptions and the Adapter Pattern.
You need to create two new classes, InvalidDistanceComputationException and InvalidComparisonException.
*** NOTE *** Because you will throw these from overridden methods that ARE NOT declared to throw, you must inherit from RuntimeException so they are both unchecked exceptions. I did this intentionally because this overriding situation often arises in practice and inheriting from RuntimeException instead of Exception is the standard way of handling it.
I have given you the main() I will use and the expected output. You must modify the code of Example Two so it produces the output when run against the main() I have provided for this assignment.
You will be adding throws where needed.
You will need to deal with the changes to the Scalable interface.
You must implement Square by using the Adapter pattern with the class Rectangle.
Equality for rectangles is defined by their areas being equal.
the desired output:
Point: Name = No name, (0,0)
Name: No name
Area: 0.0
Perimeter: 0.0
Circle: Name = NoName, Center: Point: Name = center, (0,0), Radius: 1.0
Name: NoName
Area: 3.14159
Perimeter: 6.28318
Rectangle: Name = NoName, Corner: Point: Name = corner, (0,0), Length: 2, Width: 4
Name: NoName
Area: 8.0
Perimeter: 12.0
Square: NoNameSquare, Side: 3
Name: NoNameSquare
Area: 9.0
Perimeter: 12.0
p1 == p1 true
Error: Can't Compare class Point with class Circle
c1 == c1 true
Error: Can't Compare class Circle with class Rectangle
r1 == r1 true
Error: Can't Compare class Rectangle with class Square
s1 == s1 true
Error: Can't Compare class Square with class Point
Distance p1 to p1: 0.0
Error: can't Compute Distance Between class Point and class Circle
Distance c1 to c1: 0.0
Error: can't Compute Distance Between class Circle and class Rectangle
Distance r1 to r1: 0.0
Error: can't Compute Distance Between class Rectangle and class Square
Distance s1 to s1: 0.0
Error: can't Compute Distance Between class Square and class Point
Original: Point: Name = No name, (0,0)
Scale by 2: Point: Name = No name, (2,2)
Original: Circle: Name = NoName, Center: Point: Name = center, (0,0), Radius: 1.0
Scale by 6: Circle: Name = NoName, Center: Point: Name = center, (0,0), Radius: 6.0
Original: Rectangle: Name = NoName, Corner: Point: Name = corner, (0,0), Length: 2, Width: 4
Scale by 7: Rectangle: Name = NoName, Corner: Point: Name = corner, (0,0), Length: 14, Width: 28
Original: Square: NoNameSquare, Side: 3
Scale by 12: Square: NoNameSquare, Side: 36
===========================================
The code so far:
//Name.java
public class Name
{
public Name()
{
this("NoName");
}
public Name(Name name)
{
this(name.getName());
}
public Name(String name)
{
setName(name);
}
public String getName()
{
return mName;
}
public void setName(String name)
{
mName = name;
}
private String mName;
}
==============================
//Point.java
public class Point extends Shape
{
public Point()
{
this("No name", 0,0);
}
public Point(String name, Point p)
{
this(name, p.mX, p.mY);
}
public Point(Point p)
{
this("Copy of: " + p.getName(), p.getX(), p.getY());
}
public Point(String name, int x, int y)
{
super(name);
mX = x;
mY = y;
}
@Override
public double getArea()
{
return 0.0;
}
@Override
public double getPerimeter()
{
return 0.0;
}
@Override
public double getDistance(Shape other)
{
if(!(other instanceof Point)) return 0.0;
else
{
Point p = (Point)other;
return Math.sqrt( ((mX - p.mX) * (mX - p.mX)) +
((mY - p.mY) * (mY - p.mY)) );
}
}
public int getX()
{
return mX;
}
public int getY()
{
return mY;
}
@Override
public String toString()
{
return "Point: " + super.toString() + ", (" + mX + "," + mY + ")";
}
@Override
public boolean equals(Object other)
{
if(!(other instanceof Point)) return false;
else
{
Point p = (Point)other;
return super.equals(p) && mX == p.mX && mY == p.mY;
}
}
@Override
public int compareTo(Object arg0)
{
if(!(arg0 instanceof Point))
{
return -1; // Point is least
}
else
{
Point p = (Point)arg0;
if(mX < p.mX) return -1;
else if(mX > p.mX) return 1;
else
{
if(mY < p.mY) return -1;
else if(mY > p.mY) return 1;
else return 0;
}
}
}
@Override
public String scale(int scaleFactor)
{
int x = mX + scaleFactor;
int y = mY + scaleFactor;
Point p = new Point(getName(), x, y);
return p.toString();
}
private int mX;
private int mY;
}
===============================
public interface Scalable
{
String scale(int scalefactor);
}
==============================
public abstract class Shape implements Comparable
{
public Shape(String name)
{
mName = new Name(name);
}
public Shape(Name name)
{
mName = new Name(name);
}
public String getName()
{
return mName.getName();
}
public void setName(String name)
{
mName = new Name(name);
}
@Override
public String toString()
{
return "Name = " + getName();
}
@Override
public boolean equals(Object o)
{
if(!(o instanceof Shape)) return false;
else
{
Shape s = (Shape)o;
return getName().equals(s.getName());
}
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract double getDistance(Shape other);
private Name mName;
}
======================================
public class Circle extends Shape
{
public Circle()
{
this(new Name());
}
public Circle(String name)
{
this(new Name(name));
}
public Circle(Name name)
{
this(name, new Point());
}
public Circle(Name name, Point center)
{
this(name, center, 1.0);
}
public Circle(Name name, Point center, double radius)
{
super(name);
mCenter = center;
mRadius = radius;
}
public Circle(Circle c)
{
this(new Name("Copy of " + c.getName()), new Point(c.getCenter()), c.getRadius());
}
@Override
public double getArea()
{
return PI * mRadius * mRadius;
}
@Override
public double getPerimeter()
{
return 2 * PI * mRadius;
}
@Override
public double getDistance(Shape other)
{
if(!(other instanceof Circle)) return 0.0;
else
{
Circle c = (Circle)other;
return mCenter.getDistance(c.mCenter);
}
}
@Override
public String toString()
{
return "Circle: " + super.toString() + ", Center: " + mCenter + ", Radius: " + mRadius;
}
@Override
public boolean equals(Object other)
{
if(!(other instanceof Circle)) return false;
else
{
Circle c = (Circle)other;
return super.equals(c) && mCenter.equals(c.mCenter) && mRadius == c.mRadius;
}
}
@Override
public int compareTo(Object arg0)
{
if(!(arg0 instanceof Circle))
{
if(arg0 instanceof Point) return 1; // Circle > Point
else return 0; // Should throw error
}
else
{
Circle c = (Circle)arg0;
int answer = mCenter.compareTo(c.mCenter);
if(answer != 0) return answer;
if(mRadius < c.mRadius) return -1;
else if(mRadius == c.mRadius)return 0;
else return 1;
}
}
@Override
public String scale(int scaleFactor)
{
double radius = mRadius * scaleFactor;
Circle c = new Circle(new Name(getName()), mCenter, radius);
return c.toString();
}
public Point getCenter()
{
return mCenter;
}
public double getRadius()
{
return mRadius;
}
private Point mCenter;
private double mRadius;
private static final double PI = 3.14159;
}
======================================
public class Rectangle extends Shape
{
public Rectangle()
{
this("NoName");
}
public Rectangle(String name)
{
this(name, 2);
}
public Rectangle(String name, int length)
{
this(name, length, 2 * length);
}
public Rectangle(String name, int length, int width)
{
this(name, length, width, new Point("corner", 0,0));
}
public Rectangle(String name, int length, int width, Point corner)
{
super(name);
mLength = length;
mWidth = width;
mCorner = new Point(corner);
}
public Rectangle(Rectangle r)
{
this("Copy of " + r.getName(), r.getLength(), r.getWidth(), new Point(r.getCorner()));
}
@Override
public double getArea()
{
return mLength * mWidth;
}
@Override
public double getPerimeter()
{
return 2 * mLength + 2 * mWidth;
}
@Override
public double getDistance(Shape other)
{
if(!(other instanceof Rectangle)) return 0.0;
else
{
Rectangle r = (Rectangle)other;
return mCorner.getDistance(r.mCorner);
}
}
@Override
public String toString()
{
return "Rectangle: " + super.toString() + ", Corner: " + mCorner + ", Length: " + mLength + ", Width: " + mWidth;
}
@Override
public boolean equals(Object other)
{
if(!(other instanceof Rectangle)) return false;
else
{
Rectangle r = (Rectangle)other;
return mCorner.equals(mCorner) && mLength == r.mLength && mWidth == r.mWidth;
}
}
public Point getCorner()
{
return mCorner;
}
public int getLength()
{
return mLength;
}
public int getWidth()
{
return mWidth;
}
private Point mCorner;
private int mLength;
private int mWidth;
}
===========================
public class SquareToRectangleAdapter {
}
===========================
public class Square extends Rectangle {
}
===========================
public class InvalidDistanceComputationException extends RuntimeException {
}
===========================
public class InvalidComparisonException extends RuntimeException {
}
==========================
//The main
import java.util.ArrayList; import java.util.List;
public class AssignmentFour { public static void main(String[] args) { Point p1 = new Point(); Circle c1 = new Circle(); Rectangle r1 = new Rectangle(); Square s1 = new Square(); List
static private void doScale(Shape s, int scale) { sop(""); sop("Original: " + s); s.scale(scale); sop("Scale by " + scale + ": " + s); } static private void sop(String s) { System.out.println(s); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started