Consider a librarian that wants to keep an inventory of books. Each book has a t
ID: 640878 • Letter: C
Question
Consider a librarian that wants to keep an inventory of books. Each book has a title, author, the number of pages, ISBN, and publisher. Design and implement a class called Book using the data listed above. Write a main program that creates two book objects and then prints out all the information about the books sorted by book title (hint: create a comesBefore method that compares the title of each book)
Start by completing the UML diagram:
In this exercise, each Student object represents a college student that has a school address and home address. Create two classes, one for Address and one for Student. The Student class should have a first name, last name, home address, and school address. This activity is different because you need to use the Address class inside the Student class. Create both classes in the same package along with the main. The main program should create several student objects and print out the information
Explanation / Answer
//Book.java class
public class Book
{
//class variables
private String title;
private String author;
private int pages;
private String ISBN;
private String publisher;
//Book constructor to set the title, author,pages,ISBN and publisher
public Book(String title,String author,int pages,String ISBN,String publisher)
{
this.title=title;
this.author=author;
this.pages=pages;
this.ISBN=ISBN;
this.publisher=publisher;
}
//Returns the string representation of book details
public String toString()
{
return "Title : "+title+
" Author: "+author+
" Pages : "+pages+
" ISBN : "+ISBN+
" Publisher : "+publisher;
};
public String getTitle()
{
return title;
}
public boolean comesBefore(String booktitle)
{
if(title.compareTo(booktitle)<0)
return true;
else
return false;
}
}//end of Book class
----------------------------------------------------------
//Driver program to test Book class
//BookDriver.java
public class BookDriver
{
public static void main(String[] args)
{
//Create two book objects
Book java=new Book("Introduction to java","James Gosling",800,
"1212-1212-1434","TATA-MGrill");
Book cplusplus=new Book("Introduction to c++",
"Yashwant", 400, "4454-4545-4545", "JaicC");
System.out.println("---Sort By title---");
//calling comesBefore method that returns true if passed
//book title comes after the calling Book object
if(java.comesBefore(cplusplus.getTitle()))
{
System.out.println(java.toString());
System.out.println(cplusplus.toString());
}
else
{
System.out.println(cplusplus.toString());
System.out.println(java.toString());
}
}
}
------------------------------------------------------------------------------------------------
---Sort By title---
Title : Introduction to c++
Author: Yashwant
Pages : 400
ISBN : 4454-4545-4545
Publisher : JaicC
Title : Introduction to java
Author: James Gosling
Pages : 800
ISBN : 1212-1212-1434
Publisher : TATA-MGrill
------------------------------------------------------------------------------------------------------------------
//Student.java
public class Student
{
//instance variables of class Student
private String fName;
private String lName;
private Address schoolAddress;
private Address homeAddress;
//Constructor to set first name and last name and home and school address
public Student(String fName,String lName, Address homeAddress, Address schollAddress)
{
this.fName=fName;
this.lName=lName;
this.schoolAddress=schollAddress;
this.homeAddress=homeAddress;
}
//Override toString method that returns the first name
//last name ,home address and office address.
@Override
public String toString()
{
return "First Name :"+fName+
" Last Name :"+lName+
" Home Address : "+homeAddress.toString()+
" School Address : "+schoolAddress.toString();
}
}//end of the class Student
------------------------------------------------------------------------------
//Address.java class
public class Address
{
private String HNO;
private String city;
private String state;
public Address(String HNO,String city,String state)
{
this.HNO=HNO;
this.city=city;
this.state=state;
}
@Override
public String toString()
{
return "H.NO : "+HNO+
" City : "+city+
" State : "+state;
}
}
---------------------------------------------------------------------------------------------
//Student and address class tester program
//StudentAddressDriver.java
public class StudentAddressDriver
{
public static void main(String[] args)
{
//set home address
Address homeAddress=new Address("4-5/4", "Down Town", "New York");
//set school address
Address schoolAddress=new Address("5-5/4", "Down Town", "New York");
Student student=new Student("Sachin", "Bansal", homeAddress, schoolAddress);
//print the information about student
System.out.println(student.toString());
}
}//end of StudentAddressDriver
--------------------------------------------------
Sample output:
First Name :Sachin
Last Name :Bansal
Home Address : H.NO : 4-5/4
City : Down Town
State : New York
School Address : H.NO : 5-5/4
City : Down Town
State : New York
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.