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 that calculates the number of cans of paint required to paint a wall, given the wall’s height and width. You will need to debug the code and 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. Use the Uploading Files to Eclipse and the Downloading Files From Eclipse tutorials to help you with this project.
Debugging
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.
Find and fix the three errors in the given code.
Use the input values below to test your program. If you found and fixed all errors, your output should exactly match what is below. TIP: When you run your program in Eclipse, it will prompt you to enter input in the Console window. If you cannot find this window, go to Window, then Show View, then Console.
Input value table with the numerals 30 and 25
Output value table with the wall area as 750.0 square feet and the paint needed as 2.142857142857143 gallons
Input Validation and Exception Handling
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. TIP: Consider using a try-catch block for exception handling, but it is not required.
Use the two sample invalid inputs below to test your program. Your program should respond to each by prompting the user to enter valid input. This should continue to loop until valid input is received.
A table with two columns titled Input 1 and Input 2. The items under Input 1 are the spelled-out number thirty and the numeral 25. The items under Input 2 are the numerals 30 and 0.
THINK: Why is 0 invalid input for this problem? What is the user being asked to input?
Guidelines for Submission
Attach your completed Paint1.java file to the assignment submission page.
Module Six Assignment Rubric
Criteria Proficient (100%) Needs Improvement (75%) Not Evident (0%) Value
Debugging Debugs a class so that no errors remain and calculations work correctly Meets some “Proficient” criteria, but with errors or exclusions such as one or two remaining bugs Does not attempt criterion 30
Input Validation and Exception Handling Adds loops to validate all user input and handle exceptions so that code passes all test cases Meets most “Proficient” criteria, but with errors; areas for improvement may include ability to handle all test cases Does not attempt criterion 70
Total: 100%
course_documents/Uploading Files to Eclipse Tutorial.pdf
Uploading Files to Eclipse This tutorial will guide you through the process of uploading a project into Eclipse via the STEM Lab.
1. Open the STEM Lab and select the Launch button for the Java Programming app. The virtual environment can take a couple of minutes to load.
2. Once the STEM Lab opens, be sure your browser window is in full-screen mode. Select the
upload icon on the STEM Lab tool bar.
3. In the File upload window, select a destination folder by clicking the Upload button. This is where the file will appear when you upload it. For this example, we will select Desktop.
4. Next, you are provided with a File Upload dialog box. Navigate to and select the file you want to
upload. Then, click the Open button. In this example, we’ve selected the studentproject.zip file.
5. Once the file uploads, it will be listed in the File Upload window and in the destination folder you selected. In this example, the studentproject.zip file appears in the File upload window, and the destination folder is the Desktop.
6. Close the File upload dialog window.
7. Locate the studentproject.zip folder. In this case, it is saved on the Desktop. Right-click on the folder and select Extract All…
8. This will bring up a dialog box asking you to select a destination for your extracted files. In this example, the default location is the Desktop. Click Extract, which will create an unzipped folder in the destination you have selected.
9. Next, open Eclipse by double-clicking the Eclipse icon located on the Desktop.
10. Select the Launch button in the Eclipse Launcher window to open Eclipse.
11. From the File menu, select Open Projects from File System. This will open up a dialog window.
12. You will use the Import dialog window to navigate to the project you uploaded. Next to the Import Source text box, click the Directory… button to browse for your project folder.
13. In the dialog box, navigate to the destination folder where you uploaded your project. In this example, the destination folder is the Desktop. Select your project folder and click the OK button.
14. After selecting the file to import, you should see the folder’s address in the Import Source textbox. Select the Finish button in the Import dialog window.
15. To access the project files, navigate to the Package Explorer workspace. You can view and open your project files in this workspace.
a) In the Package Explorer workspace, click on the arrow next to the project folder to view the
files. In this example, the folder is called studentproject. b) To open your files in the Package Explorer workspace and begin working, double-click on
each class file. Each file will have its own tab, so you can easily switch back and forth between them. Your project is now in Eclipse and ready for you to use!
course_documents/Paint1.zip
Paint1.java
Paint1.java
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
System . out . println ( “Enter wall height (feet): ” );
wallHeight = scnr . nextDouble ();
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall’s width
System . out . println ( “Enter wall width (feet): ” );
wallHeight = scnr . nextDouble ();
// Calculate and output wall area
wallArea = wallHeight * wallWidth ;
System . out . println ( “Wall area: 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” );
}
}
PastedImage_hgg7htmfhhn15bogqcv5xekee1negxqt00151476131.png
PastedImage_ybyd88qyk3okvpib5colytubnaluhllu00151476131.png
PastedImage_ri7i3ykzjq7b5ezw2wwpoymx34jv4hab