Chapter 8
Encapsulation and Inheritance
Class 10 - APC Understanding Computer
Applications with BlueJ
Solutions to Unsolved Java Programs
Question 1
Write a program by using a class with the following
specifications:
Class name — Prime
Data members — private int n
Member functions:
1. void input() — to input a number
2. void checkprime() — to check and display whether the
number is prime or not
Use a main function to create an object and call member
methods of the class.
import java.util.Scanner;
public class Prime
{
private int n;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}
public void checkprime() {
boolean isPrime = true;
if (n == 0 || n == 1)
isPrime = false;
else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
}
public static void main(String args[]) {
Prime obj = new Prime();
obj.input();
obj.checkprime();
}
}
Question 2
Write a program by using a class with the following
specifications:
Class name — Factorial
Data members — private int n
Member functions:
1. void input() — to input a number
2. void fact() — to find and print the factorial of the
number
Use a main function to create an object and call member
methods of the class.
import java.util.Scanner;
public class Factorial
{
private int n;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}
public void fact() {
int f = 1;
for (int i = 1; i <= n; i++)
f *= i;
System.out.println("Factorial of " + n
+ " = " + f);
}
public static void main(String args[]) {
Factorial obj = new Factorial();
obj.input();
obj.fact();
}
}
Question 3
Write a program by using a class with the following
specifications:
Class name — Salary
Data members — private int basic
Member functions:
1. void input() — to input basic pay
2. 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.
import java.util.Scanner;
public class Salary
{
private int basic;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the basic pay: ");
basic = in.nextInt();
}
public void display() {
double da = basic * 0.3;
double hra = basic * 0.1;
double gross = basic + da + hra;
System.out.println("da=" + da);
System.out.println("hra=" + hra);
System.out.println("gross=" + gross);
}
public static void main(String args[]) {
Salary obj = new Salary();
obj.input();
obj.display();
}
}
Question 4
Write a class program with the following specifications:
Class name — Matrix
Data members — int array m[][] with 3 rows and 3 columns
Member functions:
1. void getdata() — to accept the numbers in the array
2. void rowsum() — to find and print the sum of the
numbers of each row
3. 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.
import java.util.Scanner;
public class Matrix
{
private final int ARR_SIZE = 3;
private int[][] m;
public Matrix() {
m = new int[ARR_SIZE][ARR_SIZE];
}
public void getdata() {
Scanner in = new Scanner(System.in);
for (int i = 0; i < ARR_SIZE; i++) {
System.out.println("Enter elements of row "
+ (i+1) + ":");
for (int j = 0; j < ARR_SIZE; j++) {
m[i][j] = in.nextInt();
}
}
}
public void rowsum() {
for (int i = 0; i < ARR_SIZE; i++) {
int rSum = 0;
for (int j = 0; j < ARR_SIZE; j++) {
rSum += m[i][j];
}
System.out.println("Row " + (i+1) + " sum: " +
rSum);
}
}
public void colsum() {
for (int i = 0; i < ARR_SIZE; i++) {
int cSum = 0;
for (int j = 0; j < ARR_SIZE; j++) {
cSum += m[j][i];
}
System.out.println("Column " + (i+1) + " sum: " +
cSum);
}
}
public static void main(String args[]) {
Matrix obj = new Matrix();
obj.getdata();
obj.rowsum();
obj.colsum();
}
}
Output
Question 1
Enter the number: 11
Prime Number
Question 2
Enter the number: 4
Factorial of 4 = 24
Question 3
Enter the basic pay: 75000
da=22500.0
hra=7500.0
gross=105000.0
Question 4
Enter elements of row 1:
1
2
3
Enter elements of row 2:
4
5
6
Enter elements of row 3:
7
8
9
Row 1 sum: 6
Row 2 sum: 15
Row 3 sum: 24
Column 1 sum: 12
Column 2 sum: 15
Column 3 sum: 18