Java Lab Assignment
Instructions:
Solve all the problems. Different submission link will be created for submitting different
problems. So submit carefully.
Codes will be checked against MOSS Standard copy checker. Any kind of plagiarism will
be punished with negative full marks. There will be no source and copier. Both will be
under penalty. So be careful because this can cause failure in this course.
Problem 1: Library System
We provide two classes, Book and Library, that provide the functionality for the book database.
You must implement the missing methods to make these classes work. Let’s observe the
following files (copy the codes to your IDEA, then do the task). Fill up the method’s body
understanding the comment given inside each method.
You will have to submit both book.java and library.java files after completing the methods. First
Complete Book.java because you will need this in Library.java file.
Book.java
public class Book {
private String title;
private boolean borrowed;
public Book(String bookTitle) {
// Creates a new Book
}
public void borrowed() {
// Marks the book as borrowed; assign borrowed=true
}
public void returned() {
// Marks the book as not borrowed (returned); assign
borrowed=false
}
public boolean isBorrowed() {
// Returns true if the book is borrowed, false otherwise
}
public String getTitle() {
// return the title of the book
}
public static void main(String[] args) {
// Small test of the Book class
Book example = new Book("Tin Goyenda: Volume 3");
System.out.println("Title " + example.getTitle());
System.out.println("Borrowed? (should be false): " +
example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): " +
example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " +
example.isBorrowed());
}
}
Library.java
public class Library {
// Add the missing implementation to this class
String address;
Book[] bookList;
int currentNumberofBooks;
int capacity;
public Library(String address,int capacity){
//assign address, capacity. Create bookList array with
new operator. set currentNumberofBooks=0
}
void addBook(Book b){
//add this book object to bookList array. Update
currentNumberofBooks
}
void borrowBook(String title){
//search the book in the bookList array using the title
parameter. if book is available mark that book as borrowed
(Hint call method of book class)
}
void returnBook(String title){
//search the book in the bookList array using the title
parameter. mark that book as not borrowed (Hint call method of
book class)
}
void printAvailableBooks(){
//print the titles of the books which borrowed is false
}
public static void main(String[] args) {
Library firstLibrary = new Library("10 Main St.",100);
// Add four books to the first library
firstLibrary.addBook(new Book("Zafar Iqbal Shomogro-
1"));
firstLibrary.addBook(new Book("The Lost Symbol"));
firstLibrary.addBook(new Book("Feluda Part-1"));
firstLibrary.addBook(new Book("Feluda Part-2"));
firstLibrary.addBook(new Book("Bohubrihi"));
System.out.println("Borrowing Bohubrihi:");
firstLibrary.borrowBook("Bohubrihi");
System.out.println("Borrowing Feluda Part-2:");
firstLibrary.borrowBook("Feluda Part-2");
// Print the titles of all available books from firstLibrary
System.out.println("Books available in the library:");
firstLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning Bohubrihi");
firstLibrary.returnBook("Bohubrihi");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first
library:");
firstLibrary.printAvailableBooks();
}
}
Problem 2: Employee Payment
The Stark Industry needs a program to calculate how much to pay their hourly employees. They
have the following rules:
An employee gets paid (hours worked) × (base pay), for each hour up to 40 hours.
For every hour over 40, they get overtime = (base pay) × 1.5.
If the number of hours is greater than 60, print an error message.
Example: If an employee works for 45 hours, and his base pay is $8 , the he will get= (40 * 8)+
[5* (8 * 1.5)] = $380
Create a new class called StarkIndustry.
Write a method that takes the base pay and hours worked as parameters, and prints the total
pay or an error. Write a main method that calls this method for each of these employees:
Employee No Base pay Hours Worked
Employee 1 $7.20 35
Employee 2 $8.20 47
Employee 2 $10.50 73
You will have to submit StarkIndustry.java file.
Problem 3:
Complete this code: https://paste.ubuntu.com/p/kp25C3TSmG/
Description: Create a class Course and Student with the following attributes and methods;
Course:
Double gpa(private), String courseName(private), double credit(private)
course(String courseName, double gpa, double credit){Assign the corresponding variables}
Student:
Course[] C(private), String name(private), int totalCourse(private)
Student(String name, int totalCourse){Assign the corresponding variables, }
Void addCourse(Course c){Add the course to the course list}
Now in the main class, you will take n as input which will be the total number of students'
information. It will be followed by each students’ name, total course number and courseinfos’.
You have to print each student's information in the reverse order.
Sample input: Output:
2 Student name:
Rahim 2 Karim
Java 3.5 3 Courses:
Database 4 4 Course name: Numerical gpa:3 credit:3
Karim 1 Student name:
Numerical 3 3 Rahim
Courses:
Course Name: Java gpa:3.5 credit:3
Course Name: Database gpa:4 credit:4
Problem 4: Magic Square Test
A magic square of order n is an arrangement of n × n numbers, usually distinct integers, in a
square, such that the n numbers in all rows, all columns, and both diagonals sum to the same
constant.
See all rows’, columns’ and diagonal’s sum is same. Write a main method where first you need
to input n. Then input n*n integers and form a 2D matrix. Print “YES” if the matrix is magic
square and print “NO” Otherwise.
Sample input: Output:
3 YES
276
951
438
Problem 5: Implement Queue class
Queue is data structure which maintains FIFO (First IN First OUT) policy.
Create a class named Queue with following methods and attributes:
Class Queue:
double[] queueElements (public), int queueCapacity (private), int lastIndex (private)
public Queue(int capacity) {
//assign the member variables
}
void enqueue(double n) {
//add this n to end of the queueElements array, show error if no capacity to hold this
value
}
void dequeue() {
//remove the first element of the queueElements array. Update the array accordingly.
}
void printElements() {
//show the elements of queueElements array.
}
See the following main method and observe the output. Complete the Queue class to get the
output exactly shown in the table below:
QueueTest.java Ouput
public class QueueTest { Creating a Queue: Capacity 5
public static void main(String[] args) {
Queue q=new Queue(5); Enqueue 5.2: Successful
q.enqueue(5.2); Enqueue 2.1: Successful
q.enqueue(2.1); Enqueue -0.2: Successful
q.enqueue(-0.2); Enqueue 7.88: Successful
q.enqueue(7.88); Enqueue 5.5: Successful
q.enqueue(5.5); Error Enqueuing 1.1
q.enqueue(1.1);
q.printElements(); Showing Elements of queue:
q.dequeue(); 5.2 2.1 -0.2 7.88 5.5
q.printElements();
q.dequeue(); Dequeuing: Successful
q.dequeue(); Showing Elements of queue:
q.printElements(); 2.1 -0.2 7.88 5.5
q.enqueue(10.666);
q.printElements(); Dequeuing: Successful
} Dequeuing: Successful
} Showing Elements of queue:
7.88 5.5
Enqueue 10.666: Successful
Showing Elements of queue:
7.88 5.5 10.666
Problems on Java Inheritance
Problem 6: Vehicle’s Total Tax
Create a class named vehicle and two sub classes private_vehicle and public_vehicle with the
following attributes and methods.
Vehicle:
String grade (private) , double roadTax (private)
void Vehicle(String grade, double roadTax){Assign the corresponding variables}
double totalTax(){return roadTax}
Public_vehicle (A child class of vehicle):
double fitnessTax
public Public_vehicle(String grade, double roadTax, double fitnessTax){Assign the variables}
double totalTax(){
//returns fitnessTax+roadTax
Private_vehicle(A child of vehicle class):
double fitnessTax
public Private_vehicle(String grade, double roadTax, double fitnessTax){Assign the variables}
double total_tax(){
//returns 1.5*(fitnessTax+roadTax)
Now in the main class, create an array of 5 vehicles and assign some private vehicle and public
vehicle in different indexes. Now print every vehicle's total_tax.
Problem 7: Shape Hierarchy
Create a class named “Shape” which has the two sub-classes: “Circle” and “Rectangle”. Create
another class named “Square” which inherits “Rectangle” Class. The class definitions are as
follows:
Shape String color (private)
public Shape() { //set color to “red”}
public Shape(String color) {// assign the color}
public double getArea() {//return -1}
Circle double radius (private)
public Circle(double radius) {…}
public Circle(String color, double radius) {…}
public double getArea() {//returns area of the circle}
Rectangle double dim1 (private), double dim2 (private)
public Rectangle(double dim1,double dim2){…}
public Rectangle(String color, double dim1,double dim2) {….}
public double getArea() {//returns area of the rectangle}
Square public Square(double dim1) {…}
public Square(String color, double dim1) {…}
public double getArea() {//returns area of the square}
[Tips: For square dim1 and dim2 are equal]
Now complete the class methods to run the following main class. Notice you can only add
getter/setter methods in the above classes if necessary, Nothing else.
public class Main {
public static void main(String[] args) {
Shape s;
Circle c=new Circle(2);
c.getArea();
s=new Circle(3,"Green");
s.getArea();
Rectangle r= new Rectangle(“Blue",10,20);
r.getArea();
s= new Rectangle(20,30);
s.getArea();
s= new Square("Green",10);
s.getArea();
Square b=new Square(5);
b.getArea();
}
}
Problem 8: Account Management System
Suppose there is an account management system which follows the rules described below. You
have to implement all the classes and the necessary methods to fulfill the constraints of each
account.
SavingsAccount, BasicAccount inherit Account class.
a) An account holder can deposit or withdraw different amounts from a SavingsAccount and
pay an interest rate 0f 1.5% during withdrawal. A SavingsAccount can charge a fee if the
balance falls below a certain amount.
b) An account holder using a BasicAccount pays 0.5% during withdrawal and has only 3 free
transactions. Transaction includes both deposit and withdrawal of money. A BasicAccount can
charge a fee if the number of transactions exceeds the specified number of transactions.
The required variables for different classes are given. All these variables are private. You can
add any variable if you need.
Class Name Variables
Account accountNumber: int (protected)
holderName: String (public)
balance: double (protected)
void deposit (double amount)
void withdraw (double amount)
SavingsAccount minimumBalanceWithoutFee : double
feeForLowBalance : double
void deposit (double amount)
void withdraw (double amount)
BasicAccount totalNumberOfTransactions: int
extraTranasctionFee: double
void deposit (double amount)
void withdraw (double amount)
Finally write a main function: create different SavingsAccount and BasicAccount objects. Then
check the functionalities of deposit and withdraw of different amounts.