1.
Given that the distance between any two points on a plane is
calculated using the formula :
![]()
(x2 – x1)2 + (y2 – y1)2
where x1,y1 denote coordinates of the first point & x2,y2
denote coordinates of the second point.
Create a class that will implement the following:
Class name : Point
Instance variables : double x,y (denote the coordinates of current Point
object)
Methods :
public Point () constructer
public void getPoint(double i,double j) initializes coordinates to values of i & j
public void show() displays
coordinates of current Point object
public double giveDist(Point p) computes
& returns the distance between current Point object & p
2.
12 eggs make a dozen & 144 eggs
make a gross.
Thus if total number of eggs is 200 , it can be subdivided into
:
1 gross , 4 dozen
& 8 remaining.
Similarly , if total no. of eggs is 30 , it can be subdivided
into :
0 gross , 2 dozen
& 6 remaining.
Using the above information create a class as follows :
class name : EggCounter
Instance variables : int totcount (denotes the total no. of eggs )
Methods :
public EggCounter() constructer
public void getTotal(int x) initializes
totcount
public void compute() calculates
& displays how many gross , dozen & remaining
public void showTotal() displays
value of totcount
public void findTot(int gr,int doz,int rem) does the opposite of “public void compute()”
i.e.
accepts the total no. of gross(gr) , dozen(doz) & remaining
(rem)
& accordingly updates totcount.
3.
Task : To compare any
two Fraction objects & determine the greater of the two.
Class Name : Fraction
Instance variables : num
, den (both int)
Methods :
public Fraction() default
constructer
public Fraction(int i,int j) parameterized
constructer
public void show() displays
the greater Fraction object.
public Fraction giveGreater(Fraction f) compares the current object with f & returns the
greater of the two
public int findLcm(int x,int y) computes
& returns the L.C.M. of x & y.
4.
Task :
To subdivide an amount of money into
all possible denominations available in the Indian currency.
Example 1.
Amount – Rs. 4982
Output :
4
1000 Rupee notes
1 500 Rupee
notes
4
100 Rupee notes
1 50 Rupee
notes
1
20 Rupee notes
1
10 Rupee notes
1
2 Rupee notes
Example 2.
Amount – Rs. 321
Output :
3 100 Rupee notes
1
20 Rupee notes
1 1 Rupee notes
Class name : DenominationFinder
Instance variable : int
num (represents the total amount of money
Methods :
a)
public DenominationFinder(int
n)
b)
public void findAndShow()
Computes all
possible combinations & displays them.
c)
public int givedenom(int x,int
denom)
Returns the
number of notes of denomination denom that x consists of.