Bruhhhhh

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

JAVA ASSINGMENT PROGRAMS

Q2. Design a class for student entity and consider relevent abstract data. Accept and display the
data for 5 objects using array ofobjects.

import java.util.Scanner;

class Student

String name;

int age;

int rollno;

String bloodgroup;

Student(String n,int a,int r,String bg)

name=n;

age=a;

rollno=r;

bloodgroup=bg;

void display()

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

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

System.out.println("roll no:"+rollno);

System.out.println("blood group:"+bloodgroup);

public static void main(String[] args)

{
Scanner sc = new Scanner(System.in);

Student[] s= new Student[5];

for(int i=0;i<5;i++)

System.out.println("Enter name:");

String name=sc.nextLine();

System.out.println("Enter age:");

int age=sc.nextInt();

System.out.println("Enter roll no:");

int rollno = sc.nextInt();

sc.nextLine();

System.out.println("Enter blood group:");

String bloodgroup=sc.nextLine();

s[i]= new Student(name, age, rollno, bloodgroup);

for(int i=0;i<5;i++)

s[i].display();

}
Q3.Design a class 'Complex 'with data members for real and imaginary part. Provide detault and
Parameterized constructors. Write a program to perform arithmetic operations of two complex
numbers

// Design a class 'Complex 'with data members for real and imaginary part. Provide

// detault and Parameterized constructors. Write a program to perform arithmetic

// operations of two complex numbers

import java.util.*;

class Complex40 {

double real;

double imaginary;

Complex40()

real = 0;

imaginary = 0;

Complex40(double real,double imaginary)

this.real = real;

this.imaginary = imaginary;

Complex40 add(Complex40 other)

return new Complex40(this.real + other.real, this.imaginary + other.imaginary);

Complex40 sub(Complex40 other)

{
return new Complex40(this.real - other.real, this.imaginary - other.imaginary);

void display()

if (imaginary > 0) {

System.out.println(real + "+" + (imaginary) + "i");

} else {

System.out.println(real + "-" + (-imaginary) + "i");

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter the real part of 1st imaginary number:");

double real1 = sc.nextDouble();

System.out.println("enter the Imaginary part of 1st imaginary number:");

double imaginary1 = sc.nextDouble();

Complex40 c1 = new Complex40(real1, imaginary1);

System.out.println("enter the real part of 2nd imaginary number:");

double real2 = sc.nextDouble();

System.out.println("enter the Imaginary part of im2ndaginary number:");

double imaginary2 = sc.nextDouble();

Complex40 c2 = new Complex40(real2, imaginary2);

Complex40 sum = c1.add(c2);

Complex40 difference = c1.sub(c2);


System.out.println("the sum is:");

sum.display();

System.out.println("the difference is:");

difference.display();

Q5. Design and develop inheritance for a given case study, identiff objects and relationships and
implement inheritance wherever applicable.

Employee class with Emp_name, Emp_id, Address, Mail-id, and Mobile-no as members. Inherit the
classes, Programmer ,Assistant Professor, Associate Professor and Professor from employee class
Add Basic Pay (BP) AS the member of all the classes with 97% of BP AS DA, l0 % of Bp as HRA, 12% of
BP as PF,0.1 percent of BP for staff club fund.

Generate pay slips for the employees with their gross and net salary

import java.util.Scanner;

class Employee {

String emp_name;

int emp_id;

String address;

String mail_id;

String mobile_no;

float BP;

void getEmployeeDetails() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter name of employee:");

emp_name = sc.nextLine();

System.out.println("Enter employee id:");


emp_id = sc.nextInt();

sc.nextLine(); // Consume newline

System.out.println("Enter employee address:");

address = sc.nextLine();

System.out.println("Enter employee e-mail:");

mail_id = sc.nextLine();

System.out.println("Enter employee mobile-no:");

mobile_no = sc.nextLine();

void generatePaymentSlip() {

float DA, HRA, PF, SCF, gross_salary, net_Salary;

DA = 0.97f * BP;

HRA = 0.1f * BP;

PF = 0.12f * BP;

SCF = 0.001f * BP;

gross_salary = BP + DA + HRA;

net_Salary = gross_salary - PF - SCF;

System.out.println("\n++++++Employee details++++++");

System.out.println("Name of employee: " + emp_name);

System.out.println("ID of employee: " + emp_id);

System.out.println("Address of employee: " + address);

System.out.println("Mail-id of employee: " + mail_id);

System.out.println("Mobile-no of employee: " + mobile_no);

System.out.println("+++++++Pay Slip++++++++");

System.out.println("Basic pay: " + BP);

System.out.println("Gross salary: " + gross_salary);

System.out.println("Net salary: " + net_Salary);

}
class Programmer extends Employee {

void basic_pay() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your basic pay:");

BP = sc.nextFloat();

class AssistantProfessor extends Employee {

void basic_pay() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your basic pay:");

BP = sc.nextFloat();

class AssociateProfessor extends Employee {

void basic_pay() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your basic pay:");

BP = sc.nextFloat();

class Professor extends Employee {

void basic_pay() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your basic pay:");

BP = sc.nextFloat();

}
}

public class Payment {

public static void main(String[] args) {

int choice;

Scanner sc = new Scanner(System.in);

do {

System.out.println("\n++++++Choose your position++++++");

System.out.println("1. Programmer");

System.out.println("2. Assistant Professor");

System.out.println("3. Associate Professor");

System.out.println("4. Professor");

System.out.println("5. Exit");

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

choice = sc.nextInt();

switch (choice) {

case 1:

Programmer prg = new Programmer();

prg.getEmployeeDetails();

prg.basic_pay();

prg.generatePaymentSlip();

break;

case 2:

AssistantProfessor A1 = new AssistantProfessor();

A1.getEmployeeDetails();

A1.basic_pay();

A1.generatePaymentSlip();

break;

case 3:
AssociateProfessor B1 = new AssociateProfessor();

B1.getEmployeeDetails();

B1.basic_pay();

B1.generatePaymentSlip();

break;

case 4:

Professor C1 = new Professor();

C1.getEmployeeDetails();

C1.basic_pay();

C1.generatePaymentSlip();

break;

case 5:

System.out.println("Successfully Exited!!");

break;

default:

System.out.println("Invalid input, please try again.");

} while (choice != 5);

sc.close();

Q6.Design a base class shape with two double type values and member functions to input the data
and compute_areaQ for calculating area irf figure. Derive two classes' triangle and rectangle. Make
compute_areaQ as abstract function and redefine this function in the derived class to suit their
requirements. Write a program that accepts dimensions of trianglelrectangle and display calculated
area. lmplement dynamic binding :for given case study

import java.util.*;

abstract class Shape40

double value1;
double value2;

void input()

Scanner sc = new Scanner(System.in);

System.out.println("enter value 1:");

value1 = sc.nextInt();

System.out.println("enter value 2:");

value2 = sc.nextInt();

abstract void compute_area();

class Traingle extends Shape40 {

void compute_area()

double area;

area = 0.5 * value1 * value2;

System.out.println("the area of traingle wiht base"+value1+"and height"+value2+"is:"+area);

class Rectangle extends Shape40 {

void compute_area()

double area;

area =value1 * value2;

System.out.println("the area of Rectangle with length"+value1+"and breadth"+value2+"is:"+area);

}
class Same {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice;

System.out.println("choose the shape");

System.out.println("\n1.Traingle\n2.Rectangle\n3.exit");

System.out.println("enter choice:");

choice= sc.nextInt();

do {

switch (choice) {

case 1:

Shape40 t = new Traingle();

t.input();

t.compute_area();

case 2:

Shape40 r = new Rectangle();

r.input();

r.compute_area();

case 3:

System.out.println("exited from program");

default: {

System.out.println("invalid input");

while(choice!=3);
}

Q7. Design and develop a context for given case study and implement an interface for Vehicles
Consider the example of vehicles like bicycle, car, and bike. All Vehicles have common functionalities
such as Gear Change, Speed up and apply breaks 'Make an interface and put all these common
functionalities. Bicycle, Bike, Car classes should be implemented tbr all these functionalities in their
own class in their own way.

import java.util.Scanner;

interface Vehicles {

void gearChange();

void speedUp();

void applyBrakes();

class Bicycle implements Vehicles {

@Override

public void gearChange() {

System.out.println("The gear is changed in bicycle");

@Override

public void speedUp() {

System.out.println("Speed up in bicycle");

@Override

public void applyBrakes() {

System.out.println("Brakes applied in bicycle");

}
class Car implements Vehicles {

@Override

public void gearChange() {

System.out.println("The gear is changed in car");

@Override

public void speedUp() {

System.out.println("Speed up in car");

@Override

public void applyBrakes() {

System.out.println("Brakes applied in car");

class Bike implements Vehicles {

@Override

public void gearChange() {

System.out.println("The gear is changed in bike");

@Override

public void speedUp() {

System.out.println("Speed up in bike");

@Override

public void applyBrakes() {


System.out.println("Brakes applied in bike");

class Main2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice;

do {

System.out.println("Choose vehicle:");

System.out.println("\n1. Bicycle\n2. Car\n3. Bike\n4. Exit");

System.out.println("Enter your choice:");

choice = sc.nextInt();

switch (choice) {

case 1:

Bicycle by = new Bicycle();

by.gearChange();

by.speedUp();

by.applyBrakes();

break;

case 2:

Car c = new Car();

c.gearChange();

c.speedUp();

c.applyBrakes();

break;

case 3:
Bike bi = new Bike();

bi.gearChange();

bi.speedUp();

bi.applyBrakes();

break;

case 4:

System.out.println("Exited successfully");

break;

default:

System.out.println("Invalid input, try again");

break;

} while (choice != 4);

Q8. Implement a program to handle Arithmetic exception, Array Index Out OfBounds.The user
enters two numbers Numl and Num2. The division of Numl and Num2 is displayed. If Numl and
Num2 were not integers, the program would throw a Number Format Exception. If Num2 were zero,
the program would throw an Arithmetic Exception. Display the exception.

import java.util.*;

import java.io.IOException;

class Ex {

public static void main(String[] args) {

try{

Scanner sc= new Scanner(System.in);

System.out.println("enter num 1");

String value1= sc.nextLine();


System.out.println("enter num 2");

String value2= sc.nextLine();

int num1= Integer.parseInt(value1);

int num2=Integer.parseInt(value2);

int div=num1/num2;

System.out.println("the division of"+num1+"and"+num2+"is"+div);

catch (NumberFormatException e)

System.out.println(" number format exception: enter integer only ");

catch(ArithmeticException e)

System.out.println("arithematic Exception: the num 2 cannot be zero");

You might also like