For instance, take section B, you midght have noticed one question will be asked from each chapter.So try atleast TWO programs from each chapter.Let me give you some examples, but you have to write yourself.
Let us do programs in if else:
Example 1:
A library charges a fine for books returned late as given below
first five days 40 paise per day
six to ten days 65 paise per day
above ten days 80 paise per day
Design a program with relevant supporting material to calculate the fine assuming that a book is returned N days late. [ICSE 1996]
import java.util.*;
class fine
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of days");
int d=s.nextInt();-------------------------------------------1
float c;
if(d >=1 && d<=5)------------------------------------------2
c=d*0.40;---------------------------------------------------3
else if(d>=6 && d<=10)------------------------------------4
c=5*0.40 + (d-5)*0.65;-----------------------------------5
else
c=5*0.40 + 5*0.65 +(d-10)*0.80;-----------------------6
System.out.print("Fine="+c);
}
}
step 1 : Accept number of days
step 2: if the days are from 1 to 5 then
step 3: calculate the fine. Supposing if it is 3 days then 3 *0.40.
step 4 : if the days are from 6 to 10
step 5 : this case is above 5 days , therefore calculate for first five days and add the remaining.
step 6 :this is above 10 days,therefore calculate for the first five then the next five finally add the remaining.
Example 2:
Write a program to find the income tax to be paid for the following conditions.
Annual salary Rate of income tax
upto Rs.1,00,000 No tax
Rs.1,00,000 to 1,50,000 10% of the amount exceeding 1,00,000
Rs.1,50,000 to 2,50,000 Rs.5,000+ 20% of amount exceeding Rs.1,50,000Above Rs.2,50,000 Rs.25,000+30% of amount exceeding Rs.2,50,000
import java.util.*;
class fine
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the salary");
int n=s.nextInt();
float amt;
if(n>=1 && n<=1000000)
amt=0;
else if(n>100000 && n<150000)
amt=(10/100)*(n-100000);
else if (n>150000 && n<=250000)
amt=5000+(20/100)*(n-150000);
else
amt=25000+(30/100)*(n-250000);
System.out.print("tax to be paid="+amt);
}
}
Switch :
Write a menu driven program to calculate the area of the following :
circle - p*r*r [p=3.14]
square - side * side
rectangle - length * breadth
Display the menu to output the area as per the user's choice.
import java.util.*;
class fine
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("1.circle");
System.out.println("2.square");
System.out.println("3.rectangle");
System.out.println("Enter your choice");
int ch=s.nextInt();
switch(ch)
{
case 1:
int r,ac;
System.out.print("Enter radius");
r=s.nextInt();
ac=3.14f*r*r;
System.out.println("Area of circle="+ac);
break;
case 2:
int side,asq;
System.out.print("Enter side");
side=s.nextInt();
asq=side*side;
System.out.println("Area of square="+asq);
break;
case 3:
int l,b,ar;
System.out.print("Enter length and breadth");
l=s.nextInt();
b=s.nextInt();
ar=l*b;
System.out.println("Area of rectangle="+ar);
break;
default :
System.out.printI("nvalid choice");
}
}
}
Loops :
Example 1:
Write a program to input a number and check if it is a palindrome number or not.
A number is said to be Palindrome , if the original number is equal to the reverse number.
class palindrome
{
public static void main(int n)
{
int m=n;--------------------------------------1
int a,s=0;
while(n>0)-----------------------------------2
{
a=n%10;------------------------------------3
s=(s*10)+a;--------------------------------4
n=n/10;------------------------------------5
}
if(s==m)------------------------------------6
System.out.println("Palindrome number");
else
System.out.println("Not a Palindrome number");
}
}
step 1: It is important.There should be a copy of the original number,coz we are going to separate the digit from the number.
step 2:The condition here is n >0 for the reason explained in step1.
step 3: It gives the remainder that is the last digit.
step 4:reverse the number
step 5:Find the quotient
step 6: Check the sum and the copy are same.
Example 2:
Armstrong number.
e.g : 153= 13+53+33
=153
class atrmstrong
{
public static void main(int n)
{
int m=n;
int a,s;
while(n>0)
{
a=n%10;
s=s+(a*a*a);--------------------------------1
n=n/10;
}
if(s==m)
System.out.println("Armstrong number");
else
System.out.println("Not an Armstrong number");
}
}
only one change in step 1.
Example 3:
Perfect Number :
e.g : 6 the divisors are 1,2,3 and 6.Donot include 6 and add the others it should be equal the original number.
class perfect
{
public static void main(int n)
{
int i,s=0;
for(i=1;i<n;i++)-------------------------------------1
{
if(n%i==0)-----------------------------------------2
s=s+i;
}
if(s==n)
System.out.println("Perfect number");
else
System.out.println("Not a perfect number");
}
}
step 1: the loop should be < n and not <=n
step 2: divide the number from 1 to less than the number, if the remainder is zero then it is a divisor.
Example 4:
Prime number
class prime
{
public static void main(int n)
{
int i,ctr=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
ctr=ctr+1;
}
if(ctr==2)
System.out.println("Prime number");
else
System.out.println("Not a prime number");
}
}
There are different logics. What i used here is, find the number of divisors,if it is 2 i.e 1 and number itself ,then print prime number
Methods or Functions:
An automobile company has increased the cost of its vehicles as per the type of the engine using the following criteria :
Type of engine Rate of increment
2 strokes 10% of the cost
4 strokes 12% of the cost
WAP to use a class to find the new cost as per the given specifications.
Class name : Auto
Data members : int type(to accept type of engine 2 stroke or 4 stroke)
int cost(to accept previous cost)
Member methods :
void get() : accept type of engine
void find() : find the new cost
void print() : display type and new cost
import java.util.*; class Auto
{
int type,cost,rate;
void get(int t,int c)
{
type=t;
cost=t;
}
void find()
{
if(type==2)
rate=(10/100)*cost+cost;
else
rate=(12/100)*cost+cost;
}
void print()
{
System.out.println("Type="+type);
System.out.println("New Cost="+rate);
}
public static void main()
{
Auto ob=new Auto();
{
Auto ob=new Auto();
Scanner s=new Scanner(System.in);
System.out.println("Enter the type and cost");
System.out.println("Enter the type and cost");
int t=s.nextInt();
int c=s.nextInt();
ob.get(t,c);
ob.find();
ob.print();
}
}
* DONOT forget to write variable description.
* Read the questions properly.Use the reading time wisely.
* Check the syntax.
* variables should not have space
* There is no semicolon after while,for,if,method,constructor
* If you dont know how to explain the sec A questions , atleast give one example.
That is all I could post today. I will give section A questions in the coming days .All the best.
No comments:
Post a Comment