Question

In: Computer Science

Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...

Lab to be performed in Java.

Lab:

1.) 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. Write a driver class to test that demonstrates that an exception happens for these scenarios

2.) Write a class named InvalidTestScore by reusing the code in TestScores class you wrote for 1) so that it throws an InvalidTestScore exception. Write a driver class to test that demonstrates that an exception happens for these scenarios.

Note 1) and 2) are two separate programs that must be handed in as well as your test score array.

---- Create a class TestScores and TestScores constructor should accept an array of test scores as its argument. Users will enter 5 test scores use a scanner to get input from the console.

---- Create a method that returns or print to console the correct average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException.

---- Create a class InvalidTestScore by reusing the code of TestScores that print to console the line “Test score values should not be greater than 100 or negative” when IllegalArgumentException is thrown.

Solutions

Expert Solution

1.) Solution:

import java.util.*;
class TestScores{
    private int[] scores;
    public TestScores(int[] scores){
        this.scores=scores;
    }
    public int average(){
        int sum=0;
        for(int i=0;i<scores.length;i++){
            if(scores[i]>0&&scores[i]<=100){
                sum+=scores[i];
            }
            else{
                throw new IllegalArgumentException();
            }
        }
        return sum/scores.length;
    }
}
public class MyClass {
    public static void main(String args[]) {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the number of test scores:");
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          System.out.print("Enter the Test Score"+(i+1)+":");
          a[i]=sc.nextInt();
      }
      TestScores obj=new TestScores(a);
      System.out.println("\nThe average of test scores is:"+obj.average());
    }
}

2.)Solution:

import java.util.*;
class TestScores{
    public int[] scores;
    public TestScores(int[] scores){
        this.scores=scores;
    }
    public int average(){
        int sum=0;
        for(int i=0;i<scores.length;i++){
            if(scores[i]>0&&scores[i]<=100){
                sum+=scores[i];
            }
            else{
                throw new IllegalArgumentException();
            }
        }
        return sum/scores.length;
    }
}
class InvalidTestScore extends TestScores{
    public InvalidTestScore(int[] scores){
        super(scores);
    }
    public int average(){
        int sum=0;
        for(int i=0;i<scores.length;i++){
            if(scores[i]>0&&scores[i]<=100){
                sum+=scores[i];
            }
            else{
                throw new IllegalArgumentException("Test score values should not be greater than 100 or negative");
            }
        }
        return sum/scores.length;
    }
}
public class MyClass {
    public static void main(String args[]) {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the number of test scores:");
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          System.out.print("Enter the Test Score"+(i+1)+":");
          a[i]=sc.nextInt();
      }
      InvalidTestScore obj=new InvalidTestScore(a);
      System.out.println("\nThe average of test scores is:"+obj.average());
    }
}

Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
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.
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Lab Objectives Be able to declare a new class Be able to write a constructor Be...
Lab Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that take arguments Be able to instantiate an object Be able to use calls to instance methods to access and change the state of an object Introduction Everyone is familiar with a television. It is the object we are going to create in this lab. First we need a...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT