Take a sentence from user and replace a specific character from the sentence with another specific character- both are supplied by the user.
import java.io.*;
class Replace
{
String sent,mod=" ";char orig,rep,ch;int l;
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
public void take()throws IOException
{
System.out.print("Enter the sentence:");
sent=r.readLine();
System.out.print("Enter the character to be replaced:");
orig=(char)r.read();
r=null;
r=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the character with which it is to be replaced:");
rep=(char)r.read();
}
public void replace()
{
l=sent.length();
for(int i=0;i< l ;i++)
{
ch=sent.charAt(i);
if(ch==orig)
mod=mod+rep;
else
mod=mod+ch;
}
mod=mod.trim();
System.out.println("The Modifyed sentence is:"+mod);
}
public static void main(String args[])throws IOException
{
Replace ob= new Replace();
ob.take();
ob.replace();
}
}
A customer purchase a computer from a store. It may be laptop or desktop. Discount is offered against the purchase amount and the rate of discount varies with type of purchase (Laptop or Desktop). The discount rate as follows;
Purchase Amount Laptop Desktop
upto 10000 0.0% 5.0%
10001 to 20000 5.0% 7.5%
20001 to 35000 7.5% 10.0%
above 35000 10.0% 15.0%
Take the purchase amount, type of purchase and display the payable amount by the customer
import java.io.*;
class Purchase
{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
String t;
double pr,disc,p;
public void take()throws IOException
{
System.out.print("Enter the type of computer(Laptop/Desktop):");
t=r.readLine();
System.out.print("Enter the price range:");
pr=Double.parseDouble(r.readLine());
}
public void price()
{
char ch=t.charAt(0);
if(ch=='L' || ch=='l')
{
if(pr<=10000)
{
p=pr;
}
else if(pr< =20000)
{
disc=5.00;
p=(disc/100)*pr;
}
else if(pr< =35000)
{
disc=7.50;
p=(disc/100)*pr;
}
else if(pr >35000)
{
disc=10.00;
p=(disc/100)*pr;
}
}
else if(ch=='D' || ch=='d')
{
if(pr< =10000)
{
disc=5.00;
p=(disc/100)*pr;
}
else if(pr< =20000)
{
disc=7.50;
p=(disc/100)*pr;
}
else if(pr< =35000)
{
disc=10.00;
p=(disc/100)*pr;
}
else if(pr >35000)
{
disc=15.00;
p=(disc/100)*pr;
}
}
}
public void disp()
{
System.out.println("The total price is:"+(pr-p));
}
public static void main(String args[])throws IOException
{
Purchase ob;
ob=new Purchase();
ob.take();
ob.price();
ob.disp();
}
}
import java io *;
class My int
{
public static void main (string arg[])
{
bufferdReader br;
string str;
int a,b,c;
booiean bool=true;
br=new BufferedReader(new inhputstreamReader(system.io) );
while(bool)
{
system.out.println("1st no:");
try
{
str=br.readline();
a=interger.passInt(str);
bool=falose
} catch(Exception e)
{
system.out println("wrong enter");
}
}
bool=true;
while(bool)
{
system.out.printline("2nd no");
try
{
b=Interger.parseInt(br.readline());
bool=false;
}
catch(exception e)
{
system.out.println("wrong enter");
}
c=a+b;
system.out.println("sum="+c);
}
}
The following public function is a part some class. Assume ‘n’ is always positive. Answer the given question.
Give the dry run/working
int unknown(int n)
{
int i,k;
if(n%2==0)
{
i=n/2;
k=1;
}
else
{
k=n;
n--;
i=n/2;
}
while(I >0)
{
k=k*i*n;
i--;
n--;
}
return k;
}
(i) what value will be returned by the expression unknown(7) ?
Ans: Returned value will be 5040
(ii)what value will be returned by the expression unknown(6) ?
Ans: Returned value will be 720
(iii) In a line state what is being computed by the function unknown(n) ?
Ans : This function is used to calculate the factorial value of the number passed as argument to function int unknown()
Here in the function the value passed as argument is first checked whether it is even or odd. If the number is even then value/2 is assigned on ‘i’ and another variable ‘k’ is initialized with 1. In case the number is odd then ‘k’ is initialized with the number and the number , here ‘n’ is assigned the nearest even number and again ‘I’ is assigned with n/2. This ensures that ‘i’ is assigned with the middle value of the number (n).
Next the while loop multiple k,I and n until I becomes 0 and the value is stored in k. within the loop body valie of i and n is decremented by 1 in each iteration.
Dry run for 7:
k=7
i=3
n=6
While(i>0)
(
k=k*i*n;// means 7*6*3 in first iteration
i--;// I becomes 2
n--;// n becomes 5
}
in the second iteration k becomes 7*6*3*2*5
and I becomes 1 and n becomes 4 so in the last iteration k becomes 7*6*3*2*5*1*4 and the loop breaks as I becomes 0.
A public function has been designed to do a binary search for the integer ‘m’ in the sorted integer array data. The array is sorted in ascending order. If ‘m’ is present in the array it returns ‘1’ otherwise returns ‘0’. There are five places in the code marked with ?1?, ?2?, ?3?,?4?,?5? which should be replaced with expression or statement so that the program works properly.
int data (int m, int data[], int size)
{
/* int m is the value to search, int data[] is the array of integers stored in ascending order and int size is the size of the array.*/
Int found;
Int j=0,k=?1?;
If(?2?)
{
while(?3?)
{
int i=(j+k)/2;
if(m==data[i])
{
?4?;
break;
}
else
{
if(?5?)
j=i+1;
else
k=i-1;
}
}
}
return found;
}
Ans : k=size-1
What is the expression/ statement ?2?
Ans: found==0
What is the expression/ statement ?3?
Ans: j<=kWhat is the expression/ statement ?4?Ans : flag=1;
What is the expression/ statement ?5?
Ans: m>data[i]
This page is on icse sample papers . If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.
No comments:
Post a Comment