Question

JAVA This PoD, builds off the Book class that you created on Monday (you may copy...

JAVA

This PoD, builds off the Book class that you created on Monday (you may copy your previously used code). For today’s problem, you will add a new method to the class called lastName() that will print the last name of the author (you can assume there are only two names in the author name – first and last). You can use the String spilt (“\s”) method that will split the String by the space and will return an array of Strings. Only return the last name (a String).

Here is the updated Book Class:
• author (String)
• title (String)
• year (int)
• price (double)

and the following methods:
• Constructor (that sets the author, title, year, and price from defined values).
• Get and set methods.
• toString method returns the title, author and year of the book
• lastName() which returns a String that represents the last name of the author.

Then create a BookDemo class to create and use Book objects. The BookDemo class contains the main method. Use a Scanner object to read the attributes for a Book object. Create a Book object by passing in the attributes read in by the Scanner object. Then call the toString method and then call and print the lastName() method.

Details

Note: Since the author and title can be more than one word, use the Scanner object to read in author and title on different lines, and the year and price on the same line.

Input

An author, input in the following format:
{First Name} {Last Name}
{Title}
{Year Published}

Example Input:
Margaret Atwood
The Handmaid’s Tale
1987 22.99

Output

Print using the toString method, then print only the last name.

Example Output:
The Handmaid’s Tale by Margaret Atwood, 1987
Atwood

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

// Book.java

public class Book {
   //Declaring instance variables
   private String author;
   private String title;
   private int year;
   private double price;

   //Parameterized constructor
   public Book(String author, String title, int year, double price) {
       this.author = author;
       this.title = title;
       this.year = year;
       this.price = price;
   }

   //getters and setters
   public String getAuthor() {
       return author;
   }

   public void setAuthor(String author) {
       this.author = author;
   }

   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 double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   //This method will return the last name of the author
   public String lastName() {
       String arr[] = author.split(" ");
       return arr[1];
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return title + " by " + author + ", " + year;
   }

}
__________________________

//BookDemo.java

import java.util.Scanner;

public class BookDemo {

   public static void main(String[] args) {
  
//Declaring variables   
       String author;
       String title;
       int year;
       double price;
  

   /*
   * Creating an Scanner class object which is used to get the inputs
   * entered by the user
   */
   Scanner sc = new Scanner(System.in);

       //Getting the input entered by the user
System.out.print("Enter author name :");
author=sc.nextLine();
System.out.print("Enter Title name :");
title=sc.nextLine();
System.out.print("Enter year :");
year=sc.nextInt();
System.out.print("Enter price :");
price=sc.nextDouble();
  
//Creating an instance of Book class
Book b=new Book(author, title, year, price);
//Displaying the Book class info
System.out.println(b);
//Displaying the author last name
System.out.println(b.lastName());

   }

}
__________________________

Output:

Enter author name :Margaret Atwood
Enter Title name :The Handmaid’s Tale
Enter year :1987
Enter prince :22.99
The Handmaid’s Tale by Margaret Atwood, 1987
Atwood

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
JAVA This PoD, builds off the Book class that you created on Monday (you may copy...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Java Program: Design a class “Book” with the data attributes title - String, author - String,...

    Java Program: Design a class “Book” with the data attributes title - String, author - String, yearPublished - integer and price - double. Write a parameterized constructor that initializes the attributes. Write accessor and mutator methods, and a print method that prints all of the attributes. In the main method, create a single object and give it values of your choice. Call the print method to print the values. Sample Run: The Book is: The Art of Computer Programming Donald...

  • Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double...

    Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes,...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....

    Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp. Also uses Super in the TextBook Class section. Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attributes, title and price, and get/set methods for the two attributes. - price: double Вook) Book(String, double) getTitle(): String setTitle(String): void + getPrice() double setPrice(double): void toString() String + The first constructor doesn't...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes, title...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • 1- Create the base class Book that has the following instance variables, constructor, and methods title...

    1- Create the base class Book that has the following instance variables, constructor, and methods title ( String) isbn ( String) authors (String) publisher (String) edition ( int) published_year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method // that return sting representation of Book object. 2- Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title ( String) isbn...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Write a definition for a class named Book with attributes title, price and author, where author...

    Write a definition for a class named Book with attributes title, price and author, where author is a Contact object and title is a String, and price is a float number. You also need to define the Contact class, which has attributes name and email, where name and email are both String. Instantiate a couple of Book objects that represents books with title, price and author (You can come up with the attributes values for each object) Write a function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT