Class XI Workbook Solution
Question 1
import java.util.Scanner;
class Library
{
String name, author;
double price, d, r, total_amt;
Library()
{
name = "";
d = 0.0;
r = 0.0;
total_amt = 0.0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter book, author's name, cost, no. of days taken to return");
name = sc.nextLine();
author = sc.nextLine();
price = sc.nextInt();
d = sc.nextInt();
}
void compute()
{
double noed = d-5;
if(noed <= 5)
r = 2 * noed;
else if(noed <= 10)
r = 3 * noed;
else
r = 5 * noed;
total_amt = (2/100.0 * price * d) + r;
}
void show()
{
System.out.println("Book Name: " + name);
System.out.println("Author Name: " + author);
System.out.println("Book Price: " + price);
System.out.println("Total No. Return Days : " + d);
System.out.println("Fine Amount: " + r);
System.out.println("Total Amount: " + total_amt);
}
static void main()
{
Library L = new Library();
L.accept();
L.compute();
L.show();
}
}
Question 2
class employee
{
String empname;
int empcode;
double basicpay;
employee()
{
empname = "";
empcode = 0;
basicpay = 0;
}
employee(String n, int p, double q)
{
empname = n;
empcode = p;
basicpay = q;
}
double salarycal()
{
double HRA = 30.0/100 * basicpay;
double DA = 40.0/100 * basicpay;
double salary = basicpay + HRA + DA;
double SA = 0.0;
if(empcode <= 15 && salary <= 15000)
SA = 20.0/100 * salary;
if(empcode > 15)
SA = 1000;
if(SA > 2500)
SA = 2500;
salary += SA;
return salary;
}
static void main()
{
employee e1 = new employee();
employee e2 = new employee("abcd", 14, 14000);
System.out.println("Salary: " + e1.salarycal());
System.out.println("Salary: " + e2.salarycal());
}
}
Question 3
class Emirp
{
int n, rev;
Emirp(int nn)
{
n = nn;
rev = 0;
}
int isPrime(int x)
{
int count = 0;
for(int i=1; i<=x; i++)
{
if(x % i == 0)
count++;
}
if(count == 2)
return 1;
else
return -1;
}
void isEmirp()
{
int t = n;
while(t>0)
{
int d = t%10;
rev = rev*10 + d;
t = t/10;
}
if(isPrime(n) == 1 && isPrime(rev) == 1)
System.out.println(n + " is an emirp no.");
else
System.out.println(n + " is not an emirp no.");
}
void main()
{
Emirp obj = new Emirp(13);
obj.isEmirp();
}
}
Question 4
class TheString
{
String str;
int len, wordcount, cons;
TheString()
{
str="";
len=0;
wordcount=0;
cons=0;
}
TheString(String ds)
{
str = ds;
}
void countFreq()
{
len = str.length();
str = str.toUpperCase();
for(int i=0; i<len; i++)
{
char ch = str.charAt(i);
if(ch == ' ')
wordcount++;
//if not space and not a vowel cons++
else if(ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' && ch != 'U')
cons++;
}
if(len > 0)
wordcount++;
}
void Display()
{
System.out.println("Original String: " + str);
System.out.println("Word Count: " + wordcount);
System.out.println("Consonant Count: " + cons);
}
static void main()
{
TheString obj1 = new TheString("This is a test");
obj1.countFreq();
obj1.Display();
}
}
Question 5
import java.util.Scanner;
class FiboString
{
String x, y, z;
int n;
FiboString()
{
x = "a";
y = "b";
z = "ba";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
n = sc.nextInt();
}
void generate()
{
System.out.print(x + ", " + y + ", " + z);
for(int i = 4; i<=n; i++)
{
x = y; //b
y = z; //ba
z = y + x;
System.out.print(", " + z);
}
}
static void main()
{
FiboString obj = new FiboString();
obj.accept();
obj.generate();
}
}