ICSE 2011 Section II Solution
Question 4
Define a class called mobike with the following description :-
Instance variables/data members:-
int bno- to store the bike’s number.
int phno – to store the phone number of the customer.
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental charge
Member methods:-
void input() – to input and store the details of the customer
void compute() – to compute the rental charge.
The rent for a mobile is charged on the following basis:-
First five days Rs. 500 per day.
Next five days Rs. 400 per day.
Rest of the days Rs. 200 per day.
void display() – to display the details in the following format:-
Bike No. Phone No. Name No.of days Charge [ 15]
Solution :
import
java.util.Scanner;
class
mobike
{
Scanner S = new Scanner(System.in);
int bno, phno, days=0, charge;
String name;
void input()
{
System.out.println("Enter name, bike number, phone
number, days");
name = S.next();
bno = S.nextInt();
phno = S.nextInt();
days = S.nextInt();
}
void computer()
{
if(days<=5)
charge = days*500;
if((days>5)&&(days<=10))
charge = 2500+((days-5)*400);
if(days>10)
charge = 2500+2000+((days-10)*200);
}
void display()
{
System.out.println("Bike No.\tPhoneNo.\tName\tNo. of
days\tCharge");
System.out.println(bno+"
\t"+phno+"\t"+name+"\t"+days+"\t"+charge);
}
void main()
{
input();
computer();
display();
}
}
Question 5
Write a program to input and store the weight of ten people. Sort and display them in descending order using the selection sort technique. [15]
Solution :
import
java.util.Scanner;
class
Question5
{
Scanner S = new Scanner(System.in);
String name[] = new String[10];
double weight[] = new double[10];
public void main()
{
System.out.println("Enter the name and weight of 10
people");
for(int i = 0;i<10;i++)
{
name[i] = S.next();
weight[i] = S.nextDouble();
}
double h;
int p=0;
for(int i=0;i<10;i++)
{
h=weight[i];
for(int j=i+1;j<10;j++)
{
if(weight[j]>h)
{
h=weight[j];
p=j;
}
}
weight[p]=weight[i];
weight[i]=h;
}
System.out.println(" Resultant Array : - ");
for(int i =0;i<10;i++)
System.out.println("Weight : "+weight[i]);
}
}
Question 6
Write a program to input a number and print whether the number is a special number or not. (A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number).
Example: 145 is a special number, because 1 !+4!+5!= 1 +24+ 120= 145
(Where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 51=1’2’3’4’5=120). [15]
Solution :
import
java.util.Scanner;
class
Question6
{
Scanner S = new Scanner(System.in);
public boolean isSpecial(int x)
{
int r = x;
int sum = 0;
while(x!=0)
{
int d = x%10;
int fact = 1;
for(int i = 1;i<=d;i++)
fact = fact*i;
sum = sum+fact ;
x=x/10;
}
if(sum==r)
return true;
else
return false;
}
public void main()
{
System.out.println("Enter the number to be checked
for Special Number");
int no = S.nextInt();
if(isSpecial(no))
System.out.println("It is a Special No.");
else
System.out.println("Not a Special No.");
}
}
Question 7
Write a program to accept a word and convert it into lowercase if it in uppercase, and display the new words by replacing only the vowels with the character following it.
Sample Input: computer
Sample Output: cpmpvtfr [15]
Solution :
import
java.util.Scanner;
class
Question7
{
Scanner S = new Scanner(System.in);
String str, ns="";
public void main()
{
System.out.println("Enter the word");
str = S.next();
str = str.toLowerCase();
for(int i = 0;i<str.length();i++)
{
char ch = str.charAt(i);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
ch = (char)((int)ch+1);
ns = ns +ch;
}
System.out.println("Output : "+ns);
}
}
Question 8
Design a class to overload a function compare() as follows:
(a) void compare(int, int)- to compare two integer values and print the greater of the two integers.
(b) void compare(char, char) – to compare the numeric value of two characters and print the character with higher numeric value.
(c) void compare(String, String ) – to compare the length of the two strings and print the longer of the two. [15]
Solution :
class
Question8
{
void compare(int x,int y)
{
int max;
if(x>y)
max = x;
else if(y>x)
max = y;
else max = x;
System.out.println(max);
}
void compare(char a,char b)
{
char max;
int x = (int)a;
int y = (int)b;
if(x>y)
max = (char)x;
else if(y>x)
max = (char)y;
else max = (char)x;
System.out.println(max);
}
void compare(String s1,String s2)
{
String max="";
int x = s1.length();
int y = s2.length();
if(x>y)
max = s1;
else if(y>x)
max = s2;
else System.out.println("both have same
length!");
System.out.println(max);
}
}
Question 9
Write a menu driven program to perform the following: (Use switch – case statement)
(a) To print the series 0, 3, 8, 15, 24 ….. n terms (vlaue of ‘n’ is to be an input by the user.)
(b) To find the sum of the series given below :-
S = 1/2 + 3/4 + 5/6 + 7/8 …………….. 19/20. [15]
Solution :
import
java.util.Scanner;
class
Question9
{
Scanner S= new Scanner(System.in);
void series(int x)
{
System.out.print("0");
for(int i = 2;(Math.pow(i,2)-1)<=x;i++)
System.out.print(","+((int)Math.pow(i,2)-1));
}
void sum()
{
double s = 0;
for(int i= 2;i<=20;i+=2)
{
s=s+((i-1)/i);
System.out.println(s+""+i);
}
}
}
----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x