Starting Out with Java: Early Objects (6th Edition)
6th Edition
ISBN: 9780134462011
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 10, Problem 3PC
Program Plan Intro
RetailItem Exception
Program Plan:
RetailItem.java:
- Declare the class “RetailItem”.
- Declare the private variable “desp” as “string”.
- Declare the private variable “units” as “int”.
- Declare the private variable “price” as “double”.
- Initialize the constructor “RetailItem”.
- Assign “desp” as empty string.
- Assign “units” as “0”.
- Assign “price” as”0.0”.
- Initialize the parameterized constructor “RetailItem”.
- Assign “desp” as “d”.
- Assign “units” as “u”.
- Assign “price” as “p”.
- Mutator function “setDesp()”
- Assign “desp” to “d”.
- Mutator function “setUnits()”.
- Check whether the “u” is greater than “0”
- Throw the exception.
- Otherwise, assign “u” to “units”.
- Check whether the “u” is greater than “0”
- Mutator function “setPrice()”.
- Check whether the “p” is greater than “0”
- Throw the exception.
- Otherwise, assign “p” to “price”.
- Check whether the “p” is greater than “0”
- Define the accessor function “getDesp()”, “getUnits()” and “getPrice()”.
NegativeUnitsException.java:
- Definition for “NegativeUnitsException”
- Definition for non-parameterized constructor.
- Print the statement.
- Definition for parameterized constructor
- Print the statement.
- Definition for non-parameterized constructor.
NegativePriceException.java:
- Definition for “NegativePriceException”
- Definition for non-parameterized constructor.
- Print the statement.
- Definition for parameterized constructor
- Print the statement.
- Definition for non-parameterized constructor.
RetailDemo.java:
- Import the required packages.
- Definition for “RetailDemo” class.
- Definition for main class.
- Declare the required variables.
- Create an object for scanner class.
- Create a retail item object.
- Get the description from the user.
- Set the item description.
- Get the items units.
- Inside the try block,
- Set the item units.
- Assign “true” to “input.”
- Inside the catch block,
- Print the error
- Assign “false” to “input”.
- Inside the try block,
- Get the items price.
- Inside the try block,
- Set the item price.
- Assign “true” to “input.”
- Inside the catch block,
- Print the error
- Assign “false” to “input”.
- Inside the try block,
- Print the description, price and units of the item.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a Month class that holds information about the month. Write exception classes for the following error conditions:• A number less than 1 or greater than 12 is given for the month number.• An invalid string is given for the name of the month.Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.
Develop an exception class named InvalidMonthException that extends the Exception class. Instances of the class will be thrown based on the following conditions:
The value for a month number is not between 1 and 12
The value for a month name is not January, February, March, … December
Develop a class named Month. The class should define an integer field named monthNumber that holds the number of a month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods:
A no-argument constructor that sets the monthNumberto 1.
An overloaded constructor that accepts the number of the month as an argument. The constructor should set the monthNumberfield to the parameter value if the parameter contains the value 1 – 12. Otherwise, throw an InvalidMonthException exception back to the caller. The exception should note that the month number was incorrect.
An overloaded constructor that accepts a string containing the name of the month,…
Define an exception class called CoreBreachException. The class should have a default
constructor. If an exception is thrown using zero-argument constructor, getMessage should
return "Cor Breach Evacuation Ship!" The class should also define a constructor having a single
parameter of type String. If an exception is thrown using constructor, getMessage should return
the value that was used as an argument to constructor.
Chapter 10 Solutions
Starting Out with Java: Early Objects (6th Edition)
Ch. 10.1 - Prob. 10.1CPCh. 10.1 - Prob. 10.2CPCh. 10.1 - Prob. 10.3CPCh. 10.1 - Prob. 10.4CPCh. 10.1 - Prob. 10.5CPCh. 10.1 - Prob. 10.6CPCh. 10.1 - Prob. 10.7CPCh. 10.1 - Prob. 10.8CPCh. 10.1 - Prob. 10.9CPCh. 10.1 - When does the code in a finally block execute?
Ch. 10.1 - What is the call stack? What is a stack trace?Ch. 10.1 - Prob. 10.12CPCh. 10.1 - Prob. 10.13CPCh. 10.1 - Prob. 10.14CPCh. 10.2 - What does the throw statement do?Ch. 10.2 - Prob. 10.16CPCh. 10.2 - Prob. 10.17CPCh. 10.2 - Prob. 10.18CPCh. 10.2 - Prob. 10.19CPCh. 10.3 - What is the difference between a text file and a...Ch. 10.3 - What classes do you use to write output to a...Ch. 10.3 - Prob. 10.22CPCh. 10.3 - What class do you use to work with random access...Ch. 10.3 - What are the two modes that a random access file...Ch. 10.3 - Prob. 10.25CPCh. 10 - Prob. 1MCCh. 10 - Prob. 2MCCh. 10 - Prob. 3MCCh. 10 - Prob. 4MCCh. 10 - FileNotFoundException inherits from __________. a....Ch. 10 - Prob. 6MCCh. 10 - Prob. 7MCCh. 10 - Prob. 8MCCh. 10 - Prob. 9MCCh. 10 - Prob. 10MCCh. 10 - Prob. 11MCCh. 10 - Prob. 12MCCh. 10 - Prob. 13MCCh. 10 - Prob. 14MCCh. 10 - Prob. 15MCCh. 10 - This is the process of converting an object to a...Ch. 10 - Prob. 17TFCh. 10 - Prob. 18TFCh. 10 - Prob. 19TFCh. 10 - True or False: You cannot have more than one catch...Ch. 10 - Prob. 21TFCh. 10 - Prob. 22TFCh. 10 - Prob. 23TFCh. 10 - Prob. 24TFCh. 10 - Find the error in each of the following code...Ch. 10 - // Assume inputFile references a Scanner object,...Ch. 10 - Prob. 3FTECh. 10 - Prob. 1AWCh. 10 - Prob. 2AWCh. 10 - Prob. 3AWCh. 10 - Prob. 4AWCh. 10 - Prob. 5AWCh. 10 - Prob. 6AWCh. 10 - The method getValueFromFile is public and returns...Ch. 10 - Prob. 8AWCh. 10 - Write a statement that creates an object that can...Ch. 10 - Assume that the reference variable r refers to a...Ch. 10 - Prob. 1SACh. 10 - Prob. 2SACh. 10 - Prob. 3SACh. 10 - Prob. 4SACh. 10 - Prob. 5SACh. 10 - Prob. 6SACh. 10 - What types of objects can be thrown?Ch. 10 - Prob. 8SACh. 10 - Prob. 9SACh. 10 - Prob. 10SACh. 10 - What is the difference between a text file and a...Ch. 10 - What is the difference between a sequential access...Ch. 10 - What happens when you serialize an object? What...Ch. 10 - TestScores Class Write a class named TestScores....Ch. 10 - Prob. 2PCCh. 10 - Prob. 3PCCh. 10 - Prob. 4PCCh. 10 - Prob. 5PCCh. 10 - FileArray Class Design a class that has a static...Ch. 10 - File Encryption Filter File encryption is the...Ch. 10 - File Decryption Filter Write a program that...Ch. 10 - TestScores Modification for Serialization Modify...Ch. 10 - Prob. 11PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Define an exception class called CoreBreachException. The class should have a default constructor. If an exception is thrown using this zero- argument constructor, getMessage should return "Core Breach! Evacuate Ship!" The class should also define a constructor having a single parameter of type String. If an exception is thrown using this constructor, getMessage should return the value that was used as an argument to the constructor.arrow_forwardUsing Java Define a Rubric class that extends the GradedActivity class shown below. The Rubric class should determine the grade a student receives for a chapter lab. Using the following points: Algorithm: 20 points Variables: 5 points Formulas: 5 points Test Data: 15 points UML: 20 points Code: 35 points Create exception classes for 2 error conditions. You determine which errors for the exceptions. Demonstrate the class in a simple program. GradedActivity public class GradedActivity { private double score; // holds the score public void setScore (double s) { score=s; } //end method for setting the score public double getScore() { return score; }// end method to return the score public char getGrade() { // return a letter grade determined by the scores listed below char letterGrade; if (score >= 90) letterGrade = 'A'; else if (score >=80) letterGrade = 'B'; else if (score >=70)…arrow_forwardin C# i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and 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. my errors are Test Case IncompleteComplete program walkthrough, test 1 InputBook1Author110.99200Book2Author219.99100Book3Author225.00600Book4Author35.0020Book5Author48.00120OutputBook1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $19.99 100 pages.Book3 by Author2 Price $25.00 600 pages.Book4 by Author3 Price $5.00 20 pages.Book5 by Author4 Price $8.00 120 pages. ResultsFor Book2, ratio is invalid....Price is $19.99 for 100 pages.For Book4, ratio is invalid....Price is $5.00 for 20 pages.Book1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $10.00 100…arrow_forward
- in c # i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and 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. my errors are Test Case IncompleteComplete program walkthrough, test 1 InputBook1Author110.99200Book2Author219.99100Book3Author225.00600Book4Author35.0020Book5Author48.00120OutputEnter information for book #1:Title: Author: Price: Number of pages: Unhandled Exception:System.DivideByZeroException: Value was either too large or too small for a Decimal. at System.Decimal+DecCalc.VarDecDiv (System.Decimal& d1, System.Decimal& d2) [0x0003e] in <7b90a8780ac4414295b539b19eea7eea>:0 at System.Decimal.Divide (System.Decimal d1, System.Decimal d2) [0x00000] in…arrow_forwardThis is a java program read the steps carefully please : Design and implement a customized exception in a real scenario Your assignment must contain with the proper cover page:1) Description of the scenario that you decided2) Design the model of your work using Flowchart3) Implement your design model4) Your output results (clear screenshot).arrow_forwardQuestion 3 } public class WeekDay { static void daysFromSunday(int day) { String dayName = ""; try { } } if (day > 7) { throw new Exception("Invalid Day! Too high!"); } else if(day < 1){ throw new Exception("Invalid Day! Too low!"); } switch(day){ case 1: dayName="Sun"; break; case 2: dayName="Mon"; break; case 3: dayName="Tue"; break; case 4: dayName="Wed"; break; } ▬▬ } case 5: dayName="Thu"; Dreak; case 5: dayName="Thu"; break; case 6: dayName="Fri"; break; default: dayName="Sat"; System.out.println(dayName); } catch (Exception excpt) { System.out.println("Oops"); System.out.println(excpt.getMessage()); }finally{ System.out.println("Done!"); public static void main(String[] args) { daysFromSunday(10); daysFromSunday(3); daysFromSunday(0); OOops Invalid Day! Too high! Tue Oops Invalid Day! Too low! OOops Invalid Day! Too high! Done! Tue Done! Oops Invalid Day! Too low! Done! O Oops Invalid Day! Too high! Done! Oops Invalid Day! Too high! Done! Oops Invalid Day! Too low! Done! O Oops…arrow_forward
- C# program in Visual Studio Code. The main application class must meet the following requirements Ask the user to enter their age If the age is between 1 and 100 (inclusive) print a message stating the age the user entered exit the program If the age is less than 1 or greater than 100 generate an ArgumentOutOfRangeException print the exception information print an error message If the value is not an integer handle the FormatException print the exception information print an error message Continue asking the user to enter their age until a valid age is enteredarrow_forwardProgramming Problem 1 – CycleThrowTryCatch Revisit the Cycle class in Module 3. Modify your application such that the properties, numberOfWheels and weightare entered as double values interactively (at the keyboard). Exception handling will be used to determine whether a type mismatch occurs. Edit your application such that, in addition to [A], the values for numberOfWheels and weight, entered interactively, will throw a new exception “Values cannot be less than or equal to zero” only If the values are less than or equal to zero. Add or use the appropriate try and/or catch blocks. Directions Examine your application for the class called Cycle. Add Try and Catch blocks appropriately. Add the throw statement for the new exception. Display an appropriate message if an exception occurs. Display the properties of the object.arrow_forwardCreate an application that you may use to monitor your progress with respect to your curriculum. The application will record the subjects that you have to enroll in during every school term. The application should also record the grades for the subjects that you have enrolled in. The application will allow a view of the subjects that you have finished as wellas those that you will yet enroll in. The application should also provide your general weighted average. Use: Reference classes and exception handling Assumption: We are only considering the first and second years. You have enrolled in 10 subjects and have not enrolled in 10 subjects. List of subjects taken (along with their corresponding units): PHYSICAL ACTIVITY TOWARDS HEALTH AND FITNESS 2 ART APPRECIATION 3 READINGS IN PHILIPPINE HISTORY 3 UNDERSTANDING THE SELF 3 INTRODUCTION TO COMPUTING (LEC) 2 INTRODUCTION TO COMPUTING (LAB) 1 COMPUTER PROGRAMMING 1 (LEC) 2 COMPUTER PROGRAMMING 1 (LAB) 1 DISCRETE MATHEMATICS 3 PURPOSIVE…arrow_forward
- Java Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0arrow_forwardIn C#,arrow_forwardCreate a flowchart and modify the code. INSTRUCTION: Create a new class called CalculatorWithMod. This class should be a sub class of the base class Calculator. This class should also have an additional method for calculating the modulo. The modulo method should only be seen at the sub class, and not the base class! Include exception handling for instances when dividing by 0 or calculating the modulo with 0. You would need to use throw, try, and catch. The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.Example: 20 mod 3 equals 2Because 20/3 = 6 with a remainder of 2 CODE TO COPY: #include <iostream> using namespace std; class Calculator{public:Calculator(){printf("Welcome to my Calculator\n"); } int addition(int a, int b);int subtraction(int a, int b);int multiplication(int a, int b);float division(int a, int b);}; int Calculator::addition(int a, int b){return (a+b);} int Calculator::subtraction(int a, int b){return (a-b);} int…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr