Question

In: Computer Science

Create a Java project called Lab3B and a class named Lab3B. Create a second new class...

  1. Create a Java project called Lab3B and a class named Lab3B.
  2. Create a second new class named Book.
  3. In the Book class:
    1. Add the following private instance variables:
      1. title (String)
      2. author (String)
      3. rating (int)
    2. Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable.
    3. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and author. rating won’t be given a value here.
    4. Add a public String method named getTitle (no parameter) that returns title.
    5. Add a void method named setRating with an int parameter (inRating). It will set rating equal to inRating.
    6. Add a public String method toString (no parameters). It should create and return a String variable with all the book’s instance data (like you did in the Employee class.)
  4. Back in the main Lab3B class.
    1. Declare and instantiate a Book object named book1 sending the following parameters to the constructor ("A Tale of Two Cities", "Charles Dickens", 4)
    2. Declare and instantiate a Book object named book2 sending the following parameters to the constructor ("Foundation", "Isaac Asimov")
    3. Call setRating for book2 sending 5 as a parameter.
    4. Call getTitle for book1 and print the returned value.
    5. Use our toString shortcut to print all of book1’s information.
    6. Print a blank line.
    7. Use the same coding to print all of book2’s info.

Your output should be:

A Tale of Two Cities

Title: A Tale of Two Cities

Author: Charles Dickens

Rating: 4

Title: Foundation

Author: Isaac Asimov

Rating: 5

Solutions

Expert Solution

In case of any query do comment. Thanks

Code:

Book.java

//class Book
public class Book {
    //a. private instance variable  
    private String title;
    private String author;
    private int rating;
    
    //b. constructor with three parameters
    public Book(String inTitle, String inAuthor, int inRating){
        this.title = inTitle;
        this.author = inAuthor;
        this.rating = inRating;
    }
    
    //c. constructor with two parameters
    public Book(String inTitle, String inAuthor){
        this.title = inTitle;
        this.author = inAuthor;
    }
    
    //d. method to return title
    public String getTitle(){
        return this.title;
    }
    
    //e. method to setRating
    public void setRating(int inRating){
        this.rating = inRating;
    }
    
    //f. toString to return string representation of an object
    public String toString(){
        return "Title: " + this.title + "\nAuthor: " + this.author + "\nRating: " + this.rating;
    }
}

Lab3B.java

public class Lab3B
{
        public static void main(String[] args) {
                //a, declare an object book1
                Book book1 = new Book("A Tale of Two Cities", "Charles Dickens", 4);
                //b. create an object book2 with two parameter constructor
                Book book2 = new Book ("Foundation", "Isaac Asimov");
                //c. call setRating on book2 with value 5
                book2.setRating(5);
                //d. call getTitle on book 1 and print the return value
                System.out.println(book1.getTitle());
                //e.Use our toString shortcut to print all of book1’s information.
                System.out.print(book1);
                //f. Print a blank line.
                System.out.println();
                //g. Use the same coding to print all of book2’s info.
                System.out.print(book2);
                
        }
}

============screen shot of the code========

output:


Related Solutions

Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
In java: -Create a class named Animal
In java: -Create a class named Animal
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT