Chapter-7 Constructor
Solutions to Unsolved Java
Programs
Question 1
Write a program by using a class with the following specifications:
Class name — Hcflcm
Data members/instance variables:
1. int a
2. int b
Member functions:
1. Hcflcm(int x, int y) — constructor to initialize a=x and b=y.
2. void calculate( ) — to find and print hcf and lcm of both the
numbers.
import [Link];
public class Hcflcm
{
private int a;
private int b;
public Hcflcm(int x, int y) {
a = x;
b = y;
}
public void calculate() {
int x = a, y = b;
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
int hcf = x;
int lcm = (a * b) / hcf;
[Link]("HCF = " + hcf);
[Link]("LCM = " + lcm);
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter first number: ");
int x = [Link]();
[Link]("Enter second number: ");
int y = [Link]();
Hcflcm obj = new Hcflcm(x,y);
[Link]();
}
}
Question 2
An electronics shop has announced a special discount on the
purchase of Laptops as given below:
Category Discount on
Laptop
Up to ₹25,000 5.0%
₹25,001 - ₹50,000 7.5%
₹50,001 - ₹1,00,000 10.0%
More than ₹1,00,000 15.0%
Define a class Laptop described as follows:
Data members/instance variables:
1. name
2. price
3. dis
4. amt
Member Methods:
1. A parameterised constructor to initialize the data members
2. To accept the details (name of the customer and the price)
3. To compute the discount
4. To display the name, discount and amount to be paid after
discount.
Write a main method to create an object of the class and call the
member methods.
import [Link];
public class Laptop
{
private String name;
private int price;
private double dis;
private double amt;
public Laptop(String s, int p)
{
name = s;
price = p;
}
public void compute() {
if (price <= 25000)
dis = price * 0.05;
else if (price <= 50000)
dis = price * 0.075;
else if (price <= 100000)
dis = price * 0.1;
else
dis = price * 0.15;
amt = price - dis;
}
public void display() {
[Link]("Name: " + name);
[Link]("Discount: " + dis);
[Link]("Amount to be paid: " + amt);
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Customer Name: ");
String str = [Link]();
[Link]("Enter Price: ");
int p = [Link]();
Laptop obj = new Laptop(str,p);
[Link]();
[Link]();
}
}
Question 3
Write a program by using a class with the following specifications:
Class name — Calculate
Instance variables:
1. int num
2. int f
3. int rev
Member Methods:
1. Calculate(int n) — to initialize num with n, f and rev with 0
(zero)
2. int prime() — to return 1, if number is prime
3. int reverse() — to return reverse of the number
4. void display() — to check and print whether the number is a
prime palindrome or not
import [Link];
public class Calculate
{
private int num;
private int f;
private int rev;
public Calculate(int n) {
num = n;
f = 0;
rev = 0;
}
public int prime() {
f = 1;
if (num == 0 || num == 1)
f = 0;
else
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
f = 0;
break;
}
}
return f;
}
public int reverse() {
int t = num;
while (t != 0) {
int digit = t % 10;
rev = rev * 10 + digit;
t /= 10;
}
return rev;
}
public void display() {
if (f == 1 && rev == num)
[Link](num + " is prime palindrome");
else
[Link](num + " is not prime palindrome");
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int x = [Link]();
Calculate obj = new Calculate(x);
[Link]();
[Link]();
[Link]();
}
}
Question 4
Define a class Arrange described as below:
Data members/instance variables:
1. String str (a word)
2. String i
3. int p (to store the length of the word)
4. char ch;
Member Methods:
1. A parameterised constructor to initialize the data member
2. To accept the word
3. To arrange all the alphabets of word in ascending order of
their ASCII values without using the sorting technique
4. To display the arranged alphabets.
Write a main method to create an object of the class and call the
above member methods.
import [Link];
public class Arrange
{
private String str;
private String i;
private int p;
private char ch;
public Arrange(String s) {
str = s;
i = "";
p = [Link]();
ch = 0;
}
public void rearrange() {
for (int a = 65; a <= 90; a++) {
for (int j = 0; j < p; j++) {
ch = [Link](j);
if (a == [Link](ch))
i += ch;
}
}
}
public void display() {
[Link]("Alphabets in ascending order:");
[Link](i);
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a word: ");
String word = [Link]();
Arrange obj = new Arrange(word);
[Link]();
[Link]();
}
}
Question 5
Write a program by using a class in Java with the following
specifications:
Class name — Stringop
Data members:
1. String str
Member functions:
1. Stringop() — to initialize str with NULL
2. void accept() — to input a sentence
3. void encode() — to replace and print each character of the
string with the second next character in the ASCII table. For
example, A with C, B with D and so on
4. void print() — to print each word of the String in a separate
line
import [Link];
public class Stringop
{
private String str;
public Stringop() {
str = null;
}
public void accept() {
Scanner in = new Scanner([Link]);
[Link]("Enter a sentence: ");
str = [Link]();
}
public void encode() {
char[] arr = new char[[Link]()];
for (int i = 0; i < [Link](); i++) {
arr[i] = (char)([Link](i) + 2);
}
str = new String(arr);
[Link]("\nEncoded Sentence:");
[Link](str);
}
public void print() {
[Link]("\nPrinting each word on a separate line:");
int s = 0;
while (s < [Link]()) {
int e = [Link](' ', s);
if (e == -1)
e = [Link]();
[Link]([Link](s, e));
s = e + 1;
}
}
public static void main(String args[]) {
Stringop obj = new Stringop();
[Link]();
[Link]();
[Link]();
}
}
Question 6
The population of a country in a particular year can be calculated
by:
p*(1+r/100) at the end of year 2000, where p is the initial
population and r is the
growth rate.
Write a program by using a class to find the population of the
country at the end of each year from 2001 to 2007. The Class has
the following specifications:
Class name — Population
Data Members — float p,r
Member Methods:
1. Population(int a,int b) — Constructor to initialize p and r with
a and b respectively.
2. void print() — to calculate and print the population of each
year from 2001 to 2007.
import [Link];
public class Population
{
private float p;
private float r;
public Population(float a, float b)
{
p = a;
r = b;
}
public void print() {
float q = p;
for (int y = 2001; y <= 2007; y++) {
q = q * (1 + r / 100);
[Link]("Population in " + y + ": " + q);
}
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter population in the year 2000: ");
float x = [Link]();
[Link]("Enter growth rate: ");
float y = [Link]();
Population obj = new Population(x,y);
[Link]();
}
}
Question 7
Write a program in Java to find the roots of a quadratic equation
ax +bx+c=0 with the following specifications:
2
Class name — Quad
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is
the discriminant), r1 and r2 are the roots of the equation.
Member Methods:
1. quad(int x,int y,int z) — to initialize a=x, b=y, c=z, d=0
2. void calculate() — Find d=b -4ac2
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2a
import [Link];
public class Quad
{
private float a;
private float b;
private float c;
private float d;
private float r1;
private float r2;
public Quad(float x, float y, float z)
{
a = x;
b = y;
c = z;
d = 0;
}
public void calculate() {
d= (b * b) - (4 * a * c);
if (d < 0)
[Link]("Roots not possible");
else {
r1 = (float)((-b + [Link](d)) / (2 * a));
r2 = (float)((-b - [Link](d)) / (2 * a));
[Link]("r1=" + r1);
[Link]("r2=" + r2);
}
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
float x = [Link]();
[Link]("Enter b: ");
float y = [Link]();
[Link]("Enter z: ");
float z = [Link]();
Quad obj = new Quad(x,y,z);
[Link]();
}
}
Question 8
import [Link];
public class FruitJuice
{
private int product_code;
private String flavour;
private String pack_type;
private int pack_size;
private int product_price;
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
public void input() {
Scanner in = new Scanner([Link]);
[Link]("Enter Flavour: ");
flavour = [Link]();
[Link]("Enter Pack Type: ");
pack_type = [Link]();
[Link]("Enter Product Code: ");
product_code = [Link]();
[Link]("Enter Pack Size: ");
pack_size = [Link]();
[Link]("Enter Product Price: ");
product_price = [Link]();
}
public void discount() {
product_price -= 10;
}
public void display() {
[Link]("Product Code: " + product_code);
[Link]("Flavour: " + flavour);
[Link]("Pack Type: " + pack_type);
[Link]("Pack Size: " + pack_size);
[Link]("Product Price: " + product_price);
}
public static void main(String args[]) {
FruitJuice obj = new FruitJuice();
[Link]();
[Link]();
[Link]();
}
}
Question 9
import [Link];
public class Grade_Revision
{
private String name;
private int bas;
private int expn;
private double inc;
private double nbas;
public Grade_Revision() {
name = "";
bas = 0;
expn = 0;
inc = 0.0;
nbas = 0.0;
}
public void accept() {
Scanner in = new Scanner([Link]);
[Link]("Enter name: ");
name = [Link]();
[Link]("Enter basic: ");
bas = [Link]();
[Link]("Enter experience: ");
expn = [Link]();
}
public void increment() {
if (expn <= 3)
inc = 1000 + (bas * 0.1);
else if (expn <= 5)
inc = 3000 + (bas * 0.12);
else if (expn <= 10)
inc = 5000 + (bas * 0.15);
else
inc = 8000 + (bas * 0.2);
nbas = bas + inc;
}
public void display() {
[Link]("Name: " + name);
[Link]("Basic: " + bas);
[Link]("Experience: " + expn);
[Link]("Increment: " + inc);
[Link]("New Basic: " + nbas);
}
public static void main(String args[]) {
Grade_Revision obj = new Grade_Revision();
[Link]();
[Link]();
[Link]();
}
}
Question 10
import [Link];
public class Student
{
private String name;
private int mm;
private int scm;
private int comp;
public Student(String n, int m, int sc, int c) {
name = n;
mm = m;
scm = sc;
comp = c;
}
private String check() {
String course = "Not Eligible";
double avg = (mm + scm + comp) / 3.0;
if (mm >= 90 && scm >= 90 && comp >= 90)
course = "Science with Computer";
else if (avg >= 90)
course = "Bio-Science";
else if (avg >= 80)
course = "Science with Hindi";
return course;
}
public void display() {
String eligibility = check();
[Link]("Eligibility: " + eligibility);
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Name: ");
String n = [Link]();
[Link]("Enter Marks in Maths: ");
int maths = [Link]();
[Link]("Enter Marks in Science: ");
int sci = [Link]();
[Link]("Enter Marks in Computer: ");
int compSc = [Link]();
Student obj = new Student(n, maths, sci, compSc);
[Link]();
}
}
Question 11
import [Link];
public class Bill
{
private int bno;
private String name;
private int call;
private double amt;
public Bill() {
bno = 0;
name = "";
call = 0;
amt = 0.0;
}
public Bill(int bno, String name, int call) {
[Link] = bno;
[Link] = name;
[Link] = call;
}
public void calculate() {
double charge;
if (call <= 100)
charge = call * 0.6;
else if (call <= 200)
charge = 60 + ((call - 100) * 0.8);
else if (call <= 300)
charge = 60 + 80 + ((call - 200) * 1.2);
else
charge = 60 + 80 + 120 + ((call - 300) * 1.5);
amt = charge + 125;
}
public void display() {
[Link]("Bill No: " + bno);
[Link]("Name: " + name);
[Link]("Calls: " + call);
[Link]("Amount Payable: " + amt);
}
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Name: ");
String custName = [Link]();
[Link]("Enter Bill Number: ");
int billNum = [Link]();
[Link]("Enter Calls: ");
int numCalls = [Link]();
Bill obj = new Bill(billNum, custName, numCalls);
[Link]();
[Link]();
}
}
Question 12
import [Link];
public class BookFair
{
private String bname;
private double price;
public BookFair() {
bname = "";
price = 0.0;
}
public void input() {
Scanner in = new Scanner([Link]);
[Link]("Enter name of the book: ");
bname = [Link]();
[Link]("Enter price of the book: ");
price = [Link]();
}
public void calculate() {
double disc;
if (price <= 1000)
disc = price * 0.02;
else if (price <= 3000)
disc = price * 0.1;
else
disc = price * 0.15;
price -= disc;
}
public void display() {
[Link]("Book Name: " + bname);
[Link]("Price after discount: " + price);
}
public static void main(String args[]) {
BookFair obj = new BookFair();
[Link]();
[Link]();
[Link]();
}
}