English

Write a program by using class with the following specifications: Class name: Sale Data Members/Instance variables  String title, author, publication double price Member Methods: - Computer Applications

Advertisements
Advertisements

Question

Write a program by using class with the following specifications:

Class name: Sale
Data Members/Instance variables 
String title, author, publication
double price
Member Methods:
void input(): to accept title, author name and publication name and price of a book
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 specification:

Class name: Purchase
Data Members/Instance variables:
int noc
int amount;
Member Methods:
void accept(): to enter the number of copies purchased
void calculate(): to find the amount by multiplying number of copies ordered and price (i.e., noc * price)
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.  

Code Writing

Solution

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();
   }
}
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);
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 8: Encapsulation and Inheritance - EXERCISES [Page 448]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 8 Encapsulation and Inheritance
EXERCISES | Q VI. 6. | Page 448
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×