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

Value Added File

The document outlines various Java programming exercises and real-world problem-based coding questions. It covers topics such as class creation, method overloading, inheritance, encapsulation, polymorphism, and the implementation of systems like Employee Management, Online Shopping Cart, and School Management. Each section includes code examples and expected outputs to demonstrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Value Added File

The document outlines various Java programming exercises and real-world problem-based coding questions. It covers topics such as class creation, method overloading, inheritance, encapsulation, polymorphism, and the implementation of systems like Employee Management, Online Shopping Cart, and School Management. Each section includes code examples and expected outputs to demonstrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Department- School Of Technology

Name of subject - Value Added Program VI(We-Code VI)

Submitted By – Submitted To –

Radhika Verma Mr. Abhishek Sharma

BTech (Data Science)

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;

Student(String name, int rollNo, double marks) {


this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
}
public static void main(String[] args) {
Student s = new Student("Amit", 101, 89.5);
s.displayDetails();
}
}

Output:
Name: Amit
Roll No: 101
Marks: 89.5

2. Demonstrate Method Overloading with a class Calculator.

class Calculator {

int add(int a, int b) {


return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum of 2 ints: " + calc.add(5, 10));
System.out.println("Sum of 2 doubles: " + calc.add(5.5, 3.2));
System.out.println("Sum of 3 ints: " + calc.add(1, 2, 3));
}
}
Output:
Sum of 2 ints: 15
Sum of 2 doubles: 8.7
Sum of 3 ints: 6

3. Create a class BankAccount with methods for deposit(), withdraw(), and


checkBalance().

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");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.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

6. Write a Java program to demonstrate abstraction using abstract class Shape.

abstract class Shape {


abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing Rectangle");
}

public static void main(String[] args) {


Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.draw();
s2.draw();
}
}

Output:
Drawing Circle
Drawing Rectangle

7. Demonstrate this and super keyword usage in a Java class hierarchy.

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);

System.out.println("Book Title: " + b.getTitle());


System.out.println("Author: " + b.getAuthor());
System.out.println("Price: " + b.getPrice());
}
}

Output:
Book Title: Java Programming
Author: James Gosling
Price: 450.75

10. Create a class Rectangle with overloaded constructors.

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;

Person(String name, int age) {

this.name = name;

this.age = age;

void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

public static void main(String[] args) {


Person p = new Person("Alice", 25);

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;

Book(String title, String author) {


this.title = title;
this.author = author;
}
void display() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
}
}
class Library {
Book book;

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();

System.out.println("Number of Counter objects created: " + Counter.count);


}
}

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();

System.out.println("Number of Movie objects created: " +


Movie.getMovieCount());
}
}
Output:
Number of Movie objects created: 3

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();

s1.draw(); // Calls Circle's draw()


s2.draw(); // Calls Square's draw()
s3.draw(); // Calls Shape's draw()
}
}

Output:
Drawing Circle
Drawing Square
Drawing Shape

Real-World Problem-Based Java Coding Questions (5 Questions)

16. Employee Management System


Build a system where you can add employees, view them, filter by department, and
calculate total salary expense.

import java.util.ArrayList;
import java.util.List;

class Employee {
String name;
String department;
double salary;

Employee(String name, String department, double salary) {


this.name = name;
this.department = department;
this.salary = salary;
}
void display() {
System.out.println(name + " | " + department + " | $" + salary);
}
}
class EmployeeManagementSystem {
List<Employee> employees = new ArrayList<>();

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

17. Online Shopping Cart System


Design classes for Product, Cart, and Customer. Allow adding/removing items and
calculating total.

import java.util.ArrayList;
import java.util.List;

class Product {
String name;
double price;

Product(String name, double price) {


this.name = name;
this.price = price;
}
}
class Cart {
List<Product> items = new ArrayList<>();

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

18. School Management System


Create classes for Student, Teacher, and Classroom. Implement features like assign
teacher, enroll student.

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

19. Hospital Management System


Model entities like Patient, Doctor, Appointment, with functionality to assign and track
appointments.

import java.util.ArrayList;
import java.util.List;

class Patient {
String name;
int id;

Patient(int id, String name) {


this.id = id;
this.name = name;
}
}
class Doctor {
String name;
String specialty;

Doctor(String name, String specialty) {


this.name = name;
this.specialty = specialty;
}
}
class Appointment {
Patient patient;
Doctor doctor;
String date;

Appointment(Patient patient, Doctor doctor, String date) {


this.patient = patient;
this.doctor = doctor;
this.date = date;
}
void display() {
System.out.println("Appointment:");
System.out.println("Patient: " + patient.name + " (ID: " + patient.id + ")");
System.out.println("Doctor: " + doctor.name + " (" + doctor.specialty + ")");
System.out.println("Date: " + date);
System.out.println("-----------------------");
}
}
class HospitalManagementSystem {
List<Appointment> appointments = new ArrayList<>();

void assignAppointment(Patient p, Doctor d, String date) {


Appointment appt = new Appointment(p, d, date);
appointments.add(appt);
System.out.println("Appointment assigned.");
}
void showAppointments() {
if (appointments.isEmpty()) {
System.out.println("No appointments.");
} else {
for (Appointment a : appointments) {
a.display();
}
}
}
public static void main(String[] args) {
HospitalManagementSystem hms = new HospitalManagementSystem();

Patient p1 = new Patient(101, "John Doe");


Patient p2 = new Patient(102, "Jane Smith");
Doctor d1 = new Doctor("Dr. Adams", "Cardiology");
Doctor d2 = new Doctor("Dr. Baker", "Neurology");

hms.assignAppointment(p1, d1, "2025-06-10");


hms.assignAppointment(p2, d2, "2025-06-12");
hms.showAppointments();
}
}

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

20. Simple ATM Simulation


Simulate ATM functionalities like login, withdraw, deposit, and check balance using
class-based structure.
import java.util.Scanner;

class ATM {
private String pin = "1234"; // default PIN
private double balance = 1000;

boolean login(String enteredPin) {


return enteredPin.equals(pin);
}
void checkBalance() {
System.out.println("Current Balance: $" + balance);
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("$" + amount + " deposited successfully.");
} else {
System.out.println("Invalid deposit amount.");
}
}
void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient balance.");
} else if (amount <= 0) {
System.out.println("Invalid withdrawal amount.");
} else {
balance -= amount;
System.out.println("$" + amount + " withdrawn successfully.");
}
}
public static void main(String[] args) {
ATM atm = new ATM();
Scanner sc = new Scanner(System.in);

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


String inputPin = sc.nextLine();

if (atm.login(inputPin)) {
System.out.println("Login successful!");

atm.checkBalance();

System.out.print("Enter deposit amount: ");


double depositAmt = sc.nextDouble();
atm.deposit(depositAmt);
atm.checkBalance();

System.out.print("Enter withdrawal amount: ");


double withdrawAmt = sc.nextDouble();
atm.withdraw(withdrawAmt);
atm.checkBalance();
} else {
System.out.println("Invalid PIN. Access denied.");
}
sc.close();
}
}

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

You might also like