[go: up one dir, main page]

0% found this document useful (0 votes)
34 views19 pages

OOP Lab

Uploaded by

EnRyuu Castadel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views19 pages

OOP Lab

Uploaded by

EnRyuu Castadel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

NETAJI SUBHASH ENGINEERING COLLEGE

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE


LEARNING

Course Name: Object Oriented Programming Student Name:


Course Code: PCC CS 593 University Roll No.:

Sl. Exp. Problem Statement D.O.E. D.O.S. Marks Signature Remarks


No. No. 5 15 20
1 7 Create a class named 18.09.24 25.09.24
ColoredPoint inheriting the base
class Point. Base class Point have
its own coordinate value X and Y.
ColoredPoint, which is a subclass
of Point have its own personal
property Colour. The subclass
also have own method for
changeColor. Display the
information of the Point with
coordinate values and changed
colour values.
2 7 Create a package called “Shape”. 18.09.24 25.09.24
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.
3 7 Write a program using an 18.09.24 25.09.24
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
Average Marks
Problem Statement: Create a class named ColoredPoint inheriting the base class
Point. Base class Point have its own coordinate value X and Y. ColoredPoint,
which is a subclass of Point have its own personal property Colour. The subclass
also have own method for changeColor. Display the information of the Point with
coordinate values and changed colour values.
Program:
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

class Point {

Double x, y;

Point(Double x, Double y) {

this.x = x;

this.y = y;

class ColoredPoint extends Point {

String color;

ColoredPoint(Double x, Double y) {

super(x, y);

this.color = "black";

void changeColor(String newColor) {

color = newColor;

void displayInfo() {

System.out.println("Coordinates: (" + x + ", " + y + ")");

System.out.println("Color: " + color);

}
public class day7_1 {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter X coordinate: ");

Double x = Double.valueOf(reader.readLine());

System.out.print("Enter Y coordinate: ");

Double y = Double.valueOf(reader.readLine());

ColoredPoint coloredPoint = new ColoredPoint(x, y);

System.out.println("\nInitial Point Information:");

coloredPoint.displayInfo();

System.out.print("\nEnter new color: ");

String newColor = reader.readLine();

coloredPoint.changeColor(newColor);

System.out.println("\nUpdated Point Information:");

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;

public class day7_2 {

public static void main(String[] args) throws IOException{

BufferedReader reader = new BufferedReader(new


InputStreamReader(System.in));

Figure fig = new Figure();

OUTER:

while (true) {

System.out.print("Find the volume of:\ncube\ncylinder\ncuboid\nexit\n-


>");

String choice = reader.readLine();

switch (choice) {

case "cube" -> {

System.out.print("Enter side: ");

Double side = Double.valueOf(reader.readLine());

System.out.println("Volume: " + fig.volume(side));

case "cylinder" -> {

System.out.print("Enter radius: ");

Double radius = Double.valueOf(reader.readLine());

System.out.print("Enter height: ");

Double height = Double.valueOf(reader.readLine());

System.out.println("Volume: " + fig.volume(radius, height));

case "cuboid" -> {

System.out.print("Enter side 1: ");


Double side1 = Double.valueOf(reader.readLine());

System.out.print("Enter side 2: ");

Double side2 = Double.valueOf(reader.readLine());

System.out.print("Enter side 3: ");

Double side3 = Double.valueOf(reader.readLine());

System.out.println("Volume: " + fig.volume(side1, side2,


side3));

case "exit" -> {

break OUTER;

default -> System.out.println("Enter proper choice");

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 {

Double inVolume(Double s1, Double s2, Double s3);

Double outVolume(Double s1, Double s2, Double s3);

class VolumeCalculator implements Volume1 {

public Double inVolume(Double s1, Double s2, Double s3) {

return 3.14 * s1 * s2 * s3 / 3;

public Double outVolume(Double s1, Double s2, Double s3) {

return 4 * 3.14 * s1 * s2 * s3 / 3;

public class day7_3 {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new


InputStreamReader(System.in));

VolumeCalculator vol = new VolumeCalculator();

System.out.print("Enter s1: ");

Double s1 = Double.valueOf(reader.readLine());

System.out.print("Enter s2: ");

Double s2 = Double.valueOf(reader.readLine());

System.out.print("Enter s3: ");


Double s3 = Double.valueOf(reader.readLine());

System.out.println("InVolume: " + vol.inVolume(s1, s2, s3));

System.out.println("OutVolume: " + vol.outVolume(s1, s2, s3));

Output:
NETAJI SUBHASH ENGINEERING COLLEGE
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE
LEARNING

Course Name: Object Oriented Programming Student Name:


Course Code: PCC CS 593 University Roll No.:

Sl. Exp. Problem Statement D.O.E. D.O.S. Marks Signature Remarks


No. No. 5 15 20
1 8 Define an Exception
“NoMatchFoundException” that
is thrown when “Kolkata” is not
found from the following set of
strings.
city name ={ Kolkata, Chennai,
Mumbai, Delhi, Bangalore,
Ahmedabad}
2 8 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.
3 8 Write a program to declare &
instantiate an 2D-array to hold
marks obtained by students in
different subjects in a class.
Assume that there are up to 10
students in a class & there are 5
subjects. Find out the best
student according to average
marks of all subjects and display
all the marks of him/her.
4 8 Write a program in ultithreading
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.
Average Marks
Problem Statement: Define an Exception “NoMatchFoundException” that is
thrown when “Kolkata” is not found from the following set of strings.
city name ={ Kolkata, Chennai, Mumbai, Delhi, Bangalore, Ahmedabad}
Program:
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

class NoMatchFoundException extends Exception {

NoMatchFoundException(String message) {

super(message);

public class day8_1 {

static void searchCityInArray(String city, String[] cities) throws


NoMatchFoundException {

boolean found = false;

for (String c : cities) {

if (c.equalsIgnoreCase(city)) {

found = true;

break;

if (!found) {

throw new NoMatchFoundException("NoMatchFoundException: City " + city +


" not found in the list.");

public static void main(String[] args)

throws IOException{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number of cities:");


int numCities = Integer.parseInt(reader.readLine());

String[] cities = new String[numCities];

System.out.println("Enter the names of the cities:");

for (int i = 0; i < numCities; i++) {

cities[i] = reader.readLine();

System.out.println("Enter the city name to search:");

String searchCity = reader.readLine();

try {

searchCityInArray(searchCity, cities);

System.out.println("City " + searchCity + " found in the list.");

} 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&gt;=valid age&lt;=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;

class InvalidVoterAgeException extends Exception {

InvalidVoterAgeException() {

super("InvalidVoterAgeException: Age not suitable for voting");

class Voter {

boolean voterValidator (int age) throws InvalidVoterAgeException{

if (age >= 18 && age <= 135) {

return true;

} else {

throw new InvalidVoterAgeException();

public class Election {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

Voter voter = new Voter();

System.out.print("Enter age: ");


int age = Integer.parseInt(reader.readLine());

try {

if (voter.voterValidator(age)) {

System.out.println("Person is of valid age for voting...");

} catch (InvalidVoterAgeException e) {

System.out.println(e.getMessage());

Output:

Problem Statement: Write a program to declare & instantiate an 2D-array to


hold marks obtained by students in different subjects in a class. Assume that
there are up to 10 students in a class & there are 5 subjects. Find out the best
student according to average marks of all subjects and display all the marks of
him/her.
Program:
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
public class day8_3 {

public static void main(String[] args) throws IOException{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

// Constants for the number of students and subjects

int numStudents = 10;

int numSubjects = 5;

// 2D array to store marks

int[][] marks = new int[numStudents][numSubjects];

// Taking input of marks for each student

System.out.println("Enter the marks for each student in each subject:");

for (int i = 0; i < numStudents; i++) {

System.out.println("Student " + (i + 1) + ":");

for (int j = 0; j < numSubjects; j++) {

System.out.print("Subject " + (j + 1) + ": ");

marks[i][j] = Integer.parseInt(reader.readLine());

// Find the index of the best student based on average marks

int bestStudentIndex = findBestStudentIndex(marks);

// Display the marks of the best student

displayBestStudent(marks, bestStudentIndex);

// Method to find the index of the student with the highest average marks

static int findBestStudentIndex(int[][] marks) {

int bestStudentIndex = -1;

double highestAverage = 0;

for (int i = 0; i < marks.length; i++) {

int sum = 0;

for (int j = 0; j < marks[i].length; j++) {


sum += marks[i][j];

double average = sum / (double) marks[i].length;

if (average > highestAverage) {

highestAverage = average;

bestStudentIndex = i;

return bestStudentIndex;

// Method to display the marks of the best student

static void displayBestStudent(int[][] marks, int bestStudentIndex) {

if (bestStudentIndex != -1) {

System.out.println("\nBest student is Student " + (bestStudentIndex + 1)


+ " with the following marks:");

for (int mark : marks[bestStudentIndex]) {

System.out.print(mark + " ");

} else {

System.out.println("No students available.");

}
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 {

private int terms;

public FibonacciThread(int terms) {

this.terms = terms;

@Override

public void run() {

int a = 0, b = 1;

System.out.println("Fibonacci Series:");

for (int i = 1; i <= terms; i++) {

System.out.print(a + " ");

int next = a + b;

a = b;

b = next;

try {

Thread.sleep(1000); // Sleep for 1000 ms

} catch (InterruptedException e) {

System.out.println("Fibonacci thread interrupted");

System.out.println("\nFibonacci Thread completed.");

class PrimeThread extends Thread {

private int start;

private int end;

public PrimeThread(int start, int end) {


this.start = start;

this.end = end;

private boolean isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) return false;

return true;

@Override

public void run() {

System.out.println("Prime Numbers:");

for (int i = start; i <= end; i++) {

if (isPrime(i)) {

System.out.print(i + " ");

try {

Thread.sleep(500); // Sleep for 500 ms

} catch (InterruptedException e) {

System.out.println("Prime thread interrupted");

System.out.println("\nPrime Thread completed.");

public class day8_4 {

public static void main(String[] args) {

int fibonacciTerms = 10; // Number of terms in the Fibonacci series

int primeStart = 10; // Starting range for prime numbers

int primeEnd = 50; // Ending range for prime numbers

// Creating and starting the Fibonacci thread


FibonacciThread fibonacciThread = new FibonacciThread(fibonacciTerms);

fibonacciThread.start();

// Creating and starting the Prime thread

PrimeThread primeThread = new PrimeThread(primeStart, primeEnd);

primeThread.start();

Output:

You might also like