OOP Lab
OOP Lab
import java.io.IOException;
import java.io.InputStreamReader;
class Point {
Double x, y;
Point(Double x, Double y) {
this.x = x;
this.y = y;
String color;
ColoredPoint(Double x, Double y) {
super(x, y);
this.color = "black";
color = newColor;
void displayInfo() {
}
public class day7_1 {
Double x = Double.valueOf(reader.readLine());
Double y = Double.valueOf(reader.readLine());
coloredPoint.displayInfo();
coloredPoint.changeColor(newColor);
coloredPoint.displayInfo();
Output:
Problem Statement: Create a package called “Shape”. Inside this package define
a class named as “Figure”, which computes the volume of a cube, cylinder, and
rectangular box using method overloading. Access the class and methods from
another file.
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import shape.Figure;
OUTER:
while (true) {
switch (choice) {
break OUTER;
System.out.println();
Output:
Problem Statement: Write a program using an interface called Volume. Assume
that there is a part in a machine having three dimensions s1, s2, s3, and
Involume=1/3*pi*s1*s2*s3
Outvolume=4/3*pi*s1*s2*s3
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
interface Volume1 {
return 3.14 * s1 * s2 * s3 / 3;
return 4 * 3.14 * s1 * s2 * s3 / 3;
Double s1 = Double.valueOf(reader.readLine());
Double s2 = Double.valueOf(reader.readLine());
Output:
NETAJI SUBHASH ENGINEERING COLLEGE
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE
LEARNING
import java.io.IOException;
import java.io.InputStreamReader;
NoMatchFoundException(String message) {
super(message);
if (c.equalsIgnoreCase(city)) {
found = true;
break;
if (!found) {
throws IOException{
cities[i] = reader.readLine();
try {
searchCityInArray(searchCity, cities);
} catch (NoMatchFoundException e) {
System.out.println(e.getMessage());
Output:
Problem Statement: Assume an Election Management System having the
following classes:
Voter: class to represent voters
VoterValidator: method to validate the voters (in the basis of age)
InvalidVoterAgeException: an exception which may occur if the voter’s age is
invalid(say,18>=valid age<=135)
Election: class containing main() to operate election.
Create the classes with proper attributes and methods. Check whether the
voter is valid or not.
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
InvalidVoterAgeException() {
class Voter {
return true;
} else {
try {
if (voter.voterValidator(age)) {
} catch (InvalidVoterAgeException e) {
System.out.println(e.getMessage());
Output:
import java.io.IOException;
import java.io.InputStreamReader;
public class day8_3 {
int numSubjects = 5;
marks[i][j] = Integer.parseInt(reader.readLine());
displayBestStudent(marks, bestStudentIndex);
// Method to find the index of the student with the highest average marks
double highestAverage = 0;
int sum = 0;
highestAverage = average;
bestStudentIndex = i;
return bestStudentIndex;
if (bestStudentIndex != -1) {
} else {
}
Output:
Problem Statement: Write a program in multithreading where one thread is
Fibonacci and another thread is Prime. Fibonacci thread will display the
Fibonacci series for n terms with 1000 ms sleeping time, whereas the Prime
thread will print all prime numbers of a given range with 500 ms sleeping time.
Program:
class FibonacciThread extends Thread {
this.terms = terms;
@Override
int a = 0, b = 1;
System.out.println("Fibonacci Series:");
int next = a + b;
a = b;
b = next;
try {
} catch (InterruptedException e) {
this.end = end;
return true;
@Override
System.out.println("Prime Numbers:");
if (isPrime(i)) {
try {
} catch (InterruptedException e) {
fibonacciThread.start();
primeThread.start();
Output: