LCI2023004 Vedamsh OOPS LAB Assignment
LCI2023004 Vedamsh OOPS LAB Assignment
Roll Number:LCI2023004
APRIL-10
1. 1. Java Program to Access Super Class Method and Instance Variable Using
Super Keyword
2. 2. Java Program to Access Super Class Method and Instance Variable
without Super Keyword.
3. 3. Write Java program for the following:
a. Example for this operator and the use of this keyword.
b. Example for super keywords.
c. Example for static variables and methods.
4. Write a Java program to print the sum of two numbers by using this
keyword.
5. Derive sub-classes of ContractEmployee namely HourlyEmployee &
WeeklyEmployee with information number of hours & wages per hour,
number of weeks & wages per week respectively & method
calculateWages() to calculate their monthly salary. Also override getDesig()
method depending on the type of contract employee.
6. Create an Interface Vehicle with methods getColor(), getNumber(),
getConsumption() calculate the fuel consumed, name and colour for Two
Wheeler and Four Wheeler By implementing interface Vehicle.
7. Create an Interface Fare with method getAmount() to get the amount paid
for fare of travelling. Calculate the fare paid by bus and train implementing
interface Fare.
8. Create an Interface Student Fee with methods getAmount(),
getFirstName(), getLastName(), getAddress(), getContact(). Calculate the
amount paid by the Hostler and Non Hostler student by implementing
interface Student Fee.
9. Create your own exception class using the extends keyword. Write a
constructor for this class that takes a String argument and stores it inside
the object with a String reference. Write a method that prints out the stored
String. Create a try- catch clause to exercise your new exception.
10. Write a Java program to create a package called dept. Create four classes as
CSE, ECE, ME and CE add methods in each class which can display
subject names of your respective year. access this package classes from the
main class.
COLLECTION:
In the following collection framework perform these operation creation,
insertion, updation, deletion, iteration in Array list, linked list,vector.
DATA STRUCTURES:
Implement, create, update, insert, delete and iterate the following data structures
1) set- hash set, tree set
2) map- hash table, hash map
April 3rd Assignment:
Q1:
abstract class Vehicle {
private String name;
private int year;
public Vehicle(String name, int year) {
this.name = name;
this.year = year;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Model: " + model);
}
}
@Override
public void startEngine() {
System.out.println("Bike " + getName() + " is
starting its engine...");
}
Q2:
class Book {
private String name;
private String author;
private int count;
class Customer {
private int id;
private String name;
private String address;
customer1.buyBook(book1);
customer2.buyBook(book1);
customer1.buyBook(book2);
customer1.buyBook(book1);
}
}
Q3:
class OOPDemo {
static abstract class Shape {
abstract double area();
abstract double circumference();
}
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
@Override
double circumference() {
return 2 * Math.PI * radius;
}
}
@Override
double area() {
return length * width;
}
@Override
double circumference() {
return 2 * (length + width);
}
}
Q4:
class OOPDemo {
static class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String
lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
double getArea() {
return side * side;
}
@Override
double getVolume() {
return 0;
}
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
@Override
double getVolume() {
return 0;
}
}
@Override
double getArea() {
return 6 * side * side;
}
@Override
double getVolume() {
return side * side * side;
}
}
@Override
double getVolume() {
return (4.0 / 3) * Math.PI * radius *
radius * radius;
}
}
Q6:
class Circle {
protected double radius;
@Override
public double getArea() {
return radius * width;
}
}
Q7:
class Circle {
protected double radius;
@Override
public double getArea() {
return radius * width;
}
}
@Override
public double getArea() {
return radius * width;
}
}
Q9:
class Student {
protected String name;
protected int rollNumber;
public Student(String name, int rollNumber) {
this.name = name;
this.rollNumber = rollNumber;
}
}
Q10:
abstract class Employee {
abstract double getAmount();
}
@Override
double getAmount() {
return salary_per_week * total_weeks;
}
}
@Override
double getAmount() {
return hourly_rate * total_hours;
}
}
Q11:
interface Payable {
double getAmount();
}
@Override
public double getAmount() {
return amount;
}
}
@Override
public double getAmount() {
return salary;
}
}
System.out.println("Amount to be paid to
Invoice: Rs" + invoice.getAmount());
System.out.println("Amount to be paid to
Employee: Rs" + employee.getAmount());
}
}
Q12:
interface Vehicle {
String getColor();
String getNumber();
double getConsumption();
double calculateFuelConsumed(double distance);
}
@Override
public String getColor() {
return color;
}
@Override
public String getNumber() {
return null; // Not for 2wheelerss
@Override
public double getConsumption() {
return 0; // Not for 2wheelerss
}
@Override
public double calculateFuelConsumed(double
distance) {
return distance / 50.0;
}
}
class FourWheeler implements Vehicle {
private String name;
private String color;
private String number;
private double consumption;
@Override
public String getColor() {
return color;
}
@Override
public String getNumber() {
return number;
}
@Override
public double getConsumption() {
return consumption;
}
@Override
public double calculateFuelConsumed(double
distance) {
return distance / consumption;
}
}
public class OOPDemo {
public static void main(String[] args) {
TwoWheeler bike = new TwoWheeler("Honda",
"Black");
FourWheeler car = new FourWheeler("Toyota",
"White", "MH01AB1234", 15);
Q13:
interface Fare {
double getAmount();
}
@Override
public double getAmount() {
return distance * farepkm;
}
}
@Override
public double getAmount() {
return hostelFee;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getAddress() {
return address;
}
@Override
public String getContact() {
return contact;
}
}
@Override
public double getAmount() {
return tuitionFee;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getAddress() {
return address;
}
@Override
public String getContact() {
return contact;
}
}
Q1:
class SuperClass {
int num = 100;
void display() {
System.out.println("This is the display method of
SuperClass");
}
}
class SubClass extends SuperClass {
int num = 200;
void display() {
System.out.println("This is the display method of
SubClass");
}
void accessSuperMethod() {
super.display();
}
void accessSuperVariable() {
System.out.println("Value of num in SuperClass: "
+ super.num);
}
}
public class OOPDemo {
public static void main(String[] args) {
SubClass obj = new SubClass();
obj.display();
obj.accessSuperMethod();
obj.accessSuperVariable();
}
}
Q2:
class SuperClass {
int num = 100;
void display() {
System.out.println("This is the display method of
SuperClass");
}
}
Q3:
class Person {
String name;
Person(String name) {
this.name = name;
}
void display() {
System.out.println("Name: " + this.name);
}
}
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
class Counter {
static int count = 0;
Counter() {
count++;
}
static void displayCount() {
System.out.println("Count: " + count);
}
}
public class OOPDemo {
public static void main(String[] args) {
Person person = new Person("John");
person.display();
Dog dog = new Dog();
dog.sound();
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter.displayCount();
}
}
Q4:
public class OOPDemo {
private int num1;
private int num2;
public OOPDemo(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public int calculateSum() {
return this.num1 + this.num2;
}
public static void main(String[] args) {
OOPDemo calculator = new OOPDemo(6, 8);
int sum = calculator.calculateSum();
System.out.println("Sum of the two numbers: " +
sum);
}
}
Q5:
class ContractEmployee {
protected String firstName;
protected String lastName;
protected String department;
protected double monthlySalary;
@Override
public String getDesig() {
return "Hourly Contract Employee";
}
@Override
public String getDesig() {
return "Weekly Contract Employee";
}
hourlyEmployee.displayFullName();
System.out.println("Designation: " +
hourlyEmployee.getDesig());
System.out.println("Monthly Salary: Rs " +
hourlyEmployee.calculateWages());
weeklyEmployee.displayFullName();
System.out.println("Designation: " +
weeklyEmployee.getDesig());
System.out.println("Monthly Salary: Rs" +
weeklyEmployee.calculateWages());
}
}
Q6:
interface Vehicle {
String getColor();
String getNumber();
double getConsumption();
double calculateFuelConsumed(double distance);
}
@Override
public String getColor() {
return color;
}
@Override
public String getNumber() {
return null; // Not for 2wheelerss
@Override
public double getConsumption() {
return 0; // Not for 2wheelerss
}
@Override
public double calculateFuelConsumed(double
distance) {
return distance / 50.0;
}
}
@Override
public String getColor() {
return color;
}
@Override
public String getNumber() {
return number;
}
@Override
public double getConsumption() {
return consumption;
}
@Override
public double calculateFuelConsumed(double
distance) {
return distance / consumption;
}
}
@Override
public double getAmount() {
return distance * farepkm;
}
}
@Override
public double getAmount() {
return distance * farepkm;
}
}
public class OOPDemo {
public static void main(String[] args) {
Bus bus = new Bus(100, 2.5);
Train train = new Train(200, 3.0);
Q8:
interface StudentFee {
double getAmount();
String getFirstName();
String getLastName();
String getAddress();
String getContact();
}
@Override
public double getAmount() {
return hostelFee;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getAddress() {
return address;
}
@Override
public String getContact() {
return contact;
}
}
@Override
public double getAmount() {
return tuitionFee;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getAddress() {
return address;
}
@Override
public String getContact() {
return contact;
}
}
Q9:
class UnderageException extends Exception {
private String message;
try {
if (age < 18) {
throw new UnderageException("Underage:
You cannot vote if you are under 18 years old.");
} else {
System.out.println("You are eligible
to vote.");
}
} catch (UnderageException e) {
e.printMessage();
}
}
}
Q10:
// File: Main.java
import dept.*;
// File: CSE.java
package dept;
public class CSE {
public void displaySubjects() {
System.out.println("Computer Science and
Engineering subjects: ");
System.out.println("1. Data Structures and
Algorithms");
System.out.println("2. Database Management
Systems");
}
}
// File: ECE.java
package dept;
public class ECE {
public void displaySubjects() {
System.out.println("Electronics and
Communication Engineering subjects: ");
System.out.println("1. Analog Electronics");
System.out.println("2. Digital Signal
Processing");
}
}
// File: ME.java
package dept;
public class ME {
public void displaySubjects() {
System.out.println("Mechanical Engineering
subjects: ");
System.out.println("1. Thermodynamics");
System.out.println("2. Fluid Mechanics");
}
}
// File: CE.java
package dept;
public class CE {
public void displaySubjects() {
System.out.println("Civil Engineering
subjects: ");
System.out.println("1. Structural Analysis");
System.out.println("2. Geotechnical
Engineering");
}
}
COLLECTION:
import java.util.*;
// LinkedList
LinkedList<String> linkedList = new
LinkedList<>();
// Creation
linkedList.add("Dairy Milk");
linkedList.add("KitKat");
linkedList.add("Milkybar");
// Insertion
linkedList.add(1, "Ferrero Rocher");
// Updation
linkedList.set(2, "Snickers");
// Deletion
linkedList.remove("KitKat");
// Iteration
System.out.println("\nLinkedList:");
for (String chocolate : linkedList) {
System.out.println(chocolate);
}
// Vector
Vector<String> vector = new Vector<>();
// Creation
vector.add("Dairy Milk");
vector.add("KitKat");
vector.add("Milkybar");
// Insertion
vector.add(1, "Ferrero Rocher");
// Updation
vector.set(2, "Snickers");
// Deletion
vector.remove("KitKat");
// Iteration
System.out.println("\nVector:");
for (String chocolate : vector) {
System.out.println(chocolate);
}
}
}
DATA STRUCTURES:
import java.util.*;
public class DataStructuresExample {
public static void main(String[] args) {
// HashSet
HashSet<String> hashSet = new HashSet<>();
// Creation
hashSet.add("Chocolates");
hashSet.add("Candies");
hashSet.add("Biscuits");
// Iteration
System.out.println("HashSet:");
for (String snack : hashSet) {
System.out.println(snack);
}
// Check for a specific element
System.out.println("Contains 'Chocolates': " +
hashSet.contains("Chocolates"));
// Deletion
hashSet.remove("Candies");
System.out.println("HashSet after removing
'Candies': " + hashSet);
// TreeSet
TreeSet<String> treeSet = new TreeSet<>();
// Creation
treeSet.add("Chocolates");
treeSet.add("Candies");
treeSet.add("Biscuits");
// Iteration
System.out.println("\nTreeSet:");
for (String snack : treeSet) {
System.out.println(snack);
}
// Check for a specific element
System.out.println("Contains 'Chocolates': " +
treeSet.contains("Chocolates"));
// Deletion
treeSet.remove("Biscuits");
System.out.println("TreeSet after removing
'Biscuits': " + treeSet);
// Hashtable
Hashtable<Integer, String> hashtable = new
Hashtable<>();
// Creation
hashtable.put(10, "Chocolates");
hashtable.put(20, "Candies");
hashtable.put(30, "Biscuits");
// Iteration
System.out.println("\nHashtable:");
for (Map.Entry<Integer, String> entry :
hashtable.entrySet()) {
System.out.println(entry.getKey() + " -> "
+ entry.getValue());
}
// Check for a specific key
System.out.println("Contains key '10': " +
hashtable.containsKey(10));
// Deletion
hashtable.remove(20);
System.out.println("Hashtable after removing
key '20': " + hashtable);
// HashMap
HashMap<Integer, String> hashMap = new
HashMap<>();
// Creation
hashMap.put(100, "Chocolates");
hashMap.put(200, "Candies");
hashMap.put(300, "Biscuits");
// Iteration
System.out.println("\nHashMap:");
for (Map.Entry<Integer, String> entry :
hashMap.entrySet()) {
System.out.println(entry.getKey() + " -> "
+ entry.getValue());
}
// Check for a specific key
System.out.println("Contains key '100': " +
hashMap.containsKey(100));
// Deletion
hashMap.remove(300);
System.out.println("HashMap after removing key
'300': " + hashMap);
}
}