1. Write a Program to create a class called as Calc. Make 2 Methods.
Method1 to
pass value by parameter. Method2 to add value and Method3 to subtract value.
class Calc
{
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;
}//end of method subtract
}//end of class
2. Write a program to create a class called Triangle. Make 2 Methods. Method 1 to
take base and height through parameters. Method 2 to find the area of the triangle.
class Triangle
{
int base, height;
public void setData(int b, int h)
{
base = b;
height = h;
} //end of setData method
public float area()
{
float ar= base* height /2;
return ar;
}//end of method
}//end of class
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;
}
}
5. Develop a class Student
- Instance variables – name, roll number, class, division, marks in 3 subjects
- Member methods to assign values to the instance variables and calculate
and print the total marks, percentage and grade given the following criteria :
Percentage Grade
>=85 Distinction
>=65 I Div
>=55 II Div
>=40 III Div
Below 40 Fail
*/
class Student
{
String n;
int r, c;
char d;
int m1, m2,m3;
int total; double percent; String grade;
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);
}
}
6. Develop a class for Employee.
Instance variables – employee number, name and basic salary
Member methods to assign values to the instance variables
and calculate and print the DA(55% of basic salary),
HRA(30% of (basic + DA) )
PF=10% of basic salary
Total salary = basic + DA + HRA-PF
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;
}
public void calculate()
{
da=basic*55/100;
hra=(basic+da)*30/100;
total=basic+da+hra-pf;
System.out.println("Name \t\tTotal\t\t DA\t\t HRA ");
System.out.println(name+"\t\t"+total+"\t\t"+ da+"\t\t"+hra);
}
}
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;
void setdata(double qn, double rate)
q=qn;
r=rate;
void calculate()
bill=q*r;
System.out.println("The bill amount is:"+bill);
}
8. Define the class Circle.
● Instance variable –radius
● Member methods to assign values to the instance variable and calculate and return
the area and circumference.*/
class Circle
float radius;
public void setData(float r)
radius=r;
} //end of setData method
public double area()
double ar= 3.14*radius*radius;
return ar;
public double circum()
double cir=2*3.14*radius;
return cir;
}//end of method
}//end of class
9. Bank Account
● Instance variables – account number, account type, name, current balance
● Methods to 1)assign values to the instance variables 2)update the balance
by the transaction amount. If the transaction is deposit the balance should be
increased whereas if it is withdrawal the balance should be reduced by the
transaction amount.3)print method to print the final details.
The transaction type (char) and amount should be passed as parameters to the
method.
class bank1
String an, nm;
char ty;
double current, ta;
void set_data(String acc_no, char type, String name)
an=acc_no;
ty=type;
nm=name;
void deposit(int amt)
current=current+amt;
}
void withdraw(int amt)
current=current-amt;
void print()
System.out.println("Name\tAcc. No.\tType\tCurrent bal");
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
articles. Write a program in Java to compute and display the discounts.
Take the price of an article as the input.
import java.util.*;
public class Discount
public static void main()
Scanner in = new Scanner(System.in);
System.out.print("Enter price ");
double price = in.nextDouble();
double d1 = price * 30 / 100.0;
double amt1 = price - d1;
System.out.println("30% discount = " + d1);
System.out.println("Amount after 30% discount = " + amt1);
double d2 = price * 20 / 100.0;
double amt2 = price - d2;
double d3 = amt2 * 10 / 100.0;
amt2 =amt2- d3;
System.out.println("20% discount = " + d2);
System.out.println("10% discount = " + d3);
System.out.println("Amount after successive discounts = " + amt2);
11. Write a program to input two unequal numbers. Display the numbers
after swapping their values in the variables without using a third variable.
Sample Input: a = 23, b = 56
Sample Output: a = 56, b = 23
class Swap
public static void main()
int a=23,b=56;
a =a+ b;
b = a - b;
a =a- b;
System.out.println("a = " + a + ", b = " + b);
}}