(OOPL4) Constructors & Static Members
🏗️

(OOPL4) Constructors & Static Members

 

Student Class Modified

import java.util.Scanner; class Student { String name; int[] marks = new int[5]; int total, average; public Student() { this.name = ""; this.total = this.average = 0; } public Student(String name, int total, int average) { this.name = name; this.total = total; this.average = average; } void assign() { Scanner scanner = new Scanner(System.in); System.out.println("Enter student name: "); this.name = scanner.nextLine(); System.out.println("Enter marks in 5 subjects: "); for (int i = 0; i < 5; i++) { this.marks[i] = scanner.nextInt(); } } void compute() { this.total = 0; for (int i = 0; i < this.marks.length; i++) { this.total += this.marks[i]; } this.average = this.total / this.marks.length; System.out.println("Total and average marks computed."); } void display() { System.out.println("Name: " + this.name); System.out.println("Total Marks: " + this.total); System.out.println("Average Marks: " + this.average); } } public class StudentInput { public static void main(String[] args) { System.out.println("--- MANUAL ASSIGNMENT ---"); Student student = new Student(); student.assign(); student.compute(); student.display(); System.out.println("--- DEFAULT CONSTRUCTOR ---"); Student studentConst = new Student(); studentConst.display(); System.out.println("--- PARAMETERIZED CONSTRUCTOR ---"); Student studentParam = new Student("Butternaan", 489, 92); studentParam.display(); } }

Employee Class Modified

import java.util.Scanner; class Employee { String name; String id; double basic = 0; double da = 0; double it = 0; double grossSalary = 0; double netSalary = 0; public Employee() { this.name = ""; this.id = "None"; this.basic = this.da = 0; } public Employee(String name, String id, double basic) { this.name = name; this.id = id; this.basic = basic; } void read() { Scanner scanner = new Scanner(System.in); System.out.println("Enter employee name: "); this.name = scanner.nextLine(); System.out.println("Enter employee ID: "); this.id = scanner.nextLine(); System.out.println("Enter basic salary: "); this.basic = scanner.nextInt(); } void compute() { this.da = 0.52 * this.basic; this.grossSalary = this.basic + this.da; this.it = 0.3 * this.grossSalary; this.netSalary = this.grossSalary + this.it; System.out.println("Gross Salary, Net Salary and IT computed."); } void display() { System.out.println("Name: " + this.name); System.out.println("ID: " + this.id); System.out.println("DA: Rs. " + this.da); System.out.println("IT: Rs. " + this.it); System.out.println("Gross Salary: Rs. " + this.grossSalary); System.out.println("Net Salary: Rs. " + this.netSalary); } } public class EmployeeInput { public static void main(String[] args) { System.out.println("--- MANUAL ASSIGNMENT ---"); Employee employee = new Employee(); employee.read(); employee.compute(); employee.display(); System.out.println("--- DEFAULT CONSTRUCTOR ---"); Employee employeeConst = new Employee(); employeeConst.display(); System.out.println("--- PARAMETERIZED CONSTRUCTOR ---"); Employee employeeParam = new Employee("Butternaan", "EM123456", 234000); employeeParam.compute(); employeeParam.display(); } }

Bank Account

import java.util.Scanner; class BankAccount { String name; String type; long number; long balance; static float rateOfInterest = 3; BankAccount() { this.name = "NIL"; this.type = "NIL"; this.number = 0; this.balance = 0; } BankAccount(String name, String type, long number, long balance) { this.name = name; this.type = type; this.number = number; this.balance = balance; } void deposit(long amount) { this.balance += amount; } boolean withdraw(long amount) { if (balance - amount <= 10000) { return false; } else { balance -= amount; return true; } } void display() { System.out.println(); System.out.println("Name: " + this.name); System.out.println("Account Type: " + this.type); System.out.println("Account Number: " + this.number); System.out.println("Account Balance: " + this.balance); System.out.println(); } static void rateOfInterest() { System.out.println("The current rate of interest is: " + rateOfInterest); } } public class AccountInput { public static void main(String[] args) { String name; String type; long number; long balance; Scanner scanner = new Scanner(System.in); String choice = null; System.out.println("Enter account holder name: "); name = scanner.nextLine(); System.out.println("Enter account type (Savings/FD/Joint): "); type = scanner.nextLine(); System.out.println("Enter account number: "); number = scanner.nextLong(); System.out.println("Enter balance amount: "); balance = scanner.nextLong(); BankAccount bankAccount = new BankAccount(name, type, number, balance); do { System.out.print("--- STACK OPERATIONS ---"); System.out.print("\n1. Deposit Money"); System.out.print("\n2. Withdraw Money"); System.out.print("\n3. Display Account Info"); System.out.print("\n4. Show Rate of Interest."); System.out.print("\n\nYour input: "); int input = scanner.nextInt(); switch (input) { case 1: System.out.println("Enter the amount to deposit: "); bankAccount.deposit(scanner.nextLong()); bankAccount.display(); break; case 2: System.out.println("Enter the amount to withdraw: "); if (!bankAccount.withdraw(scanner.nextLong())) { System.out.println("Amount exceeds minimum balance!"); } else { bankAccount.display(); } break; case 3: bankAccount.display(); break; case 4: BankAccount.rateOfInterest(); default: System.out.println("Invalid input."); } System.out.println("Do you want to continue? [y/N]"); choice = scanner.next(); } while (choice.equals("y")); scanner.close(); } }

Counter

class Counter { static int count = 0; Counter() { System.out.println("Counter object created."); count++; } static int showCount() { return count; } } public class CounterInput { public static void main(String[] args) { Counter counter1 = new Counter(); Counter counter2 = new Counter(); Counter counter3 = new Counter(); Counter counter4 = new Counter(); System.out.println("The number of objects is: " + Counter.showCount()); } }