Question

Java programing Implement and text folling classes Parallelogram: # leftUpper : Point //the left ...

Java programing Implement and text folling classes

Parallelogram:

# leftUpper : Point //the left upper corner of the Parallelogram # leftLower : Point //the left lower corner of the Parallelogram # length : int //horizontal length

+ Parallelogram ()
+ Parallelogram (Point leftUpper, Point leftLower, int len)
+ getLeftUpper(): Point //returns the left upper corner
+ getLeftLower(): Point //returns the left lower corner
+ setLeftUpper( Point p): void //changes the left upper corner + setLeftLower( Point p): void //changes the left lower corner + get, set methods for length
+ area() : double; //height*length
+ shift(int deltX, int deltY) : void
+ toString(): String

+ getRightUpper(): Point // calculates and returns the right upper corner + getRightLower(): Point // calculates and returns the right lower corner + getHeight() : double //calculates and returns the height of the
+ getSide(): double

Square

+ Square ()
+ Square (Point upperLeft, int length)

+ area() : double
+ toString(): String
--------------------------- get above methods and Parallelogram right, no errors: You earn 20 points + contains(Square anotherSquare) : boolean
+ overlap(Square anotherSquare) : boolean
+ disjoint(Square anotherSquare) : boolean
------------------ Get every method right, good job: 25 points will be awarded to you.

1. Implement both classes with inheritance

2. Create following Squares:

squareA: its left upper corner is at (10, 20) , length is 100. //Four corners are (10, 20), (110, 20), (110,120), (10, 120)

squareB: its left upper corner is at (30, 40) , length is 25. squareC: its left upper corner is at (70, 60) , length is 80.

  1. Test whether squareB is contained in squareA; whether squareA and squareB are overlapped or disjoint.

  2. Test whether squareC is contained in squareA; whether squareA and squareC are overlapped or disjoint.

  3. Output the area of squareA

  4. Output the four corners of squareA and squareB

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Square.java


public class Square extends Parallelogram
{
public Square() { super(); }
public Square(Point leftUpper, int length)
{
super(leftUpper,new Point(leftUpper.get_x(), leftUpper.get_y()+length), length);
}
public double area() { return (get_length() * get_length()); }
public String toString()
{
String output = "Sqaure: \n";
output += "LeftUpper: ";
output += getLeftUpper().toString();
output += "\n";
output += "LeftLower: ";
output += getLeftLower().toString();
output += "\n";
output += "Length: ";
output += get_length();
output += "\n";
return output;
}
public double getSide() { return get_length(); }
public boolean contains(Square another)
{
if (((getLeftLower().get_x() < another.getLeftLower().get_x()) && (getLeftLower().get_y() > another.getLeftLower().get_y()))
&& ((getRightLower().get_x() > another.getRightLower().get_x()) && (getRightLower().get_y() < another.getRightLower().get_y())))
return true;
return false;
}
public boolean overlap(Square another)
{
Point left1 = new Point(getLeftLower());
Point left2 = new Point(another.getLeftLower());
Point right1 = new Point(getRightLower());
Point right2 = new Point(another.getRightLower());
if (left1.get_x() > right2.get_x() || left2.get_x() > right1.get_x())
return false;
if (left1.get_y() < right2.get_y() || left2.get_y() < right1.get_y())
return false;
return true;
}
public boolean disjoint(Square another)
{
if (!overlap(another))
return true;
return false;
}
static public void main(String [] argv)
{
Point pointA = new Point(10,20);
Square squareA = new Square(pointA, 100);
System.out.println(squareA);
System.out.println("Square A: " + squareA.getLeftUpper().toString() + "\n" + squareA.getLeftLower().toString() + "\n" + squareA.getRightLower().toString() + "\n" + squareA.getRightUpper().toString());
Point pointB = new Point(30,40);
Square squareB = new Square(pointB, 25);
System.out.println("Square B: " + squareB.getLeftUpper().toString() + "\n" + squareB.getLeftLower().toString() + "\n" + squareB.getRightLower().toString() + "\n" + squareB.getRightUpper().toString());
Point pointC = new Point(70,60);
Square squareC = new Square(pointC, 80);
if (squareA.contains(squareB))
System.out.println("squareB is contained in squareA");
else if (squareA.overlap(squareB))
System.out.println("squareB is overlapped with squareA");
else
System.out.println("squareB is disjoint with squareA");
if (squareA.contains(squareC))
System.out.println("squareC is contained in squareA");
else if (squareA.overlap(squareC))
System.out.println("squareC is overlapped with squareA");
else
System.out.println("squareC is disjoint with squareA");
System.out.println("Area of square A: " + squareA.area());
}
}

Parallelogram.java


import java.lang.Math;
public class Parallelogram
{
private Point leftUpper;
private Point leftLower;
private int length;

Parallelogram()
{
leftUpper = new Point();
leftLower = new Point();
length = 0;
}
Parallelogram(Point leftUpper, Point leftLower, int len)
{
this.leftUpper = leftUpper;
this.leftLower = leftLower;
this.length = len;
}

public Point getLeftUpper() { return leftUpper; }
public Point getLeftLower() { return leftLower; }
public double getHeight() { return (leftUpper.get_y() - leftLower.get_y()); }
public void setLeftUpper(Point p) { leftUpper = p; }
public void setLeftLower(Point p) { leftLower = p; }
public int get_length() { return length; }
public void set_length(int len) { length = len; }
public double area() { return getHeight() * length; }
public void shift(int deltaX, int deltaY) {
leftUpper.shift(deltaX, deltaY);
leftLower.shift(deltaX, deltaY);
}
public String toString() {
String output = "Parallelogram: \n";
output += "LeftUpper: ";
output += leftUpper.toString();
output += "\n";
output += "LeftLower: ";
output += leftLower.toString();
output += "\n";
return output;
}
public Point getRightUpper() {
Point p3 = new Point(leftUpper);
p3.shift(length, 0);
return p3;
}

public Point getRightLower() {
Point p4 = new Point(leftLower);
p4.shift(length,0);
return p4;
}
public double getSide() {
double x = leftUpper.get_x() - leftLower.get_x();
double x2 = x*x;
double y = leftUpper.get_y() - leftLower.get_y();
double y2 = y*y;
double ans = Math.sqrt(x2 + y2);
return ans;
}

}

Point.java


class Point
{
private double x;
private double y;
public double get_x() { return x; }
public double get_y() { return y; }
public Point(Point p) {
x = p.get_x();
y = p.get_y(); }
public Point()
{
x = 0;
y = 0;
}
public void shift(int deltax, int deltay)
{
x += deltax;
y += deltay;
}
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public String toString()
{
String output = new String();
output += "X : " + x + "\n";
output += "Y : " + y + "\n";
return output;
}
}

Add a comment
Know the answer?
Add Answer to:
Java programing Implement and text folling classes Parallelogram: # leftUpper : Point //the left ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Four point charges are situated at the corners of a rectangle. Q1 (bottom left corner): 2.00...

    Four point charges are situated at the corners of a rectangle. Q1 (bottom left corner): 2.00 x 10-8 C Q2 (upper left corner): 2.00 x 10-8 C Q3 (lower right corner): 3.00 x 10-8 C Q4 (upper right corner): -1.00 x 10-8 C the length is 4.00 m and the height is 3.00 m Find the net force on Q4! (our class is algebra based)

  • Given the following code fragment public class Point { public int x; // Because these are...

    Given the following code fragment public class Point { public int x; // Because these are public, you can access them public int y; // directly without getters and setters }; public class Rectangle { private Point ll; // the lower left corner of the rectangle private Point ur; // the upper right corner of the rectangle public Point getLLPoint() {return ll;} public Point getURPoint() {return ur;} } (a) Write a method equals for the Rectangle class that takes a...

  • Implement the Point class (code for this up to the isHigher method was discussed in the...

    Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...

  • **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate...

    **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate it! Everything is explained in the program via comments, and I just need help implementing the methods. Instructions for the methods are commented on top of said method. It's supposedly straightforward. Methods that need to be solved are in bold. Some might already be implemented. Just based off what's in this class, is supposed to lead to the outcome. This is all the info...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT