-
-
Save rumaisaabdulhai/d83ffbfea1a4e6e84705e770186540d0 to your computer and use it in GitHub Desktop.
Week 4 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 4 homework exercises. | |
*/ | |
public class WeekFourHW { | |
public static void main(String[] args) { | |
// -------- MANDATORY PROBLEMS -------- | |
/* Problem 1: Get Average Score | |
Suppose the following int array called math_scores is defined that | |
contains the scores of students on a math test: | |
*/ | |
int[] math_scores = new int[]{93, 85, 82, 89, 80, 82, 87, 94, 100}; | |
/* Your task: Using a for-each loop, create a Java program that calculates | |
the sum of elements in the array. Then, using this sum and the length of | |
the array, calculate the average score on the math test. Finally, print | |
the average score. | |
*/ | |
int sum = 0; | |
for (int num: math_scores) { | |
sum = sum + num; | |
} | |
int num_scores = math_scores.length; | |
int average = sum / num_scores; | |
System.out.println("\nMandatory Problem 1\n"); // to separate problems | |
System.out.println("\tAverage score: " + average); | |
/* Problem 2: Doubling Numbers | |
Suppose the following double array called numbers is defined that | |
contains the following decimal numbers: | |
*/ | |
double[] numbers = new double[]{33.1, 57.2, 90.4, 78.6, 112.3, 160.5}; | |
/* Your task: Using a for loop, create a Java program that doubles all | |
the numbers in the array. Then, print the array. Note that | |
System.out.println(numbers); does not work for the second part! | |
*/ | |
System.out.println("\nMandatory Problem 2\n"); // to separate problems | |
// doubling the numbers | |
for (int i = 0; i < numbers.length; i++) { | |
numbers[i] = numbers[i] * 2; | |
} | |
// printing the array | |
System.out.print("\tNumbers: { "); | |
for (int j = 0; j < numbers.length; j++) { | |
if (j != numbers.length - 1) | |
System.out.print(numbers[j] + ", "); | |
else | |
System.out.print(numbers[j]); | |
} | |
System.out.println(" }"); | |
// -------- OPTIONAL PROBLEMS -------- | |
/* | |
Problem 1: Find the False | |
Suppose the following boolean array called bool_list is defined that | |
contains the following boolean values: | |
*/ | |
boolean[] bool_list = new boolean[]{true, false, true, true, true, false, | |
false, true, true, true, true, false}; | |
/* | |
Your task: Create a Java program to find all the locations in the array where | |
there is a false value. If there is a false value, then print “There is a | |
false value at ” + index; | |
*/ | |
System.out.println("\nOptional Problem 1\n"); // to separate problems | |
for (int k = 0; k < bool_list.length; k++) { | |
if (!bool_list[k]) { // bool_list[k] == false is also fine | |
System.out.println("\tThere is a false value at " + k); | |
} | |
} | |
/* | |
Problem 2: Guess the Number | |
Using a while loop, create a program that satisfies the following: | |
- The computer has a number between 1 and 100 | |
- The user tries to guess it | |
- The computer tells it to go higher or lower | |
- The user will keep guessing while the guess is not equal to the correct number | |
- If the user guesses the correct number, you should print “Congratulations! | |
You guessed the right number!” and then you should exit the loop. | |
*/ | |
System.out.println("\nOptional Problem 2\n"); // to separate problems | |
Scanner userInput = new Scanner(System.in); // create a scanner object | |
int guess = 0; | |
int num = 45; | |
while (guess != num) { | |
System.out.print("\tGuess a number from 1 to 100: "); // ask user | |
String input = userInput.nextLine(); // get and store user input in string | |
guess = Integer.parseInt(input); // change string to int value | |
if (guess > num) | |
System.out.println("\t" + guess + " is too high! Go lower"); | |
else if (guess < num) | |
System.out.println("\t" + guess + " is too low! Go higher"); | |
else | |
System.out.println("\tCongratulations! You guessed the right number!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment