Question

Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attribute

Problem 2 (15 points) Reuse the Book.java as a parent class, refer to the following requirements and UML diagram, The first c

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 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. Questions: 1) Define the Book.java class according to the above diagram and description 2) Then define another class named BookApp.java which has a main() method. This BookApp class interacts with the Book class as follows: 2.1) you 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.5 to price. Finally, using b1 to call toString() method and print the returned value from the toString() method 2.2) You create another Book object, named b2, by calling the second Book constructor and passing "Ann Smith" as the first argument and 9.7 as the second argument 2.3) Finally, using b2 to call toString() method and print the returned value from the toString() method 2.4) Run the BookApp program to make sure it works correctly
Problem 2 (15 points) Reuse the Book.java as a parent class, refer to the following requirements and UML diagram, The first constructor, TextBook(), doesn't have a parameter. It assigns "" to title, 0.0 to price, 0 to courselD; The second constructor, TextBook(String, double, int) uses three passed-in parameters to initialize the three attributes toString( in TextBook returns a string value for the three attributes, title, price, and courselD. Book Questions: - title: String 1) Define a child class, TextBook, based on above requirement - price: double Book() Book(String, double) getTitle(): String and the diagram + 2) Define another class, TextBookApp. This TextBookApp class + + interacts with the Book and TextBook classes as follows: setTitle(String): void getPrice(): double setPrice(double): void toString(): String 2.1) Create a Book object, named b, by calling the second Book + constructor and passing "Ann Smith" as the first argument and + 9.7 as the second argument. Use b to call toString() method and print the returned value from the toString() method. 2.2) Create a TextBook object, named tb1, by calling the first TextBook TextBook constructor. Then using the object tb1 to call methods - courselD: int TextBook( TextBook(String, double, int) to assign "John Doe" to title, 8.5 to price, and 2050 to courselD. Finally, using tb1 to call toString() method and print out the getCourselD): int setCourselD(int): void toString(): String + returned value. + 2.3) Create another TextBook object, named tb2, by calling the second TextBook constructor and passing "Ann Smith" as the first argument, 9.7 as the second argument, and 3090 as the third parameter. Use tb2 to call toString() method and print out the returned value. 2.4) Run TextBookApp to make sure it works correctly
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I am providing the answers for above questions:

Problem 1:

class Book
{
String title;
double price;
public Book() //first constructor(default consructor)
{
this.title="";
this.price=0.0;
}
public Book(String title,double price) //second constructor with parameters
{
this.title=title;
this.price=price;
}
public String getTitle() //getTitle method to return the title
{
return this.title;
}
public void setTitle(String title) //setTitle method to assign the title
{
this.title=title;
}
public double getPrice() //getPrice method to return the price
{
return this.price;
}
public void setPrice(double price) //setTitle mehtod to assign the price
{
this.price=price;
}
public String toString() //toString method to print details
{
return "\nTitle:"+this.title+"\nPrice:"+this.price;
}
}
public class BookApp
{
public static void main(String args[])
{
Book b1=new Book(); //creating the first object b1 by calling default constructor
b1.setTitle("John Doe");
b1.setPrice(8.5);
System.out.println(b1.toString()); //calling toString method to print details of book1
  
Book b2=new Book("Ann Smith",9.7); //creating the second object b2 by calling parameter constructor.
System.out.println(b2.toString()); //calling toString method to print details of book2
  
}
}

Output:

Title: John Doe Price 8.5 Title:Ann Smith Price 9.7

Problem 2:

class TextBook extends Book //TextBook class extending the Book Class
{
int courseID;
public TextBook() //first constructor(default consructor)
{
super("",0.0);
this.courseID=0;
}
public TextBook(String title,double price,int courseID) //second constructor with parameters
{
super(title,price);
this.courseID=courseID;
}
public int getCourseID() //getCourseID method to return the courseID
{
return this.courseID;
}
public void setCourseID(int courseID) //setCourseID method to assign the courseID
{
this.courseID=courseID;
}
public String toString() //toString method to print details
{
return super.toString()+"\nCourseID:"+this.courseID;
}
}

public class TextBookApp
{
public static void main(String args[])
{
Book b=new Book("Ann Smith",9.7); //creating the book object b by calling parameter constructor
System.out.println(b.toString()); //printing the details by calling toString method.
  
TextBook tb1=new TextBook(); //creating the first object tb1 by calling default constructor
tb1.setTitle("John Doe");
tb1.setPrice(8.5);
tb1.setCourseID(2050);
System.out.println(tb1.toString()); //printing the details
  
TextBook tb2=new TextBook("Ann Smith",9.7,3090); //creating the second object t2 by calling parameter constructor.
System.out.println(tb2.toString()); //printing the details
  
}
}

Output:

Title Ann Smith Price 9.7 Title John Doe Price 8.5 CourseID: 2050 Title Ann Smith Price: 9.7 CourseID: 3090

Hoping that the above answers will help you...Thank you...

Add a comment
Know the answer?
Add Answer to:
Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....
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
  • 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....

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

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

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

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

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to...

    Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....

  • re-implement the Bookshelf class using a linkedlist and a Node class. Implement the above add, remove,...

    re-implement the Bookshelf class using a linkedlist and a Node class. Implement the above add, remove, and search methods again. here is the code for that: public class Book {    private String title; private double price;    public Book() { title=""; price=0; }    public Book(String title, double price) { this.title = title; this.price = price; }    public void setTitle(String title) { this.title = title; }    public void setPrice(double price) { this.price = price; }    public...

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