I have written the code for the following problem and would just like some feedback before I move on. The problem states:

Write a class names TestScores. The class constructor should accept an array of test scores as its arguement. 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 IllegalArguementException. Demonstrate the class in a program.

Here is my code:

import java.io.*;
import java.util.Scanner;

public class TestScores
{

   
    public static void main(String[] args) throws IOException
    {  

        try
        {
         
         Scanner keyboard = new Scanner(System.in);
         
         System.out.print( "Please enter number of tests: " );
         int numOfTests = keyboard.nextInt();

          double sumOfAllGrades = 0;//the sumOfAllGrades
         
          for( int i=0; i < numOfTests; i++ )
         
          {
         
              System.out.print( "Enter the grade for Test " +(i+1)+ ": " );
              double grade = keyboard.nextDouble();
              
              sumOfAllGrades += grade;
          }
         

          double average = sumOfAllGrades/numOfTests;
          System.out.println("The average is: " + average);

         
        }
        
        catch( Exception e )
        {
          e.printStackTrace();
        }
       
    }


}

Does this seem to meet the requirements? Thanks for your help!

I have written the code for the following problem and would just like some feedback before I move on. The problem states:

Here is my code:

Does this seem to meet the requirements? Thanks for your help!

Doesnt look like you met the requirements at all.

in your while look if they enter test i a grade stored in grade,

you need it to write to an int array.

alltests=grade;

rather than the line

sumOfAllGrades += grade;


then you need a class testscores or whatever the name was suppose to be and you pass it the int arrary as part of its constructor.

testscores abunch = new testscores(alltests);

hope that helps

good luck

Mike

I was totally off my game yesterday...finals are throwing me off a bit. I did a complete redo of the problem. I think it looks better:

import java.util.Scanner;

public class TestScores
{
	public static void main(String[]args)
	{
		int numTests = 0;
		double[] grade = new double[numTests];
		double totGrades = 0;
		double average;
		
		
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("How many tests do you have? ");
		numTests = keyboard.nextInt();
		
		grade = new double[(int) numTests];
		
		
		for (int index = 0; index < grade.length; index++)
		{
			System.out.print("Enter grade for Test " + (index + 1) + ": ");
			grade[index] = keyboard.nextDouble();	
			
			if (grade[index] < 0 || grade[index] > 100)
            {
          	  throw new IllegalArgumentException(
          			  "Invalid number used for test score");
            }
		}
		
		for (int index = 0; index < grade.length; index++)
		{
			totGrades += grade[index];
		}
		
		average = totGrades/grade.length;
		
		System.out.print("The average is: " + average);
		
		
	}
}

You really helped alot! Does it look better?

you actually need two classes to do this assignment. you gather the test scores in the array as your doing and that looks good. But that will be your interface type class. once you have them in the array, it says take that array and pass it to class test scores through the constructor. so the interface class, which seems to be what you are writing, creates a test score object and feeds the constructor the array you populated. from what i read the work of calculating an average and giving output happens in the second class , test scores, not the interface.

Mike

You need a constructor in your program, which you don't have. So, you want a method with the same name as your class, that accepts an array as an argument. Here's a little more on constructors:

http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

Also, you want a method that returns an average of the test scores. I'm guessing that the specification means actually return, and not just print out. So you want to change your main method to another method that returns a double, and return the average. You can pretty much keep everything you have in main and just change the name of the method and have it return a double. Hope that helps.

I am not really understanding why I need two classes. Doesn't the program I wrote accomplish the task? It seems to complicate the problem to add another class...

Here is problem two from the same chapter that builds on the last problem.

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Programming Challenge 1 so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid.

Here is my code for both classes:

import java.util.Scanner;

public class TestScores
{
	public static void main(String[]args)
	{
		int numTests = 0;
		double[] grade = new double[numTests];
		double totGrades = 0;
		double average;
		
		
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("How many tests do you have? ");
		numTests = keyboard.nextInt();
		
		grade = new double[(int) numTests];
		
		
		for (int index = 0; index < grade.length; index++)
		{
			System.out.print("Enter grade for Test " + (index + 1) + ": ");
			grade[index] = keyboard.nextDouble();	
			
			if (grade[index] < 0 || grade[index]> 100)
			{
				try {
					throw new InvalidTestScore();
				} catch (InvalidTestScore e) 
				{
					e.printStackTrace();
				}
			}
		}
		
		for (int index = 0; index < grade.length; index++)
		{
			totGrades += grade[index];
		}
		
		average = totGrades/grade.length;
		
		System.out.print("The average is: " + average);
		
		
	}
}
public class InvalidTestScore extends Exception
{
	public InvalidTestScore()
	{
		super ("Invalid number used for test score");
	}
}

It seems to work....I don't know :)

There is another part to the problem that I am going to work on next:

Modify the TestScores class that you created for Programming Challenge 1 to be serializable. Write a program that creates an array of at least 5 TestScore objects and serializes them. Write another program that deserializes the objects from the file.

I am going to re-read the section serialization and let you know how it goes.

Mainly i dont see how you can implement this requirement otherwise, "The class constructor should accept an array of test scores as its arguement". I just got that from your problem. I don't see any constructors in your code. Really all you are doing is creating a class with a constructor , and an average method that returns the average. The constructor you will have to write new, the average method and display of output you can simply move from where it is to being under the new class for the most part, though the class would have to populate the test scores array from the constructor and it would use that data. your exception is probably thrown in this new class.

Mike


Mike

:) Ok.....so - back to the drawing board! I will try it again. This time with your suggestions!

Modify the TestScores class that you created for Programming Challenge 1 to be serializable[\quote]

i'm not as up on serialization. If your set of homework problems is covering multiple topics, I might recommend starting a new post with a clear title, like java serialization question. The concern is that as this first thread grows with more posts, perhaps fewer people might be willing to dive in or they may think most issues are resolved and you are just doing followup.

I remember when i was in school i needed prompt answers to specific questions because hours or days wait can cause the flow of helpful information to not come in, in time, to meet my deadline.

Mike

1.
write a program tha reads a line of text from the keyboard and prints the acronym formed from the last letter of each word.
Example a line a text: Just another a vaulable artfact

Why did you add your question on an existing thread instead of starting your own?
Your problem has no relationship with the topic of this thread.

Do you have any specific questions about the program you are trying to write?

I think you should revisit the first problem by actually creating a constructor and an average method.

Constructor :

public TestScores ( double[] grade )
{
... //probably want to copy the inputted array into your own array to manipulate in the average method
}

You should also not throw your exception in your try/catch block. Logically you could use an if/else statement to handle any problems there. I think you should create two classes. A driver class and a class with the constructor and the average method. Use the driver class to call/manipulate the methods from the other class as well as implement the try/catch block.

I believe you should also override the exception method with a string parameter :

public InvalidTestScore ( String message )
{
super(message);
}

You really should start a new thread for new problems to get the best help.

public class TestScores {
	public TestScores(int[] arg) {
		System.out.println(average(arg));
	}
	public int average(int[]arg)
	{
		int temp=0;
		for (int i = 0; i < arg.length; i++) {
			if(arg[i]<0 || arg[i]>100)
			{
				IllegalArgumentException e = new IllegalArgumentException();
				throw e;
			}
			else
			{
				temp+=arg[i];
			}
		}
		return temp/arg.length;
	}
	public static void main(String[] args) {
		int []ar={4,78,33,89};
		TestScores ts=new TestScores(ar);
	}

}

Now make a swing interface for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.