Inheritance's Programs

 Create a class Student which store the name and roll no. of a student. Create another class Marks which inherits class Student and store the marks of 3 subjects. Create Class Result which inherits class Marks and store the information of a student and display name, roll and total marks of a student.
```import java.util.Scanner;
class Student {
String name;
int roll;
}
class Marks extends Student {
int m1,m2,m3;
}
class Result extends Marks {
int total;
void input() {
Scanner s=new Scanner(System.in);
System.out.print("Enter Name: ");
name = s.nextLine();
System.out.print("Enter Roll: ");
roll = Integer.parseInt(s.nextLine());
System.out.print("Enter Marks1: ");
m1 = Integer.parseInt(s.nextLine());
System.out.print("Enter Marks2: ");
m2 = Integer.parseInt(s.nextLine());
System.out.print("Enter Marks3: ");
m3 = Integer.parseInt(s.nextLine());
}
void display() {
total = m1+m2+m3;
System.out.println("Name- "+name);
System.out.println("Roll- "+roll);
System.out.println("Total- "+total);
}
}
class Test {
public static void main(String[] a)
{
Result r = new Result();
r.input();
r.display();
}
}```
OUTPUT :
```Enter Name: Ayan
Enter Roll: 12
Enter Marks1: 90
Enter Marks2: 90
Enter Marks3: 90
Name- Ayan
Roll- 12
Total- 270```
 WAP to calculate nCr and 1!+2!+3!+4! using inheritance. FORMULA: nCr = n!/(r!*(n-r)!)
```import java.util.Scanner;
class Factorial {
Scanner s=new Scanner(System.in);
int fact(int x) {
int res = 1;
for(int i=1; i<= x; i++) {
res = res * i;
}
return res;
}
}
class SeriesA extends Factorial{
public void calculate() {
System.out.print("Enter N: ");
int n = s.nextInt();
System.out.print("Enter R: ");
int r = s.nextInt();
int res=fact(n)/(fact(n-r)*fact(r));
System.out.println("Result- "+res);
}
}
class SeriesB extends Factorial {
public void show() {
System.out.print("Enter N: ");
int n = s.nextInt();
int sum =0;
for(int i=1; i<=n; i++) {
sum = sum+fact(i);
}
System.out.print("Result- "+sum);
}
}
class Main{
public static void main(String[] a)
{
SeriesA s = new SeriesA();
s.calculate();
SeriesB s2 = new SeriesB();
s2.show();
}
}```
OUTPUT :
```Enter N: 5
Enter R: 3
Result- 10
Enter N: 5
Result- 153```
 Define a class Calculator ,which has method for basic functionality like add, sub, mul, divde. Define another class Calc which inherits Calculator and add some method to like sin, cosine . WAP to demonstrate the working of Inheritance.
```class Calculator {
void add(int a, int b) {
int c = a+b;
}
void sub(int a, int b) {
int c = a-b;
System.out.println("Sub = "+c);
}
void mul(int a, int b) {
int c = a*b;
System.out.println("Mul = "+c);
}
void div(int a, int b) {
float c = (float)a/b;
System.out.println("Div = "+c);
}
}
class Calc extends Calculator {
void getSin(int a) {
double res = Math.sin(x);
System.out.println("Result = "+res);
}
void getCos(int a) {
double res = Math.cos(x);
System.out.println("Result = "+res);
}
}
class Main {
public static void main(String[] a)
{
Calc c = new Calc();
c.sub(20,10);
c.mul(20,10);
c.div(20,10);
c.getSin(90);
c.getCos(0);
}
} ```
OUTPUT :
```Add = 30
Sub = 10
Mul = 200
Div = 2.0
Result = 1.0
Result = 1.0```
 Create a class Student which store the name and roll no. of a student. Create another class Marks which inherits class Student and store the marks of 3 subjects, store the information of a student and display name, roll and total marks of a student. Use super keyword to take input.
```import java.util.Scanner;
class Student {
String name;
int roll;
Scanner s=new Scanner(System.in);
void input(){
System.out.print("Enter Name: ");
name = s.nextLine();
System.out.print("Enter Roll: ");
roll = Integer.parseInt(s.nextLine());
}
}

class Result extends Student {
int m1,m2,m3,total;
void input() {
super.input();
System.out.print("Enter Marks1: ");
m1 = Integer.parseInt(s.nextLine());
System.out.print("Enter Marks2: ");
m2 = Integer.parseInt(s.nextLine());
System.out.print("Enter Marks3: ");
m3 = Integer.parseInt(s.nextLine());
}
void display() {
total = m1+m2+m3;
System.out.println("Name- "+name);
System.out.println("Roll- "+roll);
System.out.println("Total- "+total);
}
}
class Test {
public static void main(String[] a)
{
Result r = new Result();
r.input();
r.display();
}
}  ```
OUTPUT :
```Enter Name: Sangeet
Enter Roll: 12
Enter Marks1: 90
Enter Marks2: 90
Enter Marks3: 90
Name- Sangeet
Roll- 12
Total- 270```

 Create a class Employee which has two data memeber name and id and a funtion to take input. Derive two classes "Regular" and "Part_Time". In Regular class, calculate the gross salary using Formula, Gross salary = Basic Salary+HA+DRA,where HA and DRA should be stored as static data member of class. In Part_Time class, Calculate the salary of Part_Time Employee using Formula Gross salary = Pay per hrs * no. of hrs. where pay per hrs is should be stored as static const.
```import java.util.*;
class Employee{
String name;
int id;
static final int HA = 3000;
static final int DRA = 5000;
static final int pay = 1000;
Scanner s=new Scanner(System.in);
void input() {
System.out.print("Enter Name: ");
name = s.nextLine();
System.out.print("Enter Id: ");
id = s.nextInt();
}
}
class Regular extends Employee {
int bsal,gsal;
void input() {
System.out.println("Regular Emp");
super.input();
System.out.print("Enter Salary: ");
bsal = s.nextInt();
}
void display() {
gsal = bsal+HA+DRA;
System.out.println("Salary- "+gsal);
}
}
class Part_Time extends Employee {
int hr,gsal;
void input() {
System.out.println("Part_Time EMP");
super.input();
System.out.print("Enter No. of Hrs: ");
hr = s.nextInt();
}
void display() {
gsal = hr*pay;
System.out.println("Income- "+gsal);
}
}
class Salary {
public static void main(String[] a)
{
Regular r= new Regular();
r.input();
r.display();
Part_Time p = new Part_Time();
p.input();
p.display();
}
}```
OUTPUT :
```Regular Emp
Enter Name: Alok
Enter Id: 13
Enter Salary: 25000
Salary- 33000
Part_Time EMP
Enter Name: Deepak
Enter Id: 30
Enter No. of Hrs: 30
Income- 30000```
 WAP to demonstrate the order of call of Constructor.
```class Fruit {
String name;
Fruit(){
name ="Apple";
}
}
class Apple extends Fruit {
int price;
Apple() {
price = 80;
}
void display() {
System.out.println("Name- "+name);
System.out.println("Price- "+price);
}
}
class Main {
public static void main(String[] a)
{
Apple p = new Apple();
p.display();
}
}```
OUTPUT :
```Name- Apple
Price- 80 ```

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824