CO3
CO3
Write a program to create a “distance” class with methods where distance is computed
in terms of feet and inches, how to create objects of a class
//23BCP471
class Distance {
int feet;
int inches;
// Constructor
this.feet = feet;
this.inches = inches;
// Clone method
Distance cloneDistance() {
}
}
d1.displayDistance();
d2.displayDistance();
System.out.print("Clone: ");
d3.displayDistance();
OUTPUT:
Modify the “distance” class by creating constructor for assigning values (feet and inches) to
the distance object. Create another object and assign second object as reference variable to
another object reference variable. Further create a third object which is a clone of the first
object.
//23BCP471
class Demo {
// Public getter
return privateVar;
privateVar = value;
}
class Test {
modifyPrimitive(primitive);
System.out.println("After modifying primitive: " + primitive); // No change
modifyObject(obj);
System.out.println("After modifying object: " + obj.publicVar); // Changed
Output:
Write a program that implements two constructors in the class. We call the other constructor
using ‘this’ pointer, from the default constructor of the class.
class Demo {
int value;
// Default constructor
Demo() {
// Parameterized constructor
Demo(int value) {
this.value = value;
System.out.println("Parameterized constructor called with value: " + value);
Output:
Write a program in Java in which a subclass constructor invokes the constructor of the super
class and instantiate the values.
class Parent {
int parentValue;
// Constructor of superclass
Parent(int value) {
this.parentValue = value;
System.out.println("Parent constructor called with value: " + value);
int childValue;
// Constructor of subclass
Child(int parentValue, int childValue) {
this.childValue = childValue;
}
}
Output:
class Animal {
void eat() {
dog.bark();
}
}
Output:
2)
class Animal {
void eat() {
void walk() {
}
}
class Dog extends Mammal {
void bark() {
}
}
Output:
Java Program to demonstrate the real scenario (e.g., bank) of Java Method Overriding where
three classes are overriding the method of a parent class. Creating a parent class.
class Bank {
double getRateOfInterest() {
return 0; // Base rate
@Override
double getRateOfInterest() {
return 5.4;
@Override
double getRateOfInterest() {
return 6.8;
double getRateOfInterest() {
return 7.1;
}
}
Output:
Write a java program for the use of super and this keyword
class Employee {
String name;
int id;
this.id = id;
void display() {
emp.display();
Output:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
@Override
void eat() {
System.out.println("Dog is eating.");
}
}
dog.eat();
}
}
Output:
class Vehicle {
System.out.println("Vehicle is starting.");
}
}
System.out.println("Car is driving.");
car.drive();
-FINAL VARIABLE
-FINAL CLASS
System.out.println("Drawing a shape.");
}
}
// class Circle extends Shape { // Error: Cannot inherit from final class
// }
shape.draw();
Write a program that implements simple example of Runtime Polymorphism with multilevel
inheritance. (e.g., Animal or Shape)
class Animal {
void sound() {
void sound() {
}
class Dog extends Mammal {
@Override
void sound() {
Output:
Write a program to compute if one string is a rotation of another. For example, pit is rotation
of tip as pit has same character as tip
if (isRotation(str1, str2)) {
} else {
Output:
// Constructor
public Employee(String name, int id, double baseSalary) {
this.name = name;
this.id = id;
this.baseSalary = baseSalary;
// Abstract methods
abstract double calculateSalary();
// Subclass Manager
// Constructor
// Implement calculateSalary
@Override
double calculateSalary() {
// Implement displayInfo
@Override
void displayInfo() {
System.out.println("Manager Info:");
System.out.println("Name: " + name + ", ID: " + id + ", Salary: " + calculateSalary());
// Subclass Programmer
// Constructor
this.overtimePay = overtimePay;
// Implement calculateSalary
@Override
double calculateSalary() {
// Implement displayInfo
@Override
void displayInfo() {
System.out.println("Programmer Info:");
System.out.println("Name: " + name + ", ID: " + id + ", Salary: " + calculateSalary());
manager.displayInfo();
programmer.displayInfo();
Output:
EXP7: STUDY AND IMPLEMENT EXCEPTION HANDLING IN JAVA
import java.util.Scanner;
try {
// Perform division
} catch (ArithmeticException e) {
// Always executed
System.out.println("Execution completed.");
OUTPUTS:
Refine the student manager program to manipulate the student information from files by
using the BufferedReader and BufferedWriter CO4 vi. Write a program to manipulate the
information from files by using the Reader and Writer class. Assume suitable data.
import java.io.*;
import java.util.*;
class Student {
this.name = name;
this.age = age;
this.rollNumber = rollNumber;
// Getter methods
return name;
}
return age;
return rollNumber;
}
System.out.println("Name: " + name + ", Age: " + age + ", Roll Number: " +
rollNumber);
writer.newLine();
student.saveToFile(writer);
}
System.out.println("Student data has been added to the file.");
} catch (IOException e) {
e.printStackTrace();
String line;
System.out.println(line);
}
} catch (IOException e) {
displayStudentDataFromFile(fileName);
}
OUTPUT:
File :
EXP9: STUDY AND IMPLEMENT MULTI-THREADED APPLICATION IN JAVA
Write a Java program to demonstrate how to create and start a thread using both theThread
class and the Runnable interface.
Output:
Write a Java program that illustrates thread synchronization by ensuring multiple threads can
safely access a shared resource without causing data inconsistency.
class Counter {
count++;
Counter counter;
MyThread(Counter counter) {
this.counter = counter;
counter.increment();
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
Output:
import javax.swing.*;
import java.awt.event.*;
public WindowEventDemo() {
setSize(400, 300);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}
@Override
System.out.println("Window opened.");
@Override
System.out.println("Window is closing.");
// System.exit(0);
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Window closed.");
@Override
System.out.println("Window minimized.");
@Override
@Override
System.out.println("Window activated.");
@Override
System.out.println("Window deactivated.");
}
}
OUTPUT:
WINDOWS: