Computer Applications
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 specifications:
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
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
Related Questions
Write a program by using a class with the following specifications:
Class name — Salary
Data members — private int basic
Member functions:
- void input() — to input basic pay
- void display() — to find and print the following:
da = 30% of basic
hra = 10% of basic
gross = basic + da + hra
Use a main function to create an object and call member methods of the class.
Write a class program with the following specifications:
Class name — Matrix
Data members — int array m[][] with 3 rows and 3 columns
Member functions:
- void getdata() — to accept the numbers in the array
- void rowsum() — to find and print the sum of the numbers of each row
- void colsum() — to find and print the sum of numbers of each column
Use a main function to create an object and call member methods of the class.
Write a program to use a class Account with the following specifications:
Class name — Account
Data members — int acno, float balance
Member Methods:
- Account (int a, int b) — to initialize acno = a, balance = b
- void withdraw(int w) — to maintain the balance with withdrawal (balance - w)
- void deposit(int d) — to maintain the balance with the deposit (balance + d)
Use another class Calculate which inherits from class Account with the following specifications:
Data members — int r,t ; float si,amt;
Member Methods:
- void accept(int x, int y) — to initialize r=x,t=y,amt=0
- void compute() — to find simple interest and amount
si = (balance * r * t) / 100;
a = a + si; - void display() — to print account number, balance, interest and amount
main() function need not to be used
Write a program to define class with the following specifications:
Class name — Number
Data members/ Instance variables:
- int n — to hold an integer number
Member methods:
- void input() — to accept an integer number in n
- void display() — to display the integer number input in n
Now, inherit class Number to another class Check that is defined with the following specifications:
Class name — Check
Data members/ Instance variables:
- int fact
- int revnum
Member methods:
- void find() — to find and print factorial of the number used in base class
- void palindrome() — to check and print whether the number used in base class is a palindrome number or not
[A number is said to be palindrome if it appears the same after reversing its digits. e.g., 414, 333, 515, etc.]