Thursday, July 17, 2008

comp projects

Q1) Write a program to accept three numbers and find the greatest number between them.
A) class numbers
{
private int a;
private int b; // data members
private int c;
public numbers () // default constructor
{
a=0;
b=0;
c=0;
}
public numbers (int x, int y, int z) // parametrised constructor
{
a=x;
b=y;
c=z;
}
public void check () // pure or accessor function
{
int max=a;
if (b>a)
max=b;
if (c>max)
max=c;
System.out.println (max+" is the greatest number");
} // close check
} // close class

public class numbers1
{
public static void main ()
{
numbers n1=new numbers (10,20,11);
n1.check (); // to call the functions on the objects created
numbers n2=new numbers (20,30,10);
n2.check ();
} // close main
} // close class
Output
20 is the greatest number

30 is the greatest number

Q2) Write a program to input four numbers from the user and find the sum of the first two and difference of the last two.If the sum is greater than difference print “OK” else print “Cancel”.

A) class sumdif
{
private int a;
private int b; // data members
private int c;
private int d;
public sumdif () // default constructor
{
a=0;
b=0;
c=0;
d=0;
}
public sumdif (int w, int x, int y, int z) // parametrised constructor
{
a=w;
b=x;
c=y;
d=z;
}
public void check () // pure or accessor function
{
int sum=a+b;
int dif=c-d;
if (sum>dif)
System.out.println ("OK");
else
System.out.println ("CANCEL");
} // close check
} // close class

public class sumdiff
{
public static void main ()
{
sumdif s1=new sumdif (10,20,40,30);
sumdif s2=new sumdif (100,200,400,300);
s1.check (); // to call the functions on the objects created
s2.check ();
} // close main
} // close class

Output
OK

OK

Q3) Write a program to accept 3 numbers from the user and find the smallest between them and print its square.

A) class small
{
private int a;
private int b;
private int c; // data members
public small () // default constructor
{
a=0;
b=0;
c=0;
}
public small (int p,int q,int r) // parametrised constructor
{
a=p;
b=q;
c=r;
}
public void check () // pure or accessor function
{
int min=a;
if (b min=c;
System.out.println (min+" is the smallest number");
double sq=min*min;
System.out.println (sq+" is the square of the smallest number");
} // close check
} // close class

public class small2
{
public static void main ()
{
small s1=new small (10,20,40);
small s2=new small (100,200,400);
s1.check (); // to call the functions on the objects created
s2.check ();
} // close main
} // close class

Output
10 is the smallest number

100.0 is the square of the smallest number

100 is the smallest number

10000.0 is the square of the smallest number

Q4) Write a program to accept 3 numbers and check the greatest among them

A) class greatest
{
private int a;
private int b;
private int c; // data members
public greatest () // default constructor
{
a=0;
b=0;
c=0;
}
public greatest (int x,int y,int z) // parametrised constructor
{
a=x;
b=y;
c=z;
}
public void check () // pure or accessor function
{
if (a>b&&a>c)
System.out.println (a+" is the greatest number");
if (b>a&&b>c)
System.out.println (b+" is the greatest number");
if (c>a&&c>b)
System.out.println (c+" is the greatest number");
} // close check
} // close class

public class greatest3
{
public static void main ()
{
greatest g1=new greatest (1,2,3);
greatest g2=new greatest (10,20,30);
g1.check (); // to call the functions on the objects created
g2.check ();
} // close main
} // close class

Output
3 is the greatest number

30 is the greatest number

Q5) Write a program to accept the number and check whether it is EVEN or ODD.

A) class number
{
private int n; // data member
public number () // default constructor
{
n=0;
}
public number (int b) // parametrised constructor
{
n=b;
}
public void check () // pure or accessor function
{
if (n%2==0)
System.out.println (n+" is an even number");
else
System.out.println (n+ " is an odd number");
} // close check
} // close class

public class number4
{
public static void main ()
{
number n1=new number (20);
number n2=new number (21);
n1.check (); // to call the functions on the objects created
n2.check ();
} // close main
} // close class

Output
20 is an even number

21 is an odd number

Q6) Write a program to accept the marks of student in 5 subjects and calculate percentage and display grade
Percentage Grade
<40 F
40-50 E
51-60 D
61-70 C
71-80 B
81-90 A
>90 A+

A) class grade
{
private float maths;
private float comp;
private float science; // data members
private float social;
private float eng;
public grade () // default constructor
{
maths=0;
comp=0;
science=0;
social=0;
eng=0;
}
public grade (float m, float c, float s, float o, float e) // parametrised constructor
{
maths=m;
comp=c;
science=s;
social=o;
eng=e;
}
public void print () // pure or accessor function
{
double percent=(maths+science+social+comp+eng)/5;
if (percent<40)
System.out.println ("Grade is F");
else if (percent>=40&&percent<=50)
System.out.println ("Grade is E");
else if (percent>50&&percent<=60)
System.out.println ("Grade is D");
else if (percent>60&&percent<=70)
System.out.println ("Grade is C");
else if (percent>70&&percent<=80)
System.out.println ("Grade is B");
else if (percent>80&&percent<=90)
System.out.println ("Grade is A");
else
System.out.println ("Grade is A+");
} // close print
} // close class

public class grade1
{
public static void main ()
{
grade g1=new grade (95,96,89,86,91);
grade g2=new grade (97,94,88,82,93);
g1.print (); // to call the functions on the objects created
g2.print ();
} // close main
} // close class

Output
Grade is A+

Grade is A+

Q7) A library charges fine from its users by collecting fine in the following manner.Now design a program to input the number of days is given rate and calculate the fine.
Days Fine
<5 2
5-10 3
11-15 4
16-20 5
21-25 6
26-30 10

A) class library
{
private int days; // data member
public library () // default constructor
{
days=0;
}
public library (int d) // parametrised constructor
{
days=d;
}
public void calc () // pure or accessor function
{
double fine;
if (days<5)
fine=2*days;
else if (days>=5&&days<=10)
fine=8+(days-4)*3;
else if (days>=11&&days<=15)
fine=8+15+(days-10)*4;
else if (days>=16&&days<=20)
fine=8+15+20+(days-15)*5;
else if (days>=21&&days<=25)
fine=8+15+20+25+(days-20)*6;
else
fine=8+15+20+25+30+(days-25)*10;
System.out.println ("The fine is equal to "+fine);
} // close calc
} // close class

public class library1
{
public static void main ()
{
library l1=new library (20);
library l2=new library (15);
l1.calc (); // to call the functions on the objects created
l2.calc();
} // close main
} // close class

Output
The fine is equal to 68.0

The fine is equal to 43.0

Q8) A Private electricity distributing charges bill from its users in the following format.
No. of units Amount
<=100 Free
101-300 Rs. 1
301-500 Rs. 2
>500 Rs. 3

A) class electricity
{
private int nou; // data member
public electricity () // default constructor
{
nou=0;
}
public electricity (int n) // parametrised constructor
{
nou=n;
}
public void calc () // pure or accessor function
{
double amt;
if (nou<=100)
amt=0;
else if (nou>100&&nou<=300)
amt=(nou-100)*1;
else if (nou>300&&nou<=500)
amt=200+(nou-300)*2;
else
amt=600+(nou-500)*3;
System.out.println ("The final amount is equal to "+amt);
} // close calc
} // close class

public class electricity1
{
public static void main ()
{
electricity e1=new electricity (200);
electricity e2=new electricity (144);
e1.calc (); // to call the functions on the objects created
e2.calc ();
} // close main
} // close class

Output
The final amount is equal to 100.0

The final amount is equal to 44.0

Q9) Write a program to check if a given year is leap year or not.

A) class year
{
private int year; // data member
public year () // default constructor
{
year=0;
}
public year (int y) // parametrised constructor
{
year=y;
}
public void check () // pure or accessor function
{
if (year%4==0&&year%100==0)
System.out.println (year+" is a leap year");
else
System.out.println (year+" is not a leap year");
} // close check
} // close class

public class year3
{
public static void main ()
{
year y1=new year (1900);
year y2=new year (1947);
y1.check (); // to call the functions on the objects created
y2.check ();
} // close main
} // close class

Output
1900 is a leap year

1947 is not a leap year

Q10) Write a program to accept day number and print the day of the week in words if a number is >7 print invalid day number.

A) class day
{
private int dayno; // data member
public day () // default constructor
{
dayno=0;
}
public day (int a) // parametrised constructor
{
dayno=a;
}
public void print () // pure or accessor function
{
if (dayno==1)
System.out.println ("Sunday");
else if (dayno==2)
System.out.println ("Monday");
else if (dayno==3)
System.out.println ("Tuesday");
else if (dayno==4)
System.out.println ("Wednesday");
else if (dayno==5)
System.out.println ("Thursday");
else if (dayno==6)
System.out.println ("Friday");
else if (dayno==7)
System.out.println ("Saturday");
else
System.out.println ("Invalid");
} // close print
} // close class

public class day3
{
public static void main ()
{
day d1=new day (9);
day d2=new day (5);
d1.print (); // to call the functions on the objects created
d2.print ();
} // close main
} // close class

Output
Invalid

Thursday


Q11) Write a program to print any string n times.

A) class str
{
private String x; // data members
private int n;
public str () // default constructor
{
x=null;
n=0;
}
public str (String a, int b) // parametrised constructor
{
x=a;
n=b;
}
public void print () // pure or accessor function
{
for (int i=1; i<=n; i++) // for loop
{
System.out.println ("The word is "+x);
} // close for loop
} // close print
} // close class

public class string
{
public static void main ()
{
str s1=new str ("Dee", 20);
str s2=new str ("pak", 36);
s1.print (); // to call the functions on the objects created
s2.print ();
} // close main
} // close class

Output
The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is Dee

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

The word is pak

Q12) Write a program to find the sum of first n natural numbers.

A) class natural
{
private int n; // data member
public natural () // default constructor
{
n=0;
}
public natural (int a) // parametrised constructor
{
n=a;
}
public void sum () // pure or accessor function
{
int sum=0;
for (int i=0; i<=n; i++) // for loop
{
sum+=i;
} // close for loop
System.out.println ("The sum of first "+n+" natural numbers is "+sum);
} // close sum
} // close class

public class natural1
{
public static void main ()
{
natural n1=new natural (10);
natural n2=new natural (43);
n1.sum (); // to call the functions on the objects created
n2.sum ();
} // close main
} // close class

Output
The sum of first 10 natural numbers is 55

The sum of first 43 natural numbers is 946

Q13) Write a program to find the sum of the squares of the first n natural numbers.

A) class square
{
private int n; // data member
public square () // default constructor
{
n=0;
}
public square (int a) // parametrised constructor
{
n=a;
}
public void sum () // pure or accessor function
{
int sum=0;
for (int i=0; i<=n; i++) // for loop
{
sum+=i*i;
} // close for loop
System.out.println ("The sum of first "+n+"natural numbers is "+sum);
} // close sum
} //close class

public class square2
{
public static void main ()
{
square s1=new square (90);
square s2=new square (65);
s1.sum (); // to call the functions on the objects created
s2.sum ();
} // close main
} // close class

Output
The sum of first 90natural numbers is 247065

The sum of first 65natural numbers is 93665




Q14) Write a program to print odd numbers less than n.

A) class odd
{
private int n; // data member
public odd () // default constructor
{
n=0;
}
public odd (int x) // parametrised constructor
{
n=x;
}
public void calc () // pure or accessor function
{
for (int i=1; i {
System.out.println ("The first "+n+" odd numbers are "+i);
} // close for loop
} // close calc
} // close class

public class odds
{
public static void main ()
{
odd o1=new odd (54);
odd o2=new odd (12);
o1.calc (); // to call the functions on the objects created
o2.calc ();
} // close main
} // close class

Output
The first 54 odd numbers are 1

The first 54 odd numbers are 3

The first 54 odd numbers are 5

The first 54 odd numbers are 7

The first 54 odd numbers are 9

The first 54 odd numbers are 11

The first 54 odd numbers are 13

The first 54 odd numbers are 15

The first 54 odd numbers are 17

The first 54 odd numbers are 19

The first 54 odd numbers are 21

The first 54 odd numbers are 23

The first 54 odd numbers are 25

The first 54 odd numbers are 27

The first 54 odd numbers are 29

The first 54 odd numbers are 31

The first 54 odd numbers are 33

The first 54 odd numbers are 35

The first 54 odd numbers are 37

The first 54 odd numbers are 39

The first 54 odd numbers are 41

The first 54 odd numbers are 43

The first 54 odd numbers are 45

The first 54 odd numbers are 47

The first 54 odd numbers are 49

The first 54 odd numbers are 51

The first 54 odd numbers are 53

The first 12 odd numbers are 1

The first 12 odd numbers are 3

The first 12 odd numbers are 5

The first 12 odd numbers are 7

The first 12 odd numbers are 9

The first 12 odd numbers are 11

Q15) To print the multiplication table for any given number upto 20.

A) class multiply
{
private int n; // data member
public multiply () // default constructor
{
n=0;
}
public multiply (int a) // parametrised constructor
{
n=a;
}
public void calc () // pure or accessor function
{
for (int i=1; i<=20; i++) // for loop
{
double x=i*n;
System.out.println (n+"*"+i+"="+x);
} // close for loop
} // close calc
} // close class

public class multiplicationtable
{
public static void main ()
{
multiply m1=new multiply (20);
multiply m2=new multiply (87);
m1.calc (); // to call the functions on the objects created
m2.calc ();
} // close main
} // close class

Output
20*1=20.0

20*2=40.0

20*3=60.0

20*4=80.0

20*5=100.0

20*6=120.0

20*7=140.0

20*8=160.0

20*9=180.0

20*10=200.0

20*11=220.0

20*12=240.0

20*13=260.0

20*14=280.0

20*15=300.0

20*16=320.0

20*17=340.0

20*18=360.0

20*19=380.0

20*20=400.0

87*1=87.0

87*2=174.0

87*3=261.0

87*4=348.0

87*5=435.0

87*6=522.0

87*7=609.0

87*8=696.0

87*9=783.0

87*10=870.0

87*11=957.0

87*12=1044.0

87*13=1131.0

87*14=1218.0

87*15=1305.0

87*16=1392.0

87*17=1479.0

87*18=1566.0

87*19=1653.0

87*20=1740.0

Q16) Print even numbers less than 1000.

A) class even
{
public void print () // pure or accessor function
{
for (int i=1; i<1000; i++) // for loop
{
if (i%2==0)
{
System.out.println ("The first 1000 even numbers are "+i);
}
} // close for loop
} // close print
} // close class

public class evennumbers
{
public static void main ()
{
even e1=new even ();
e1.print (); // to call the functions on the objects created
} // close main
} // close class

Output
The first 1000 even numbers are 920

The first 1000 even numbers are 922

The first 1000 even numbers are 924

The first 1000 even numbers are 926

The first 1000 even numbers are 928

The first 1000 even numbers are 930

The first 1000 even numbers are 932

The first 1000 even numbers are 934

The first 1000 even numbers are 936

The first 1000 even numbers are 938

The first 1000 even numbers are 940

The first 1000 even numbers are 942

The first 1000 even numbers are 944

The first 1000 even numbers are 946

The first 1000 even numbers are 948

The first 1000 even numbers are 950

The first 1000 even numbers are 952

The first 1000 even numbers are 954

The first 1000 even numbers are 956

The first 1000 even numbers are 958

The first 1000 even numbers are 960

The first 1000 even numbers are 962

The first 1000 even numbers are 964

The first 1000 even numbers are 966

The first 1000 even numbers are 968

The first 1000 even numbers are 970

The first 1000 even numbers are 972

The first 1000 even numbers are 974

The first 1000 even numbers are 976

The first 1000 even numbers are 978

The first 1000 even numbers are 980

The first 1000 even numbers are 982

The first 1000 even numbers are 984

The first 1000 even numbers are 986

The first 1000 even numbers are 988

The first 1000 even numbers are 990

The first 1000 even numbers are 992

The first 1000 even numbers are 994

The first 1000 even numbers are 996

The first 1000 even numbers are 998