-
-
Save rumaisaabdulhai/c85cf3262117b811e275b997dc62c32d to your computer and use it in GitHub Desktop.
Week 3 Homework Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* This class contains the solutions to | |
* the Week 3 homework exercises. | |
*/ | |
public class WeekThreeHW { | |
public static void main(String[] args) { | |
/* Problem 1: Guess the number | |
--------------------------------------------- | |
Create a program that satisfies the following: | |
- The computer has a number between 1 & 100 | |
- The user tries to guess it | |
- The computer tells it to go higher or lower | |
- If the person gets the number right, the | |
computer says so | |
*/ | |
System.out.println("\nHomework Problem 1:\n"); // to separate Problems | |
int num = 67; // variable holds the correct number | |
Scanner userInput = new Scanner(System.in); // create a scanner object | |
System.out.println("\tGuess a number from 1 to 100: "); // ask user | |
String input = userInput.nextLine(); // get and store user input in string | |
int guess = Integer.parseInt(input); // change string to int value | |
// if the guess is greater than the correct number | |
if (guess > num) { | |
System.out.println("\t" + guess + " is too high! Go lower"); | |
} | |
// if the guess is less than the correct number | |
else if (guess < num) { | |
System.out.println("\t" + guess + " is too low! Go higher"); | |
} | |
// if the guess equal to the correct number | |
else { | |
System.out.println("\tYou guessed right! Well done :)"); | |
} | |
/* Problem 2: Calculating Arithmetic Operations with User Input | |
--------------------------------------------------------------- | |
Create a program that: | |
- Asks for 2 numbers | |
- Executes all the arithmetic operations on those 2 numbers | |
and returns them to the user. | |
- Hint: Think about user data, how many variables you need, | |
and what the operations are! | |
Note: If you store numbers as double and use Double.parseDouble() | |
it is fine too! | |
*/ | |
System.out.println("\nHomework Problem 2:\n"); // to separate Problems | |
System.out.println("\tEnter first number: "); // ask user | |
// we are using scanner object from before (userInput) | |
String s1 = userInput.nextLine(); // get and store user input in string | |
int x = Integer.parseInt(s1); // change string to int value | |
System.out.println("\tEnter second number: "); // ask user | |
String s2 = userInput.nextLine(); // get and store user input in string | |
int y = Integer.parseInt(s2); // change string to int value | |
// All arithmetic operations | |
System.out.println("\t" + x + " + " + y + " = " + (x + y)); // addition | |
System.out.println("\t" + x + " - " + y + " = " + (x - y)); // subtraction | |
System.out.println("\t" + x + " * " + y + " = " + (x * y)); // multiplication | |
System.out.println("\t" + x + " / " + y + " = " + (x / y)); // division | |
System.out.println("\t" + x + " % " + y + " = " + (x % y)); // modulo | |
/* Optional Problem | |
-------------------- | |
Problem 1: Roller coaster | |
Create a program that satisfies the following: | |
- If I am taller than 1.5 m then print (“I can ride the roller coaster”) | |
- If I am taller than 1 m and older than 15 then print (“I can ride”) | |
- If there is anything else print (“I can’t ride”) | |
- Hint: Think about the variables you will need | |
Note: If you get height and age from user input that is fine too | |
*/ | |
System.out.println("\nOptional Problem 1:\n"); // to separate Problems | |
// try changing these values to see what happens | |
double height = 1.2; // in meters | |
int age = 17; | |
// If height is greater than 1.5 m | |
if (height > 1.5) { | |
System.out.println("\tI can ride the roller coaster"); | |
} | |
// If height is greater than 1 m and older than 15 | |
else if (height > 1 && age > 15) { | |
System.out.println("\tI can ride"); | |
} | |
// anything else | |
else { | |
System.out.println("\tI can't ride"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment