Sie sind auf Seite 1von 7

Classes and Objects

A class can be defined as a template/blue print that describes the behaviors/states


An object is an instance of a class.
class classname
{
member variables
member methods
}
Example 1:
class Circle
{
int radius;
void setRadius(int r)
{
radius = r;
}
float area() {
return (3.14*radius*radius);
}
float circumference() {
return (2*3.14*radius);
}
}

Declaring and creating an object


ClassName objectReference;
ObjectReference=new Classname();
Example 2:
Circle c1;
c1=new Circle();
(OR)
Circle c1=new Circle();

Where c1 is the objectReference.

Accessing the member variables and methods of a class


Members are accessed using dot notation
objectReference.member
Example 3:
c1.area();
Example 4:
class Circle
{
int radius;
void setRadius(int r)
{
radius = r;
}
float area() {
return (3.14*radius*radius);
}
float circumference() {
return (2*3.14*radius);
}
}
class TestCircle
{
public static void main(String a[])
{
Circle c1=new Circle();
// Object c1 is created
c1.setRadius(5);
// Invoking a method of class Compute using Object c1
float a = c1.area();
float circum = c1.circumference();
System.out.println(Area=+ a);
System.out.println(Circumference=+ circum);
}
}

TODO 1
Create a class BMICalculator with
Instance variables: height, weight
Method : calculateBMI() this method accepts height and weight as parameters and returns
calculated bmi and comment.
[use different parameter names not same as instance variables]
Create class that creates objects for BMICalculator and tests it.
< 18.5kg/m2 UnderWeight
18.5 - 25kg/m2 Healthy
25 30 kg/m2 - slightly overweight
> 30kg/m2 heavily overweight

Weight in Kilograms

BMI =(Height in Meters) x (Height in Meters)


For example, a person who weighs 99.79 Kilograms and is 1.905 Meters (190.50 centimeters) tall has a BMI of 27.5.

99.79 Kg
= 27.5
(1.905 m) x (1.905 m)
Sample output:
Your BMI is 24.5 Healthy

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)
Purpose:
Perform the same or similar tasks, but on different types or different numbers of arguments.
Example 5:
public class compute
{
int square(int x)
{
System.out.println("Called square with argument: "+x);
return x*x;
}
double square(double y)
{
System.out.println("Called square with argument: "+y);
return y*y;
}
public static void main(String[] args)
{
compute c1=new compute();
System.out.println("Square of integer 8 equals to "+c1.square(8));
System.out.println("Square of double 8.5 equals to "+c1.square(8.5));
}
}
}

TODO 2
Modify the BMICalculator class created above as follows:
Add another instance variables : unitHeight, unitWeight
Add another overloaded method : calculateBMI() with parameters height, weight, unitForHeight and
unitForWeight. [use different parameter names not same as instance variables]
This method should identify the units given for height and weight and then use the appropriate formula
to calculate BMI.

The formula to calculate BMI when unitForHeight is in inches and unitForWeight is in pounds

BMI =

Weight in Pounds

x 703

(Height in inches) x (Height in inches)

For example, a person who weighs 220 pounds and is 75 inches tall has a BMI of 27.5.

220 lbs.
(75 inches) x (75 inches)

) x 703 = 27.5

Constructor

Used to initialize objects and automatically invoked when an object is created.


They should have the same name as the class name.

Example 6:
class Circle
{
int radius;
Circle (int r)
{
radius = r;
}
float area() {
return (3.14*radius*radius);
}
float circumference() {
return (2*3.14*radius);
}
}

TODO 3
Modify the BMICalculator class created above as follows:
Create a constructor to initialize height and weight

Das könnte Ihnen auch gefallen