Question

In: Computer Science

Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...

Consider the following incomplete declaration of a Book class for 1a-1c.

public class Book

{

   private String title, author, edition;

   private int numPages;

   public Book { //part (a) } //default

   public Book(String t, String a, String e, int np) { //part (b) }

   public String getTitle() {…} //returns the title of this Book

   public String getAuthor() {…} //returns the author of this Book

   public String getEdition() {…} //returns the edition of this Book

   public int getNumPages() {…} //returns numPages of this Book

   public void printBookInfo() {

System.out.print(title + “ written by “ + author);

   }

}

1a) Write the default constructor for the Book class. Our default book will have the following values:

title: Surviving CSC330 in Times of Crisis

author: Richard Weir

edition: 1

numPages = 100

1b) Write the parameterized constructor for the Book class. All instance variables need to be assigned values.

1c) Declare an instance of the Book class called myBook. myBook can have any values you would like (that are appropriate!).

Solutions

Expert Solution

Program Code Screenshot

Sample Output

Program Code to Copy

class Book {
    private String title, author, edition;
    private int numPages;
    public Book()
    {
        title = "Surviving CSC330 in Times of Crisis";
        author = "Richard Weir";
        edition = "1";
        numPages = 100;
    } //default

    public Book(String t, String a, String e, int np) {
        this.title = t;
        this.author = a;
        this.edition = e;
        this.numPages = np;
    }

    public String getTitle() {
        return title;
    } //returns the title of this Book

    public String getAuthor() {
        return author;
    } //returns the author of this Book

    public String getEdition() {
        return edition;
    } //returns the edition of this Book

    public int getNumPages() {
        return numPages;
    } //returns numPages of this Book

    public void printBookInfo() {
        System.out.print(title + " written by " + author);
    }
}

class Main{
    public static void main(String[] args) {
        Book book = new Book("Harry Potter","J K Rowling","5",1298);
        book.printBookInfo();
    }
}

Related Solutions

Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
Consider the following incomplete declaration of a Name class for 2a-2c. public class Name something missing...
Consider the following incomplete declaration of a Name class for 2a-2c. public class Name something missing {    private String first;    private String last;    public Name(String firstName, String lastName)    {       first = firstName;      last = lastName;    }    //additional methods } 2a) In the first two parts, you will modify the Name class so that it implements the Comparable interface. Show here what should go in place of something missing in the first line...
Consider a class Book that contains information about a Book. The class should has the following...
Consider a class Book that contains information about a Book. The class should has the following attributes: • The title of the book • The author of the book • Year of publication (e.g. 1994) • The number of people that have rated this book as a 1 (Terrible) • The number of people that have rated this book as a 2 (Bad) • The number of people that have rated this book as a 3 (OK) • The number...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Write C++ program Consider the following SimpleString class: class simpleString {     public:          simpleString( );...
Write C++ program Consider the following SimpleString class: class simpleString {     public:          simpleString( );                                 // Default constructor          simpleString(int mVal );                    // Explicit value constructor          ~simpleString() { delete [ ] s;}          // Destructor void readString();                              // Read a simple string          void writeString() const;                    // Display a simple string char at(int pos) const;                       // Return character at (pos)          int getLength() const;                        // Return the string length          int getCapacity() const;...
To be done in Java Consider the partially complete book class given.    Make the following...
To be done in Java Consider the partially complete book class given.    Make the following additions to the book class. Add a getAuthor() and a getTitle() method to the book class. They should take no parameters and should return a String. Change the 3-parameter constructor for the class so that it correctly initializes the member variables. Write an accessor method called getPages(). Add a zero parameter getDetails() method to the Book class. It should assemble the information for a...
1A. Is pentane hydrophilic or hydrophobic? 1B. Is pentanol hydrophilic or hydrophobic? 1C. Explain how the...
1A. Is pentane hydrophilic or hydrophobic? 1B. Is pentanol hydrophilic or hydrophobic? 1C. Explain how the chemical difference between pentane and pentanol changes their relationship to water.
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1");...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1"); super.method2(); } public void method2() { System.out.println("Bay 2"); } } //********************************* class Pond { public void method2() { System.out.println("Pond 2"); } } //************************** class Ocean extends Bay { public void method2() { System.out.println("Ocean 2"); } } //********************************* class Lake extends Pond { public void method3() { System.out.println("Lake 3"); method2(); } } //**************************** class Driver { public static void main(String[] args) { Object var4 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT