IcSe 2012 PrOgRaMmInG SoLuTiOn
Question 4
Define a class called Library with the following description:
Instance variables/data members:
Int acc_num – stores the accession number of the book
String title – stores the title of the book stores the name of the author
Member Methods:
(i)void input() – To input and store the accession number, title and author.
(ii)void compute – To accept the number of days late, calculate and display and fine charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member methods. [15]
Solution :
import
java.util.Scanner;
class
Library
{
Scanner S = new Scanner(System.in);
Scanner Sc= new Scanner(System.in);
int acc_num;
String title,author;
void input()
{
System.out.println("Enter the following credentials
:-");
System.out.println("Accession number :");
acc_num = S.nextInt();
System.out.println("Title :");
title = Sc.nextLine();
System.out.println("Author :");
author = Sc.nextLine();
}
void compute()
{
System.out.println("Enter the number of days late
:-");
int n = S.nextInt();
System.out.println("Fine to be charged :
"+(n*2));
}
void display()
{
System.out.println(" Accession Number
Title Author");
System.out.println(" "+acc_num+"
"+title+"
"+author);
}
void main()
{
input();
compute();
display();
}
}
Question 5
Given below is a hypothetical table showing rates of Income Tax for male citizens below the age of 65 years:
Taxable Income (TI) in RS. Income Tax in Rs.
Does not exceed Rs. 1,60,000 Nil
Is greater than Rs. 1,60,000 & less (TI – 1,60,000)*10%
Than or equal to Rs. 5,00,000 (TI – 1,60,000)*10%
Is greater than Rs. 5,00,000 &less [(TI – 5,00,000) * 20%]+3400
than or equal to Rs. 8,00,000 [(TI – 5,00,000) * 20%]+3400
Is greater than Rs. 8,00,000 [(TI – 800,000) * 30%]+94,000
Write a program to input the age, gender (male or female) and Taxable Income of a person.
If the age is more than 65 years or the gender is female, display “wrong category*.
If the age is less than or equal to 65 years and the gender is male, compute and display the Income Tax payable as per the table given above. [15]
Solution :
import
java.util.Scanner;
class
Question5
{
Scanner S = new Scanner(System.in);
String gender;
int age;
double taxinc;
double tax;
public void main()
{
System.out.println("Enter your gender, age and
taxable Income");
gender = S.next();
if(gender.equalsIgnoreCase("female"))
{
System.out.println("Not eligible for
Income Tax");
System.exit(0);
}
age = S.nextInt();
if(age>=65)
{
System.out.println("Not eligible for
Income Tax");
System.exit(0);
}
taxinc = S.nextDouble();
if(taxinc<=160000)
tax = 0.0;
else if(taxinc<=500000)
tax= (taxinc-160000)*0.1;
else if(taxinc<=800000)
tax = 34000+((taxinc-500000)*0.2);
else
tax = 94000 + ((taxinc-800000)*0.3);
System.out.println(" Tax Payable = "+tax);
}
}
Question 6
Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4 [15]
Solution :
import
java.util.Scanner;
class
Question6
{
Scanner S = new Scanner(System.in);
String s;
int nod=0;
void main()
{
System.out.println("Enter the String");
s = S.nextLine();
s = s.toUpperCase();
for(int i = 0;i<(s.length()-1);i++)
if(s.charAt(i)==s.charAt(i+1))
nod++;
System.out.println("No. of double letters =
"+nod);
}
}
Question 7.
Design a class to overload a function polygon() as follows:
(i)void polygon(int n, char ch) : with one integer argument and one character type argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol ‘@’
(iii)void polygon( ) : with no argument that draws a filled triangle shown below.
Example:
(i) Input value of n=2, ch=’O’
Output: OO
OO
(ii)Input value of x=2, y=5
Output: @@@@@
@@@@@
Output: *
**
*** [15]
Solution :
class Question7
{
void polygon()
{
for(int i = 1;i<=3;i++)
{
for(int
j=1;j<=i;j++)
System.out.print("*");
System.out.println();
}
}
void polygon(int x,int y)
{
for(int i = 1;i<=x;i++)
{
for(int j =
1;j<=y;j++)
System.out.print("@");
System.out.println();
}
}
void polygon(int n,char ch)
{
for(int i = 1;i<=n;i++)
{
for(int j =
1;j<=n;j++)
System.out.print(ch);
System.out.println();
}
}
}
Question 8
Using the switch statement, writw a menu driven program to:
(i)Generate and display the first 10 terms of the Fibonacci series 0,1,1,2,3,5 ……
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be displayed.[15]
Solution :
import java.util.Scanner;
class Question8
{
Scanner S = new Scanner(System.in);
void fibonacci(int size)//Array method
{
int fib[] = new int[size];
fib[0] = 0;
fib[1] = 1;
for(int i=2;i<size;i++)
fib[i] = fib[i-1]+fib[i-2];
for(int i = 0;i<size;i++)
System.out.print("
"+fib[i]+" ");
}
void sum(int x)//summing of digits method
{
int s = 0;
while(x>0)
{
s = s+(x%10);
x=x/10;
}
System.out.println("Sum of
digits = "+s);
}
void main()//main method
{
Question8 object = new
Question8();
System.out.println("Press 1
display the first 10 terms of the fibonacci series.");
System.out.println("Press 2
to enter a number and print the sum of its digits.");
System.out.println("Press
any othe key to exit.");
switch(S.nextInt())
{
case 1 :
object.fibonacci(10);
break;
case 2 :
System.out.println("Enter the number :-");
int no =
S.nextInt();
object.sum(no);
break;
default :
System.exit(0);
}
}
void fibonassi()//Alternate traditional method
{
int x=0,a=1,e=0;
System.out.print("0,1,");
for(int i=1;i<=8;i++)
{
e=x+a;
if(i<8)
System.out.print(e+",");
else
System.out.print(e);
x=a;
a=e;
}
}
}
Question 9
write a program to accept the names of 10 cities in a single dimension string array and their STD (Subscribers Trunk Dialing} codes in another single dimension integer array. Search for a name of a city input by the user in the list. If found, display “Search Successful” and print the name of the city along with its STD code, or else display the message “Search Unsuccessful, No such city in the list’. [15]
Solution :
import java.util.Scanner;
class Question9
{
Scanner S = new Scanner(System.in);
Scanner Sc = new Scanner(System.in);
String city[] = new String[10];
int std[] = new int[10];
String cityname = "";
void initialize()
{
for(int i = 0;i<10;i++)
{
city[i]="";
std[i]=0;
}
}
void input()
{
System.out.println("Enter
the cities and their STD codes :-");
for(int i = 0;i<10;i++)
{
System.out.println("Enter the name of "+(i+1)+" city and its STD
code");
city[i] =
Sc.nextLine();
std[i] =
S.nextInt();
}
}
void search()
{
System.out.println("Enter
the City to be searched :-");
cityname = Sc.nextLine();
for(int i = 0;i<10;i++)
{
if(city[i].equalsIgnoreCase(cityname))
{
System.out.println("Search Successful! \n City Name
:"+cityname+"\n STD
:"+std[i]);
System.exit(1);
}
}
System.out.println("Search
Unsuccessful, No such city in the list.");
}
void main()
{
initialize();
input();
search();
}
}
Download the printable PDF file >>HERE<<
----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x
import
java.util.Scanner;
class
Library
{
Scanner S = new Scanner(System.in);
Scanner Sc= new Scanner(System.in);
int acc_num;
String title,author;
void input()
{
System.out.println("Enter the following credentials
:-");
System.out.println("Accession number :");
acc_num = S.nextInt();
System.out.println("Title :");
title = Sc.nextLine();
System.out.println("Author :");
author = Sc.nextLine();
}
void compute()
{
System.out.println("Enter the number of days late
:-");
int n = S.nextInt();
System.out.println("Fine to be charged :
"+(n*2));
}
void display()
{
System.out.println(" Accession Number
Title Author");
System.out.println(" "+acc_num+"
"+title+"
"+author);
}
void main()
{
input();
compute();
display();
}
}
Given below is a hypothetical table showing rates of Income Tax for male citizens below the age of 65 years:
Taxable Income (TI) in RS. | Income Tax in Rs. |
Does not exceed Rs. 1,60,000 | Nil |
Is greater than Rs. 1,60,000 & less | (TI – 1,60,000)*10% |
Than or equal to Rs. 5,00,000 | (TI – 1,60,000)*10% |
Is greater than Rs. 5,00,000 &less | [(TI – 5,00,000) * 20%]+3400 |
than or equal to Rs. 8,00,000 | [(TI – 5,00,000) * 20%]+3400 |
Is greater than Rs. 8,00,000 | [(TI – 800,000) * 30%]+94,000 |
If the age is less than or equal to 65 years and the gender is male, compute and display the Income Tax payable as per the table given above. [15]
import
java.util.Scanner;
class
Question5
{
Scanner S = new Scanner(System.in);
String gender;
int age;
double taxinc;
double tax;
public void main()
{
System.out.println("Enter your gender, age and
taxable Income");
gender = S.next();
if(gender.equalsIgnoreCase("female"))
{
System.out.println("Not eligible for
Income Tax");
System.exit(0);
}
age = S.nextInt();
if(age>=65)
{
System.out.println("Not eligible for
Income Tax");
System.exit(0);
}
taxinc = S.nextDouble();
if(taxinc<=160000)
tax = 0.0;
else if(taxinc<=500000)
tax= (taxinc-160000)*0.1;
else if(taxinc<=800000)
tax = 34000+((taxinc-500000)*0.2);
else
tax = 94000 + ((taxinc-800000)*0.3);
System.out.println(" Tax Payable = "+tax);
}
}
import
java.util.Scanner;
class
Question6
{
Scanner S = new Scanner(System.in);
String s;
int nod=0;
void main()
{
System.out.println("Enter the String");
s = S.nextLine();
s = s.toUpperCase();
for(int i = 0;i<(s.length()-1);i++)
if(s.charAt(i)==s.charAt(i+1))
nod++;
System.out.println("No. of double letters =
"+nod);
}
}
Design a class to overload a function polygon() as follows:
(i)void polygon(int n, char ch) : with one integer argument and one character type argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol ‘@’
Output: OO
OO
(ii)Input value of x=2, y=5
Output: @@@@@
@@@@@
Output: *
**
*** [15]
class Question7
{
void polygon()
{
for(int i = 1;i<=3;i++)
{
for(int
j=1;j<=i;j++)
System.out.print("*");
System.out.println();
}
}
void polygon(int x,int y)
{
for(int i = 1;i<=x;i++)
{
for(int j =
1;j<=y;j++)
System.out.print("@");
System.out.println();
}
}
void polygon(int n,char ch)
{
for(int i = 1;i<=n;i++)
{
for(int j =
1;j<=n;j++)
System.out.print(ch);
System.out.println();
}
}
}
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be displayed.[15]
import java.util.Scanner;
class Question8
{
Scanner S = new Scanner(System.in);
void fibonacci(int size)//Array method
{
int fib[] = new int[size];
fib[0] = 0;
fib[1] = 1;
for(int i=2;i<size;i++)
fib[i] = fib[i-1]+fib[i-2];
for(int i = 0;i<size;i++)
System.out.print("
"+fib[i]+" ");
}
void sum(int x)//summing of digits method
{
int s = 0;
while(x>0)
{
s = s+(x%10);
x=x/10;
}
System.out.println("Sum of
digits = "+s);
}
void main()//main method
{
Question8 object = new
Question8();
System.out.println("Press 1
display the first 10 terms of the fibonacci series.");
System.out.println("Press 2
to enter a number and print the sum of its digits.");
System.out.println("Press
any othe key to exit.");
switch(S.nextInt())
{
case 1 :
object.fibonacci(10);
break;
case 2 :
System.out.println("Enter the number :-");
int no =
S.nextInt();
object.sum(no);
break;
default :
System.exit(0);
}
}
void fibonassi()//Alternate traditional method
{
int x=0,a=1,e=0;
System.out.print("0,1,");
for(int i=1;i<=8;i++)
{
e=x+a;
if(i<8)
System.out.print(e+",");
else
System.out.print(e);
x=a;
a=e;
}
}
}
import java.util.Scanner;
class Question9
{
Scanner S = new Scanner(System.in);
Scanner Sc = new Scanner(System.in);
String city[] = new String[10];
int std[] = new int[10];
String cityname = "";
void initialize()
{
for(int i = 0;i<10;i++)
{
city[i]="";
std[i]=0;
}
}
void input()
{
System.out.println("Enter
the cities and their STD codes :-");
for(int i = 0;i<10;i++)
{
System.out.println("Enter the name of "+(i+1)+" city and its STD
code");
city[i] =
Sc.nextLine();
std[i] =
S.nextInt();
}
}
void search()
{
System.out.println("Enter
the City to be searched :-");
cityname = Sc.nextLine();
for(int i = 0;i<10;i++)
{
if(city[i].equalsIgnoreCase(cityname))
{
System.out.println("Search Successful! \n City Name
:"+cityname+"\n STD
:"+std[i]);
System.exit(1);
}
}
System.out.println("Search
Unsuccessful, No such city in the list.");
}
void main()
{
initialize();
input();
search();
}
}
Download the printable PDF file >>HERE<<