Random
Random
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;
@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);
try{
System.out.print("First Name: ");
student.setFirstName(sc.next());
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 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 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;
}
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();
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;
}
@Override
public String toString() {
return "Rectangle [Length=" + length + ", Width=" + width + "]";
}
System.out.println(rectangle1);
System.out.println(rectangle2);
System.out.println(rectangle3);
Q4
package mypackage;
//3165 Honey Shah
//Div B
public class ASG3Q4 {
public static void main(String[] args) {
try {
Shape shape;
Q5
package mypackage;
//3165 Honey Shah
//Div B
public class ASG3Q5 implements Exam, Classify {
private int mark;
private int 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);
}
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;
}
@Override
public String toString() {
return "Account No: " + accountNo + ", Balance: " + balance;
}
}
@Override
public String toString() {
return super.toString() + ", Interest Rate: " + interestRate;
}
}
@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;
}
}
try {
System.out.print("Enter Account No: ");
String accountNo = sc.nextLine();
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);
}
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{
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;
}
@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);
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 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);
}
}
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;
}
}
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;
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;
}
@Override
public void hike_Salary(double percentage) {
if (percentage < 0) {
throw new IllegalArgumentException("Percentage cannot be negative");
}
salary += (salary * percentage) / 100;
}
}
@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];