Solutions to Selected Exercises in Java

This post is part of our Java Programming: A Comprehensive Guide for Beginners series.
This section provides solutions to selected exercises presented throughout the book. It's designed to help readers check their understanding and implementation of the concepts covered in each exercise.

B.1 Chapter 3: Variables and Data Types

Exercise 3.1

Problem: Create a Java program that calculates the area of a rectangle. Prompt the user to enter the length and width of the rectangle.
import java.util.Scanner;

public class RectangleAreaCalculator {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
}


B.2 Chapter 6: Object-Oriented Programming (OOP)

Exercise 6.2

Problem: Create a Java program that models a simple library system. Define classes for Book and Library, and implement methods to check out and return books.
public class Book {

private String title;
private String author;
private boolean checkedOut;

public Book(String title, String author) {
this.title = title;
this.author = author;
this.checkedOut = false;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public boolean isCheckedOut() {
return checkedOut;
}

public void checkOut() {
if (!checkedOut) {
checkedOut = true;
System.out.println("Book checked out successfully.");
} else {
System.out.println("Book is already checked out.");
}
}

public void returnBook() {
if (checkedOut) {
checkedOut = false;
System.out.println("Book returned successfully.");
} else {
System.out.println("Book is not checked out.");
}
}
}


import java.util.ArrayList;

public class Library {

private ArrayList<Book> books;

public Library() {
this.books = new ArrayList<>();
}

public void addBook(Book book) {
books.add(book);
}

public void listBooks() {
for (Book book : books) {
System.out.println(
"Title: " + book.getTitle() + ", Author: " + book.getAuthor()
);
}
}

public void checkOutBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
book.checkOut();
return;
}
}
System.out.println("Book not found in the library.");
}

public void returnBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
book.returnBook();
return;
}
}
System.out.println("Book not found in the library.");
}
}


B.3 Chapter 11: Unit Testing with JUnit

Exercise 11.1

Problem: Write a JUnit test for the calculateTotal method in the following class:
public class ShoppingCart {

private double total;

public void addItem(double price) {
total += price;
}

public double calculateTotal() {
return total;
}
}



import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ShoppingCartTest {

@Test
void testCalculateTotal() {
ShoppingCart cart = new ShoppingCart();
cart.addItem(25.0);
cart.addItem(15.0);
double expectedTotal = 40.0;
double actualTotal = cart.calculateTotal();
assertEquals(expectedTotal, actualTotal, 0.01);
}
}



0 comments:

Post a Comment