Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Java Lab Exercises 2

Uploaded by

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

Java Lab Exercises 2

Uploaded by

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

Week 3 (10 Jan -15 Feb 2025) – Java Programming Lab

Exercise 1: Create and Display Student Information

Objective: Create a class with attributes and display their values using methods.

Task:

1. Create a class Student with the following attributes:


o name (String)
o rollNumber (int)
o marks (float)
2. Create a method displayInfo() to print the student's details.
3. In the main method, create an object of Student, assign values manually, and call
displayInfo().

Exercise 2: Implement a Simple Bank Account

Objective: Work with methods that modify object attributes.

Task:

1. Create a class BankAccount with:


o accountHolder (String)
o balance (double)
2. Add methods:
o deposit(double amount) → Adds amount to balance.
o withdraw(double amount) → Deducts amount if enough balance is available.
o displayBalance() → Displays the balance.
3. In main, create a BankAccount object, assign values, and test the methods.

Exercise 3: Represent a Book in a Library

Objective: Work with multiple object instances.

Task:

1. Create a class Book with:


o title (String)
o author (String)
o price (float)
2. Create a method showBookInfo() to display book details.
3. In main, create two objects of Book, assign values, and display their details.

Exercise 4: Simple Employee Salary Calculation

Objective: Use methods to calculate values.

Task:

1. Create a class Employee with:


o name (String)
o basicSalary (double)
2. Create a method calculateSalary() that:
o Adds a 10% bonus to the basic salary.
o Prints the total salary.
3. In main, create an Employee object, assign values, and display the calculated salary.

Exercise 5: Creating a Rectangle Class

Objective: Work with multiple attributes and return values.

Task:

1. Create a class Rectangle with:


o length (double)
o width (double)
2. Add methods:
o setDimensions(double l, double w) → Assigns values to length and width.
o calculateArea() → Returns the area of the rectangle.
o calculatePerimeter() → Returns the perimeter.
3. In main, create a Rectangle object, assign values, and display the area and perimeter.

You might also like