0
public class Average
{
    private double [] Score;
    int index;

    public Average(double [] s)
    {
        Score = new double[s.length];



        for(index=0; index<s.length; index++)
        {

            if(Score[index]>100 && Score[index]<0)
              Score[index]=s[index];
         }


      }









    public double getAverage()
    {
        double total=0.0;
        double average = 0.0;
        for(double value: Score)
        total+=value;
         average= Score.length/total;
         return average;

    }
}

the actual assingment is to write an IllegalArgumentException method inoder to prevent the program from accepting negative or score grater than 100

Edited by Nick Evan: Fixed formatting

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by javaAddict
0

by try catch block not working

Attachments
public class Average
{
	private double [] Score;
	int index;

	public Average(double [] s)


	{

		Score = new double [s.length];





		for(index=0; index<s.length; index++)

		{

			if(Score[index]>100)
			{
				throw new IllegalArgumentException("Score is too high:");
			}
			if(Score[index]<0)
				throw new IllegalArgumentException("Score is too high:");
				Score[index] = s[index];

		 }


	  }











	public double getAverage()
	{
		double total=0.0;
		double average = 0.0;
		for(double value: Score)
		total+=value;
		 average= Score.length/total;
		 return average;

	}
}
import java.util.Scanner;// need for input

public class AverageDemo
{
	public static void main(String [] args)
	{
		final int Tscore_size=5;
		double [] scores = new double[Tscore_size];


		getValues(scores);




		Average Testa= new Average(scores);

		System.out.println();
		System.out.println("The Average of tests: "  +Testa.getAverage());

	}

	private static void getValues(double [] array)
	{
				try
				{

				Scanner keyboard = new Scanner(System.in);
				System.out.println("enter score of each test:");





			for(int index=0; index<array.length; index++)
			{
			System.out.println("scores " + (index+1) + ":");


			array[index]=keyboard.nextDouble();
		}

	}



		catch(IllegalArgumentException e)
		{
			System.out.println(e.getMessage());
		}

	}
}
0

In your class the "s" input is the one that has the values not the Score. I mean it is the "s" that you pass as parameter, then you put values from s to Score. Why on earth are you checking the values of Score if you put the values "s" in it after the checking?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.