Answer to UnknownHero's Q2 of his Exam2.
I only had to change 1 method from my previous program for Q1, ahh the goodness of software reuse
Code:
import java.io.*;
public class exam2 {
private static String input = "";
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
/**
* *Constructor
*/
public static void main(String[] args) {
*
*int num = -1;
*
*try {
*
* num = getNumber("a number");
* in.close();
*}
*catch(IOException ex) {
* ex.printStackTrace();
*}
*
*calcWeirdMethod(num);
}
private static void calcWeirdMethod(int num) {
*int x = 0;
*
*for(int i=1; i <= num; i++) {
* x = x+i;
*}
*
*System.out.println("Answer is : "+x);
}
/**
* *Function getNumber(String)
* *
* *@exception NumberFormatException
* *@throws IOException
* *@return int
*/
private static int getNumber(String which) throws IOException
{
*boolean stop = false;
*int num = 0;
*
*while(!stop) {
*
* try {
* *System.out.print("Enter "+which+": ");
* *input = in.readLine();
* *num = Integer.parseInt(input);
* *
* *if(num < 0) {
* * num = num*-1;
* *}
*
* *stop = true;
* }
* catch(NumberFormatException nfe) {
* *System.out.println("Not a valid integer, try again");
* *stop = false;
* }
*}
*
*return num;
}
}//end class