Value Added File
Value Added File
Submitted By – Submitted To –
Qid - 22030071
1. Create a Class Student with fields name, rollNo, and marks, and print their details.
class Student {
String name;
int rollNo;
double marks;
Output:
Name: Amit
Roll No: 101
Marks: 89.5
class Calculator {
class BankAccount {
private double balance;
BankAccount(double initialBalance) {
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance!");
}
}
void checkBalance() {
System.out.println("Current Balance: " + balance);
}
public static void main(String[] args) {
BankAccount acc = new BankAccount(1000);
acc.deposit(500);
acc.withdraw(300);
acc.checkBalance();
}
}
Output:
Deposited: 500.0
Withdrawn: 300.0
Current Balance: 1200.0
4. Implement Inheritance with classes Animal → Dog and Cat, and show
method overriding.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
Output:
Dog barks
Cat meows
5. Create an interface Playable with a method play(), and implement it in classes Guitar
and Piano.
interface Playable {
void play();
}
class Guitar implements Playable {
public void play() {
System.out.println("Playing Guitar");
}
}
class Piano implements Playable {
public void play() {
System.out.println("Playing Piano");
}
public static void main(String[] args) {
Playable g = new Guitar();
Playable p = new Piano();
g.play();
p.play();
}
}
Output:
Playing Guitar
Playing Piano
Output:
Drawing Circle
Drawing Rectangle
class Person {
String name = "Person";
Person() {
System.out.println("Person constructor called");
}
}
class Student extends Person {
String name = "Student";
Student() {
super(); // Calls parent class constructor
System.out.println("Student constructor called");
System.out.println("Super name: " + super.name); // Refers to parent variable
System.out.println("This name: " + this.name); // Refers to current class
variable
}
public static void main(String[] args) {
Student s = new Student();
}
}
Output:
Person constructor called
Student constructor called
Super name: Person
This name: Student
8. Create a class Vehicle with a static variable count to track number of objects created.
class Vehicle {
static int count = 0;
Vehicle() {
count++;
}
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
Vehicle v2 = new Vehicle();
Vehicle v3 = new Vehicle();
System.out.println("Total Vehicles created: " + Vehicle.count);
}
}
Output:
Total Vehicles created: 3
9. Write a program to implement encapsulation with proper getters and setters for class
Book.
class Book {
private String title;
private String author;
private double price;
// Getters
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
// Setters
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
if (price > 0)
this.price = price;
}
public static void main(String[] args) {
Book b = new Book();
b.setTitle("Java Programming");
b.setAuthor("James Gosling");
b.setPrice(450.75);
Output:
Book Title: Java Programming
Author: James Gosling
Price: 450.75
class Rectangle {
int length, width;
// Default constructor
Rectangle() {
length = 0;
width = 0;
}
// Constructor with one parameter
Rectangle(int side) {
length = width = side;
}
// Constructor with two parameters
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
void display() {
System.out.println("Length: " + length + ", Width: " + width);
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5);
Rectangle r3 = new Rectangle(4, 6);
r1.display();
r2.display();
r3.display();
}
}
Output:
Length: 0, Width: 0
Length: 5, Width: 5
Length: 4, Width: 6
11. Create a class Person with fields name and age, and a method
displayDetails() to print them.
class Person {
String name;
int age;
this.name = name;
this.age = age;
void displayDetails() {
p.displayDetails();
Output:
Name: Alice
Age: 25
12. Create a class Book with title and author. Create another class Library that can add
and display one Book.
class Book {
String title, author;
void addBook(Book b) {
book = b;
}
void showBook() {
if (book != null) {
book.display();
} else {
System.out.println("No book in library.");
}
}
public static void main(String[] args) {
Book b1 = new Book("Clean Code", "Robert C. Martin");
Library lib = new Library();
lib.addBook(b1);
lib.showBook();
}
}
Output:
Title: Clean Code
Author: Robert C. Martin
13. Create a class Counter with a static variable to count how many objects are created.
class Counter {
static int count = 0;
Counter() {
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Output:
Number of Counter objects created: 3
14. Create a class Movie with a static method that returns the count of Movie objects
created.
class Movie {
private static int movieCount = 0;
Movie() {
movieCount++;
}
static int getMovieCount() {
return movieCount;
}
public static void main(String[] args) {
new Movie();
new Movie();
new Movie();
15. Demonstrate polymorphism using a parent class Shape and child classes Circle,
Square.
class Shape {
void draw() {
System.out.println("Drawing Shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing Square");
}
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Square();
Shape s3 = new Shape();
Output:
Drawing Circle
Drawing Square
Drawing Shape
import java.util.ArrayList;
import java.util.List;
class Employee {
String name;
String department;
double salary;
void addEmployee(Employee e) {
employees.add(e);
}
void viewEmployees() {
System.out.println("Name | Department | Salary");
for (Employee e : employees) {
e.display();
}
}
void filterByDepartment(String dept) {
System.out.println("Employees in Department: " + dept);
for (Employee e : employees) {
if (e.department.equalsIgnoreCase(dept)) {
e.display();
}
}
}
double totalSalaryExpense() {
double total = 0;
for (Employee e : employees) {
total += e.salary;
}
return total;
}
public static void main(String[] args) {
EmployeeManagementSystem ems = new EmployeeManagementSystem();
ems.addEmployee(new Employee("Alice", "HR", 60000));
ems.addEmployee(new Employee("Bob", "IT", 75000));
ems.addEmployee(new Employee("Charlie", "HR", 62000));
ems.viewEmployees();
ems.filterByDepartment("HR");
System.out.println("Total Salary Expense: $" + ems.totalSalaryExpense());
}
}
Output:
Name | Department | Salary
Alice | HR | $60000.0
Bob | IT | $75000.0
Charlie | HR | $62000.0
Employees in Department: HR
Alice | HR | $60000.0
Charlie | HR | $62000.0
Total Salary Expense: $197000.0
import java.util.ArrayList;
import java.util.List;
class Product {
String name;
double price;
void addItem(Product p) {
items.add(p);
}
void removeItem(Product p) {
items.remove(p);
}
double calculateTotal() {
double total = 0;
for (Product p : items) {
total += p.price;
}
return total;
}
void displayItems() {
System.out.println("Cart items:");
for (Product p : items) {
System.out.println(p.name + " - $" + p.price);
}
}
}
class Customer {
String name;
Cart cart = new Cart();
Customer(String name) {
this.name = name;
}
}
public class ShoppingCartSystem {
public static void main(String[] args) {
Customer c = new Customer("John");
Product p1 = new Product("Laptop", 1200);
Product p2 = new Product("Headphones", 150);
c.cart.addItem(p1);
c.cart.addItem(p2);
c.cart.displayItems();
System.out.println("Total: $" + c.cart.calculateTotal());
c.cart.removeItem(p2);
c.cart.displayItems();
System.out.println("Total after removal: $" + c.cart.calculateTotal());
}
}
Output:
Cart items:
Laptop - $1200.0
Headphones - $150.0
Total: $1350.0
Cart items:
Laptop - $1200.0
Total after removal: $1200.0
import java.util.ArrayList;
import java.util.List;
class Student {
String name;
Student(String name) {
this.name = name;
}
}
class Teacher {
String name;
Teacher(String name) {
this.name = name;
}
}
class Classroom {
String name;
Teacher teacher;
List<Student> students = new ArrayList<>();
Classroom(String name) {
this.name = name;
}
void assignTeacher(Teacher t) {
this.teacher = t;
}
void enrollStudent(Student s) {
students.add(s);
}
void displayDetails() {
System.out.println("Classroom: " + name);
System.out.println("Teacher: " + (teacher != null ? teacher.name : "No teacher
assigned"));
System.out.println("Students enrolled:");
for (Student s : students) {
System.out.println("- " + s.name);
}
}
public static void main(String[] args) {
Classroom c1 = new Classroom("10th Grade");
Teacher t1 = new Teacher("Mr. Smith");
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
c1.assignTeacher(t1);
c1.enrollStudent(s1);
c1.enrollStudent(s2);
c1.displayDetails();
}
}
Output:
Classroom: 10th Grade
Teacher: Mr. Smith
Students enrolled:
- Alice
- Bob
import java.util.ArrayList;
import java.util.List;
class Patient {
String name;
int id;
Output:
Appointment assigned.
Appointment assigned.
Appointment:
Patient: John Doe (ID: 101)
Doctor: Dr. Adams (Cardiology)
Date: 2025-06-10
-----------------------
Appointment:
Patient: Jane Smith (ID: 102)
Doctor: Dr. Baker (Neurology)
Date: 2025-06-12
-----------------------
class ATM {
private String pin = "1234"; // default PIN
private double balance = 1000;
if (atm.login(inputPin)) {
System.out.println("Login successful!");
atm.checkBalance();
Output:
Enter PIN: 1234
Login successful!
Current Balance: $1000.0
Enter deposit amount: 500
$500.0 deposited successfully.
Current Balance: $1500.0
Enter withdrawal amount: 700
$700.0 withdrawn successfully.
Current Balance: $800.0