Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

taking user input in array in java using constructor

/**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.
*/

public class TestScores {

   private double testScores[];
   ScoresDemo TD = new ScoresDemo();

    public TestScores(double scores[]) {
        testScores = scores;
        try {
            for(int i = 0; i < testScores.length; i++) {
                if(scores[i] < 0 || scores[i] > 100) {
                    throw new IllegalArgumentException("Test scores must be between 0 and 100");
                }
                else {
                    testScores[i] = scores[i];
                }
            }
        }catch(IllegalFormatException ex) {
            System.out.println(ex);
        }
   }

    public double averageScores() {
        double average = 0;
        int count = testScores.length;
        int sum = 0;
            for(int i = 0; i < testScores.length; i++) {
                sum += testScores[i];
            }
         average = sum / count;
         return average;
    } 
}
Comment

taking user input in array in java using constructor

public class ScoresDemo {

    public static void main(String[] args) {
        double testScores[] = new double [5];
        TestScores scores = new TestScores(testScores);
        Scanner scan = new Scanner(System.in);

        for(int i = 0; i < testScores.length; i++) {
            System.out.println("Enter some test scores: ");
            testScores[i] = scan.nextDouble();
        }
        System.out.println("The average of the test scores is " + scores.averageScores());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: SPOJ Prime1 
Java :: jframe centerlaized 
Java :: grava 
Java :: csv file data structure java 
Java :: Person[] people = in java 
Java :: java get current free disc space 
Java :: class c { public static void main(string[] args){ system.out.println("hello"+args[0]);}} output 
Java :: force_dark_auto android webview 
Java :: what is collection fromework 
Java :: about action Listioner in java Swing 
Java :: implement elasticsearch filter in java 
Java :: int[] left = Arrays.copyOfRange(arr, l, m + 1); 
Java :: find node from pos linkedlist java 
Java :: public static void nPrintln(String message, int n) { 
Java :: java program operations by classes and objects 
Java :: mei mei bad 
Java :: java program on stack for beginners 
Java :: ex javaloop 
Java :: android capture view and animation 
Java :: bigint is built in object 
Java :: how to apply validation on aiphanumeric series starting with 4 characters with 4 letters in java 
Java :: a Java-8 stream of batches, 
Java :: https://www.baeldung.com/java-stream-findfirst-vs-findany 
Java :: java code for image encryption & decryption 
Java :: governmental transparency 
Java :: java notnull returns null 
Java :: sibling search java program 
Java :: java random number generator 6 
Java :: rerun main method 
Java :: UserRedirectRequiredException: A redirect is required to get the users approval spring boot 5 security 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =