8 Replies - 658 Views - Last Post: 23 March 2012 - 03:55 PM Rate Topic: -----

#1 KW1Xan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 12

Just had exam what did I do wrong?

Posted 23 March 2012 - 09:00 AM

The question asked...

A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, fifteen of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 15 students. Next to each name is written a 1 if the student passed the exam or a 2 if the student failed.

Your program should analyze the result of the exam as follows:

1. Input each test result (i.e., a 1 or a 2). Display the message “Enter result” on the screen each time the program requests another test result.
2. If the input is not a 1 or a 2, display an error message and request another test result.
3. Count the number of test results of each type.
4. Display a summary of the test results indicating the number of students who passed and the number who failed.
5. If more than 12 students passed the exam, print the message “Raise tuition.”

This is what I did
//*****************************************************************************************
// Exam2.java			Kristopher Willis
//
// A program to summarize the results for the state licensing exam for real estate brokers
// ****************************************************************************************

import java.io.PrintStream;
import java.util.Scanner;

public class Exam2 
{
	 static Scanner scan = new Scanner(System.in);
	 PrintStream out = System.out;

	static int pass;
	final static int students = 15;
	
	public static void main(String[] args) 
	{
		for(int i = 0; i < students; i++) 
		{
			try {
				System.out.print("Enter result of each student 1-15: ");
				String add = scan.nextLine();
				int bob = Integer.parseInt(add);
				
				if(bob < 0 || bob > 2) throw new Exception();
				if(bob == 1) pass++;
				} 
				catch(Exception e) 
				  {
				System.out.println("Input not 1 or 2, try again please");
				i--;
				}
		}

		System.out.println("Summary\n-------");
		System.out.println("Passed: " + pass);
		System.out.println("Failed: " + (students - pass));

		if(pass > students) 
			{
			System.out.println("You should raise tuition.");
			}
	}
}

This post has been edited by jon.kiparsky: 23 March 2012 - 09:13 AM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Just had exam what did I do wrong?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 09:17 AM

What did you do wrong? Probably any number of things over the course of your life, most of us do.

If you ask a more specific question, you'll get a more specific answer.

Hint: what seems to be the problem? What are the symptoms of the problem you're trying to diagnose?

What did you expect the program to do, and what did it do instead?


Also, please use the [code] tags in the future. It's easy to do automatically, just select the text of your program and press the [code] button at the top of the editor window.
Was This Post Helpful? 1
  • +
  • -

#3 KW1Xan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 12

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 09:35 AM

I apologize for not being clear. Thanks Jon for the help in my post.

Basically I had a problem with #5 "If more than 12 students passed the exam, print the message “Raise tuition.” "

Im not sure how i could have done that other than to give a int value of 12 and then use an if statement, however every time I tried that it didn't work.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 09:45 AM

Could it be this simple?


final static int students = 15;

Was This Post Helpful? 0
  • +
  • -

#5 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 09:54 AM

This is bugging me.


//code below allows bob to be 0,1, or 2. I assume you don't want that
if(bob < 0 || bob > 2) throw new Exception();

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 10:03 AM

View Postnunc, on 23 March 2012 - 11:54 AM, said:

This is bugging me.


//code below allows bob to be 0,1, or 2. I assume you don't want that
if(bob < 0 || bob > 2) throw new Exception();



Yeah, that's not good. Will you explain why, or should I?
Was This Post Helpful? 0
  • +
  • -

#7 codeMonkey_1066  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 71
  • Joined: 22-March 12

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 11:12 AM

Wasn't the threshold for the tuition raise 12 not the number of students (15) ?
As you have it you only raise tuition if <b>all</b> the students pass.
Was This Post Helpful? 0
  • +
  • -

#8 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 03:04 PM

I'll explain jon.

Op the following code has 2 errors

if(bob < 0 || bob > 2) throw new Exception();


You allow for 3 values of bob to be available, instead of the two you want.
If the value is 0 (which you don't want) then it still slips by in your program.

As a side note, your thrown exception is very vague. I'd suggest looking into creating your own exception. Or look for a more specific exception already created. Even better, get rid of the exception entirely and put the code asking for a 1 or 2 inside a loop. Keep looping if the input is not what you want.

This post has been edited by nunc: 23 March 2012 - 03:09 PM

Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Just had exam what did I do wrong?

Posted 23 March 2012 - 03:55 PM

Good stuff. I'd only add that a while loop might be a more efficient way to handle this error condition. Exceptions are really geared toward packing up an error state and passing it along to another class to handle. In this case I'd do something like

boolean validInput=false;
while (!validInput)
{
  // get input
  // check input
  // if input is valid, set validInput to true
}

// if we get out of the loop, we must have got good input



But yes, the Exception class is really a base class - I'd think it would have been best implemented as an abstract class, honestly. It's never a good idea to throw a straight Exception, you should always use an Exception that reflects the error state you're handling.

This post has been edited by jon.kiparsky: 23 March 2012 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1