In: Computer Science
Question(Design Java Method).
There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program.
The Student class is following:
import java.lang.Integer; import java.util.ArrayList; import java.util.List; class Student implements Comparable<Student> { private int id; private String name; private double gpa; private List<Book> books; public Student(String n, int i, double gpa) { name = n; id = i; this.gpa = gpa; books = new ArrayList<Student.Book>(); } private static class Book { private String title; private String ISBN; public Book(String title, String ISBN) { super(); this.title = title; this.ISBN = ISBN; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } } public void addBook(String title, String ISBN) { Book book = new Book(title, ISBN); books.add(book); } public boolean hasBook(String ISBN) { for (int i = 0; i < books.size(); i++) { Book b = books.get(i); if (b.getISBN() == ISBN) return true; } return false; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getGpa() { return gpa; } public void setGpa(double gpa) { this.gpa = gpa; } public String toString() { return "(" + name+ " " + id + " " + gpa + ")";} public boolean equals(Object rhs) { // more compact code, but still correct if (rhs == null || getClass() != rhs.getClass()) return false; Student other = (Student) rhs; return id == other.id; } public int hashCode() { return Integer.valueOf(id).hashCode(); } public int compareTo(Student other) { return Integer.valueOf(id).compareTo(Integer.valueOf(other.id)); } }
Here is the completed code for Student.java file as well as Test.java for testing the new method. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// Student.java
import java.lang.Integer;
import java.util.ArrayList;
import java.util.List;
class Student implements Comparable<Student> {
private int id;
private String name;
private double gpa;
private List<Book> books;
public Student(String n, int i, double gpa) {
name = n;
id = i;
this.gpa = gpa;
books = new ArrayList<Student.Book>();
}
private static class Book {
private String title;
private String ISBN;
public Book(String title, String ISBN) {
super();
this.title = title;
this.ISBN = ISBN;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
}
public void addBook(String title, String ISBN) {
Book book = new Book(title, ISBN);
books.add(book);
}
public boolean hasBook(String ISBN) {
for (int i = 0; i < books.size(); i++) {
Book b = books.get(i);
//always use equals method to compare Strings instead of ==
if (b.getISBN().equals(ISBN))
return true;
}
return false;
}
/**
* newly implemented method, given an ISBN, returns the title of the book,
* if this student has a book with this ISBN, otherwise returns null
*
* @param ISBN
* - ISBN of book to search
* @return the title if found, null if not
*/
public String getTitleOfBook(String ISBN) {
//looping through each book
for (int i = 0; i < books.size(); i++) {
Book b = books.get(i);
//if current book's ISBN equals target, returning title of book
if (b.getISBN().equals(ISBN))
return b.getTitle();
}
return null; //not exists
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public String toString() {
return "(" + name + " " + id + " " + gpa + ")";
}
public boolean equals(Object rhs) { // more compact code, but still correct
if (rhs == null || getClass() != rhs.getClass())
return false;
Student other = (Student) rhs;
return id == other.id;
}
public int hashCode() {
return Integer.valueOf(id).hashCode();
}
public int compareTo(Student other) {
return Integer.valueOf(id).compareTo(Integer.valueOf(other.id));
}
}
//Test.java for testing new method
public class Test {
public static void main(String[] args) {
// creating a Student and adding some books
Student student = new Student("Oliver", 123, 3.9);
student.addBook("CSC 101", "ABCDEFGH");
student.addBook("ENG 13", "1234XYZ");
student.addBook("Programming in Java", "1010UUIJKL1");
student.addBook("Introduction to C++", "90908765ETYU");
// displaying if student has a book with isbn: 1010UUIJKL1, should be
// true
System.out.println("has book with ISBN 1010UUIJKL1: "
+ student.hasBook("1010UUIJKL1"));
// displaying title of a book with isbn: 1010UUIJKL1, should be
// "Programming in Java"
System.out.println("title of book with ISBN 1010UUIJKL1: "
+ student.getTitleOfBook("1010UUIJKL1"));
// displaying if student has a book with isbn: 1010101010, should be
// false
System.out.println("has book with ISBN 1010101010: "
+ student.hasBook("1010101010"));
// displaying title of a book with isbn: 1010101010, should be
// null
System.out.println("title of book with ISBN 1010101010: "
+ student.getTitleOfBook("1010101010"));
}
}
/*OUTPUT*/
has book with ISBN 1010UUIJKL1: true
title of book with ISBN 1010UUIJKL1: Programming in Java
has book with ISBN 1010101010: false
title of book with ISBN 1010101010: null