Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Peterman Publishing Company has decided that no published book should cost m

ID: 3625176 • Letter: T

Question

The Peterman Publishing Company has decided that no published book should cost more than 10 cents per page. Create a BookException class whose constructor requires three arguments: a string Book title, a double price, and an int number of pages. 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. For example, an error message might be: For Goodnight Moon, ratio is invalid. ...price is $12.99 for 25 pages. Create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. Throw a Book Exception if a client program tries to construct a Book object for which the price is more than 10 cents per page. Create a program that creates at least four Book objects-some where the ratio is acceptable and others where it is not. Catch any thrown exceptions and display the Book Exception Message. Save the file as BookExceptionDemo.cs.

Explanation / Answer

// Calculation logic : // if [(price in cents)/number of pages] > 10 ] // then // throw exception. using System; using System.Collections.Generic; namespace BookExceptionDemo { public class Book { public string title; public string author; public double price; public int pages; // supply price in dollars public Book(string title, string author, double price, int pages) { this.title = title; this.author = author; this.price = price; this.pages = pages; } } class sample { static void Main(string[] args) { Book book1 = new Book("book1","author 1", 40, 40); Book book2 = new Book("book2", "author 2", 40, 400); Book book3 = new Book("book3", "author 3", 50, 400); Book book4 = new Book("book4", "author 4", 12.83, 128); Book book5 = new Book("book5", "author 5", 12.83, 129); List list_of_books = new List(); list_of_books.Add(book1); list_of_books.Add(book2); list_of_books.Add(book3); list_of_books.Add(book4); list_of_books.Add(book5); string msg = string.Empty; double _pricePerPage = 0.0f; foreach (Book bTemp in list_of_books) { // converting price in cents _pricePerPage = (bTemp.price * 100) / bTemp.pages; try { if (_pricePerPage > 10) { msg = String.Format("For {0} , the ratio is invalid . Given price is {1} $ and number of pages is {2} " , bTemp.title, bTemp.price, bTemp.pages); throw new BookException(msg); //Throws the BookException } } catch (Exception ex) { Console.WriteLine(ex.ToString()); continue; } } Console.ReadLine(); } } public class BookException : Exception { public BookException(string msg) : base(msg) { } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote