Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey! These questions are all linked so I can't upload them separately since you wouldn't know what I'm talking about otherwise. This is why I'm

Hey! These questions are all linked so I can't upload them separately since you wouldn't know what I'm talking about otherwise. This is why I'm posting them together. Please answer ONLY if you can answer all parts.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

, height Write a class called Rectangle with the following attributes and methods. (All attributes should be declared as private.) x-> int width y->int width> int height -> int Methods get and set methods for all attributes. getPerimeter() method retums the perimeter of the rectangle. .getArea() method returns the area of the rectangle. You can use the following template for your Rectangle.java file: return x; public class Rectangle private double x; // write other attributes public Rectangle(double xe, double ye, double w, double h){ x = x; y = ye; width = w; height-h; } public double getX(){ ) public void setX(double x){ X = x; ) // write other get and set methods public double getPerimeter(){ // compute and return the perimeter } public double getArea(){ // compute and return the area ) ) Test your class by creating another file 01.java in addition to your Rectangle.java file. Then copy and complete the following code: import java.util.Scanner; public class 01{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter x y width height: "); // here, read 4 doubles from user and // create a rectangle object using these 4 doubles // then output as in the following examples below } You should get the following outputs with the following inputs when you run Q1.java: Input: Enter x y width height: 0.0 0.2 5. 8.0 Output: The perimeter is: 26.0 The area is: 40.0 Input: Enter x y width height: -1.0 6.0 3.0 5.0 Output The perimeter is: 16.0 The area is: 15.0 You should submit 2 files for this question: 21.java and Rectangle.java Add a new method called contains to the Rectangle class you wrote in question 1. This method should take another Rectangle as the parameter and checks if that rectangle is inside the first one. public boolean contains (Rectangle r) { // Write the code that returns true if the rectangle r is inside this 1/ rectangle. Return false otherwise. } Analyze the following diagram and you can see which conditions should be satisfied. As a hint you should calculate and make comparisons between two comer points of the rectangles. x11,y11 x21,21 x22,y22 x12,412 Test your class by creating another file 02.java in addition to your Rectangle.java file. Then copy and complete the following code: import java.util.Scanner; public class 024 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter x y width height for first rectangle:"); // here, read 4 doubles from user and // create first rectangle using these 4 doubles System.out.println("Enter x y width height for second rectangle: "); // here, read another 4 doubles from user and // create second rectangle using these 4 doubles // then output as in the following examples below You should get the following outputs with the given inputs when you run 02.java: Input Enter x y width height for first rectangle: 4.8 4.0 8.0 8.0 Enter x y width height for second rectangle: 3.0 3.8 2.0 2.0 3 Output: contains Input: Enter x y width height for first rectangle: 0.0 0.0 6.0 2.0 Enter x y width height for second rectangle: 3. 4. 5.2 7.0 Output: does not contain You should submit 2 files for this question: 22.java and Rectangle.java Question 3 Add a new method called intersects to the Rectangle class you wrote in question 1. This method should take another Rectangle as the parameter and checks if that rectangle intersects with the first one. public boolean intersects (Rectangler){ // write the code that returns true if the rectangle r intersects with 1/ this rectangle. Return false otherwise. Hint: You can see a visualization in https://silentmatt.com/rectangle-intersection, where you can drag rectangles around and see the comparisons you should check. Test your class by creating another file 03.java in addition to your Rectangle.java file. Then copy and complete the following code: import java.util.Scanner; public class 234 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter x y width height for first rectangle: "); // here, read 4 doubles from user and // create first rectangle object using these 4 doubles System.out.println("Enter x y width height for second rectangle:"); 1/ here, read another 4 doubles from user and // create second rectangle object using these 4 doubles // then output as in the following examples below You should get the following outputs with the given inputs when you run 23.java: Input Enter x y width height for first rectangle: 4.0 4.0 8.0 8.0 Enter x y width height for second rectangle: 9.8 4.0 4.0 2.0 Output intersects Input: Enter x y width height for first rectangle: 0.0 0.0 2.0 2. Enter x y width height for second rectangle: 3.0 3.0 2.0 2.0 Output does not intersect You should submit 2 files for this question: Q3.java and Rectangle.java

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions