Create a workspace named Lab08 and place each part below as a separate project w
ID: 3635783 • Letter: C
Question
Create a workspace named Lab08 and place each part below as a separate project within this workspace. Note: only one project is active at a time. To work (Build/Run) a different project, right click on the project's name and select "Set as active project". You are not allowed to use switch, break, or System.exit() in your code.--------------------------------------------------------------------------------
Create a new project Lab08, and write an “Book” class which has the following specifications (variables, constructors and methods):
Variables:
· title: type of String
· author: type of String
· year: type of int
· noOfPages: type of int
Constructors:
(1) Default constructor: Without taking any parameters, it assigns “unknown” to all String type of variables and it assigns current year to year and 0 to noOfPages variables.
Hint: Use Calendar class for getting the current year value to initialize variable year
(2) Constructor: It takes four parameters; first one is for title, the second one is for author, the third one is for year, the fourth one is for noOfPages.
(3) Copy constructor: This constructor takes a Book instance and initializes its variables with the variable values of that instance.
Accessors:
Getter methods for all instance variables.
Mutators:
Setter methods for all instance variables.
Other methods:
- equals: Like the equals() method of String, this method compares the variable values of different instances. Note that in a Book instance we have more than one variable as its properties therefore different than String class you should check whether all of the variable properties are same or not and return a boolean value accordingly. In more details this method of the class takes a Book instance as its parameter and checks the variable values of that instance with the ones of its own. If all of them are the same, it should return true.
- printInfo: Without taking any parameters, this method returns the String representation of a Book instance.
For example when called the printed output of this method should be like:
Title: Moby Dick
Author: Herman Melville
Year: 1851
Number of pages: 822
Explanation / Answer
//prog :Book .java package com.my.bookself; import java.util.Calendar; public class Book { private String title; private String author; private int year; private int noOfPages; public Book() { super(); Calendar cal = Calendar.getInstance(); this.title = "Unknown"; this.author = "Unknown"; this.year = cal.YEAR; this.noOfPages = 0; } public Book(String title, String author, int year, int noOfPages) { super(); this.title = title; this.author = author; this.year = year; this.noOfPages = noOfPages; } public Book(Book book) { this(book.getTitle(),book.getAuthor(),book.getYear(),book.getNoOfPages()); } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getNoOfPages() { return noOfPages; } public void setNoOfPages(int noOfPages) { this.noOfPages = noOfPages; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String toString(){ StringBuffer sb = new StringBuffer(); sb.append("Title:").append(this.title).append(" "); sb.append("Author:").append(this.author).append(" "); sb.append("Year:").append(this.year).append(" "); sb.append("Number of pages:").append(this.noOfPages).append(" "); return sb.toString(); } public boolean equals(Book book){ if ( this.author.equals(book.getAuthor()) && this.title.equals(book.getTitle()) && this.year == book.getYear() && this.noOfPages == book.getNoOfPages() ){ return true; }else{ return false; } } } //Prog : BookTest.java package com.my.bookself; public class BookTest { /** * @param args */ public static void main(String[] args) { Book book1 = new Book("Glory of Heven","John cena",1988,500); Book book2 = new Book("Glory of Heven","John cena",1988,500); Book book3 = new Book("Glory of Hell","John cena",1988,500); System.out.println(book1.toString()); System.out.println(book2.toString()); System.out.println(book3.toString()); System.out.println("Book1 == Book2 :"+book1.equals(book2)); System.out.println("Book2 == Book3 :"+book2.equals(book3)); System.out.println("Book1 == Book3 :"+book1.equals(book3)); } } // for more help write to me
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.