Week 3 of OOP in JAVA

Create a base class Student with data members name and rollNumber.
Create a derived class Result that inherits from Student and contains marks of three subjects.
• Use constructors to initialize the data members.
• Write a method to calculate total and average marks.
• Display the student details and result.


class Student{
	String name;
	int roll;
	
	Student(String text, int no){
		name = text;
		roll = no;
	}
}

class Result extends Student{
	int phy, chem, maths;
	Result(String text,int no, int p, int c, int m){
		super(text,no);
		phy = p;
		chem = c;
		maths = m;
	}

int total(){
	return phy+chem+maths;
}

double average(){
	return total()/3;
}

void details(){
System.out.println("Name : " + name);System.out.println("Roll no : " + roll);
System.out.println("Physics : " + phy);
System.out.println("Chemistry : " + chem);
System.out.println("Maths : " + maths);
System.out.println("Total Marks : " + total());
System.out.println("Average Marks: " + average());

}
}
public class Main {
	public static void main(String[] args) {
		Result s1 = new Result("Tia",14,87,89,91);
		s1.details();
	}
}


Create a base class Employee containing employeeID and name.
Create a derived class Salary that inherits from Employee and contains basicSalary.
• Calculate the gross salary by adding HRA and DA to the basic salary.
• Display employee details and gross salary.


class Employee{
	String employeeID, name;
	Employee(String ID, String text){
		employeeID = ID;
		name = text;
	}
}

class Salary extends Employee{
	int basicSalary;
	Salary(String ID, String text,int sal){
		super(ID,text);
		basicSalary = sal;
	}
int gross(int hra, int da){
	return basicSalary + hra + da;
}

void details(){
System.out.println("Name : " + name);
System.out.println("Employee ID : " + employeeID);
System.out.println("Basic Salary : " + basicSalary);
}
}

public class Main{
	public static void main(String[] args){
		Salary e1 = new Salary("IT239","Rohan",256000);
		e1.details();
	}
}


Create a base class Person containing name and age.
Create a class Employee derived from Person that contains employee ID and salary.
Create another class Manager derived from Employee that contains department name.
• Use constructors to initialize values.
• Display all the details of the manager.


class Person{
	String name;
	int age;
	Person(String text,int year){
		name = text;
		age = year;
	}
}

class Employee extends Person{
	String employeeID;
	int salary;
	Employee(String text, int year, String ID, int sal){
		super(text,year);
		employeeID = ID;
		salary = sal;
	}
}

class Manager extends Employee{
	String dept;
	Manager(String text, int year, String ID,int sal, String D){
		super(text,year,ID,sal);
		dept = D;
	}

void details(){
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Employee ID : " + employeeID);
System.out.println("Salary : " + salary);
System.out.println("Department : " + dept);
}
}

public class Main {
	public static void main(String[] args) {
		Manager m1 = new Manager("Rohit",36, "FIN6768",1250000,"Finance");
		m1.details();
	}
}


Create a base class Shape with a method to display a message.
Create two derived classes:
• Rectangle (with length and breadth)
• Circle (with radius)
• Use constructors to initialize dimensions.
• Write methods to calculate and display the area of each shape.


abstract class Shape{
	abstract void area();
}

class Rectangle extends Shape{
	double length,breadth;
	Rectangle(double l, double b){
		length = l;
		breadth = b;
	}
	
void area(){
System.out.println("The area rectangle is  " + length*breadth);
}
}

class Circle extends Shape{
	double radius;
	Circle(double r){
		radius = r;
	}
void area(){
System.out.println("The area of circle is " + 3.14*radius*radius);
}
}

public class Main {
	public static void main(String[] args) {	Rectangle r = new Rectangle(41.3,86.7);
Circle c = new Circle(3.6);
r.area();
c.area();
	}
}


Create a base class Vehicle containing data members brand and speed.
Create a derived class Car that adds the data member model.
• Initialize values using constructors.
• Display complete information about the car.


class Vehicle{
	String brand;
	int speed;
    Vehicle(String b, int s){
    	brand = b;
    	speed = s;
    }
}

class Car extends Vehicle{
	String model;
	Car(String b, int s, String m){
		super(b,s);
		model = m;
	}

void info(){
System.out.println("Brand : " + brand);
System.out.println("Max Speed : " + speed);
System.out.println("Model : " + model);
}
}

public class Main {
	public static void main(String[] args) {
		Car c1 = new Car("GTX",170,"Road Crasher");
		c1.info();
	}
}


Comments

Popular Posts