Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview Many Java programs that you create will receive and process user input. In order to ensure that user input is accurate, you will need

Overview

Many Java programs that you create will receive and process user input. In order to ensure that user input is accurate, you will need to use input validation and exception handling. For example, if you ask for a zip code, you want to ensure the user inputs a five-digit number. In this activity, you are given buggy code for a paint program. This program calculates the number of cans of paint required to paint a wall, given the walls height and width. You will first need to debug the existing code. Then you will improve it by adding code to validate user input and handle exceptions.

Prompt

For this assignment, you will complete the paint program by debugging the original code and adding code that validates user input and handles exceptions.

Before you begin, open the Virtual Lab by clicking on the link in the Virtual Lab Access module. Then open your IDE. Upload the Paint1.zip folder and review the code for the Paint1.java file. Use the Uploading Files to Eclipse Tutorial PDF to help you with this step.

First, you will debug the Paint1 class so that no errors remain and all calculations work correctly by completing the following:

Find and fix the three errors in the given code.

Use the following sample input values to test your program. If you have found and fixed all the errors, your output should exactly match the sample.

Next, you will add loops to validate all user input and handle exceptions so that code passes all test cases by completing the following:

Review the code, looking for the two code blocks where user input is required.

Implement a do-while loop in both blocks of code to ensure that input is valid and any exceptions are handled.

Use the two following sample sets of input to test your program. Since these inputs are invalid, your program should respond to each by prompting the user to enter valid input. This should continue to loop until valid input is received.

I'm a bit confused about this as it is giving me an error.

Error when looking at the console in Eclipse.

""Exception in thread ""main" java.lang.error: unresolved compilation problems:

UnputMismatchException cannot be resolved to a type

at Paint1.main(Paint1.java:29)

at Paint1.main(Paint1.java:51)"

Below is the code. Not really sure what I am doing wrong? Please be plain but give clear instruction!

import java.util.Scanner; public class Paint1 { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); double wallHeight = 0.0; double wallWidth = 0.0; double wallArea = 0.0; double gallonsPaintNeeded = 0.0; final double squareFeetPerGallons = 350.0; // Implement a do-while loop to ensure input is valid // Prompt user to input wall's height do { try { System.out.println("Enter wall height (feet): "); wallHeight = scnr.nextDouble(); if(wallHeight <= 0) { System.out.println("**Invalid Entry**"); } } catch (InputMismatchException ex) { System.out.println("Enter valid wall height..."); scnr.nextLine(); } }while ((wallHeight <= 0)); // Implement a do-while loop to ensure input is valid // Prompt user to input wall's width do { try { System.out.println("Enter wall width (feet): "); wallWidth = scnr.nextDouble(); if(wallWidth <= 0) { System.out.println("**Invalid Entry**"); } } catch (InputMismatchException ex) { System.out.println("Enter valid wall width..."); scnr.nextLine(); } } while (wallWidth <= 0); // Calculate and output wall area wallArea = wallHeight * wallWidth; System.out.println("Wall area: " + wallArea + " square feet"); // Calculate and output the amount of paint (in gallons) needed to paint the wall gallonsPaintNeeded = wallArea/squareFeetPerGallons; System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

Have the complimentary soaps and shampoos been replenished?

Answered: 1 week ago