Skip to content

Instantly share code, notes, and snippets.

@maf946
Last active October 12, 2021 01:27
Show Gist options
  • Save maf946/c5383490edf9ce22273784062c06eec9 to your computer and use it in GitHub Desktop.
Save maf946/c5383490edf9ce22273784062c06eec9 to your computer and use it in GitHub Desktop.
package unit3practicequizmodel;
import java.util.Scanner;
/**
*
* @author marcfriedenberg
*/
public class Unit3PracticeQuizModel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/*1. Write a Java snippet to request a value of type double from the user (call it temp).
This is the value of a sample of water. Use the following rules to decide its state
and output the result.
If temp is 32.0 or less, output "Solid"; if it is 212.0 or higher, output "Gas";
otherwise output "Liquid".
*/
System.out.print("Please enter a temperature: ");
double temp = scnr.nextDouble();
if (temp <= 32.0)
{
System.out.println("Solid");
}
else if (temp >= 212.0)
{
System.out.println("Gas");
}
else
{
System.out.println("Liquid");
}
/*2. Examine the expression below. What is the value of the boolean variable myBoolean?
boolean myBoolean = (100 > 250 || 110 > 100) && 50 >= 250;
*/
/*
This evaluates to false. You can verify this by running
System.out.println((100 > 250 || 110 > 100) && 50 >= 250)
in NetBeans, and it will print "false."
If we break the expression into smaller pieces, we could start with:
(false || true) && false
Which further decomposes to:
true && false
This returns false. It's the same as asking "Is it true that
both 'true' and 'false' are 'true'?" It is not, which is why the
myBoolean evaluates to false.
*/
/*3. Write a Java snippet to request a value of type integer from the user (call it selection).
Validate that the user has entered an integer. If so read it, then use a switch-case statement to
branch on the value using the following rules. If the value equals 1, output "First";
if the value equals 2, output "Second", otherwise output "Invalid entry".
*/
System.out.print("Please enter an integer: ");
if (scnr.hasNextInt())
{
int selection = scnr.nextInt();
switch (selection)
{
case 1: System.out.println("First"); break;
case 2: System.out.println("Second"); break;
default: System.out.println("Invalid entry"); break;
}
}
else
{
System.out.println("Invalid entry");
}
/* 4. Write a Java snippet to determine the topic in IST 140.
Ask the user to enter the day of the week: Monday, Wednesday or Friday.
Ask the user to enter the number of the week: 7, 8, 9, or 10.
Validate that the user entered an integer. If invalid, set integer flag to true.
If valid entries:
If it is Monday, Wednesday or Friday, and it is week 7 or 8, output “The topic is loops.”
If it is Monday, Wednesday or Friday, and it is week 9 or 10, output “The topic is arrays.”
Else print a message “Invalid entry.”
*/
System.out.print("Please enter a day of the week (Monday, Wednesday, or Friday): ");
String dayOfWeek = scnr.nextLine();
/*I'm not sure we've covered the necessity of the line below. Without it,
both prompts will be displayed to the user back-to-back, without waiting
for user input in between. It would work, but wouldn't look great. The problem
is that the nextInt() call below doesn't capture the \n that the user entered
when they entered the day of the week. So, we need to capture it here, and just
stuff it in a variable that we'll never actually use. "Foo" is a common
variable name for just this sort of thing. It's a play on FUBAR, a military
term you can Google if you're interested. */
String foo = scnr.nextLine();
System.out.print("Enter the number of the week (7, 8, 9, or 10): ");
if (scnr.hasNextInt()){
int numberOfWeek = scnr.nextInt();
if (dayOfWeek.equals("Monday") || dayOfWeek.equals("Wednesday") || dayOfWeek.equals("Friday"))
{
switch (numberOfWeek)
{
case 7: System.out.println("The topic is loops."); break;
case 8: System.out.println("The topic is loops."); break;
case 9: System.out.println("The topic is arrays."); break;
case 10: System.out.println("The topic is arrays."); break;
default: System.out.println("Invalid entry."); break;
}
}
else
{
//user has entered a day other than Monday, Wednesday, or Friday
System.out.println("Invalid entry.");
}
}
else
{
//user has not entered an integer
boolean isInteger = false;
System.out.println("Invalid entry.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment