NTIC Faculty L1/S2 2023–2024
Introduction to Object-Oriented Programming
Lab 2
Exercise 1
Write a program that takes a positive real number r and displays the largest even number strictly less
than r.
Hints:
- Use a Scanner object scr for input created by Scanner scr=new Scanner(System.in);
- Import the Scanner class by adding import java.util.Scanner; at the beginning of the file.
- scr.nextDouble(); retrieves a real number entered via the keyboard.
Exercise 2
1. Write the Point class :
Point
double x
double y
Point symY()//gives the point symmetric to the y-axis
Point symX()//gives the point symmetric to the x-axis
double distance(Point)// gives the distance to another point
2. Write the Circle class :
Circle
Point centre
double radius
double surface()//gives the surface area of the circle
boolean includes(Point)// checks if a point belongs to the circle
double distance(Point)// gives the distance to another point
3. Write the Rectangle class :
Rectangle
Point corner1
Point corner2// sides are parallel to the x and y axes.
double surface()//gives the surface area of the rectangle
double perimeter ()//gives the perimeter of the rectangle
4. Write a program to:
a. Enter the coordinates of 3 points a, b, and c.
b. Display the coordinates of the symmetric points to each of the three points with respect to the
x and y axes.
c. Calculate the distances between the three points to determine the type of triangle.
d. Enter the radii of three circles with centres a, b, and c.
e. Display the areas of the three circles.
f. Check if a point d entered by the user belongs to any of the circles.
g. Display the surface area and perimeter of the rectangle with a and b as two opposite corners.