Ok this code is the 'keyboard class,I have posted it below, I have to somehow use it to create the following output:
Please enter the maximum grade: -10
The maximum grade must be greatr than zero
Please enter the maximum grade: 0
The maximum grade must be greater than zero
Please enter the maximum grade: 21
Please enter the achieved grade: 24
Please enter a number less than 22
Please enter the achieved grade: -10
Please enter a positive interger
Please enter the achieved grade: 11
The grade enterd was: 52.38095
This is the keyboard class:
// A demonstration application to show how to call specific methods provided
// in a separate class.
import java.io.*; //tell the
java compiler that we'll be doing i/o
import java.util.StringTokenizer;
class Keyboard
{
private static BufferedReader inputStream = new BufferedReader
(new InputStreamReader(System.in));
// Read an integer from the keyboard and return it
public static int getInteger( )
{
try
{
return (Integer.valueOf(inputStream.readLine( ).trim( )).intValue( ));
}
catch (Exception e)
{
e.printStackTrace( );
return 0;
}
}
// read a line of integers separated by one or more blanks and return an aray of integers
static int[] getIntegerArray( )
{
try
{
StringTokenizer iline = new StringTokenizer (inputStream.readLine( )) ;
int[] values = new int[iline.countTokens( )];
for (int i = 0 ; i < values.length ; i++ )
{
values[i] = Integer.parseInt (iline.nextToken( ));
}
return values ;
}
catch (Exception e)
{
e.printStackTrace( );
return null;
}
}
// Read a double from the keyboard and return it
public static double getDouble( )
{
try
{
return (Double.valueOf(inputStream.readLine( ).trim( )).doubleValue( ));
}
catch (Exception e)
{
e.printStackTrace();
return 0.0;
}
}
// read a sequence of doubles separated by one or more blanks and return an array of doubles
static double[ ] getDoubleArray( )
{
try
{
StringTokenizer dline = new StringTokenizer (inputStream.readLine());
double [ ] values = new double[dline.countTokens( )];
for (int i = 0 ; i < values.length ; i++)
{
values[i] = new Double (dline.nextToken( )).doubleValue();
}
return values;
}
catch (Exception e)
{
e.printStackTrace( );
return null;
}
}
// Read a float from the keyboard and return it
public static float getFloat( )
{
try
{
return (Float.valueOf(inputStream.readLine( ).trim( )).floatValue( ));
}
catch (Exception e)
{
e.printStackTrace( );
return 0.0f;
}
}
// Read a
string of text from the keyboard and return it
public static String getString( )
{
try
{
return inputStream.readLine( );
}
catch (Exception e)
{
e.printStackTrace( );
return "";
}
}
// Read a char from the keyboard and return it
public static char getCharacter( )
{
try
{
String in = inputStream.readLine( ).trim( );
if (in.length() == 0)
return '\0';
else
return (in.charAt(0));
}
catch (Exception e)
{
e.printStackTrace( );
return '\0';
}
}
// Read a String from the keyboard and return an array of char
static char[ ] getCharacterArray( )
{
try
{
String cline = inputStream.readLine( ) ;
char[] values = new char[cline.length( )] ;
cline.getChars(0, cline.length( ), values, 0);
return values ;
}
catch (Exception e)
{
e.printStackTrace( );
return null;
}
}
// Read a boolean from the keyboard in the format of true or false and return it
public static boolean getBoolean( )
{
try
{
return (Boolean.valueOf(inputStream.readLine( ).trim( )).booleanValue( ));
}
catch (Exception e)
{
e.printStackTrace( );
return false;
}
}
}
class KbTester
{
// a
test program
public static void main(String [ ] args)
{
System.out.println("Enter an integer.");
int anInt = Keyboard.getInteger( );
System.out.println(anInt);
System.out.println("Enter a sequence of integers separated by one or more blanks.");
int [ ] anIntArray = Keyboard.getIntegerArray( );
for (int i = 0; i < anIntArray.length; ++i)
{
System.out.print(anIntArray[i] + " ");
}
System.out.println( );
System.out.println("Enter a double.");
double aDouble = Keyboard.getDouble( );
System.out.println(aDouble);
System.out.println("Enter a sequence of doubles separated by one or more blanks.");
double [ ] aDoubleArray = Keyboard.getDoubleArray( );
for (int i = 0; i < aDoubleArray.length; ++i)
{
System.out.print(aDoubleArray[i] + " ");
}
System.out.println( );
System.out.println("Enter a float.");
float aFloat = Keyboard.getFloat( );
System.out.println(aFloat);
System.out.println("Enter a string.");
String aString = Keyboard.getString( );
System.out.println(aString);
System.out.println("Enter a char.");
char aChar = Keyboard.getCharacter( );
System.out.println(aChar);
System.out.println("Enter a String.");
char [ ] charArray = Keyboard.getCharacterArray( );
for (int i = 0; i < charArray.length; ++i)
{
System.out.print(charArray[i] + " ");
}
System.out.println( );
System.out.println("Enter a boolean as true or false.");
boolean aBoolean = Keyboard.getBoolean( );
System.out.println(aBoolean);
}
}