Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using...

Free

80.2K

Verified Solution

Question

Programming

Use Java to:

1. Write a method with a Rectangle and a Point as parameters.Without using methods from the Rectangle class, return true if thepoint is inside the rectangle and false otherwise.

2. Write a second method with the same functionality as Exercise1. However, use a method from the Rectangle class this time.

3. Normally, the == operator cannot be used to compare twostrings for equality. There are 2 main exceptions we talked about.The first is through compiler optimization when we define twoStrings in code with the same value. Demonstrate the second case ina method named stringEquality. Your method does not need anyparameters. You may define the two strings inside the method. Yourmethod should work even if a Scanner is used (no compileroptimization tricks).

Answer & Explanation Solved by verified expert
3.8 Ratings (576 Votes)

ANSWER :-

1)

class Rectangle{//Defining my own rectangle class
int x1 = 0, y1 = 0, //points at bottom-left corner of rectangle
x2 = 7, y2 = 5; //points at top-right corner of rectangle
public boolean containsPoint(int x,int y){
if (x > x1&& x < x2 &&y > y1 && y < y2)
return true;
else
return false;
}
}
public class Main{
public static void main(String args[]) {
Rectangle r=new Rectangle();
System.out.println(r.containsPoint(6,5));
}
}

2)

import java.awt.Rectangle;
public class Main{
public static void main(String args[]) {
Rectangle r=new Rectangle(5,6,8,4);//USING IN BUILT RECTANGLE CLASS
System.out.println(r.contains(6,5));
}
}

3)

import java.util.*;
public class MyClass{
public boolean checkStrings(){
Scanner sc=new Scanner(System.in);
System.out.print(\"Enter String 1:\");
String s1=sc.nextLine();
System.out.print(\"Enter String 2:\");
String s2=sc.nextLine();
if(s1.equals(s2))
return true;
else
return false;
}
public static void main(String args[]) {
MyClass m=new MyClass();
System.out.println(m.checkStrings());
}
}

  


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students