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...

  • 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....

  • 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,...

    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...

  • 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...

  • 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...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

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
ADVERTISEMENT