Question

In: Computer Science

Need some assistance with a java program “Square,” that will implement a data type Square that...

Need some assistance with a java program “Square,” that will implement a data type Square that represents squares with the x and y coordinates of their upper-left corners and the length.

The API should be as follows.

Square(double x, double y, double length) //constructor

double area() //returns the area of the square

double perimeter() //returns the perimeter of the square

boolean intersects(Square b) //does this square intersect b? Two squares would intersect if they share one or more common points

boolean contains(Square b) //does this square contain b?

void draw() //draw this square on standard drawing

Note: The program should include a main method to test that it does the following.

  • Instantiate a Square object whose upper-left corner coordinates and length are given as command-line arguments. It should print out the area and perimeter of the square.

  • Prompt the user for a second square’s upper-left corner coordinates and length, and indicate whether it intersects with the square specified earlier and also whether it contains the square specified earlier.

  • Provide a pop-up window that displays the two squares.

A sample run would be as follows.

>java Square 0.2 0.7 0.3

The area is 0.09

The perimeter is 1.2

Enter the upper-left coordinates and the length of a square: 0.3 0.6 0.4

Solutions

Expert Solution

import java.util.Scanner;

public class Square {

        private double x;
        private double y;
        private double length;

        public Square(double x, double y, double length) {
                this.x = x;
                this.y = y;
                this.length = length;
        }

        public double area() {
                return length * length;
        }

        public double perimeter() {
                return 4 * length;
        }

        public boolean intersects(Square other) {

                // Two Square do not overlap, if one rectangle is on left of other, or on
                // top of other.

                // If one Square is on left side of other
                if ((this.x >= (other.x + other.length)) || (other.x >= (this.x + this.length))) {
                        return false;
                }

                // If one Square is above other
                if ((this.y >= (other.y + other.length)) || (other.y >= (this.length + this.y))) {
                        return false;
                }

                return true;
        }
        
        public boolean contains(Square b) {
                return (this.x <= b.x) && (this.x + length < b.x + b.length) 
                                && (this.y <= b.y) && (this.y + length < b.y + b.length);
        }
        
        public void draw() {
                System.out.println("The area is " + area());
                System.out.println("The perimeter is " + perimeter());
        }

        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);

                Double x = Double.parseDouble(args[0]);
                Double y = Double.parseDouble(args[1]);
                Double l = Double.parseDouble(args[2]);
                
                Square s = new Square(x, y, l);
                s.draw();
                
                System.out.println("Enter the upper-left coordinates and the length of a square: ");

                x = Double.parseDouble(in.nextLine());
                y = Double.parseDouble(in.nextLine());
                l = Double.parseDouble(in.nextLine());

                Square r = new Square(x, y, l);
                r.draw();

                if(s.intersects(r)) {
                        System.out.println("Squares intersect.");
                } else {
                        System.out.println("Squares do not intersect.");
                }

                if(s.contains(r)) {
                        System.out.println("the new Squares is inside old square.");
                } else {
                        System.out.println("the new Squares is not contained in old square.");
                }
                
                in.close();
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2:...
Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Hello, I am in need of some assistance in interpreting the data for the two variables...
Hello, I am in need of some assistance in interpreting the data for the two variables I did in a t-test for in Excel. Variable 1 is Relationship with Direct Supervisor and Variable 2 is the Workplace Happiness Rating. I am supposed to write a 125- to 175-word summary of my interpretation of the results of the t test. t-Test: Two-Sample Assuming Equal Variances Variable 1 Variable 2 Mean 2.5 7.4 Variance 1.030612245 2 Observations 50 50 Pooled Variance 1.515306122...
Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Hello I need some assistance with these questions I need not so long answers but not...
Hello I need some assistance with these questions I need not so long answers but not too short please Give some examples of How much decisions. What are the implicit costs of having an Airbnb in your neighborhood? What is marginal analysis? What is marginal cost? Under what conditions do marginal costs increase?
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we...
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT