LAB REPORT
CSE222: Object-Oriented Programming Lab
Topic: Class & Object
Submitted To
Shahriar Hossain Shanto (SHS)
Designation
Department of CSE, Daffodil International University
Submitted By
Student ID: 0242310005101263
Section: 64_K2
Student Name: Md Rakibul Hasan
Date of Assignment Submission: 22nd November 2024
01
Experiment No: 01 Mapping: CO1 and CO2
Experiment Name Class & Object
Experiment Details:
Class:
A class in Java is a blueprint or template used to create objects. It defines properties (attributes) and behaviors
(methods) that the objects of the class will have. Think of a class as a prototype for creating similar entities.
Real-World Example
Consider the concept of a Car. Every car shares attributes like brand, model, color, speed, and behaviors like
start, stop, and accelerate. The class car would define these attributes and behaviors.
Object:
An object is an instance of a class. It represents a specific entity with a state (attributes) and behavior
(methods). Objects are created using the new keyword in Java.
Real-World Example
A specific car, like a red Toyota Corolla, is an object of the class Car. This object will have specific values for
its attributes, like color: red, brand: Toyota, and model: Corolla, and can perform behaviors like start, and
accelerate.
Code:
//file Name : Main
class Car {
String brand;
String model;
String color;
int speed;
Car(String brand, String model, String color, int speed) {
this.brand = brand;
this.model = model;
this.color = color;
this.speed = speed;
}
void start() {
System.out.println(brand + " " + model + " is starting.");
}
void accelerate(int increment) {
speed += increment;
System.out.println(brand + " " + model + "Current speed: " + speed + "
km/h");
}
void stop() {
System.out.println(brand + " " + model + " has stopped.");
speed = 0;
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", "Red", 0);
Car car2 = new Car("Tesla", "Model 3", "Blue", 0);
car1.start();
car1.accelerate(60);
car1.stop();
car2.start();
car2.accelerate(80);
car2.stop();
}
}
Obtained Output:
Toyota Corolla is starting.
Toyota Corolla Current speed: 60 km/h
Toyota Corolla has stopped.
Tesla Model 3 is starting.
Tesla Model 3Current speed: 80 km/h
Tesla Model 3 has stopped.
Desired
Output?
YES

Class And Object- Java Object Oriented Programming Lab Report

  • 1.
    LAB REPORT CSE222: Object-OrientedProgramming Lab Topic: Class & Object Submitted To Shahriar Hossain Shanto (SHS) Designation Department of CSE, Daffodil International University Submitted By Student ID: 0242310005101263 Section: 64_K2 Student Name: Md Rakibul Hasan Date of Assignment Submission: 22nd November 2024 01
  • 2.
    Experiment No: 01Mapping: CO1 and CO2 Experiment Name Class & Object Experiment Details: Class: A class in Java is a blueprint or template used to create objects. It defines properties (attributes) and behaviors (methods) that the objects of the class will have. Think of a class as a prototype for creating similar entities. Real-World Example Consider the concept of a Car. Every car shares attributes like brand, model, color, speed, and behaviors like start, stop, and accelerate. The class car would define these attributes and behaviors. Object: An object is an instance of a class. It represents a specific entity with a state (attributes) and behavior (methods). Objects are created using the new keyword in Java. Real-World Example A specific car, like a red Toyota Corolla, is an object of the class Car. This object will have specific values for its attributes, like color: red, brand: Toyota, and model: Corolla, and can perform behaviors like start, and accelerate. Code: //file Name : Main class Car { String brand; String model; String color; int speed; Car(String brand, String model, String color, int speed) { this.brand = brand; this.model = model; this.color = color; this.speed = speed; }
  • 3.
    void start() { System.out.println(brand+ " " + model + " is starting."); } void accelerate(int increment) { speed += increment; System.out.println(brand + " " + model + "Current speed: " + speed + " km/h"); } void stop() { System.out.println(brand + " " + model + " has stopped."); speed = 0; } } public class Main { public static void main(String[] args) { Car car1 = new Car("Toyota", "Corolla", "Red", 0); Car car2 = new Car("Tesla", "Model 3", "Blue", 0); car1.start(); car1.accelerate(60); car1.stop(); car2.start(); car2.accelerate(80); car2.stop(); } } Obtained Output: Toyota Corolla is starting. Toyota Corolla Current speed: 60 km/h Toyota Corolla has stopped. Tesla Model 3 is starting. Tesla Model 3Current speed: 80 km/h Tesla Model 3 has stopped. Desired Output? YES