Item Inheritance ISC Computer Science 2022 Semester 2 Theory

A super class Item has been defined to store the details of an item sold by a wholesaler to a retailer. Define a subclass Taxable to compute the total amount paid by the retailer along with service tax.

Some of the members of both the classes are given below:

Class name: Item
Data members/instance variables:
name: to store the name of the item
code: integer to store the item code
amount: stores the total sale amount of the item (in decimals)
Member functions/methods:
Item(...): parameterized constructor to assign values to the data members
void show(): to display the item details

Class name: Taxable
Data members/instance variables:
tax: to store the service tax (in decimals)
totamt: to store the total amount (in decimals)
Member functions/methods:
Taxable(...): parameterized constructor to assign values to the data members of both the classes
void calTax(): calculates the service tax @ 10.2% of the total sale amount. Calculates the total amount paid by the retailer as (total sale amount + service tax)
void show(): to display the item details along with the total amount

Assume that the super class Item has been defined. Using the concept of inheritance, specify the class Taxable, giving details of the constructor, void calTax() and void show().

The super class, main function and algorithm need not be written.

import java.util.Scanner;
class Item{
    protected String name;
    protected int code;
    protected double amount;
    public Item(String n, int c, double a){
        name = n;
        code = c;
        amount = a;
    }
    public void show(){
        System.out.println("Name: " + name);
        System.out.println("Code: " + code);
        System.out.println("Amount: " + amount);
    }
}
class Taxable extends Item{
    double tax;
    double totamt;
    public Taxable(String n, int c, double a){
        super(n, c, a);
        totamt = 0.0;
        tax = 0.0;
    }
    public void calTax(){
        tax = 10.2 / 100 * amount;
        totamt = amount + tax;
    }
    public void show(){
        super.show();
        System.out.println("Total amount: " + totamt);
    }
}
class MyInheritance{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Name: ");
        String n = in.nextLine();
        System.out.print("Code: ");
        int c = Integer.parseInt(in.nextLine());
        System.out.print("Sale amount: ");
        double a = Double.parseDouble(in.nextLine());
        Taxable obj = new Taxable(n, c, a);
        obj.calTax();
        obj.show();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory