0% found this document useful (0 votes)
4 views

Lab 2_Object and Class

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 2_Object and Class

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

OOP Laboratory 2

Exercise 1: Methods

Objective: Learn how to define and call methods in Java.

 Instructions:
1. Create a Java class named MethodExample.
2. Define a method that takes two integers as parameters and returns their sum.
3. Call this method from the main method and print the result.

Expected Outcome:

Correctly compute and print the sum of two integers using the defined method.

Exercise 2: Object-Oriented Programming (OOP) - Classes and Objects

Objective: Understand how to define classes and create objects in Java.

 Instructions:
1. Create a Car class with properties like make, model, and year.
2. Define a constructor to initialize the properties.
3. Create an object of the Car class in the main method and print the car's details.

Expected Outcome: Correctly define a class, create objects, and print object details.

Lab Exercise 3: Student Class with Methods

Objective: Create a Student class with instance variables, constructors, and methods.

Task:

1. Create a Student class with the following attributes:


o studentID (int)
o name (String)
o age (int)
o major (String)
2. Write a constructor to initialize these values.
3. Implement a method displayStudentDetails() that prints the student's information.
4. Create another method isAdult() to check if the student is an adult (18 or older).
Steps:

1. Create the Student class with appropriate attributes.


2. Define the constructor.
3. Implement the displayStudentDetails() and isAdult() methods.
4. In the Main class, create a Student object and call the methods to test the functionality.

// Student class

class Student {

int studentID;

String name;

int age;

String major;

// Constructor

public Student(int studentID, String name, int age, String major) {

this.studentID = studentID;

this.name = name;

this.age = age;

this.major = major;

// Method to display student details

public void displayStudentDetails() {

System.out.println("Student ID: " + studentID);

System.out.println("Name: " + name);


System.out.println("Age: " + age);

System.out.println("Major: " + major);

// Method to check if student is an adult

public boolean isAdult() {

return age >= 18;

// Main class

public class Main {

public static void main(String[] args) {

// Creating a student object

Student student = new Student(101, "John Doe", 20, "Computer Science");

// Display student details

student.displayStudentDetails();

// Check if the student is an adult

if (student.isAdult()) {

System.out.println(student.name + " is an adult.");

} else {

System.out.println(student.name + " is not an adult.");

}
}

Expected Output:

Student ID: 101


Name: John Doe
Age: 20
Major: Computer Science
John Doe is an adult.

Lab Exercise 2: Bank Account Class

Objective: Create a BankAccount class with methods to deposit, withdraw, and check balance.

Task:

1. Create a BankAccount class with the following attributes:


o accountNumber (String)
o accountHolder (String)
o balance (double)
2. Implement methods:
o deposit(double amount) - to add money to the account.
o withdraw(double amount) - to subtract money from the account (check if the
balance is sufficient).
o getBalance() - to return the current balance.
3. Create a Main class to create a BankAccount object and test the methods.

// BankAccount class

class BankAccount {

String accountNumber;

String accountHolder;

double balance;

// Constructor

public BankAccount(String accountNumber, String accountHolder, double balance) {

this.accountNumber = accountNumber;
this.accountHolder = accountHolder;

this.balance = balance;

// Method to deposit money

public void deposit(double amount) {

balance += amount;

System.out.println(amount + " deposited. New balance: " + balance);

// Method to withdraw money

public void withdraw(double amount) {

if (amount <= balance) {

balance -= amount;

System.out.println(amount + " withdrawn. New balance: " + balance);

} else {

System.out.println("Insufficient balance. Withdrawal denied.");

// Method to get current balance

public double getBalance() {

return balance;

}
// Main class

public class Main {

public static void main(String[] args) {

// Creating a BankAccount object

BankAccount account = new BankAccount("123456789", "John Doe", 1000.0);

// Performing operations

account.deposit(500.0);

account.withdraw(300.0);

account.withdraw(1500.0); // Should give an error for insufficient balance

// Check balance

System.out.println("Current balance: " + account.getBalance());

Expected Output:

500.0 deposited. New balance: 1500.0


300.0 withdrawn. New balance: 1200.0
Insufficient balance. Withdrawal denied.
Current balance: 1200.0

Lab Exercise 3: Array of Objects

Objective: Create an array of objects and access them.

Task:

1. Create a Book class with:


o title (String)
o author (String)
o price (double)

2. In the Main class, create an array of Book objects and initialize them with different
values. Display the details of each book.

// Book class

class Book {

String title;

String author;

double price;

// Constructor

public Book(String title, String author, double price) {

this.title = title;

this.author = author;

this.price = price;

// Method to display book details

public void displayBookInfo() {

System.out.println("Title: " + title);

System.out.println("Author: " + author);

System.out.println("Price: " + price);

// Main class
public class Main {

public static void main(String[] args) {

// Creating an array of Book objects

Book[] books = new Book[3];

// Initializing books in the array

books[0] = new Book("Java Programming", "John Doe", 29.99);

books[1] = new Book("Advanced Java", "Jane Doe", 39.99);

books[2] = new Book("Data Structures", "Jim Bean", 49.99);

// Displaying book details

You might also like