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.
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
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.
C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\ JetBrains\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=61639:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\ ShipCostCalculator\out\production\ShipCostCalculator ShippingCostCalculator Enter the price of the item: $99.5 Shipping cost: $1.99 Total price: $101.49 Process finished with exit code 0 Task 2 (5 pts): Project name: BirthMonth A program that asks the user to enter their birth month (integer 1 – 12 inclusive). If the user enters a value in range, the program echoes the input (“Your birth month is: N”) If the value is not in the range it outputs an error msg (“You entered an incorrect month value: N”). Here N should be the value they entered. Test runs: (insert the output widow copies here for the test runs - valid input in range 1 - 12 - invalid input out of range Be sure to add the file to the repo and commit and push it. C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62336:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\ BirthMonth_attempt2\out\production\BirthMonth_attempt2 BirthMonth Enter your birth month (1 - 12): 1 Your birth month is: 1 Process finished with exit code 0 C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62384:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\ BirthMonth_attempt2\out\production\BirthMonth_attempt2 BirthMonth Enter your birth month (1 - 12): 33 You entered an incorrect month value: 33 Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Process finished with exit code 0 Task 3 (5 pts): Project name: PartyAffiliation (This task uses Strings:) A program that prompts the user for their party affiliation (D emocrat, R epublican, or I ndependent) and responds accordingly with a Donkey, Elephant, Person, or Other. (i.e. “You get a Democratic Donkey.”) Notes: create a menu so the user chooses D, R, or I and assume that any other choice will be Other. Tests: just the four options D, R, I, Other. Use as cascaded if structure not separate if statements! \\ Test runs: (insert the output widow copies here for the 4 test runs) Be sure to add the file to the repo and commit and push it. C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62432:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\PartyAffiliation\out\ production\PartyAffiliation PartyAffiliation Choose your party affiliation: D - Democrat R - Republican I - Independent Enter your choice: F You get Other. Process finished with exit code 0 C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62439:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\PartyAffiliation\out\ production\PartyAffiliation PartyAffiliation Choose your party affiliation: D - Democrat 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
R - Republican I - Independent Enter your choice: R You get a Republican Elephant. Process finished with exit code 0 Task 4 (5 pts): Project name: TheaterKiosk As people pass through an entry kiosk at the theater, they are prompted to enter their age. If they are 21 or older, they get a paper wrist band. Code a logic program that asks the user to enter their age and then if they are 21 or over displays a message that they get a wrist band. (Note that the program does nothing if they are not 21 or over…) Test runs: (insert the output widow copies here for the 2 test runs) Be sure to add the file to the repo and commit and push it. C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62482:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\TheaterKiosk\out\ production\TheaterKiosk TheaterKiosk Enter your age: 10 Process finished with exit code 0 C:\Users\joeyh\.jdks\openjdk-21.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\ IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62494:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 - Dsun.stderr.encoding=UTF-8 -classpath C:\Users\joeyh\IdeaProjects\TheaterKiosk\out\ production\TheaterKiosk TheaterKiosk Enter your age: 432 Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
You get a wristband. Process finished with exit code 0 Task 5 (3 pts Extra or Graduate Credit): Project name: NumCompare A program that takes two numbers as inputs from the user and compares them. It indicates if they are equal or if they are not indicates the one that is less. This program should be bullet-proofed. If either input is not a number, the program should indicate that the user should try again and terminate. EMBED SCREEN SHOTS OF NETBEANS HERE SHOWING YOUR PROGRAM RUN: Be sure to add the file to the repo and commit and push it. Submitting your work: Save or rename this word docx file as Lastname_Firstname_Lab_05.docx using your name. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Everyone submits this file. If you are using GitHub put the link to your repo here: https://github.com/JosephHeffron If you are not using GitHub: create a new compressed .zip archive folder. (Don’t give me any other type of archive!) called Lastname_Firstname_Lab_05.zip using your name. Place each of your IntelliJ project folders in this archive. ( Do not individually zip the projects! ) Place an extra copy of this word doc file with your screen output shots within the archive as well. (Don’t put this in the individual project folders, put it in the top level in the archive so I can access it easily.) Submit this archive as well If you did the extra credit option, which I urge you to try, submit the entire assignment again exactly the same way using the extra credit link. 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