Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Inft231101081 OOP Lab 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

JAVA METHODS,

CONSTRUCTORS AND METHOD


OVERLOADING
Lab-06

Submitted by: Abdul Samad.


Submitted to: Ms Kiran Yaqoob
Roll no: Inft231101081.

LAB 06 Methods, Constructors and Method Overloading

Lab Objectives:
1. Understand java methods, arguments passing and return
2. Understand the purpose and implementation of constructors
3. Understand and implement the mechanism of method overloading
Software Required:
JDK & Notepad/ Textpad

Introduction:
A simplified general form of a class definition is shown here:
class classname {
type instance-variable1; type
instance-variable2;
// ...
type instance-variableN; type m
ethodname1(parameter-list) {// body of method
} type methodname2(parameter-list)
{
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
Sample code:
/* Here, Box uses a parameterized constructor to initialize the dimensions of a box. */
class Box { double width; double height;

double depth;

// This is the constructor for Box.


Box (double w, double h, double d) {
width = w; height = h; depth
= d;
}
// compute and return volume double
volume() { return width * height *
depth;
}
}

class BoxDemo7 {

public static void main(String args[]) {


// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;

// get volume of first box vol


= mybox1.volume();
System.out.println("Volume is " + vol);

// get volume of second box vol


= mybox2.volume();
System.out.println("Volume is " + vol);
}
}

Practice Problems:

Task 1: Swap Variables


• Declare a class swapDemo

• Create two variable for holding values of x and y as 10 and 100 respectively. Initialize
using constructor.

• Write a method swapVar which swaps the two digits.

• Create object of class swap

• Call class swap method swapVar and pass x and y as arguments.


• Print both original and swapped values.

Task 2: You will create a class that keeps track of the total cost, average cost,
and number of items in a shopping bag.

Create a class called ShoppingBag. Objects of this class represent a single shopping bag. Attributes
of such an object include the number of items in the bag and the total retail cost of those items.
Provide a constructor that accepts a tax rate as a float parameter.
Provide a transformer method called place that accepts an int parameter indicating the number of
the particular items that are being placed in the bag and a float parameter that indicates the cost of
each of the items. For example, myBag.place (5, 10. 5); represents placing 5 items that cost $10.50
each into myBag.
Provide getter methods for the number of items in the bag and their total retail cost. Provide a
totalCost method that returns the total cost with tax included.
Provide at oString method that returns a nicely formatted string that summarizes the current status
of the shopping bag.
Finally, provide a program, a “test driver,” that demonstrates that your ShoppingBag class
performs correctly.
Method Overloading

Methods of the same name can be declared in the same class, as long as they have different sets of
parameters (determined by the number, types and order of the parameters)—this is called method
overloading.

Math Class Overloaded Methods


S.N. Method & Description

1 static double abs(double a)


This method returns the absolute value of a double value.

2 static float abs(float a)


This method returns the absolute value of a float value.

3 static int abs(int a)


This method returns the absolute value of an int value.

4 static long abs(long a)


This method returns the absolute value of a long value.

5 static double copySign(double magnitude, double sign)


This method returns the first floating-point argument with the sign of the second floating-point
argument.

6 static float copySign(float magnitude, float sign)


This method returns the first floating-point argument with the sign of the second floating-point
argument.

7 static int getExponent(double d)


This method returns the unbiased exponent used in the representation of a double.

8 static int getExponent(float f)


This method returns the unbiased exponent used in the representation of a float.

9 static double max(double a, double b)


This method returns the greater of two double values.
10 static float max(float a, float b)
This method returns the greater of two float values.

11 static int max(int a, int b)


This method returns the greater of two int values.

12 static long max(long a, long b)


This method returns the greater of two long values.

13 static double min(double a, double b)


This method returns the smaller of two double values.

14 static float min(float a, float b)


This method returns the smaller of two float values.

15 static int min(int a, int b)


This method returns the smaller of two int values.

16 static long min(long a, long b)


This method returns the smaller of two long values.

17 static double nextAfter(double start, double direction)


This method returns the floating-point number adjacent to the first argument in the direction of
the second argument.

18 static float nextAfter(float start, double direction)


This method returns the floating-point number adjacent to the first argument in the direction of
the second argument.

19 static double nextUp(double d)


This method returns the floating-point value adjacent to d in the direction of positive infinity.

20 static float nextUp(float f)


This method returns the floating-point value adjacent to f in the direction of positive infinity.

21 static int round(float a)


This method returns the closest int to the argument.
22 static double scalb(double d, int scaleFactor)
This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded
floatingpoint multiply to a member of the double value set.

23 static float scalb(float f, int scaleFactor)


This method return f × 2scaleFactor rounded as if performed by a single correctly rounded
floatingpoint multiply to a member of the float value set.

24 static double signum(double d)


This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the
argument is greater than zero, -1.0 if the argument is less than zero.

25 static float signum(float f)


This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the
argument is greater than zero, -1.0f if the argument is less than zero.

26 static double ulp(double d)


This method returns the size of an ulp of the argument.

27 static double ulp(float f)


This method returns the size of an ulp of the argument.

Task 3: Create a class named as DemoOverloading. Now create following


methods in the class:

Method named as test() which takes no parameters and returns nothing.

Method named as test() which takes one integer parameter and return nothing.

Method named as test() which takes two integer parameters and returns nothing.

Method named as test() which takes two integer parameters and return their integer type sum.

Method test() which takes one double parameter and returns double type value.
Method test() which takes two argument first one is integer and second one is float and returns
nothing.

Method test() which takes two arguments, first one is float and second one is integer and return
nothing.

Now Create another class named as demo and call each method of class DemoOverloading, one by
one by passing appropriate parameters.

Task 4: Write a method minimum3 that returns the smallest of three


floatingpoint numbers. Use the Math.min method to implement minimum3.
Incorporate the method into an application that reads three values from the
user, determines the smallest value and displays the result.
Task 5: Modify the program in TASK 4 to compute the minimum of four double
values.

QUESTIONS

Please Fill the blank space with respective answers to following questions:

Question 1: What is the purpose of creating constructors?


Question 2: Suppose there is a class name “MyClass”. How will its object be
created?

Question 3: When is a constructor called?

Question 4: What is a default constructor? Explain the difference between


default constructor and parametrized constructor.

Question 5: What is wrong with following code?

class Student
{
String name;
int marks; char
section;
}
Question 6: What are the three rules on which a method can be overloaded?

THE END

You might also like