Using your own scenario from Assignment 2, identify all the objects and write out Java classes to represent each of them. A minimum of 5 objects is required.
Question
Using your own scenario from Assignment 2, identify all the objects and write out Java classes to represent each of them. A minimum of 5 objects is required.
Solution
To successfully create Java classes representing various objects based on a common scenario, let's consider a scenario involving a "Library System." This system will encompass a few objects: Book
, Member
, Library
, Loan
, and Author
. Below are the steps to define the classes for each object.
1. Class: Book
public class Book {
private String title;
private String isbn;
private Author author;
private boolean isAvailable;
public Book(String title, String isbn, Author author) {
this.title = title;
this.isbn = isbn;
this.author = author;
this.isAvailable = true; // Books are available by default
}
// Getters and setters
public String getTitle() {
return title;
}
public String getIsbn() {
return isbn;
}
public Author getAuthor() {
return author;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
}
2. Class: Author
public class Author {
private String name;
private String biography;
public Author(String name, String biography) {
this.name = name;
this.biography = biography;
}
// Getters
public String getName() {
return name;
}
public String getBiography() {
return biography;
}
}
3. Class: Member
public class Member {
private String memberId;
private String name;
private String email;
public Member(String memberId, String name, String email) {
this.memberId = memberId;
this.name = name;
this.email = email;
}
// Getters
public String getMemberId() {
return memberId;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
4. Class: Loan
import java.time.LocalDate;
public class Loan {
private Book book;
private Member member;
private LocalDate loanDate;
private LocalDate returnDate;
public Loan(Book book, Member member, LocalDate loanDate) {
this.book = book;
this.member = member;
this.loanDate = loanDate;
this.returnDate = null; // Not returned yet
}
// Getters and setters
public Book getBook() {
return book;
}
public Member getMember() {
return member;
}
public LocalDate getLoanDate() {
return loanDate;
}
public LocalDate getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDate returnDate) {
this.returnDate = returnDate;
}
}
5. Class: Library
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books;
private List<Member> members;
private List<Loan> loans;
public Library() {
books = new ArrayList<>();
members = new ArrayList<>();
loans = new ArrayList<>();
}
// Methods to add books, members, and loans
public void addBook(Book book) {
books.add(book);
}
public void addMember(Member member) {
members.add(member);
}
public void addLoan(Loan loan) {
loans.add(loan);
loan.getBook().setAvailable(false); // Book is no longer available
}
}
Final Answer
The Java classes outlined above represent a simple library system consisting of five objects: Book
, Author
, Member
, Loan
, and Library
. Each class encapsulates attributes pertinent to its role and provides methods for interaction.
Similar Questions
How many types of assignment operators in java?Options: Pick one correct answer from below3546
Create a class College and Faculty. Create at least 3 objects of each class and display all details using generic method display
Interfaces in Java can be used to achieve:Question 4Answera.Multiple inheritance.b.Code encapsulation.c.Method overloading.d.Static method implementation.
Describe the process of creating an object of a class that contains objects of other classes as data members.
We can create ___ number of objects for one single class.*1 pointonethreezeroany
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.