Inft231101081 OOP Lab 6
Inft231101081 OOP Lab 6
Inft231101081 OOP Lab 6
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;
class BoxDemo7 {
Practice Problems:
• Create two variable for holding values of x and y as 10 and 100 respectively. Initialize
using constructor.
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.
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.
QUESTIONS
Please Fill the blank space with respective answers to following questions:
class Student
{
String name;
int marks; char
section;
}
Question 6: What are the three rules on which a method can be overloaded?
THE END