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

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...
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...
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,...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
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.
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?
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT