Vous êtes sur la page 1sur 10

Write a program that displays a studentID number and asks the user to enter a

numeric test score for the student. Create an "Exception Handling" class, and throw
an Exception for that class if the user does not enter a valid score (greater than 0 but
less than or equal to100). Catch the exception and then display an appropriate
message. Repeat until a valid score is entered and display the studentID and score.

I think i have a handle on it for the most part. The issue is....well the first issue is I am not totally sure I am doing it write.
the second is everything else is working....except the try and catch that i am using.

here is my code.

The first is my testScore class.


view source

01 public class testScore {

02 private int testScore;

03

04 public testScore() {

05 }

06

07 public testScore(int p_testScore) {

08 testScore = p_testScore;

09 }

10

11 public int getTestScore() {

12 return(testScore);

13 }

14
15 }

Then my exception class WrongTestScore


view source

01 public class WrongTestScore {

02 private int testScore;

03

04 public WrongTestScore(int p_testScore) {

05 testScore = p_testScore;

06 }

07

08 public String getMessage() {

09 if (testScore <= 100)

return("\nYou have entered an invalid test score. Please


10
enter a test score from 1 - 100.");

11 else

12 return("");

13 }

14

15 }

and the last is the test itself TestStdTestScore

view source
01 import javax.swing.*;

02

03 public class TestStdTestScores {

04

05 public static void main(String[] args) {

06

07 testScore Jeremy = new testScore();

08 int studentID = 1211;

09

10 try {

JOptionPane.showInputDialog(null, "Student ID#: "+ studentID +


11
"\nPlease enter your test Scores below.");

12

13 System.exit(0);

14 }

15

16 catch (NegativeNumberException e) {

17 JOptionPane.showInputDialog(null, e.getMessage());

18 System.exit(-1);
19

20 }

21

22 }

23

24 }

public class WrongTestScore extends RuntimeException {

2 public WrongTestScore(String msg) {

3 super(msg)

4 }

5 }

In the try block you want to have the code that will generate the error and the catch block is supposed to catch the error
and run the code in the catch block. Right now, your code isn't even catching your custom created exception. Another
problem is that youre using JOptionPane which (i think) is part of a GUI yet you have no GUI defined. You should try
something more along the lines of:
view source

1 try {

2 testScore(-10);

3 }

4 catch (WrongTestScore e) {
5 System.out.println("Wrong test score entered, please try again.");

6 }

If this doesnt work try having your testScore() method throw the WrongTestScore error if an invalid number is entered.
Hope this helps.

So what you want are actually two things:


1. main() class should display the JOptionPane and ask for input
2. The input should be sent testScore, where it will be checked for correctness

Here is roughly what testScore should be:


testScore should filter your input. In other words, it should look at the input and determine if that input is acceptable. If it
is, nothing happens. If it isn't, then testScore will through an exception giving the error message. Now for this example, I
used the generic Exception class. You can choose to create your own exception type by creating a class which extends
Exception.
view source

01 public class testScore throws Exception{

02 private int testScore;

03

04 public testScore() {

05 }

06

07 public void setTestScore(String input){

08 try{

09 int i = Integer.parseInt(input);

10 if (i<0 || i>100)

11 throw new Exception("wrong input blah blah");

12 }catch(Exception e){
13 throw new Exception("wrong input blah blah");

14 }

15 }

16

17 public int getTestScore() {

18 return(testScore);

19 }

20

21 }

And here is main:


So basically, you want to ask for input, and then send that input into testScore as a filter. If it passes cleanly, then you
show the correct message, otherwise you show the wrong input message, and loop through asking for input again. This
continues until you get a correct input.
view source

01 public class TestStdTestScores {

02

03 public static void main(String[] args) {

04

05 testScore Jeremy = new testScore();

06 int studentID = 1211;

07 while(true){
08 try {

Jeremy.setScore(JOptionPane.showInputDialog(null,
09
"Student ID#: "+ studentID + "\nPlease enter your test Scores below."));
JOptionPane.showMessageDialog("Student ID# is: " +
10
studentID + "\nScore is: " + Jeremy.getScore());

11 break;

12 }

13 catch (Exception e) {

14 JOptionPane.showInputDialog(null, e.getMessage());

15 }

16 }

17 }

18

19 }

So what you want are actually two things:


1. main() class should display the JOptionPane and ask for input
2. The input should be sent testScore, where it will be checked for correctness

Here is roughly what testScore should be:


testScore should filter your input. In other words, it should look at the input and determine if that input is acceptable. If it
is, nothing happens. If it isn't, then testScore will through an exception giving the error message. Now for this example, I
used the generic Exception class. You can choose to create your own exception type by creating a class which extends
Exception.
view source

print?

01 public class testScore throws Exception{


02 private int testScore;

03

04 public testScore() {

05 }

06

07 public void setTestScore(String input){

08 try{

09 int i = Integer.parseInt(input);

10 if (i<0 || i>100)

11 throw new Exception("wrong input blah blah");

12 }catch(Exception e){

13 throw new Exception("wrong input blah blah");

14 }

15 }

16

17 public int getTestScore() {

18 return(testScore);

19 }
20

21 }

And here is main:


So basically, you want to ask for input, and then send that input into testScore as a filter. If it passes cleanly, then you
show the correct message, otherwise you show the wrong input message, and loop through asking for input again. This
continues until you get a correct input.
view source

print?

01 public class TestStdTestScores {

02

03 public static void main(String[] args) {

04

05 testScore Jeremy = new testScore();

06 int studentID = 1211;

07 while(true){

08 try {

Jeremy.setScore(JOptionPane.showInputDialog(null,
09
"Student ID#: "+ studentID + "\nPlease enter your test Scores below."));
JOptionPane.showMessageDialog("Student ID# is: " +
10
studentID + "\nScore is: " + Jeremy.getScore());

11 break;

12 }

13 catch (Exception e) {

14 JOptionPane.showInputDialog(null, e.getMessage());
15 }

16 }

17 }

18

19 }

Vous aimerez peut-être aussi