Java Practical Assignment -3
Name: Heer Chokshi
Roll Number: 3012
Div: A
Faculty: Pritesh Sir
Q1
package mypackage;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException;
//3165 Honey Shah
//Div B
class Person{
private String firstName;
private String middleName;
private String lastName;
private String address;
private int age;
// public Person(String firstName,String middleName,String lastName,String address,int age) {
// this.firstName=firstName;
// this.lastName=lastName;
// this.middleName=middleName;
// this.address=address;
// this.age=age;
// }
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName(){
return middleName;
}
public void setMiddleName(String middleName){
this.middleName = middleName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
}
class Student extends Person implements Comparable<Student>{
private int rollNo;
private String division;
private String dateOfBirth;
// public Student(int rollNo, String division, String dateOfBirth) {
// this.rollNo=rollNo;
// this.division=division;
// this.dateOfBirth=dateOfBirth;
// }
public int getRollNo(){
return rollNo;
}
public void setRollNo(int rollNo){
this.rollNo = rollNo;
}
public String getDivision(){
return division;
}
public void setDivision(String division){
this.division = division;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth){
this.dateOfBirth = dateOfBirth;
}
@Override
public String toString() {
return "Student Roll No: " + rollNo + ", Division: " + division + ", Birth date: " + dateOfBirth;
}
@Override
public int compareTo(Student student) {
return Integer.compare(this.rollNo, student.rollNo);
}
}
class Employee extends Person {
private int empId;
private double da;
private double hra;
private double netSalary;
// public Employee(int empId, double da, double hra, double netSalary) {
// this.empId=empId;
// this.da=da;
// this.hra=hra;
// this.netSalary=netSalary;
// }
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public double getDa() {
return da;
}
public void setDa(double da) {
this.da = da;
}
public double getHra() {
return hra;
}
public void setHra(double hra) {
this.hra = hra;
}
public double getNetSalary() {
return netSalary;
}
public void setNetSalary(double netSalary) {
this.netSalary = netSalary;
}
@Override
public String toString() {
return "Employee ID: " + empId + ", DA: " + da + ", HRA: " + hra + ", Net Salary: " + netSalary;
}
}
public class ASG3Q1{
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
ArrayList<Employee> employees = new ArrayList<>();
System.out.println("Enter Student details:");
for (int i = 0; i < 5; i++) {
Student student = new Student();
try{
System.out.print("First Name: ");
student.setFirstName(sc.next());
System.out.print("Middle Name: ");
student.setMiddleName(sc.next());
System.out.print("Last Name: ");
student.setLastName(sc.next());
System.out.print("Address: ");
student.setAddress(sc.next());
System.out.print("Age: ");
student.setAge(sc.nextInt());
System.out.print("Roll No: ");
student.setRollNo(sc.nextInt());
System.out.print("Division: ");
student.setDivision(sc.next());
System.out.print("Date of Birth: ");
student.setDateOfBirth(sc.next());
students.add(student);
}catch(InputMismatchException e){
System.out.println("Invalid input entered. Enter again: ");
sc.nextLine();
i--;
}
}
System.out.println("Enter Employee details:");
for (int i = 0; i < 5; i++) {
Employee employee = new Employee();
try{
System.out.print("First Name: ");
employee.setFirstName(sc.next());
System.out.print("Middle Name: ");
employee.setMiddleName(sc.next());
System.out.print("Last Name: ");
employee.setLastName(sc.next());
System.out.print("Address: ");
employee.setAddress(sc.next());
System.out.print("Age: ");
employee.setAge(sc.nextInt());
System.out.print("Employee ID: ");
employee.setEmpId(sc.nextInt());
System.out.print("DA: ");
employee.setDa(sc.nextDouble());
System.out.print("HRA: ");
employee.setHra(sc.nextDouble());
System.out.print("Net Salary: ");
employee.setNetSalary(sc.nextDouble());
employees.add(employee);
}
catch(InputMismatchException e){
System.out.println("Invalid input entered. Enter again: ");
sc.nextLine();
i--;
}
}
Collections.sort(students);
Collections.sort(employees, (e1, e2) -> Double.compare(e1.getNetSalary(), e2.getNetSalary()));
System.out.println("Sorted Students");
for (Student student : students) {
System.out.println(student);
}
System.out.println("Sorted Employees:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
Q2
package mypackage2;
import java.util.InputMismatchException;
import java.util.Scanner;
//3165 Honey Shah
//Div B
class Person {
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public void display() {
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("Age: " + age);
}
}
class Employee extends Person {
private int employeeID;
private String designation;
private double salary;
public Employee(String firstName, String lastName, int age, int employeeID, String designation, double salary) {
super(firstName, lastName, age);
this.employeeID = employeeID;
this.designation = designation;
this.salary = salary;
}
public int getEmployeeID() {
return employeeID;
}
public String getDesignation() {
return designation;
}
public double getSalary() {
return salary;
}
public void display() {
super.display();
System.out.println("Employee ID: " + employeeID);
System.out.println("Designation: " + designation);
System.out.println("Salary: " + salary);
}
}
class Student extends Person {
private int rollNumber;
private String address;
private double percentage;
public Student(String firstName, String lastName, int age, int rollNumber, String address, double percentage) {
super(firstName, lastName, age);
this.rollNumber = rollNumber;
this.address = address;
this.percentage = percentage;
}
public int getRollNumber() {
return rollNumber;
}
public String getAddress() {
return address;
}
public double getPercentage() {
return percentage;
}
public void display() {
super.display();
System.out.println("Roll Number: " + rollNumber);
System.out.println("Address: " + address);
System.out.println("Percentage: " + percentage);
}
}
public class ASG3Q2{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter Employee details:");
System.out.print("First Name: ");
String empFirstName = sc.next();
System.out.print("Last Name: ");
String empLastName = sc.next();
System.out.print("Age: ");
int empAge = sc.nextInt();
System.out.print("Employee ID: ");
int empID = sc.nextInt();
System.out.print("Designation: ");
String empDesignation = sc.next();
System.out.print("Salary: ");
double empSalary = sc.nextDouble();
Employee employee = new Employee(empFirstName, empLastName, empAge, empID, empDesignation,
empSalary);
System.out.println("\nEnter Student details:");
System.out.print("First Name: ");
String stuFirstName = sc.next();
System.out.print("Last Name: ");
String stuLastName = sc.next();
System.out.print("Age: ");
int stuAge = sc.nextInt();
System.out.print("Roll Number: ");
int stuRollNumber = sc.nextInt();
System.out.print("Address: ");
String stuAddress = sc.next();
System.out.print("Percentage: ");
double stuPercentage = sc.nextDouble();
Student student = new Student(stuFirstName, stuLastName, stuAge, stuRollNumber, stuAddress, stuPercentage);
System.out.println("\nEmployee Details:");
employee.display();
System.out.println("\nStudent Details:");
student.display();
} catch (InputMismatchException e) {
System.err.println("Invalid input. Please enter the correct data type.");
}
}
}
Q3
package mypackage3;
//3165 Honey Shah
//Div B
class Rectangle{
private double length;
private double width;
private static int numberOfObjects = 0;
static {
System.out.println("Static initializer block: It runs once before the constructor or any other method.");
}
{
numberOfObjects++;
System.out.println("Initializer block: It runs every time an object is created.");
}
public Rectangle() {
this.length = 0;
this.width = 0;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public Rectangle(Rectangle rect) {
this.length = rect.length;
this.width = rect.width;
}
public double getArea() {
return length * width;
}
public static int getNumberOfObjects() {
return numberOfObjects;
}
@Override
public String toString() {
return "Rectangle [Length=" + length + ", Width=" + width + "]";
}
public static void displayNumberOfObjects() {
System.out.println("Number of Rectangle objects created: " + numberOfObjects);
}
}
public class ASG3Q3{
public static void main(String[] args) {
try {
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle(5, 10);
Rectangle rectangle3 = new Rectangle(rectangle2); // Copy constructor
System.out.println(rectangle1);
System.out.println(rectangle2);
System.out.println(rectangle3);
System.out.println("Area of rectangle2: " + rectangle2.getArea());
Rectangle.displayNumberOfObjects();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Q4
package mypackage;
//3165 Honey Shah
//Div B
public class ASG3Q4 {
public static void main(String[] args) {
try {
Shape shape;
shape = new Triangle(4.0, 6.0);
System.out.println(shape);
System.out.println("Area of Triangle: " + shape.area());
shape = new Rectangle(5.0, 7.0);
System.out.println(shape);
System.out.println("Area of Rectangle: " + shape.area());
shape = new Circle(3.0);
System.out.println(shape);
System.out.println("Area of Circle: " + shape.area());
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q5
package mypackage;
//3165 Honey Shah
//Div B
public class ASG3Q5 implements Exam, Classify {
private int mark;
private int average;
public ASG3Q5(int mark, int average) {
this.mark = mark;
this.average = average;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public int getAverage() {
return average;
}
public void setAverage(int average) {
this.average = average;
}
@Override
public boolean Pass(int mark) {
return mark >= 50;
}
@Override
public String Division(int average) {
if (average >= 60) {
return "First";
} else if (average >= 50) {
return "Second";
} else {
return "No division";
}
}
@Override
public String toString() {
return "Mark: " + mark + ", Average: " + average + ", Pass: " + Pass(mark) + ", Division: " + Division(average);
}
public static void main(String[] args) {
try {
ASG3Q5 result = new ASG3Q5(75, 65);
System.out.println(result);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q6
import java.util.Scanner;
//3165 Honey Shah
//Div B
abstract class Account {
protected String accountNo;
protected double balance;
public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}
public double checkBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}
@Override
public String toString() {
return "Account No: " + accountNo + ", Balance: " + balance;
}
}
class Savings extends Account {
private double interestRate;
public Savings(String accountNo, double balance, double interestRate) {
super(accountNo, balance);
this.interestRate = interestRate;
}
public void calculateInterest() {
double interest = balance * interestRate / 100;
deposit(interest);
}
@Override
public String toString() {
return super.toString() + ", Interest Rate: " + interestRate;
}
}
class Current extends Account {
private double overdraftLimit;
public Current(String accountNo, double balance, double overdraftLimit) {
super(accountNo, balance);
this.overdraftLimit = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
} else {
System.out.println("Transaction not allowed. Overdraft limit exceeded.");
}
}
@Override
public String toString() {
return super.toString() + ", Overdraft Limit: " + overdraftLimit;
}
}
public class ASG3Q6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter Account No: ");
String accountNo = sc.nextLine();
System.out.print("Enter Initial Balance: ");
double initialBalance = sc.nextDouble();
System.out.print("Enter Interest Rate (for Savings) or Overdraft Limit (for Current): ");
double rateOrLimit = sc.nextDouble();
Account account;
if (rateOrLimit >= 0) {
account = new Savings(accountNo, initialBalance, rateOrLimit);
} else {
account = new Current(accountNo, initialBalance, -rateOrLimit);
}
System.out.println("Account created successfully.");
System.out.println(account);
System.out.print("Enter amount to deposit: ");
double depositAmount = sc.nextDouble();
account.deposit(depositAmount);
System.out.println("Updated Account: " + account);
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = sc.nextDouble();
account.withdraw(withdrawAmount);
System.out.println("Updated Account: " + account);
if (account instanceof Savings) {
Savings savingsAccount = (Savings) account;
System.out.println("Calculating interest...");
savingsAccount.calculateInterest();
System.out.println("Updated Account: " + savingsAccount);
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q7
package shape;
import geometery.Figure;
import geometery.Circle1;
import java.util.Scanner;
//3012 Heer Chokshi
//Div 4
public class ASG3Q7{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
try{
System.out.println("Enter the number of objects: ");
int num=sc.nextInt();
for(int i=0;i<num;i++){
Figure obj1;
System.out.println("Type C for Circle and R for Rectangle: ");
char ans = sc.next().charAt(0);
if(ans == 'C'|| ans == 'c'){
System.out.println("Enter the radius of circle: ");
double radius = sc.nextDouble();
obj1 = new Circle1(radius);
System.out.println("Area of object: " + ((Circle1) obj1).area());
System.out.println("Perimeter of object: " + ((Circle1) obj1).perimeter());
}else if(ans == 'R'|| ans == 'r'){
System.out.println("Enter length and height of rectangle: ");
double length = sc.nextDouble();
double height = sc.nextDouble();
obj1 = new Rectangle1(length,height);
System.out.println("Area of object: " + ((Rectangle1) obj1).area());
System.out.println("Perimeter of object: " + ((Rectangle1) obj1).perimeter());
}else{
throw new Exception("Invalid input");
}
}
}catch(Exception e){
System.err.println("Input Invalid");
}
}
}
Q8
package mca;
//3165 Honey Shah
//Div B
import java.util.InputMismatchException;
public class ASG3Q8{
public static void main(String[] args){
try{
double[] marks={50,50,50,50,50};
Subject sub = new Subject(001,"Heer","Ahmedabad",marks,205,"Java",false);
System.out.println("ID: "+sub.getStudentId());
System.out.println("Name : "+sub.getStudentName());
System.out.println("Address : "+sub.getAddress());
sub.calculateGrade();
System.out.println("Grade : "+sub.getGrade());
System.out.println();
System.out.println("ID: "+sub.getSubjectId());
System.out.println("Subject Name :"+sub.getSubjectName());
System.out.println("Is the Subject Elective: "+sub.isElective());
}catch(InputMismatchException e){
System.err.println("Input Mismatch Exception: " + e.getMessage());
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q9
package mypackage;
//3165 Honey Shah
//Div B
import java.util.Scanner;
class Supplier {
private int sup_id;
private String sup_name;
private String address;
private String[] product_name = new String[3];
private double[] price_of_product = new double[3];
private double total_price;
public Supplier(int sup_id, String sup_name, String address, String[] product_name, double[] price_of_product) {
this.sup_id = sup_id;
this.sup_name = sup_name;
this.address = address;
this.product_name = product_name;
this.price_of_product = price_of_product;
}
public void calculate_total_price() {
total_price = 0;
for (double price : price_of_product) {
total_price += price;
}
}
@Override
public String toString() {
return "Supplier ID: " + sup_id +
", Supplier Name: " + sup_name +
", Address: " + address +
", Total Price of Products: " + total_price;
}
}
public class ASG3Q9{
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Supplier ID: ");
int sup_id = scanner.nextInt();
System.out.print("Enter Supplier Name: ");
scanner.nextLine(); // Consume newline character after reading int
String sup_name = scanner.nextLine();
System.out.print("Enter Address: ");
String address = scanner.nextLine();
String[] product_name = new String[3];
double[] price_of_product = new double[3];
for (int i = 0; i < 3; i++) {
System.out.print("Enter Product " + (i + 1) + " name: ");
product_name[i] = scanner.nextLine();
System.out.print("Enter Price of Product " + (i + 1) + ": ");
price_of_product[i] = scanner.nextDouble();
}
Supplier supplier = new Supplier(sup_id, sup_name, address, product_name, price_of_product);
supplier.calculate_total_price();
System.out.println(supplier);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q10
import java.util.Scanner;
//3165 Honey Shah
//Div B
abstract class Amazon_item {
protected int item_id;
protected String product_type;
protected String item_name;
protected int item_display_price;
protected int item_net_price;
public Amazon_item(int item_id, String product_type, String item_name, int item_display_price) {
this.item_id = item_id;
this.product_type = product_type;
this.item_name = item_name;
this.item_display_price = item_display_price;
}
public abstract void calculate_net_price();
public abstract void display_price();
class cloth_item extends Amazon_item {
private String texture_type;
public cloth_item(int item_id, String product_type, String item_name, int item_display_price, String texture_type) {
super(item_id, product_type, item_name, item_display_price);
this.texture_type = texture_type;
}
@Override
public void calculate_net_price() {
if (item_display_price > 5000) {
item_net_price = (int) (item_display_price * 0.85);
} else if (item_display_price > 4000) {
item_net_price = (int) (item_display_price * 0.90);
} else if (item_display_price > 3000) {
item_net_price = (int) (item_display_price * 0.95);
} else {
item_net_price = item_display_price;
}
}
@Override
public void display_price() {
System.out.println("Item ID: " + item_id);
System.out.println("Product Type: " + product_type);
System.out.println("Item Name: " + item_name);
System.out.println("Texture Type: " + texture_type);
System.out.println("Item Display Price: " + item_display_price);
System.out.println("Item Net Price: " + item_net_price);
}
}
public class ASG3Q10 {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
cloth_item item1, item2;
System.out.println("Enter details for the first cloth item:");
System.out.print("Item ID: ");
int item_id1 = scanner.nextInt();
System.out.print("Product Type: ");
String product_type1 = scanner.next();
System.out.print("Item Name: ");
scanner.nextLine();
String item_name1 = scanner.nextLine();
System.out.print("Item Display Price: ");
int item_display_price1 = scanner.nextInt();
System.out.print("Texture Type: ");
String texture_type1 = scanner.next();
item1 = new cloth_item(item_id1, product_type1, item_name1, item_display_price1, texture_type1);
item1.calculate_net_price();
item1.display_price();
System.out.println("\nEnter details for the second cloth item:");
System.out.print("Item ID: ");
int item_id2 = scanner.nextInt();
System.out.print("Product Type: ");
String product_type2 = scanner.next();
System.out.print("Item Name: ");
scanner.nextLine();
String item_name2 = scanner.nextLine();
System.out.print("Item Display Price: ");
int item_display_price2 = scanner.nextInt();
System.out.print("Texture Type: ");
String texture_type2 = scanner.next();
item2 = new cloth_item(item_id2, product_type2, item_name2, item_display_price2, texture_type2);
item2.calculate_net_price();
item2.display_price();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q11
import java.util.Scanner;
//3165 Honey Shah
//Div B
class Bank_account{
private int account_id;
private String mobile_number;
private String account_holder_name;
private String account_type;
private double account_balance;
private double credit_limit;
public Bank_account(int account_id,String mobile_number,String account_holder_name,String account_type, double
account_balance, double credit_limit){
this.account_id = account_id;
this.mobile_number = mobile_number;
this.account_holder_name = account_holder_name;
this.account_type = account_type;
this.account_balance = account_balance;
this.credit_limit = credit_limit;
}
public Bank_account(int account_id,String mobile_number,String account_holder_name){
this.account_id = account_id;
this.mobile_number = mobile_number;
this.account_holder_name = account_holder_name;
this.account_type = "Savings Account";
this.account_balance = 0.0;
this.credit_limit = 0.0;
}
public Bank_account(Bank_account b){
this.account_id = b.account_id;
this.mobile_number = b.mobile_number;
this.account_holder_name = b.account_holder_name;
this.account_type = b.account_type;
this.account_balance = b.account_balance;
this.credit_limit = b.credit_limit;
}
public void update_account(double amount){
account_balance+=amount;
}
public double getBalance(){
return account_balance;
}
@Override
public String toString() {
return "Account ID: " + account_id +
", Mobile Number: " + mobile_number +
", Account Holder Name: " + account_holder_name +
", Account Type: " + account_type +
", Account Balance: " + account_balance +
", Credit Limit: " + credit_limit;
}
}
class ASG3Q11{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
try{
System.out.println("Enter details for Bank Account ");
System.out.print("Account ID: ");
int account_id=sc.nextInt();
System.out.print("Mobile Number: ");
String mobile_number = sc.nextLine();
System.out.print("Account Holder Name: ");
String account_holder_name = sc.nextLine();
System.out.print("Account Type: ");
String account_type = sc.nextLine();
System.out.print("Account Balance: ");
double account_balance = sc.nextDouble();
System.out.print("Credit Limit: ");
double credit_limit = sc.nextDouble();
Bank_account ba1 = new Bank_account(account_id, mobile_number, account_holder_name, account_type,
account_balance, credit_limit);
ba1.update_account(3000);
ba1.toString();
Bank_account ba2= new Bank_account(ba1);
System.out.println("Balance of ba2: "+ba2.getBalance());
}catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q12
import java.util.Scanner;
//3165 Honey Shah
//Div B
class Bill{
private int bill_id;
private double[] item_price;
private int total_number_of_items;
private double total_amount;
public Bill(int bill_id, double[] item_price, int total_number_of_items) {
this.bill_id = bill_id;
this.item_price = item_price;
this.total_number_of_items = total_number_of_items;
calculate_total_amount();
}
public void calculate_total_amount() {
total_amount = 0;
for (double price : item_price) {
total_amount += price;
}
}
public double getTotalAmount() {
return total_amount;
}
}
class ASG3Q12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter Bill ID: ");
int bill_id = scanner.nextInt();
System.out.print("Enter Total Number of Items: ");
int total_number_of_items = scanner.nextInt();
double[] item_price = new double[total_number_of_items];
for (int i = 0; i < total_number_of_items; i++) {
System.out.print("Enter price for item " + (i + 1) + ": ");
item_price[i] = scanner.nextDouble();
}
Bill bill = new Bill(bill_id, item_price, total_number_of_items);
System.out.println("Total Amount: " + bill.getTotalAmount());
if (bill.getTotalAmount() < 0) {
throw new Exception("Total amount cannot be negative.");
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Q13
package gtu;
//3165 Honey Shah
//Div B
class Supplier{
private int sup_id;
private String sup_name;
private String address;
private String[] product_name = new String[3];
private double[] price_of_product = new double[3];
private double total_price;
public Supplier(int sup_id, String sup_name, String Address, String[] product_name, double[] price_of_product, double
total_price){
this.sup_id = sup_id;
this.sup_name = sup_name;
this.address = address;
this.product_name = product_name;
this.price_of_product = price_of_product;
}
public void calculate_total_price(){
total_price=0;
for(double price : price_of_product[]){
total_price+=price;
}
System.out.println("Total Price: " + total_price);
}
}
class Book_supplier extends Supplier{
private int[] discount;
public book_supplier(int sup_id, String sup_name, String address, String[] product_name, double[] price_of_product, int[]
discount) {
super(sup_id, sup_name, address, product_name, price_of_product);
this.discount = discount;
}
@Override
public void calculate_total_price() {
total_price = 0;
for (int i = 0; i < price_of_product.length; i++) {
double newPrice = price_of_product[i] - (price_of_product[i] * discount[i] / 100.0);
total_price += newPrice;
}
System.out.println("Total Price after Discounts: " + total_price);
}
}
Q14
import java.util.Scanner;
//3165 Honey Shah
//Div B
abstract class Person {
protected String name;
protected double salary;
public Person(String name, double salary) {
this.name = name;
this.salary = salary;
}
public abstract void hike_Salary(double percentage);
public void display_data() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
class Employee extends Person {
public Employee(String name, double salary) {
super(name, salary);
}
@Override
public void hike_Salary(double percentage) {
if (percentage < 0) {
throw new IllegalArgumentException("Percentage cannot be negative");
}
salary += (salary * percentage) / 100;
}
}
class Manager extends Employee {
public Manager(String name, double salary) {
super(name, salary);
}
@Override
public void hike_Salary(double percentage) {
if (percentage < 0) {
throw new IllegalArgumentException("Percentage cannot be negative");
}
salary += (salary * percentage) / 100 + 5000;
}
}
class ASG3Q14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
Person[] people = new Person[2];
// Creating an Employee and a Manager object
Employee emp = new Employee("Heer", 50000);
Manager manager = new Manager("Mittal", 80000);
people[0] = emp;
people[1] = manager;
for (Person person : people) {
System.out.println("Before hike:");
person.display_data();
System.out.print("Enter percentage increase for " + person.name + ": ");
double percentage = scanner.nextDouble();
person.hike_Salary(percentage);
System.out.println("After hike:");
person.display_data();
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}