1.

The Postal  Department charges the following rates for sending parcels :

Upto one kg                                                                            ---        Rs. 15/-

 

For every additional 500 grams or fraction thereof             ---        Rs.  8/-

 

Using the above information , design a class that will  compute the parcel charge

 

Class name     :           ParcelCostFinder

 

I.V.                  :           int wt  (weight in grams)

                                    int cost (cost to be computed)

Methods

public ParcelCostFinder()                 

 Constructer

public void getWeight(int k, int g)    

 Takes the parcel weight in kilograms(k) and grams (g) &  stores it appropriately in wt

public void computeCost()                

 Computes the cost

public void show()                              

 Displays the computed cost

 

2.

A class called PrimeGenerator  includes  the  following functions :

 

public void nextnPrimes(int m,int n)             

  displays first n Prime numbers after m

public void nextnNonPrimes(int m,int n)      

  displays first n  non Prime numbers after m

public boolean isPrime(int num)                   

  returns true if  num is Prime, otherwise returns false

 

Assuming that the other members of the class are prewritten ,define only the above  three functions.

 

 

3.

Pythagorean Triplets are a set of  three  integers  a , b & c  such that  a2 + b2 = c2

 

Thus ,  the  numbers  3 , 4 &  5  are Pythagorean Triplets.  Similarly 6 ,  8  & 10  are also Pythagorean Triplets.

 

Create a class to generate all Pythagorean Triplets  upto a given limit .

 

Class name                             :  Triplets

 

I.V.                                          :  int limit

 

Methods

public Triplets(int n)                          

  constructer that initializes limit

public void generate()                                   

  outputs all the Pythagorean Triplets

public boolean isValid(int s1,int s2,int s3)   

  returns true if s1,s2 & s3 are a valid set of  Pythagorean Triplets , otherwise returns false

 

N.B. -- The output must exclude any duplicate combination.

 

For example , if  3 , 4 & 5  are displayed then  any of the combinations  4 ,  5 , 3  or  5 , 3 , 4  or  3 ,  5 ,  4  must not  be displayed.

 

4.

It is required to model a distance in feet and inches as an object.

 

Design a class called Distance that will do the following tasks :

 

Class name     :           Distance

I.V.                  :           int  ft,in  (representing feet & inch measurement )

 

Methods

public Distance()                               

  default constructor

public Distance( int x , int y) 

  parameterized constructor  

public Distance diff(Distance d)       

  returns the difference between current Distance and d

public Distance greater(Distance d)

  compares current Distance with d and returns the greater Distance

public int dist_to_inch()                     

  converts Distance to inches and returns it

public void  inch_to_dist(int x)          

  converts x , which is in inches , to feet and inches

public void show()                              

  displays  current Distance measurements             

 

5.

A class called NumberProblems includes , besides other members , the following two methods :

 

a)                  public boolean isPerfectSquare(int num)

which  returns true if  num is a perfect square , otherwise returns false.

 

b)                  public boolean  isTriangular(int num)

which returns true if num is a triangular number , otherwise returns false.

 

N.B.  – a number is triangular if it is equal to the sum of natural nos. starting from 1.

 

            Thus , 6,  15 &  36 are examples of triangular numbers since

             1 + 2 + 3 = 6

             1 + 2 + 3 + 4 + 5 = 15

             1 + 2  + …….+  7 + 8 = 36

 

Assuming that the other methods of the class NumberProblems already exist , define these two methods.

 

 

6.

An Automorphic number is such a number which is contained to the right of its square. Therefore 5 , 6 and 25 are examples of Automorphic numbers since :

52  =  25           ,           62  =  36           ,           252  =  625      

 

Task  --           To generate all Automorphic numbers upto a particular integer limit

 

Class name                 :           Autonum

 

Instance variables      :           int limit

 

Methods :

                        public Autonum()                               

constructer

                        public void getlimit(int n)                  

initializes limit

                        public int digitcount(int a)                 

returns the total number of digits present in a

                        public boolean isAuto(int num)         

returns true if num is an Automorphic number , otherwise returns false

                        public void showAuto()                      

displays all Automorphic numbers upto limit

 

7.

The digital root of any integer is obtained by repeatedly summing its digits until the sum obtained becomes a single digit number.

 

Consider the integer 29989.

 

Summing the digits of  29989 , we get 37 (2 + 9 + 9 + 8 + 9)

 

Since 37 is not a single digit number ,  its digits are summed  ( 3 + 7 )  to get 10.

 

10 is still not a single digit number , so we sum up the digits of  10 ( 1+ 0)  to get 1.

 

Therefore , 1 is the digital root of  29989.

 

Task  --   To compute the digital root of any integer.

 

Class name                 :           DgRootFinder

 

Instance variables      :           int num            ( integer whose digital root is to be computed )

                                                int  droot         ( the digital root of num )

Methods :

                        public DgRootFinder(int n)   

constructer to initialize num

                        public void compute()            

computes the digital root of num

                        public int digitSum(int x)                   

returns the sum of all the digits of  x

                        public void show()                  

displays the original integer  & its digital root

                       

                       

 

 

 

 

 

 

Hosted by www.Geocities.ws

1