Question

In: Computer Science

C#: A book publisher has limited the cost of every book they publish to no more...

C#:

A book publisher has limited the cost of every book they publish to no more than 10 cents per page.

  1. Create a BookException class with a constructor that requires three (3) arguments for each book: a string title, a double price and an int number of pages.

  2. Create an error message that is passed to the Exception class constructor for the Message property when a book does not meet the price-to-pages ratio. The error message might be:

                                                                         For Goodnight Moon, ratio is invalid.

                                                                         ...Price is $12.99 for 25 pages

    The price-to-pages ratio is determined by checking the following:  price > RATE * pages where RATE = 0.10

  3. Create a Book class that has the following fields:  string title, string author, double price and int number of pages.

  4. Create properties (accessors) for each field.

  5. Throw a BookException if a book object is created that has a price that is more than 10 cents per page.

  6. In Main create 4 Book objects and use the constructor for Book to pass the four inputs when each object is instantiated, some where the ratio is correct and some where the ratio is not correct.

  7. Catch any thrown exceptions and display BookException Message.

  8. Internal Documentation.

  9. Possible output:

        The inputs passed were:

                ("Goodnight Moon", "Brown", 12.99, 25)

                ("World History", "Stein", 72.99, 900)

                ("The Grapes of Math", "Stoltz", 30.99, 300)

                ("Steal This Book", "Hoffman", 72.99, 800)

        The results were:

    For Goodnight Moon, ratio is invalid.
    ...Price is $12.99 for 25 pages.
    For The Grapes of Math, ratio is invalid.
    ...Price is $30.99 for 300 pages.
    Press any key to continue . . .

Solutions

Expert Solution

Working code implemented in C# and appropriate comments provided for better understanding:

Source code for BookException.cs:

using System;
class BookExceptionDemo
{
static void Main()
{
try
{
Book book1 = new Book("Goodnight Moon", "Brown",
12.99, 25);
}
catch (BookException e)//except thrown in Book-class-objects are caught here(in client-class)
{
Console.WriteLine(e.Message);
}
try
{
Book book2 = new Book("World History",
"Stein", 72.99, 900);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
try
{
Book book3 = new Book("The Grapes of Math", "Stoltz",
30.99, 300);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
try
{
Book book4 = new Book("Steal This Book",
"Hoffman", 72.99, 800);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
}
}

class Book
{
private string title; //since property is NOT auto-implemented, so we need to declare these fields too.
private string author;
private double price;
private int pages;
const double RATE = 0.10;

public Book(string title, string author, double price,
int pages)
{
Title = title;
Author = author;
if (price > RATE * pages)
throw (new BookException(title, price, pages)); //no "catch" in this class. excep is caught in the client-class, which must have try-catch block.
Price = price;
Pages = pages;
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Pages
{
get
{
return pages;
}
set
{
pages = value;
}
}

}

public class BookException : Exception
{
public BookException(string title, double price, int pages) :
base("For " + title + ", ratio is invalid.\n...Price is " + price.ToString("C") + " for " + pages + " pages.")
{
}
}

Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.


Related Solutions

A book publisher is planning to produce a book in three different bindings: paperback, book club...
A book publisher is planning to produce a book in three different bindings: paperback, book club and library. A paperback takes 2 minutes to sew, 4 minutes to glue, and sells for a profit of 25 cents. A book club edition takes 2 minutes to sew, 6 minutes to glue, and sells for a profit of 40 cents. A library edition takes 3 minutes to sew, 10 minutes to glue, and sells for a profit of 60 cents. The sewing...
The East Chester Tribune must decide whether to publish an online Sunday edition. The publisher thinks...
The East Chester Tribune must decide whether to publish an online Sunday edition. The publisher thinks that the probability that this Sunday edition would be a success is equal to 60%, and it will be failure 40% of the time. If it is a success the profit will be $100. If it is a failure, it will be only $64. a. If the manager is risk-averse (consider ?(?)=√?) what will be the premium for an insurance that will assure the...
Imagine a Book table that had the following: Book Table BookId Category Price Discount Publisher Date...
Imagine a Book table that had the following: Book Table BookId Category Price Discount Publisher Date Next, let's say you had to group by Publisher and Category while retrieving the total price and total number of rows within that grouping. 1. Write the SQL to figure out how many types of Publisher values and Category values there are. 2. How would you write an equation to demonstrate the number of groupings you would expect if you knew the exact number...
Gansac Publishing Company signed a contract with an author to publish her book. The signing took...
Gansac Publishing Company signed a contract with an author to publish her book. The signing took place on January 1, 2016, and a payment of $20,000 was made to obtain a copyright. Gansac expects to sell 200,000 books evenly between 2016 and 2020 at a price of $10 per book. Required: 1. Prepare journal entries to record the events related to the copyright and sales of the book during 2016 and 2017, assuming that sales were as projected. 2. Next...
Every company has limited or not enough of what is needed, such as not having enough...
Every company has limited or not enough of what is needed, such as not having enough staff, money , time, etc., available for projects, and the constant need to develop processes, increase capacity and create new products, and countless of other needs. It is therefore necessary to use effective methods to select the most crucial and beneficial projects to run the business. Find two approaches that you believe would provide an effective way to select the most important and most...
The publisher of a regional telephone book surveyed a simple random sample of 10 of its...
The publisher of a regional telephone book surveyed a simple random sample of 10 of its commercial yellow-page ad customers to determine whether the size of their ads in square inches were correlated with the proportion of calls to the business that were generated by the ads. a. Calculate the correlation coefficient b. Calculate the coefficient of determination What percentage of the variation in the proportion of calls generated by the ad is explained by the size of the ad...
A book publisher knows that it takes an average of nine business days from when the...
A book publisher knows that it takes an average of nine business days from when the material for the book is finalized until the first edition is printed and ready to sell. Suppose the exact amount of time has a standard deviation of four days. Use an appropriate normal transformation to answer the following questions. a. Suppose the publisher examines the printing time for a sample of 36 books. What is the probability that the sample mean time is shorter...
the publisher decided to introduce the book to the market with five different colour, each having...
the publisher decided to introduce the book to the market with five different colour, each having a different colour. the first edition includes 300 copies the following frequency of each colour is observed red 52 blue 77 green 62 pink 64 orange 45 the publisher wants to check if there is evidence that fhe number of each equal is not equal 1) write the hypothesis of the test for this case 2) based on data, what is the test stat...
Exercise 4. A publisher for textbooks has a total cost of TC(Q) = 25,000 − 50Q...
Exercise 4. A publisher for textbooks has a total cost of TC(Q) = 25,000 − 50Q + 15Q^2. a) Find the publisher’s marginal cost, average cost, average variable cost, and average fixed cost. b) Find the value of Q for where the marginal cost curve crosses the average cost curve and average variable cost curve. c) Find the output elasticity ε_TC,Q
A particular book publisher is thinking about starting up a new national magazine in a small...
A particular book publisher is thinking about starting up a new national magazine in a small town. It's thought that this publisher would have to get over 12% of the book market to be financially secure. While planning to launch this magazine, a survey was taken of a sample of 400 readers. After providing an inside look into this magazine, one question asked the participants if they would subscribe to this magazine if the cost didn't exceed $20 per month....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT