Set numMatches to the number of elements in userValues (having NUM_VALS elements
ID: 3575522 • Letter: S
Question
Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userValues = {2, 2, 1, 2}, then numMatches = 3.
import java.util.Scanner;
public class FindMatchValue {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] userValues = new int[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = -99; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
/* Your solution goes here */
System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
return;
}
}
Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.
import java.util.Scanner;
public class StoreGuesses {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_GUESSES = 3;
int[] userGuesses = new int[NUM_GUESSES];
int i = 0;
//solution here
for (i = 0; i < NUM_GUESSES; ++i){
System.out.print(userGuesses[i] + " ");
}
return;
}
}
3) Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.
import java.util.Scanner;
public class SumOfExcess {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] testGrades = new int[NUM_VALS];
int i = 0;
int sumExtra = -9999; // Assign sumExtra with 0 before your for loop
testGrades[0] = 101;
testGrades[1] = 83;
testGrades[2] = 107;
testGrades[3] = 90;
/* Your solution goes here */
System.out.println("sumExtra: " + sumExtra);
return;
}
}
4)
Note that the last element is not followed by a comma, space, or newline.
import java.util.Scanner;
public class PrintWithComma {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] hourlyTemp = new int[NUM_VALS];
int i = 0;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
/* Your solution goes here */
System.out.println("");
return;
}
}
90, 92, 94, 95
Note that the last element is not followed by a comma, space, or newline.
import java.util.Scanner;
public class PrintWithComma {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] hourlyTemp = new int[NUM_VALS];
int i = 0;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
/* Your solution goes here */
System.out.println("");
return;
}
}
Explanation / Answer
Question 1:
import java.util.Scanner;
public class FindMatchValue {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] userValues = new int[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = -99; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
/* Your solution goes here */
numMatches =0;
for(i=0; i<userValues.length; i++){
if(userValues[i] == matchValue){
numMatches++;
}
}
System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
return;
}
}
Output:
matchValue: 2, numMatches: 3
Question 2:
import java.util.Scanner;
public class StoreGuesses {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_GUESSES = 3;
int[] userGuesses = new int[NUM_GUESSES];
int i = 0;
/* Your solution goes here */
for (i = 0; i < NUM_GUESSES; ++i){
System.out.print("Enter User Guess: ");
userGuesses[i] = scnr.nextInt();
}
for (i = 0; i < NUM_GUESSES; ++i){
System.out.print(userGuesses[i] + " ");
}
return;
}
}
Output:
Enter User Guess: 9
Enter User Guess: 5
Enter User Guess: 2
9 5 2
Question 3:
import java.util.Scanner;
public class SumOfExcess {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] testGrades = new int[NUM_VALS];
int i = 0;
int sumExtra = -9999; // Assign sumExtra with 0 before your for loop
testGrades[0] = 101;
testGrades[1] = 83;
testGrades[2] = 107;
testGrades[3] = 90;
/* Your solution goes here */
sumExtra = 0;
for( i=0; i<testGrades.length; i++){
if(testGrades[i]>100)
sumExtra = sumExtra + testGrades[i]%100;
}
System.out.println("sumExtra: " + sumExtra);
return;
}
}
Output:
sumExtra: 8
Question 4:
import java.util.Scanner;
public class PrintWithComma {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] hourlyTemp = new int[NUM_VALS];
int i = 0;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
/* Your solution goes here */
for(i=0; i<hourlyTemp.length; i++){
if(i!=hourlyTemp.length-1)
System.out.print(hourlyTemp[i]+",");
else
System.out.print(hourlyTemp[i]);
}
System.out.println("");
return;
}
}
Output:
90,92,94,95
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.