KnowledgeBoat Logo
|

Computer Applications

Write a program by using class with the following specifications:

Class name — Sale

Data members/ Instance variables:

  1. String title, author,publication
  2. double price

Member methods:

  1. void input() — to accept title, author name and publication name and price of a book
  2. void display() — to display title, author name and publication name and price of a book

Now, create another class 'Purchase' that inherits class 'Sale' having the following specifications:

Class name — Purchase

Data members/ Instance variables:

  1. int noc
  2. int amount;

Member methods:

  1. void accept() — to enter the number of copies purchased
  2. void calculate( ) — to find the amount by multiplying number of copies ordered and price (i.e., noc * price)
  3. void show() — to display the elements describes in base class along with the number of copies purchased and amount to be paid to the shopkeeper

Java

Encapsulation & Inheritance in Java

25 Likes

Answer

import java.util.Scanner;

public class Sale
{
    protected String title;
    protected String author;
    protected String publication;
    protected double price;
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter book title: ");
        title = in.nextLine();
        System.out.print("Enter book author: ");
        author = in.nextLine();
        System.out.print("Enter publication name: ");
        publication = in.nextLine();
        System.out.print("Enter book price: ");
        price = in.nextDouble();
    }
    
    public void display() {
        System.out.println("Book Title: " + title);
        System.out.println("Book Author: " + author);
        System.out.println("Publication: " + publication);
        System.out.println("Price: " + price);
    }
}
import java.util.Scanner;

public class Purchase extends Sale
{
    private int noc;
    private double amount;
    
    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter no. of copies purchased: ");
        noc = in.nextInt();
    }
    
    public void calculate() {
        amount = noc * price;
    }
    
    public void show() {
        display();
        System.out.println("No. of copies: " + noc);
        System.out.println("Amount: " + amount);
    }
    
    public static void main(String args[]) {
        Purchase obj = new Purchase();
        obj.input();
        obj.accept();
        obj.calculate();
        obj.show();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program by using class with the following specifications: Class name — Sale Data members/ Instance variables: (a) String title, author,publication (b) double price Member methods: (c) void input() — to accept title, author name and publication name and price of a book (d) void display() — to display title, author name and publication name and price of a book Now, create another class 'Purchase' that inherits class 'Sale' having the following specifications: Class name — Purchase Data members/ Instance variables: (e) int noc (f) int amount; Member methods: (g) void accept() — to enter the number of copies purchased (h) void calculate( ) — to find the amount by multiplying number of copies ordered and price (i.e., noc * price) (i) void show() — to display the elements describes in base class along with the number of copies purchased and amount to be paid to the shopkeeper

Answered By

9 Likes


Related Questions