CSE 215lab - 6
CSE 215lab - 6
Everything in Java is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
Object oriented programming allows us to view the entities of the world as objects. For example,
a student, a circle, a car, a book, all of them can be thought of as objects with some properties.
For Example:
public class Dog
{
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
Create an Object
An object of a class can be created within the same class or in a different class.
void barking() {
}
void hungry() {
}
void sleeping() {
}
Remember that the name of the java file should match the class name.
Here we have 2 classes. So, corresponding to that, we have 2 files also with the same name in the
same directory/folder.
• Dog.java
• Main.java
void hungry() {
}
void sleeping() {
}
//multiple Objects
Dog Obj2 = new Dog();
}
}
Constructors:
• A constructor in Java is a special method with no return type and same method name as
the class.
• The constructor is called when an object of a class is created.
• It can be used to set initial values for object attributes
• It can have zero to multiple parameters
}
}
Task:
1. Write a program to print the area and perimeter of a circle having radius of 5 units by
creating a class named 'Circle' without any parameter in its constructor. Give another
attribute named “color” then print the value of color in the console.
2. Write a program to print the area and perimeter of a circle having radius of 5 units by
creating a class named 'Circle' with parameters in its constructor. Give another attribute
named “color” then print the value of color in the console.