Java
Java
setters to work with tables. In this exercise, we'll create a simple representation
of a library system with three classes: Library, Book, and Member. The Library
class will manage books and members, while the Book and Member classes will
represent individual books and library members. Each class will have getters and
setters to access and modify their properties.
import java.util.ArrayList;
import java.util.List;
class Book {
private String title;
private String author;
private int year;
class Member {
private String name;
private int memberId;
class Library {
private List<Book> books;
private List<Member> members;
public Library() {
books = new ArrayList<>();
members = new ArrayList<>();
}
System.out.println("Library Books:");
for (Book book : libraryBooks) {
System.out.println("Title: " + book.getTitle() + ", Author: " +
book.getAuthor() + ", Year: " + book.getYear());
}
System.out.println("\nLibrary Members:");
for (Member member : libraryMembers) {
System.out.println("Name: " + member.getName() + ", Member ID: " +
member.getMemberId());
}
}
}
In this exercise, you have a Book class with getters and setters for title, author,
and year, a Member class with getters and setters for name and member ID, and a
Library class that allows you to add and remove books and members while providing
access to the lists of books and members through getters. The Main class
demonstrates how to use these classes by creating a library, adding books and
members, and displaying their information.
User
use now sort by name
ChatGPT
To sort the library's books and members by name, you can modify the Library class
to provide sorting methods. Here's an updated version of the Library class that
includes sorting methods by name for both books and members:
java
Copy code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Library {
private List<Book> books;
private List<Member> members;
public Library() {
books = new ArrayList<>();
members = new ArrayList<>();
}
java
Copy code
public class Main {
public static void main(String[] args) {
// Create a library
Library library = new Library();
User
use heritage
ChatGPT
Sure, let's introduce inheritance (heritage in some regions) to our classes. In
this modified version, we'll create a common base class LibraryItem that both Book
and Member classes will inherit from. This base class will contain common
properties such as a name/title for items in the library. Then, we'll use
inheritance to demonstrate how you can use this approach to simplify the code.
java
Copy code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class LibraryItem {
private String name;
class Library {
private List<Book> books;
private List<Member> members;
public Library() {
books = new ArrayList<>();
members = new ArrayList<>();
}
User
make try and exeption for error interception
ChatGPT
To add error interception using try-catch blocks for potential exceptions, you can
modify the code to handle exceptions that may occur during operations like adding
or removing items from the library. In this example, we will handle exceptions when
trying to remove a non-existing book or member.
java
Copy code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class LibraryItem {
private String name;
class Library {
private List<Book> books;
private List<Member> members;
public Library() {
books = new ArrayList<>();
members = new ArrayList<>();
}
User
give me another exercice and use all that i mentioned before and use arrays and
array sorts
ChatGPT
Certainly, here's another Java exercise that combines the concepts of classes,
inheritance, error interception with try-catch, arrays, and array sorting. In this
exercise, we'll create a simple student management system with three classes:
Student, Course, and StudentManager. Students can enroll in courses, and we'll
provide functionality to add, remove, and sort students within courses.
java
Copy code
import java.util.Arrays;
import java.util.Comparator;
class Student {
private String name;
private int studentId;
@Override
public String toString() {
return "Student [Name: " + name + ", Student ID: " + studentId + "]";
}
}
class Course {
private String courseName;
private Student[] students;
private int studentCount;
class StudentManager {
public static void sortStudentsByName(Student[] students) {
Arrays.sort(students, Comparator.comparing(Student::getName));
}
// Create students
Student student1 = new Student("Alice", 1);
Student student2 = new Student("Bob", 2);
Student student3 = new Student("Charlie", 3);
Student student4 = new Student("David", 4);
Student student5 = new Student("Eve", 5);
// Enroll students in courses
mathCourse.enrollStudent(student1);
mathCourse.enrollStudent(student2);
mathCourse.enrollStudent(student3);
historyCourse.enrollStudent(student3);
historyCourse.enrollStudent(student4);
historyCourse.enrollStudent(student5);
Here's another Java exercise that combines classes, inheritance, error interception
with try-catch, arrays, and array sorting. In this exercise, we'll create a simple
banking system with three classes: Bank, Account, and BankManager. Accounts can be
opened at the bank, and we'll provide functionality to add, remove, and sort
accounts by balance.
java
Copy code
import java.util.Arrays;
import java.util.Comparator;
class Account {
private int accountNumber;
private String accountHolder;
private double balance;
@Override
public String toString() {
return "Account [Account Number: " + accountNumber + ", Account Holder: " +
accountHolder + ", Balance: " + balance + "]";
}
}
class Bank {
private Account[] accounts;
private int accountCount;
// Open accounts
bank.openAccount(1, "Alice", 1000.0);
bank.openAccount(2, "Bob", 2000.0);
bank.openAccount(3, "Charlie", 1500.0);
// Close an account
bank.closeAccount(2);
System.out.println("Bank Accounts:");
for (Account account : bank.getAccounts()) {
System.out.println(account);
}