Programs March_ April
Programs March_ April
Method1 to
pass value by parameter. Method2 to add value and Method3 to subtract value.
class Calc
{
int ans;
3. Develop a class Calc and add methods to add, subtract, multiply, divide, obtain the
remainder and clear.
class Calc1
{
int ans;
public void setData(int n)
{ ans=n;
System.out.println(ans);
}
public int add(int n)
{ ans+=n;
return ans;
}//end of method add
public int subtract(int n)
{
ans-= n;
return ans;
}
public int multiply(int n)
{
ans*= n;
return ans;
}
public float divide(int n)
{
ans/= n;
return ans;
}
public float remainder(int n)
{
ans%= n;
return ans;
}
public void clear()
{
ans=0;
}
}//end of class
4. Develop a class for Rectangle that will contain the instance variables as length and
breadth and methods to calculate and return the area and perimeter. Create 3 objects using
the above class.
class Rectangle
{ int l, b;
float a,p;
public void getdata(int len, int br)
{
l=len;
b=br;
}
float area()
{
a=l*b;
return a;
}
float peri()
{
p=2*(l+b);
return p;
}
}
public void setData(String name, int roll, int cl, char div,int mk1, int mk2, int mk3 )
{
n=name;
r=roll;
c=cl;
d=div;
m1=mk1;
m2=mk2;
m3=mk3;
} //end of setData method
public void calculate()
{
total=m1+m2+m3;
percent=total/3;
if(percent>0&& percent<40)
{
grade="Fail";
}
else if(percent>=40&& percent<55)
{
grade="Third Division";
}
else if(percent>=55&& percent<65)
{
grade="Second Division";
}
else if(percent>=65&& percent<85)
{
grade="First Division";
}
else
{
grade="Distinction";
}
System.out.println("Name\t\tTotal marks\tPercent\t\tGrade");
System.out.println(n+"\t\t"+total+"\t\t"+percent+"\t\t"+grade);
}
}
class Employee
{
int eno; String name; double basic;
double da,hra,pf, total;
public void set_data(int n, String nm, double b)
{
eno=n;
name=nm;
basic=b;
}
}
}
7. Develop a class for bill. Your class should contain the fields quantity purchased and rate.
Write methods – to set data and to calculate the bill amount.
class bill
double q, r, bill;
q=qn;
r=rate;
void calculate()
bill=q*r;
● Member methods to assign values to the instance variable and calculate and return
the area and circumference.*/
class Circle
float radius;
radius=r;
return ar;
double cir=2*3.14*radius;
return cir;
}//end of method
}//end of class
9. Bank Account
The transaction type (char) and amount should be passed as parameters to the
method.
class bank1
char ty;
an=acc_no;
ty=type;
nm=name;
current=current+amt;
}
void withdraw(int amt)
current=current-amt;
void print()
System.out.println(nm+"\t"+an+"\t"+ty+"\t"+current);
10. A shopkeeper offers 30% discount on purchasing articles whereas the other
shopkeeper offers two successive discounts 20% and 10% for purchasing the same
import java.util.*;
11. Write a program to input two unequal numbers. Display the numbers
after swapping their values in the variables without using a third variable.
class Swap
int a=23,b=56;
a =a+ b;
b = a - b;
a =a- b;
}}