Class 12 ISC - Java Inheritance Programs

Class 12th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 12th java topics includes revision of class 11th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1:

A superclass Product has been defined to store the details of a product sold by a wholesaler to a retailer. Define a subclass Sales to compute the total amount paid by the retailer with or without fine along with service tax.
Some of the members of both classes are given below:

Class name: Product

Data members/instance variables:

name: stores the name of the product
code: integer to store the product code
amount: stores the total sale amount of the product (in decimals)

Member functions/methods:

Product (String n, int c, double p): parameterized constructor to assign data members: name = n, code = c and amount = p
void show(): displays the details of the data members

Class name: Sales

Data members/instance variables:

day: stores number of days taken to pay the sale amount
tax: to store the sen ice tax (in decimals)
totamt: to store the total amount (in decimals)

Member functions/methods:

Sales(….): parameterized constructor to assign values to data members of both the classes
void compute(): calculates the service tax @ 12.4% of the actual sale amount calculates the fine @ 2.5% of the actual sale amount only if the amount paid by the retailer to the wholesaler exceeds 30 days calculates the total amount paid by the retailer as (actual sale amount + service tax + fine)
void show (): displays the data members of the superclass and the total amount

Assume that the superclass Product has been defined. Using the concept of inheritance, specify the class Sales giving the details of the constructor (…), void compute()  and void show(). 

The superclass, main function and algorithm need NOT be written.
                
               
Solution:

import java.util.*;
class Product
{
String name;
int code;
double amount;
Product(String n, int c, double p) {
name = n;
code = c;
amount = p;
}
void show() {
System.out.println("Name is :"+ name);
System.out.println("Code is :" + code);
System.out.println("Total Sale Amount:" + amount);
}
}

class Sales extends Product {
    int day;
    double tax;
    double totamt;
    double fine = 0.0;
    Sales(String n, int c, double p, int d) {
    super(n, c, p);
    day = d;
    }
    void compute() 
    {
    if(day < 30)
    { 
        tax = 12.4 * amount /100; totamt = amount + tax; 
    } 
    if(day > 30) 
    {
    tax= 12.4 * amount /100;
    fine = 2.5 * amount /100;
    totamt = amount + tax + fine;
    }
    }
    void show () {
    super.show();
    System.out.println("Total amount to be paid::"+ totamt);
    }
    }

    class base
{
    public static void main(String args[])
    {
        Sales obj = new Sales ("Anuj", 12345, 4567.90, 6);
        obj.compute();
        obj.show();
    }
}

Output :
Name is :Anuj Code is :12345 Total Sale Amount:4567.9 Total amount to be paid::5134.3196


Program 2:

A superclass Bank has been defined to store the details of a customer. Define a sub-class Account that enables transactions for the customer with the bank. The details of both the classes are given below:

Class name: Bank

Data members/instance variables:

name: stores the name of the customer
accno: stores the account number
P: stores the principal amount in decimals

Member functions/methods:

Bank(….): parameterized constructor to assign values to the instance variables
void display (): displays the details of the customer

Class name: Account

Data member/instance variable:

amt: stores the transaction amount in decimals

Member functions/methods:

Account(…): parameterized constructor to assign values to the instance variables of both the classes
void deposit(): accepts the amount and updates the principal as p=p+amt
void withdraw(): accepts the amount and updates the principal as p=p-amt
If the withdrawal amount is more than the principal amount, then display the message “INSUFFICIENT BALANCE”.
If the principal amount after withdrawal is less than 500, then a penalty is imposed by using the formula.
p=p-(500-p)/10
void display(): displays the details of the customer

Assume that the superclass Bank has been defined.

Using the concept of Inheritance; specify the class Account giving details of the constructor(…), void deposit(), void withdraw() and void display() The superclass and the main function need not be written.
                
Solution:

import java.util.*;
class Bank
{
    String name;
    String accno;
    double p;
    
    public Bank(String n, String a, double pp)
    {
        name=n;
        accno=a;
        pp=p;
    }
    public void display()
    {
        System.out.println("Name of customer=" +name);
        System.out.println("Account number=" +accno);
        
    }
}

class Account extends Bank
{ 
Scanner sc=new Scanner(System.in);
double amt;
Account(String n, String a, double pp)
{ 
super(n,a,pp);
amt=0.0;
}
void deposit()
{ 
System.out.println("Enter amount");
amt=sc.nextDouble();
p=p+amt;
}
void withdraw()
{ 
System.out.println("Enter amount for withdrwal");
amt=sc.nextDouble();
if(amt > p)
System.out.println("INSUFFICIENT BALANCE");
else
{ 
p=p-amt;
if(p < 500)
{
p= p- (500-p) / 10.0;
}
}
}
 public void display()
{ super.display();
System.out.println(" Balance Amount = " +p);
}
}

class customer
{
    public static void main(String args[])
    {
        Account obj= new Account("anuj","12345677890",350000.0);
        obj.deposit();
        obj.withdraw();
        obj.display();
    }
}

               

Output :

Enter amount 12000 Enter amount for withdrwal 1000 Name of customer=anuj Account number=12345677890 Balance Amount =11000.0
Program 3:

A superclass Number is defined to calculate the factorial of a number. Define a subclass Series to find the sum of the series S = 1! + 2! + 3! + 4! + ………. + n! [5]
The details of the members of both classes are given below:

Class name: Number

Data member/instance variable:

n: to store an integer number

Member functions/methods:

Number(int nn): parameterized constructor to initialize the data member n=nn
int factorial(int a): returns the factorial of a number
(factorial of n = 1 × 2 × 3 × …… × n)
void display(): display the result

Class name: Series

Data member/instance variable:

sum: to store the sum of the series

Member functions/methods:

Series(…) : parameterized constructor to initialize the data members of both the classes
void calsum(): calculates the sum of the given series
void display(): displays the data members of both the classes

Assume that the superclass Number has been defined. Using the concept of inheritance, specify the class Series giving the details of the constructor(…), void calsum() and void display().

The superclass, main function and algorithm need NOT be written.
                
Solution:
import java.util.*;

class Number{
int n;
public Number(int nn) 
{
n = nn;
}
public int factorial(int a) 
{
if(a <= 1)
return 1;
return a * factorial(--a);
}

public void display() 
{
System.out.println("Number =" + n);
}
}

class Series extends Number {
    int sum;
    public Series(int n) {
    super(n);
    sum = 0;
    }
    public void calcSum() 
    {
    for(int i = 1; i < = n; i++) 
    { 
    sum += super.factorial(i); 
    } 
    } 
    public void display() { 
    super.display(); 
    System.out.println("Series :" + sum); 
    } 
    } 

    class Factorial 
{ 
public static void main(String args[]) { 
Scanner sc = new Scanner(System.in); 
System.out.print("Enter the number :"); 
int num = sc.nextInt(); 
Series obj = new Series(num); 
obj.calcSum(); 
obj.display(); 
} 
}
                   

Output :

Enter the number :4 Number =4 Series :33
Program 4:

A superclass Record contains names and marks of the students in two different single dimensional arrays. Define a subclass Highest to display the names of the students obtaining the highest mark [5]
The details of the members of both classes are given below:

Class name: Record

Data member/instance variable:
n[] : array to store names
m[]: array to store marks
size: to store the number of students

Member functions/methods:

Record(int cap): parameterized constructor to initialize the data member
size = cap
void readarray() : to enter elements in both the arrays
void display() : displays the array elements

Class name: Highest

Data member/instance variable:

ind: to store the index

Member functions/methods:

Highest(…): parameterized constructor to initialize the data members of both the classes
void find(): finds the index of the student obtaining the highest mark and assign it to ‘ind’
void display(): displays the array elements along with the names and marks of the students who have obtained the highest mark

Assume that the superclass Record has been defined. Using the concept of inheritance, specify the class Highest giving the details of the constructor(…), void find() and void display().

The superclass, main function and algorithm need NOT be written.
                
               
Solution:
import java.util.*;
class Record
{
 String n[];
 int m[];
 int size;
public Record(int cap) 
{
    size=cap;
    n=new String[size];
    m=new int[size];
}
public void readarray() 
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter names : ");
    for(int i=0;i < size;i++)
    {
        n[i]= sc.next();
    }

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

    for(int i= 0;i < size;i++)
    {
        m[i]= sc.nextInt();
    }
}
public void display() 
{
    for(int i=0;i < size;i++)
    {
        System.out.print(n[i] + " ");
    }

    System.out.println();
    for(int i=0;i < size;i++)
    {
        System.out.print(m[i] + " ");
    }
    System.out.println();

}
}

class Highest extends Record
{ 
int ind;
Highest(int cap)
{ super(cap);
  ind=-1;
}
void find()
{ 
readarray();
int hm=m[0];
for (int i=0;ihm)
   { 
    hm=m[i];
    ind=i;
    } 
 } 
}
public void display()
  { 
    super.display();
    for(int i=0;i < size;i++)
   { 
    if(m[i] == m[ind])
    {
     System.out.println(" Highest obtained by " +n[i] + " marks " +m[i]);
    }
   }
  }
}

class rec
{
    public static void main(String args[])
    {
     Highest obj = new Highest(5);
     obj.find();
     obj.display();
    }
}

                        

Output :

Enter names : aditya sahil anuj abhishek aryan Enter marks : 98 87 86 99 75 aditya sahil anuj abhishek aryan 98 87 86 99 75 Highest obtained by abhishek marks 99
Program 5:

A line on a plane can be represented by coordinates of the two-end points p1 and p2 as p1(x1,
y1) and p2(x2, y2). A super class Plane is defined to represent a line and a sub class Circle to find the length of
the radius and the area of circle by using the required data members of super class.
Some of the members of both the classes are given below:

Class name : Plane

Data members/instance variables:
x1 : to store the x-coordinate of the first end point
y1 : to store the y-coordinate of the first end point

Member functions/methods:

Plane( int nx, int ny ) : parameterized constructor to assign the data

members x1=nx and y1=ny
void Show( ) : to display the coordinates

Class name : Circle

Data members/instance variables:

x2 : to store the x-coordinate of the second end point
y2 : to store the y-coordinate of the second end point
radius : double variable to store the radius of the circle
area : double variable to store the area of the circle

Member functions / methods :

Circle(...) : parameterized constructor to assign values to data

members of both the classes

void findRadius( ) : to calculate the length of radius using the  formula:
(√(x22 - x12 + y22 - y12))/2 assuming that x1, x2, y1, y2 are the coordinates of
the two ends of the diameter of a circle
void findArea( ) : to find the area of circle using formula: πr2. The
value of pie (π) is 22/7 or 3.14
void Show( ) : to display both the coordinates along with the
length of the radius and area of the of the circle

Specify the class Plane giving details of the constructor and void Show( ). Using the concept of inheritance, specify the class Circle giving details of the constructor, void
findRadius( ), void findArea( ) and void Show( ). The main function and algorithm need not be written.
               
Solution:
import java.util.*;
class Plane
{
double x1,y1;
Plane(double nx, double ny)
{ x1=nx;
y1=ny;
}
void Show()
{ 
    System.out.println("First x - coordinate= "+ x1);
    System.out.println("First y - coordinate= "+ y1);
}
}

class Circle extends Plane
{
double x2, y2, radius, area;
Circle(double nx, double ny, double a, double b)
{ super(nx,ny);
x2=a;
y2=b;
}
void findRadius()
{ radius= ( Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) ) ) / 2;
}
void findArea()
{ area = 3.14 * radius * radius ;
}
void Show()
{ super.Show();
System.out.println("Second x- coordinate= "+ x2);
System.out.println("Second y- coordinate= "+ y2);
System.out.println("Length of radius = "+radius);
System.out.println("Area = " + area);
}
}

class shape
{
  public static void main(String args[])
  {
    Circle obj= new Circle(2.2, 3.4, 4.8, 6.7);
    obj.findRadius();
    obj.findArea();
    obj.Show();
  }
    
}

                        

Output :

First x - coordinate= 2.2 First y - coordinate= 3.4 Second x- coordinate= 4.8 Second y- coordinate= 6.7 Length of radius = 2.1005951537600005 Area = 13.855250000000002

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com