In: Computer Science
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 the point is inside the rectangle and false otherwise.
2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time.
3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first is through compiler optimization when we define two Strings in code with the same value. Demonstrate the second case in a method named stringEquality. Your method does not need any parameters. You may define the two strings inside the method. Your method should work even if a Scanner is used (no compiler optimization tricks).
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());
}
}