Explanation of Solution
Creating “Main.java”:
- Create a class named “Main”.
- Define the “main ()” method.
- Declare required variables.
- Do till the user enters “n” using “while” condition.
- Get the string from the user.
- Check if the length of the string is greater than or equal to 20.
- Create an object “e” for the class. Here the exception is thrown using default constructor.
- Get and print the message using the method “e.getMessage()”.
- Else,
- Print the number of characters.
- Ask whether the user wants to continue or not and store the response in a variable “response”.
- Check if the response is equal to “n”.
- Break the loop.
- Define the “main ()” method.
Creating “MessageTooLongException.java”:
- Create a class named “MessageTooLongException” that extends “Exception”.
- Define a default constructor that calls the parent class’s method using “super ()” by passing a message.
- Define a parameterized constructor that calls the parent class’s method using “super ()” by passing a message that is given as the argument.
Program:
MessageTooLongException.java:
//Define a class
public class MessageTooLongException extends Exception
{
//Default constructor
public MessageTooLongException()
{
//Call the parent class by passing the message
super("Message Too Long!");
}
//Parameterized constructor
public MessageTooLongException(String message)
{
//Call the parent class by passing the message
super(message);
}
}
Main.java:
//import the package
import java.util.Scanner;
//Main class
class Main
{
//Define main method
public static void main(String[] args)
{
//Create an object for the scanner class
Scanner sc = new Scanner(System.in);
//Declare required variables
String str, response = "y";
//Do till the user enters 'n'
while(response...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Write a Java program to read the mobile number of an Employee. If the mobile number does not start with 9 or doesn't contain exactly 8 digits, throw a user defined exception InvalidNumberException. If the number entered is valid, display the message “correct mobile number is entered!” otherwise display “Entered mobile number is invalid!!” [Hint: Use String.valueOf(num).length() methods to first convert the gsm number (num) to string and then find the length of the string]arrow_forwardSuppose you are asked to test the code below. The code: public String allocateRoom(int numOfEmps) throws IllegalArgumentException { if (numOfEmps == 0) throw new IllegalArgumentException(); if (numOfEmps < 5) return "Small"; if (numOfEmps < 10) return "Standard"; return "Large"; } Throws exception if there are no employees Returns small up to 5 employees Returns standard for 5 to 9 employees Returns large for 10 or more employees List the minimum amount of test inputs that achieve 100% branch coverage. You need to specify the branch(es) each input covers. List the minimum amount of test inputs that achieve 100% path coverage. You need to specify the path each input covers. Explain if executing the test suites above will reveal any defects.arrow_forwardTask 5: ScanningMoney.java Write a program that reads User Input using the Scanner class. Your program should ask the user how many quarters, nickels, dimes, and pennies they have, then display the total amount in dollars. This can be done in main method. Expected Output The highlighted text denotes text that you type into the console yourself. It is not printed by the program. Due to floating point values being used your answer may have the trailing 1 or not. ----JGRASP exec: java ScanningMoney Quarters: 4 Dimes: 3 Nickels: 2 Pennies: 1 You have $1.4100000000000001 ----JGRASP: operation complete.arrow_forward
- Complete the code below according to the instructions below and the example test: 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.0 Incomplete java code: public class Account{ private String accntNumber; private String accntName; private double balance; public Account(){} public Account(String num, String name, double bal){ accntNumber = num; accntName = name; balance = bal; } public double getBalance(){ return balance;}}//your class herearrow_forwardWrite the code in java and please don't plagiarize or copy from other sources write it on your own. Read carefully and follow the instructions in the question. Thank you.arrow_forwardIn Python, create a program that calculates the interest on a loan. This program should make it easy for the user to enter numbers.Specifications:*Use the Decimal class to make sure that all calculations are accurate. It should round the interest that's calculated to two decimal places. *Assume that the user will enter valid decimal values for the loan amount and interest rate with these exceptions: 1) If the user enters a dollar sign, remove it from the string begore converting the string to a number. 2) If the user enters a comma in the loan amount, remove it from the string before converting the string 3) If the user enters a K at the end of the loan amount, remove the K from the end of the string and multiply the loan amount by 1000. For example, a loan amount of 50K should be converted to a value of 50,000. 4) If the user enters a percent sign for the interest rate, remove it from the string before converting the string to a number. Sample Output: Welcome to the Interest…arrow_forward
- Java Exceptions: Your parents gave you an emergency cash card that has 2 restrictions: it can't be used to purchase alcohol, and the purchase price must be less than $100. Write a program segment in main() that will read 2 inputs from the user describing a purchase you are trying to make using the cash card: the item purchased (eg runners) and the purchase price (eg 79.99). Your code must check for the 2 restrictions mentioned above and generate exceptions if either one of them occurs (InvalidItemException or InvalidPurchasePriceException. Assume that these have already been coded so you don't need to write them). Your code must also deal with each exception by writing out an appropriate (specific) message stating what the problem is. If there is no problem your code should generate a "Payment accepted" message.arrow_forwardJava programming See attached The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. import java.util.Scanner;import java.util.InputMismatchException; public class NameAgeChecker { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputName; int age; inputName = scnr.next(); while (!inputName.equals("-1")) { // FIXME: The following line will throw an InputMismatchException. // Insert a try/catch statement to catch the exception. age = scnr.nextInt(); System.out.println(inputName + " " + (age + 1)); inputName = scnr.next(); } }}arrow_forwardThe given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34 NameAgeChecker.java import java.util.Scanner;import java.util.InputMismatchException; public class NameAgeChecker {public static void main(String[] args) {Scanner scnr = new Scanner(System.in); String inputName;int age;inputName = scnr.next();while (!inputName.equals("-1")) {// FIXME: The following line will throw an InputMismatchException.// Insert a try/catch statement to catch the exception.age = scnr.nextInt();System.out.println(inputName + " " + (age + 1));inputName = scnr.next();}}}arrow_forward
- C++ One common security function in check-writing requires that the amount be written in numbers and spelled out in words as well. Even if someone is able to alter the numerical amount of the check, it’s extremely difficult to change the amount in words. Write a program that receives a numeric check amount, that is less than $1000.00, from the user and writes the word equivalent of the amount. For example, the amount 112.43 should be written as: One Hundred Twelve and 43/100 dollars Do not accept invalid amounts. Allow the user to run the program as many times as possible until a sentinel value of zero (0) has been entered for the check amount. Don’t forget to include the developerInfo function. No input, processing, or output should happen in the main function. All work should be delegated to other functions. Include the recommended minimum documentation for each functionarrow_forwardIn Java Create a class for a runtime exception called InvalidRangeException. Then, re-write the constructor for the Airplane class so that it throws the InvalidRangeException if the range is a negative number.arrow_forwardWrite a program that will ask user to enter string, it should print valid if string start will alphabet and contain any digit in between, otherwise it should throw exception of invalid string. Also dry run your code. I need this in javaarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr