Question

In: Computer Science

Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...

Using the following in Java-

package intersectionprinter;
import java.awt.Rectangle;

public class IntersectionPrinter {

public static void main(String[] args) {
Rectangle r1 = new Rectangle(0,0,100,150);
System.out.println(r1);
  
Rectangle r2 = new Rectangle(50,75,100,150);
System.out.println(r2);
  
Rectangle r3 = r1.intersection(r2);
  
}

}

Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the program print that there is no overlap.

Solutions

Expert Solution

Please find the code below:

package intersectionprinter;

import java.awt.Rectangle;

public class IntersectionPrinter {

public static void main(String[] args) {

Rectangle r1 = new Rectangle(0,0,100,150);

System.out.println(r1);

Rectangle r2 = new Rectangle(50,75,100,150);

System.out.println(r2);

Rectangle r3 = r1.intersection(r2);

//if heigth and width is less than 0 than there is no overlap

if(r3.getWidth()<0 && r3.getHeight()<0){

System.out.println("No overlap");

}else{

System.out.println("Rectangles overlap. Coordinates is as below");

System.out.println(r3);

}

}

}

output:


Related Solutions

Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args)...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args) {             // TODO Auto-generated method stub             System.out.println("*** 1 ***");             ArrayList<Integer>list1=new ArrayList<Integer>();             for(int i=0;i<30;i++)             {                   list1.add(new Integer(i));             }             System.out.print("[");             for(int i=0;i<list1.size();i++)             {                   System.out.print(list1.get(i)+" ");             }             System.out.println("]");                           System.out.println("*** 2 ***");             ArrayList<Integer>list2=new ArrayList<Integer>();             for(int i=30;i<60;i++)             {                   list2.add(i); //Auto Boxing an Integer not need to use new Integer             }             System.out.println(list2); //toString for an ArrayList --created by the Java Programmers                           System.out.println("*** 3 ***");             ArrayList<Integer>list3=new ArrayList<Integer>();             list3.add(list1.remove(22)); //when...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how to use Queue that includes add,peek,remove,pool elements. * Use For Each loop and while loop with Iterator to retrieve data. * */ } }
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how to use Map that includes storing and retrieving elements. * Add List<String> into a Map. Like, Map<String, List<string>> list = new HashMap<String, List<String>>(); * Use For Each loop and while loop with Iterator to retrieve data. * * Use any databases[MongoDB, Oracle, MySql] to store data and retrieve data. */ } }
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){...
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){ ....... } public static int getInt( String prompt ){ ....... } } Using these method you see above to create a main method in your class called MyExam in the different package to get input and output. The output will tell the user what number is entered. If you entered a 5, the output will be: "The number you entered is 5.". QUESTION 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT