Practical OOP
Practical OOP
I. Write a Program that displays Welcome to Java, Learning Java Now and Programming is
fun.
Code:
Public class WelcomeMessage
{
Public Static void main(String[] args)
{
System.out.println(“Welcome to Java”);
System.out.println(“Learn Java Now”);
System.out.println(“Programming is fun”);
}
}
Output:
Page | 1
II. Write a Java Program to calculate simple interest.
Code:
import java.unit.Scanner;
Scanner.close();
}
}
Output:
Page | 2
III. Write a Java Program to check Leap Year.
Code:
import java.unit.Scanner;
if(isLeap)
{
System.out.println(year + “is a Leap Year.”);
}
else
{
System.out.println(year + “is NOT a Leap Year.”);
}
Scanner.close();
}
}
Output:
Page | 3
IV. Write a Java Program to add two binary numbers.
Code:
import java.unit.Scanner;
Scanner.close();
}
}
Output:
Page | 4
Practical No:-2
I. Write a Java Program to check if a number is Armstrong from given array.
Code:
import java.util.ArrayList;
Page | 5
Page | 6
II. Write a Java Program to perform bubble sort on Strings.
Code:
import java.util.Arrays;
Output:
Page | 7
III. Write a Java program to perform different methods of string buffer class.
Code:
sb.append(" World");
System.out.println("After append: " + sb);
sb.delete(5, 10);
System.out.println("After delete: " + sb);
sb.reverse();
System.out.println("After reverse: " + sb);
Output:
Page | 8
Practical No:-3
I. Create a class named Product with instance variables MRP and QUANTITY
and methods display (), setdata (). In display () method,
display the instance variables value (MRP and QUANTITY). And in setdata ()
method set the instance variable values (MRP and QUANTITY)).
Code:
Output:
Page | 9
II. Create a class named Account with instance variables Ac_No,Name and
Balance and methods display (), setdata (),deposit(). In display () method,
display the instance variables value (Ac_No,Name and Balance). And in
setdata () method set the instance variable values (Ac_No,Name and Balance)
and in deposit() method the amount that user wants to deposit that will be
deposited.
Code:
import java.util.Scanner;
Page | 10
account.display();
Output:
Page | 11
III. Create a class named Student with static variable Enrollment no. and instance
variable Name and methods display (), setdata (). in display () method, display
the variables value (Enrollment no. and Name). And in setdata () method set the
variable values (Enrollment no. and Name).
Code:
Output:
Page | 12
Practical No:- 4
I. Create a default constructor of class Product to print- “Welcome to Super-
Market.” And parameterized constructor to get the values of variables. (using
Command Line Arguments)
Code:
public Product() {
System.out.println("Welcome to Super-Market.");
}
Output:
Page | 13
II. Create a default constructor of class Account to print- “Welcome to Bank.”
And parameterized constructor to get the values of variables. (Using Command
Line Argument).
Code:
public Account() {
System.out.println("Welcome to Bank.");
}
Output:
Page | 14
III. Create a default constructor of class Student to print- “Welcome to Student-
Information system.” And parameterized constructor to get the values of
variables (Enrollment no. and Name). (using Command Line Arguments)
Code:
public Student() {
System.out.println("Welcome to Student-Information system.");
}
Output:
Page | 15
Page | 16
Practical No:-5
I. Create three inherited classes named-Crockery, Household, Food-items from
Product super class and inherit the instance method(display () and setdata ())
and variables(MRP and QUANTITY) of super class Product.
Code:
class Product {
protected double MRP;
protected int QUANTITY;
@Override
public void display() {
super.display();
System.out.println("Material: " + material);
}
}
Page | 17
this.usage = usage;
}
@Override
public void display() {
super.display();
System.out.println("Usage: " + usage);
}
}
@Override
public void display() {
super.display();
System.out.println("Expiry Date: " + expiryDate);
}
}
Output:
Page | 18
II. Create two inherited classes named-Savings,Current from Account super
class and inherit the instance method(display (),setdata () and deposit()) and
variables(Ac_No,Name and Balance) of super class Account.
Code:
class Account {
protected int Ac_No;
protected String Name;
protected double Balance;
Output:
Page | 20
III. Create two inherited classes named-BE,ME from Student super class and
inherit the instance method(display () and setdata ()) and variables(Enrollment
no and Name) of super class Student and make an instance variable Entry_year
and final variable duration for each of the above class
Code:
class Student {
protected int EnrollmentNo;
protected String Name;
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
Output:
Page | 22
Practical No:-6
I. Create a interface named place with method search() and variable BlockNo and
implement it together on all the above classes. Use binary I/O
Code:
import java.io.*;
interface Place {
int BlockNo = 0;
void search();
}
@Override
public void search() {
System.out.println("Searching student in Block: " + BlockNo);
}
}
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
oos.writeObject(beStudent);
oos.writeObject(meStudent);
Page | 24
}
Output:
Page | 25
II. Create an interface named Branch with method search() and variable IFSC
Code and implement it together on all the above classes.
Code:
import java.io.*;
interface Place {
int BlockNo = 0;
void search();
}
interface Branch {
String IFSC_Code = "DEFAULT_IFSC";
void search();
}
@Override
public void search() {
System.out.println("Searching student in Block: " + BlockNo + ", IFSC Code: " +
IFSC_Code);
}
}
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
@Override
public void display() {
super.display();
System.out.println("Entry Year: " + EntryYear + ", Duration: " + Duration + " years");
}
}
oos.writeObject(beStudent);
oos.writeObject(meStudent);
readBeStudent.display();
Page | 27
readMeStudent.display();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
Page | 28
III. Create a interface named Result with method getMarks() and variable
percentage and implement it together on all the above classes. in getMarks()
get the marks of 3 subjects and calculate the average inside the method.
Code:
import java.util.Scanner;
interface Result {
double percentage = 0.0;
void getMarks();
}
@Override
public void getMarks() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter marks for Student A (3 subjects): ");
double sub1 = scanner.nextDouble();
double sub2 = scanner.nextDouble();
double sub3 = scanner.nextDouble();
@Override
public void getMarks() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter marks for Student B (3 subjects): ");
double sub1 = scanner.nextDouble();
double sub2 = scanner.nextDouble();
double sub3 = scanner.nextDouble();
Output:
Page | 30
Practical No:-7
I. Define three objects for all the classes named- Crockery, Household, Food-items and
store the initial values for all the objects in arraylist or collection.
Code:
import java.util.*;
class Crockery {
String name;
String material;
double price;
class Household {
String itemName;
String category;
int quantity;
class FoodItems {
String itemName;
String expiryDate;
Page | 31
double weight;
Output:
Page | 32
II. Define three objects for all the classes named- Savings,Current and store the
initial values for all the objects in arraylist or collection.
Example: Class Savings extends Account implements Branch; Define objects.
Code:
import java.util.ArrayList;
class Savings {
String accountHolder;
double balance;
class Current {
String accountHolder;
double balance;
Page | 33
accounts.add(new Current("Emma", 20000));
accounts.add(new Current("Frank", 25000));
Output:
Page | 34
III. Define two objects for all the classes named- BE,ME and store the initial values
for all the objects in arraylist or collection.
Example: Class BE extends Student, implements Result; Define objects of BE.
Code:
import java.util.*;
class BE {
String name;
int duration;
class ME {
String specialization;
int duration;
Page | 35
}
}
}
Output:
Page | 36
Practical No:-8
I. Create a Package and put all the classes mentioned above in package.
Code:
Savings.java
package Bank;
public class Savings{
public void display(){
System.out.println("Savings Account\nBank: AXIS\nBranch: Ahmedabad");
}
}
Current.java
package Bank;
public class Current{
public void display(){
System.out.println("Current Account\nBank: HDFC\nBranch: Surat");
}
}
Practical.java
import java.util.Scanner;
class Crockery1
{
public void display()
{
System.out.println("Crocker includes Plates, jug, bowls, etc.");
}
class Food_items1
{
public void display()
{
System.out.println("Food-items includes Sunflower-oil, Rice,Wheat etc.");
}
}
class Household1
{
public void display()
{
System.out.println("Household product includes spoons, knives,forks, etc.");
}
}
Page | 37
public class Practical
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Crockery");
System.out.println("Enter 2 for Household");
System.out.println("Enter 3 for Food_items");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
System.out.print("\n");
switch(ch)
{
case 1:
Crockery1 cr=new Crockery1();
cr.display();
break;
case 2:
Household1 hr=new Household1();
hr.display();
break;
case 3:
Food_items1 fr= new Food_items1();
fr.display();
break;
default:
System.out.println("Invalid Choice");
}
}
}
Output:
Page | 38
II. Create a package and put all the classes mentioned above in the package.
Code:
package Bank;
public class Current{
public void display(){
System.out.println("Current Account\nBank: HDFC\nBranch: Surat");
}
}
package Bank;
public class Savings{
public void display(){
System.out.println("Savings Account\nBank: AXIS\nBranch: Ahmedabad");
}
}
Practical8b.java
//File: Practical18b.java
import Bank.*;
import java.util.Scanner;
class Practical8b
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Savings");
System.out.println("Enter 2 for Current");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
System.out.print("\n");
switch(ch)
{
case 1:
Output:
Page | 40
III. Create a Package and put all the classes mentioned above in
package.
Code:
package Student;
public class ME{
public void display(){
System.out.println("ME Student Info\nDegree: IT\nYears: 2 years");
}
}
package Student;
public class BE{
public void display(){
System.out.println("BE Student Info\nDegree: IT\nYears: 4 years");
}
}
Practical8c.java
import Student.BE;
import Student.ME;
import java.util.Scanner;
class Practical8c{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for BE");
System.out.println("Enter 2 for ME");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
System.out.print("\n");
switch(ch){
case 1:
BE cr=new BE();
cr.display();
break;
Page | 41
case 2:
ME hr=new ME();
hr.display();
break;
default:
System.out.println("Invalid Choice");
}
}
}
Output:
Page | 42
Practical No:-9
I. Create a method named buy () in the main function performing
exception handling.
Example: Banana = 10; Banana_Buy = 12 Here, Banana_Buy > Banana (throw
exception).
Code:
import java.util.Scanner;
Page | 43
Output:
Page | 44
II. Create a method named Withdraw () in the main function
performing exception handling.
Example: Balance = 1000; Withdraw = 12000 Here, Balance<Withdraw (throw
exception)
Code:
import java.util.Scanner;
try {
System.out.print("Enter the amount to withdraw: ");
double amount = scanner.nextDouble();
if (amount <= 0) {
throw new IllegalArgumentException("Withdrawal amount must be greater
than zero.");
}
if (amount > balance) {
throw new IllegalArgumentException("Insufficient balance.");
}
balance -= amount;
System.out.printf("Withdrawal successful! New balance: $%.2f%n", balance);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
} finally {
scanner.close(); // Close the scanner to prevent memory leaks
}
return balance;
}
}
Page | 45
Output:
Page | 46
III. Create a method named searchStudent() in the main () Function
performing exception handling.
Example: if we search the student name and if it is present in the list then it will
represent the details else it will throw an exception.
Code:
import java.util.HashMap;
import java.util.Scanner;
try {
searchStudent(studentName, students);
} catch (StudentNotFoundException e) {
System.out.println("Error: " + e.getMessage());
} finally {
scanner.close();
}
}
Page | 47
Output:
Page | 48
Practical No:-10
I. Save object data in the file using File Writer class, using
parameterized constructor.
Code:
import java.io.FileWriter;
import java.io.IOException;
class Person {
String name;
int age;
Page | 49
II. Save object data in the file using File Writer class, using
parameterized constructor.
Code:
import java.io.FileWriter;
import java.io.IOException;
class Person {
String name;
int age;
Page | 50
III. Save object data in the file using File Writer class, using
parameterized constructor.
Code:
import java.io.FileWriter;
import java.io.IOException;
class Person {
String name;
int age;
Page | 51
Page | 52
Practical No:-11
I. Create customer class which extends thread class and contains two
instance variables name, ProductName and static variable Product
Quantity. For example if two customers are trying to buy the same
product at once then follow synchronization of two customers
extending thread class.
Code:
class Customer extends Thread {
String name;
String productName;
static int productQuantity = 10;
c1.start();
c2.start();
}
}
Output:
Page | 53
II. Create Customer class which extends thread class and contains two
instance variables name, BankName and static variable Product
Balance. if two customers are trying to Withdraw from the same
Account at once then then follow synchronization of two customers
extending thread class.
Code:
class Customer extends Thread {
String name;
String bankName;
static int accountBalance = 100;
static Object lock = new Object();
c1.start();
c2.start();
}
}
Page | 54
Output:
Page | 55
III. Create Number class which extends thread class and create two
objects a1, a2 displaying the number having even Enrollment no.
and odd Enrollment no.
Code:
class Number extends Thread {
int enrollmentNo;
Number(int enrollmentNo) {
this.enrollmentNo = enrollmentNo;
}
a1.start();
a2.start();
}
}
Output:
Page | 56
Practical No:-12
I. Create the above system using a menu and implement it using switch
statement. For example, in the above system implement the menu
driven like..
1. For setting a value of MRP and QUANTITY for the product.
2. For buying a product.
3. For searching a product.
4. To delete a product from storage.
5. To show stored object data
Code:
import java.util.ArrayList;
import java.util.Scanner;
class Product {
String name;
double mrp;
int quantity;
Page | 57
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
setProduct();
break;
case 2:
buyProduct();
break;
case 3:
searchProduct();
break;
case 4:
deleteProduct();
break;
case 5:
showProducts();
break;
case 6:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
Page | 58
for (Product p : products) {
if (p.name.equalsIgnoreCase(name) && p.quantity > 0) {
p.quantity--;
System.out.println("Product purchased successfully!");
return;
}
}
System.out.println("Product not found or out of stock!");
}
Page | 59
Output:
Page | 60
II. Create the above system using a menu and implement it using switch
statement. For example, in the above system implement the menu
driven like.
1. For Setting a value of Ac_No,Name and Balance for the Bank
2. For Withdraw from a Account.
3. For Searching a Account.
4. To delete an Account from Bank.
5. To show stored object data
Code:
import java.util.ArrayList;
import java.util.Scanner;
class BankAccount {
int accountNumber;
String name;
double balance;
Page | 61
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
setAccount();
break;
case 2:
withdraw();
break;
case 3:
searchAccount();
break;
case 4:
deleteAccount();
break;
case 5:
showAccounts();
break;
case 6:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
Page | 62
System.out.print("Enter Amount to Withdraw: ");
double amount = scanner.nextDouble();
scanner.nextLine();
for (BankAccount acc : accounts) {
if (acc.accountNumber == accNo) {
if (acc.balance >= amount) {
acc.balance -= amount;
System.out.println("Withdrawal successful! Remaining Balance: " +
acc.balance);
} else {
System.out.println("Insufficient funds!");
}
return;
}
}
System.out.println("Account not found!");
}
Page | 63
if (accounts.isEmpty()) {
System.out.println("No accounts available!");
} else {
for (BankAccount acc : accounts) {
System.out.println(acc);
}
}
}
}
Output:
Page | 64
III. Create the above system using a menu and implement it using switch
statement. For example, in the above system implement the menu
driven like..
1. Add Student details.
2. Update student Details.
3. For Searching a Student.
4. Delete Student Details.
Code:
import java.util.ArrayList;
import java.util.Scanner;
class Student {
int id;
String name;
int age;
String course;
Page | 65
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
updateStudent();
break;
case 3:
searchStudent();
break;
case 4:
deleteStudent();
break;
case 5:
showStudents();
break;
case 6:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
Page | 66
scanner.nextLine();
for (Student s : students) {
if (s.id == id) {
System.out.print("Enter New Name: ");
s.name = scanner.nextLine();
System.out.print("Enter New Age: ");
s.age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter New Course: ");
s.course = scanner.nextLine();
System.out.println("Student details updated successfully!");
return;
}
}
System.out.println("Student not found!");
}
Page | 67
System.out.println("No students available!");
} else {
for (Student s : students) {
System.out.println(s);
}
}
}
}
Output:
Page | 68
Practical No:-13
I. Create the above system use interface of menu using JAVAFX.
Code:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
class Student {
int id;
String name;
int age;
String course;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Student Management System");
Page | 69
Button searchButton = new Button("Search Student");
Button deleteButton = new Button("Delete Student");
Button showButton = new Button("Show All Students");
Page | 70
selected.age = Integer.parseInt(parts[1].trim());
selected.course = parts[2].trim();
studentListView.refresh();
}
});
}
}
Output:
Page | 71
Page | 72