Question

In: Computer Science

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

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).

Solutions

Expert Solution

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());
}
}

  


Related Solutions

Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT