Question

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. Define the Book.java class. 2. define another class named BookApp.java which has a main() method. 3. create a Book object, named b1, by calling the first Book constructor. Then using the object b1 to call setTitle() and setPrice() methods to assign “John Doe” to title and 8.1 to price. using b1 to call toString() method and print the returned value from the toString() method. create another Book object, named b2, by calling the second Book constructor and passing Ann Smith as the first argument and 9.2 as the second argument. using b2 to call toString() method and print the returned value from the toString() method.

in JAVA (NetBeans IDE 8.2)

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

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here below java classes are created.

Book.java :

//Java class
public class Book {
//attributes
   private String title;
   private double price;
   //default constructor
   public Book()
   {
       this.title="";//set title to empty string
       this.price=0.0;//set price to 0
   }
   //parameterized constructor
   public Book(String t, double p)
   {
       this.title=t;
       this.price=p;
   }
   //getter methods
   public String getTitle() {
       return this.title;//return title
   }
   public double getPrice() {
       return this.price;//return price
   }
   //setter methods
   public void setTitle(String t)
   {
       this.title=t;//set title
   }
   public void setPrice(double p)
   {
       this.price=p;//set price
   }
   //method to return title and price
   public String toString()
   {
       return "Title : "+this.title+", Price : "+this.price;//return title and price
   }
}

**********************************

BookApp.java :

//Java class
public class BookApp {
   //entry point of class , main method
   public static void main(String[] args) {
       //creating object of Book class
       Book b1=new Book();
       b1.setTitle("John Doe");//set title
       b1.setPrice(8.1);//set price
       //print title and price using b1
       System.out.println(b1.toString());
       //creating object of Book class
       Book b2=new Book("Ann Smith",9.2);
       //print title and price using b2
       System.out.println(b2.toString());
   }

}

======================================================

Output : Compile and Run BookApp.java to get the screen as shown below

Screen 1 :BookApp.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double...
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
  • 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...

  • 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 class named Book containing: Two instance variables named title and author of type String....

    Write a class named Book containing: Two instance variables named title and author of type String. A constructor that accepts two String parameters. The value of the first is used to initialize the value of title and the value of the second is used to initialize author. A method named toString that accepts no parameters. toString returns a String consisting of the value of title, followed by a newline character, followed by the value of author.

  • Below is a class definition (.h) for the Bookclass. class Book { private:     int numPages;     string...

    Below is a class definition (.h) for the Bookclass. class Book { private:     int numPages;     string author;     string isbn;     double price; public:     string title;      public:     Book ();     void setAuthor(string);     string getAuthor();     void setIsbn(string);     string getIsbn ();     void setPrice(double);     double getPrice ();     string getTitle (); };                Assume you are writing code within the maindriver program: declare variable of type Book. Using the Bookvariable, set your book’s author to Edgar Allan Poe Using the Bookvariable, set your book’s title to The...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

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

  • Write an entire class called Movie that stores an instance variable named title for the title...

    Write an entire class called Movie that stores an instance variable named title for the title of the movie as a string. Write the corresponding getter and setter methods (getTitle, setTitle), and a constructor that takes one parameter (string for the title). Include both the class definition and method definitions! Be sure to use appropriate visibility modifiers. Assume all includes and using namespace std are already in the file. Do not try to make header files.

  • 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