Question

In: Computer Science

Consider the following class TestScores. All the methods have direct access to the scores array. Please...

Consider the following class TestScores. All the methods have direct access to the scores array. Please type in the answers here.

TestScores

  • SIZE : int //Size of the array

  • scores : int [ ]

  • TestScores()       

  • findMax() : int

  • findMin() : int

  • findScore(value: int) : bool

  • countScores(): int


  1. Write the constructor method. It fills the array with random number from 0 -100. Find below the code to get you started.

   public TestScores ()

  {

    Random rand = new Random();

    for(int i = 0; i < SIZE ; i++)

        {

           scores[i] = rand.nextInt(101);

        }

  }


  1. Finding the maximum value in an array. Write the method findMax. It returns the maximum value in the array.


  1. Finding the minimum value in an array. Write the method findMin. It returns the minimum value in the array.




  1. Write the method countScores. The method returns the number of exams where the score is greater or equal to 90. This is an example of a linear search.



  1. Write the method called findScore that accepts one int value. The method should check if the value exists in the array and if it does it returns true. Otherwise, the method should return false.

Solutions

Expert Solution

import java.util.Random;

public class TestScores {
  
   public int[] testScores;
   public int SIZE = 10;
   // constructor accepts an array and tests it for valid scores (between 0 - 100)
   public TestScores ()
   {
       testScores = new int[SIZE];
   Random rand = new Random();
   for(int i = 0; i < SIZE ; i++)
{
   testScores[i] = rand.nextInt(100);
}
   }
  
   // the method countScores. The method returns the number of exams where the score is greater or equal to 90
   public int countScores() {
      
       int count = 0;
      
       for (int index = 0; index < testScores.length; index++)
       {
           if(testScores[index]>=90) count++;
       }
       return count;
   }
   //the method called findScore that accepts one int value. The method should check if the value exists in the array and if it does it returns true.
   public boolean findScore (int val) {
      
       int count = 0;
      
       for (int index = 0; index < testScores.length; index++)
       {
           if(testScores[index]==val) return true;
       }
      
       return false;
   }
  
   //Finding the maximum value in an array. Write the method findMax. It returns the maximum value in the array.
   public int findMax() {
      
       int max = testScores[0];
      
       for (int index = 1; index < testScores.length; index++)
       {
           if(testScores[index]>max) {
               max = testScores[index];
           }
       }
      
       return max;
   }
  
   //Finding the minimum value in an array. Write the method findMin. It returns the minimum value in the array
   public int findMin() {
      
       int min = testScores[0];
      
       for (int index = 1; index < testScores.length; index++)
       {
           if(testScores[index]<min) {
               min = testScores[index];
           }
       }
      
       return min;
   }
  
   public static void main(String[] args) {
       TestScores obj = new TestScores();
       System.out.println("Max - "+obj.findMax());
       System.out.println("Min - "+obj.findMin());
       System.out.println("10 presence in the array - "+obj.findScore(10));
       System.out.println("Scores greater than 90 = "+obj.countScores());
   }
}


Related Solutions

Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class...
****Please answer them all, if you cannot, please skip**** Here are scores received by a class...
****Please answer them all, if you cannot, please skip**** Here are scores received by a class of Biology students on a 40 point exam. Each number is the score, out of 40 points, received by one student. The data are separated by gender. Scores, out of 40 points, received by female students: 17, 20, 30, 31, 31, 33.6, 34, 34.5, 35, 35, 36, 35.6, 36, 36, 36, 37, 37.5, 37, 38, 38 Scores, out of 40 points, received by male...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
Write array methods that carry out the following tasks for an array of integers by creating...
Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called ‘Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[ ] initialValues) {values = initialValues;} //constructor public void shiftRight( ) { … } public Boolean...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
Two sections of a class in statistics were taught by two different methods. Students’ scores on...
Two sections of a class in statistics were taught by two different methods. Students’ scores on a standardized test are shown in Table 5.12 . Do the results present evidence of a difference in the effectiveness of the two methods? (Use α = 0.05.) Class A: 74, 97, 79, 88, 78, 93, 76, 75, 82, 86, 100, 94 Class B: 78, 92, 94, 78, 71, 85, 70, 79, 76, 93, 82, 69, 84 Include R code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT