Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogarciacs authored Oct 30, 2021
0 parents commit 798d866
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 0 deletions.
Binary file added Lab 1/CECS277-Lab1.pdf
Binary file not shown.
Binary file added Lab 1/Group6L1.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab 1/Lab 1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added Lab 1/out/production/Lab 1/CheckInput.class
Binary file not shown.
Binary file added Lab 1/out/production/Lab 1/Main.class
Binary file not shown.
133 changes: 133 additions & 0 deletions Lab 1/src/CheckInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import java.util.Scanner;

/**
* Static functions used to check console input for validity.
*
* Use: Place CheckInput class in the same project folder as your code.
* Call CheckInput functions from your code using "CheckInput."
*
* Example: int num = CheckInput.getInt();
*
* @author Shannon Cleary 2020
*/
public class CheckInput {

/**
* Checks if the inputted value is an integer.
* @return the valid input.
*/
public static int getInt() {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is an integer and
* within the specified range (ex: 1-10)
* @param low lower bound of the range.
* @param high upper bound of the range.
* @return the valid input.
*/
public static int getIntRange( int low, int high ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input <= high && input >= low ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a non-negative integer.
* @return the valid input.
*/
public static int getPositiveInt( ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input >= 0 ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is a double.
* @return the valid input.
*/
public static double getDouble() {
Scanner in = new Scanner( System.in );
double input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextDouble() ) {
input = in.nextDouble();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Takes in a string from the user.
* @return the inputted String.
*/
public static String getString() {
Scanner in = new Scanner( System.in );
String input = in.nextLine();
return input;
}

/**
* Takes in a yes/no from the user.
* @return true if yes, false if no.
*/
public static boolean getYesNo(){
boolean valid = false;
while( !valid ) {
String s = getString();
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) {
return true;
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) {
return false;
} else {
System.out.println( "Invalid Input." );
}
}
return false;
}
}
52 changes: 52 additions & 0 deletions Lab 1/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.Random;

class Main {
public static void main(String[] args) {

// initializing variables
Random random = new Random();

// random number between 0 - 100
int randomNumber = random.nextInt(101);

int maxRange = 100;
int guess = 0;
int minRange = 0;
int count = 0;

// initialize boolean guess
Boolean notGuessed = true;

// prompt the user to enter a number
System.out.print("I’m thinking of a number. Guess a value (1-100): ");

while (notGuessed){
// check if input is valid
guess = CheckInput.getIntRange(minRange, maxRange);

// increment counter
count ++;

// check if guess is correct
if (guess == randomNumber){
notGuessed = false;
}

// if the guess is not correct, tell the user
else{
String message = "";
// if guess is too high, store message
if (guess > randomNumber){
message = "Too High.";
//else if guess is too low, store message
} else{
message = "Too Low. ";
}
// prompt the user to ask again
System.out.print(message + " Guess again: ");
}
}
// tell the user they guessed the number correct
System.out.println("Correct! You got it in " + count + " tries.");
}
}

0 comments on commit 798d866

Please sign in to comment.