JAVA MODEL EXAM PROGRAMS 1 Data Processing System for a Small Business Task: The business needs a program that processes daily sales data using various looping structures and stores them in arrays for analysis. Question: Design a program that reads daily sales amounts for a week using appropriate looping structures and stores them in an array. Calculate the total and average sales, and identify the day with the highest sales. PROGRAM import java.util.Scanner; public class SalesProcessor { public stati c void main(String[] args) { double[] sales = new double[7]; Scanner sc = new Scanner(System.in); // Using for loop to read sales for (int i = 0; i < 7; i++) { System.out.print("Enter sales for day " + (i + 1) + ": "); sales[i] = sc.nextDouble(); } // Using while loop to calculate total and find highest int i = 0; double total = 0; int highestDay = 0; while (i < 7) { total += sales[i]; if (sales[i] > sales[highestDay]) { highestDay = i; } i++; } double average = total / 7; System.out.println("Total Sales: " + total); System.out.println("Average Sales: " + average); System.out.println("Highest Sales on Day " + (highestDay + 1)); } } 2 Educational Portal Task: The portal needs to manage users like Students, Teachers, and Admins. Question: Write a program to demonstrate inheritance where the base class is User, and derived classes are Student, Teacher, and Admin, each with specific attributes and methods. PROGRAM class User { String name; void login() { System.out.println(name + " logged in"); } } class Student extends User { void study() { System.out.println(name + " is studying"); } } class Teacher extends User { void teach() { System.out.println(name + " is teaching"); } } class Admin extends User { void manage() { System.out.println(name + " is managing"); } } public class Portal { public static void main(String[] args) { Student s = new Student(); s.name = "John"; s.login(); s.study(); Teacher t = new Teacher(); t.name = "Mr. Smith"; t.login(); t.teach(); Admin a = new Admin(); a.name = "Admin"; a.login(); a.manage(); } } 3 Online Shopping Cart 1010 Task: The shopping cart system needs to handle different types of item prices and discounts. Question: Create a program that illustrates the concept of method overloading by defining multiple calculateTotal () methods that accept different sets of parameters (e.g., price only, price with discount, price with qua ntity PROGRAM public class ShoppingCart { void calculateTotal(double price) { System.out.println("Total: " + price); } void calculateTotal(double price, double discount) { System.out.println("Total after discount: " + (price - discount)); } void calculateTotal(double price, int quantity) { System.out.println("Total for quantity: " + (price * quantity)); } public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.calculateTotal(100); cart.calculateTotal(100, 10); cart.calculateTotal(100, 3); } } 4 METHOD OVERRIDING Vehicle management system Use class name as vehicle Use method name as start_engine Use derived class name as car,bike,truck PROGRAM class Vehicle { void start_engine() { System.out.println("Vehicle engine started"); } } class Car extends Vehicle { void start_engine() { System.out.println("Car engine started"); } } class Bike extends Vehicle { void start_engine() { System.out.println("Bike engine started"); } } class Truck extends Vehicle { void start_engine() { System.out.println("Truck engine started"); } } publ ic class Main { public static void main(String[] args) { Vehicle v = new Vehicle(); Car c = new Car(); Bike b = new Bike(); Truck t = new Truck(); v.start_engine(); c.start_engine(); b.start_ engine(); t.start_engine(); } } 5 file download using multi threading printing downloading progress simultaneously PROGRAM class DownloadThread extends Thread { String file; DownloadThread(String file) { this.file = file; } public void run() { for (int i = 1; i <= 5; i++) { System.out.println(file + " downloading... " + (i * 20) + "%"); try { Thread.sleep(500); } catch (InterruptedException e) {} } System.out.println(file + " download complete."); } public static void main(String[] args) { DownloadThread file1 = new DownloadThread("File1"); DownloadThread file2 = new DownloadThread("File2"); file1.st art(); file2.start(); } } 6 ATM system to handle gentle Exceptions Components - To create an ATM system application using exception handling in cases like insufficient balance, invalid input and server down PROGRAM import java.util.*; class InsufficientBalanceException extends Exception { InsufficientBalanceException(String msg) { super(msg); } } public class ATM { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double balance = 1000; int choice; while (true) { try { System.out.println(" \ n1.Check 2.Deposit 3.Withdraw 4.Exit"); choice = sc.nextInt(); if (Math.random () < 0.05) throw new Exception("Server down. Try later."); if (choice == 1) { System.out.println("Balance: ₹" + balance); } else if (choice == 2) { System.out.print("Amount: "); double amt = sc.nextDouble(); if (amt <= 0) throw new IllegalArgumentException("Must be > 0"); balance += amt; System.out.println("Deposited: ₹" + amt); } else if (c hoice == 3) { System.out.print("Amount: "); double amt = sc.nextDouble(); if (amt <= 0) throw new IllegalArgumentException("Must be > 0"); if (amt > balance) throw new Insuffic ientBalanceException("Not enough balance"); balance - = amt; System.out.println("Withdrawn: ₹" + amt); } else if (choice == 4) { System.out.println("Thank you!"); break; } else { System.out.println("Invalid choice."); } } catch (InputMismatchException e) { System.out.println("Enter numbers only!"); sc.nextLine(); // cle ar input } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } } } 7 Library management system by using package Each package Should have components such as books,member,loans PROGRAM BOOK.JAVA package library; public class Book { public String title = "Java Basics"; } MEMBER.JAVA package library; public class Member { public String name = "Alice"; } LOAN.JAVA package library; public class Loan { public void issue() { System.out.println("Book issued to member."); } } MAIN.JAVA import library.*; public class Main { public static void main(String[] args) { Book b = new Book (); Member m = new Member(); Loan l = new Loan(); System.out.println("Book: " + b.title); System.out.println("Member: " + m.name); l.issue(); } } 8 Multiple inheritance using interface To turn on or off and control smart devices PROGRAM interface Light { void turnOn(); void turnOff(); } interface Fan { void start(); void stop(); } class SmartDevice implements Light, Fan { public void turnOn() { System.out.println("Light O N"); } public void turnOff() { System.out.println("Light OFF"); } public void start() { System.out.println("Fan started"); } public void stop() { System.out.println("Fan stopped"); } public static void main(String[] args) { SmartDevice device = new SmartDevice(); device.turnOn(); device.start(); device.stop(); device.turnOff(); } }