Question

Write an application TestBook that creates two Book objects with the following specifications: book1: pages ->...

Write an application TestBook that creates two Book objects with the following specifications:

book1: pages -> user input

book2: pages -> a random integer between 200-300, inclusive

The application prints to the screen the average number of pages of the two books. It then creates a third book object with number of pages equal to the sum of the number of pages of the previous two books and prints to the screen the information about that third book, i.e. toString() method.

Show how to do this toString method for this

Java

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

SOURCE CODE:

import java.util.Random; //importing random for random number generation
import java.util.Scanner;

class Book //Book class
{
   int pages; //pages instance variable
  
   public String toString() //toString() method
   {
       return " contains "+pages+" Pages.";
      
   }
}
public class TestBook {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in); //creating Scanner object
       Book book1=new Book(); //Creating book1
       System.out.println("Enter the Number of pages in Book1: ");
       book1.pages=sc.nextInt(); //reading book1 pages from user and storing in book1.pages
       Book book2=new Book(); //creating book2
       Random rand=new Random(); //creating Random object
       int rand_number=rand.nextInt((300-200)+1)+200; //generating random number (200-300)
       book2.pages=rand_number; //adding random number to book2.pages
      
       float Avg_pages=(book1.pages+book2.pages)/(float)2; //calculating average books
       System.out.println("The average number of pages of two books: "+Avg_pages); //printing avg_pages
      
       Book book3=new Book(); //creating book3
       book3.pages=(book1.pages+book2.pages); //sum of book1 and book2 pages to book3.pages
      
       System.out.println("Book3 "+book3.toString()); //calling toString() method on book3

   }

}

CODE SCREENSHOT:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write an application TestBook that creates two Book objects with the following specifications: book1: pages ->...
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
  • Write a java application that should have a menu system along the following lines (you can...

    Write a java application that should have a menu system along the following lines (you can have sub-menus if you deem that to be necessary) 1) Ask the user how many books their application should store • The application then creates a suitable storage component for the books (i.e. an array of Book objects) 2) Add book details • You as the reader can add details of a book here (i.e. a Book object’s properties are given values). 3) Display...

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

  • 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 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Write a programming java that prints the sum of cubes. Prompt for and read two integer...

    Write a programming java that prints the sum of cubes. Prompt for and read two integer values from the user and print the sum of each value raised to the third power, i.e., (?3+?3). Sample output: (user input) Enter x as an int: 3 Enter y as an int: 4 (3^3) + (4^3) = 91

  • Write an application that asks the user to enter two integers, obtains them from the user...

    Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division).(i.e. Scanner, nextInt and printf) (this has to be in java) Enter·first·integer:31 ·Enter·second·integer:69 ·31+69=100 31*69=2139 31-69=-38 31/69=0

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

  • Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and...

    Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and average. An input() method will prompt for and input a sequence of double inputs, negative to quit, calculating and updating the five fields as the doubles are input. A getMax() method returns the maximum of the doubles. A getMin() method returns the minimum of the doubles. A getSum() method returns the sum of the doubles. A getCount() method returns the number of doubles that...

  • In the Book class you created in Exercise 4a, overload the Object class Equals() method to...

    In the Book class you created in Exercise 4a, overload the Object class Equals() method to consider two Books equal if they have the same ISBN. Create a program that declares three Books; two should have the same ISBN and one should have a different one. Demonstrate that the Equals() method works correctly to compare the Books. Save the program as BookDemo2. c. Write an application that declares two Book objects and uses an extension method named DisplayTitleAndAuthor() with each....

  • 2. Create ONE java file in Dr Java according the specifications below: A. Create a Country...

    2. Create ONE java file in Dr Java according the specifications below: A. Create a Country class a. Each Country object of the Country class should know (state) i. its name ii. its area (in square miles) iii. its population b. Each Country object should be able to (behavior) i. set initial population (setter) ii. get density (number of people per square mile) (getter) iii. adjust the set population by some amount (setter) iv. provide its name, area, and population...

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