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

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

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

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

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

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

  • Write a class called Book. Here are the relevant attributes: title author yearPublished            bookPriceInCAD Provide...

    Write a class called Book. Here are the relevant attributes: title author yearPublished            bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...

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

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