Computer Applications
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.]
Java
Encapsulation & Inheritance in Java
30 Likes
Answer
import java.util.Scanner;
public class Number
{
protected int n;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}
public void display() {
System.out.print("Number = " + n);
}
}public class Check extends Number
{
private int fact;
private int revnum;
public void find() {
fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
System.out.println("Factorial of " + n + " = " + fact);
}
public void palindrome() {
revnum = 0;
int t = n;
while (t != 0) {
int digit = t % 10;
revnum = revnum * 10 + digit;
t /= 10;
}
if (n == revnum)
System.out.println(n + " is Palindrome number");
else
System.out.println(n + " is not Palindrome number");
}
public static void main(String args[]) {
Check obj = new Check();
obj.input();
obj.find();
obj.palindrome();
}
}Variable Description Table
Program Explanation
Output
![BlueJ output of Write a program to define class with the following specifications: Class name — Number Data members/ Instance variables: (a) int n — to hold an integer number Member methods: (b) void input() — to accept an integer number in n (c) 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: (d) int fact (e) int revnum Member methods: (f) void find() — to find and print factorial of the number used in base class (g) 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.] BlueJ output of Write a program to define class with the following specifications: Class name — Number Data members/ Instance variables: (a) int n — to hold an integer number Member methods: (b) void input() — to accept an integer number in n (c) 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: (d) int fact (e) int revnum Member methods: (f) void find() — to find and print factorial of the number used in base class (g) 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.]](https://cdn1.knowledgeboat.com/img/apc10/chap_8-p7.jpg)
Answered By
13 Likes
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 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