Heffron_Joseph_Lab_05

.docx

School

University of Cincinnati, Main Campus *

*We aren’t endorsed by this school

Course

1090C

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

9

Uploaded by AgentJackal749

Report
IT 1090C Computer Programming I IT 6090C Java Programming Prof. Tom Wulf Lab 05 Conditionals 20 pts (3 gr or extra credit pts) Learning Goals: Conditional Structures in Java if, if else, cascaded if, nested if, switch Input with Scanner Testing. Make sure your programs run by testing them. Correct them if they do not. This is an individual lab. Complete each of the programs here. (I may do the first with you in class.) Note that these are programs that we previously did as pseudo code. Copy or re-write the pseudo code as single line // java comments as an outline for your program. Create a separate IntelliJ java project for each program using the names I specified. Don’t forget to code the pseudo code for your program first within the java main() section as single line comments // comment….. Then code the java statements for the pseudo code. In each task, use a test suite that covers each of the logical paths the program can take. e.g. (for the first program) user enters an item less than $100, or user enters one > $100 Your screen shots should support that you did this testing! (Provide shots for each case, for each task.) We will use scanner for input. Since we cant loop yet, we can only halt the program under control if the user makes an error. Later, we can loop back and repeat until they get it right. If your instructor directed you to use GitHub create a single repo for the lab. All University Students should be using GitHub. Mini lecture: We use the Scanner class to get input from the user through the console. We have to import the Scanner class and create an instance of Scanner using the default input stream System.in. import java.util.Scanner; // imports go at the top of the file BEFORE the class! Scanner in = new Scanner(System.in); // it is very common to name the Scanner “in” or console // Scanner has a variety of methods that read data of a specific Java type: int val = in. nextInt() ; // reads an int value throws an error if the input cannot be an int Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
double doubleVal = in. nextDouble() ; // reads a double value error if the input isn’t a double String line = in. nextLine (); // reads an entire line to a \n newline character as a String String word = in. next (); // reads part of the line up to a delimiter (space) or the \n … The methods that return numeric types can throw an error if the input cannot be parsed into the correct type. The following methods allow you to safely determine if you can successfully read the numeric value: in. hasNextInt(); // returns true if nextInt will succeed in. hasNextDouble(); // returns true if nextDouble will succeed Since anything the user types can be read in as a legal java String, the next() and nextLine() methods cannot generate an error. We will need to use the java if and if .. else structures for this. So let’s take a look at them now. if (CONDITION) { Code statements in this block execute if the CONDITION is true } // this is the end of the block like the endIf in our pseudo code And here is the if..else: if (CONDITION) { Code statements in this block execute if the CONDITION is true } else { Code statements in this block execute if the CONDITION is false } // this is the end of the block like the endIf in our pseudo code C ascaded if: if(CONDITION) { } else if (CONDITON) { } // more else if (CONDITON) blocks go here Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
else // no condition a default when all the other if tests fail { } Here is an example code fragment that shows how to safely input a number: import java.util.Scanner; // before the class at top of the file // this code is in main: Scanner in = new Scanner(System.in); // create a Scanner in to read from the console double wage = 0; System.out.print(“Enter your hourly wage: “); if(in.hasNextDouble()) { // OK safe to read in a double wage = in.nextDouble(); in.nextLine(); // clear the buffer System.out.println(“\nYou said your wage was: “ + wage); } In this example (above) we leave wage equal to 0 if the user enters something other than a valid amount. (i.e. double) Here is a better approach with if else: import java.util.Scanner; // before the class at top of the file // this code in main: Scanner in = new Scanner(System.in); double wage = 0; String trash = “”; // use for bad input which will read as a String System.out.print(“Enter your hourly wage: “); if(in.hasNextDouble()) { // OK safe to read in a double wage = in.nextDouble(); Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
in.nextLine(); // clears the newline from the buffer } else { // Not a double can’t use nextDouble() read as String with nextLine() instead trash = in.nextLine(); // Ok have to read the input as a String System.out.println(“\nYou said your wage was: “ + trash); System.out.println(“Run the program again and enter a valid amount!”); } As you test each program, you have to run it multiple times. Instead of screen shots, just copy the output window from IntelliJ into this doc for EACH test run. There will be several for each program. Please make sure that your output is readable, that’s what we use to grade your work. Task 1 (5 pts): Project name: ShipCostCalculator [Create a new Java file with this name and include a main method OR rename the existing main.java. Add your new file to the GitHub repo and commit it.] An application program where the user enters the price of an item and the program computes shipping costs. If the item price is $100 or more, then shipping is free otherwise it is 2% of the price. The program should output the shipping cost and the total price. Test runs: (insert the output widow copies here for the test runs) - valid input less than 100 - valid input greater than 100 Be sure to add the file to the repo and commit and push it. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help