Question

In: Computer Science

Write a loop that subtracts 1 from each element in lowerScores. If the element was already...

Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.

please bold the solution

import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] lowerScores = new int[SCORES_SIZE];
int i;

for (i = 0; i < lowerScores.length; ++i) {
lowerScores[i] = scnr.nextInt();
}

/* Your solution goes here */

for (i = 0; i < lowerScores.length; ++i) {
System.out.print(lowerScores[i] + " ");
}
System.out.println();
}
}

Solutions

Expert Solution

Java program :

StudentScores.java

//import package
import java.util.Scanner;
//Java class
public class StudentScores {
   //main() method
   public static void main(String[] args) {
       //Scanner class Object
       Scanner scnr = new Scanner(System.in);
       //variable that store array size
       final int SCORES_SIZE = 4;
       //integer array with name lowerScores
       int[] lowerScores = new int[SCORES_SIZE];
       int i;//declaring variable i
       //using for loop to take input from user
       for (i = 0; i < lowerScores.length; ++i) {
           //reading each score entered by user and
           //storing in the array
           lowerScores[i] = scnr.nextInt();
       }
       //using for loop
       for (i = 0; i < lowerScores.length; ++i) {
           //checking element
           if(lowerScores[i]<=0)
           {
               //if element is negative then assign 0
               lowerScores[i]=0;//set element as 0
           }
           else {
               //when element is non zero this means positive then
               //subtract -1 from the element
               lowerScores[i]=lowerScores[i]-1;
           }
       }

      
       //using for loop to print the array elements
       for (i = 0; i < lowerScores.length; ++i) {
           //print each array element
           System.out.print(lowerScores[i] + " ");
       }
       System.out.println();//used for new line
   }
}

=========================================

Output :


Related Solutions

In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a for loop from 1 to 3 using i as the variable. For each value...
Write a for loop from 1 to 3 using i as the variable. For each value of i: Create a vector x of 10 random numbers between 0 and 1. Create a second vector t which is equal to ten integers from 1 to 10. Plot x versus t in figure 1. Use hold on to keep each plot. Use a different color for the line for each value of i. At the very end, add the text 'time' using...
Write a Formula for the ionic compound that forms from each pair of element Aluminum and...
Write a Formula for the ionic compound that forms from each pair of element Aluminum and oxygen Beryllium and iodine Calcium and sulfur Calcium and iodine
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
Write a for-loop in MATLAB that generates a list of numbers such that each number is...
Write a for-loop in MATLAB that generates a list of numbers such that each number is the sum of the previous three. Initialize your list of numbers at the values of 0, 0 and 1. In other words, "0" is the first element of the list, "0" is the second element of the list, and "1" is the third element of the list. What is the 20th value in the list?
Write the multiplication table for D8. Find the order of each element D8.
Write the multiplication table for D8. Find the order of each element D8.
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
Explain and summarise each calculatin and result which already calculated below. 1) Coversion to bond from...
Explain and summarise each calculatin and result which already calculated below. 1) Coversion to bond from commercial paper •idy=(Pf-Po)/Pf × 360/h •4%?0.04= ($1M-Po)/$1M=360/80 days •0.04 =($1M-Po)/$1M ×4.5 •0.00889 = ($1M-Po)/$1M •8889=$1M-Po •Po = $1M-8889 •   =$991,111 •ibey=(Pf-Po)/Po × 365/h •=($1M-$991,111)/$991,111 × 365/80 •=8,889/$991,111 ×365/80 •=3,244,485/79,288,8880 •=0.0040919794 = 4.0920% •The Commercial paper equivalent yield to bond is 4.0920% 2) Conversion to CD •CD has 5 % single payment yield •ibey=ispy(365/360) •ibey=0.05 × (365/360) •=0.050694 •=5.07% •The bond equivalent yield for the CD...
Contrast the modern construct for F ISIM with a measure that subtracts deposits from the financial...
Contrast the modern construct for F ISIM with a measure that subtracts deposits from the financial sector’s lending to measure value added in the sector. How will these two measures differ in terms of size and sensitivity to risk? Can you give differing views of a world without finance for each to be the proper measure of value added?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT