wood burning stoves*
The moose likes Beginning Java and the fly likes Keyboard.Class/Anyone with brains.pt2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Keyboard.Class/Anyone with brains.pt2" Watch "Keyboard.Class/Anyone with brains.pt2" New topic
Author

Keyboard.Class/Anyone with brains.pt2

Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
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);
}
}
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
To be honest if someone could even come up with the the Class name so I could actually run the Keyboard class then I would be very greatful.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You can not run the Keyboard class as it doesn't have a main method. You can however run the KbTester program that uses the Keyboard classes methods.
Compile the program using
and then run the program using

Don't forget to use code tags when posting
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Yes thats what I an using the Kb Tester so java KbTester should allow me to run this then? I'll give it a try! Thanks!
Cool KbTester works, how dumb am I not to figure that one out!

It says 'Enter an Integer'
I need it to output all of the System.out's that was just the first one, how do I get it to do the rest aswell ?

[ January 06, 2005: Message edited by: Michael Munro ]
[ January 06, 2005: Message edited by: Michael Munro ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Basicahave to change what is in the System.outs to try and make it give the outputs I posted above, but it is easier said than done....
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
From your first post, basically what you need to do is ask a question, wait for user input, test that input and then reply with another question or a statement. If you must use the Keyboard class think of ways in which you can use the supplied methods but then test the returned variables so as to produce the desired output.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Yes, I know I have to try and use the supplied methods but I'm not sure which ones and in which order to use them in.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This should get you started.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Please use CODE tags: When posting code here, click on the "CODE" button below the text field. This will add the beginning and ending CODE tags. Put your code between these.

For anyone else trying to follow this, here's the indented code...

[ January 07, 2005: Message edited by: marc weber ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
So Marc, do I leave the KbTester class as it is in your above post, then create a new class underneth, like 'class input'. Or Am just supposed to be altering the System.outs... of the kbtester class ???
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

KbTester just demonstrates what Keyboard does. Now that you see how Keyboard provides static methods to get Command Line input and return it as the appropriate type, you no longer need KbTester.

You should be writing a new class (like the one I started above) to use with Keyboard. Note, for example, how Keyboard.getInteger() is used to assign a user-input integer to a variable...

[ January 09, 2005: Message edited by: marc weber ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I have come up with the following code, so far so good I think!

class KbTester
{
// a test program
public static void main(String [ ] args)
{

System.out.println("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
if(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");
{
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Excellent! Looks like you're on the right track.

Something to consider: Your code tests the input number the first time to make sure it's greater than zero; and if it's not, then it asks the user to re-input. But what happens if the user keeps entering a negative number? (Hint: See my code above.)
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Thanks Marc, I'm not that advanced yet, I'm just trying to keep it as simple as possible, one step at a time. It took me ages to to that part, thats about half of this part of the exercise. This the final part. When its finished I have to connect it to the 'MarksCalculator' which we did before.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ok, the above code has now changed slightly, to this:

class KbTester
{
// a test program
public static void main(String [ ] args)
{

System.out.println("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
if(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
if(achieved >= anInt)
{
}
if(achieved < 0)
{
}
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");

}

}
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
This the outgoing testing method which needs to be produced by the keyboardtester code and what operations it needs to check.

Please enter the maximum grade: -10
The maximum grade must be greater 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 posative integer
Please enter the achieved grade: 11
The grade enterd was: 52.38095

In the example above the maximum grade enterd was -10(line 1), the code says that it must be greater than zero(line2) and therfore asks for the maximum grade to be enterd again(line3) you enter zero. The code then states again that it needs to be greater than zero(line4). Then you are asked to enter the maximum grade again(line5) you decide that the maximum grade is 21.
It then asks you to enter the achieved grade(line6) you enter 24, this higher than the maximum possable grade of 21. So it then needs to ask you to enter a number less than 22(line7).Then asks you to enter the achived grade again(line8).You enter -10. this is obviously not possable so it asks you to enter a posative integer(line9). Then asks you to enter the achieved grade again(line10) you enter 11. The final output line should then read. The grade enterd was: 52.38095 .
The last line is calculated using the grading system used in the pt1 previous thread.
Can anyone add anymore on to what I have done so far








You than decide that the maximum grade
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As long as the user keeps putting in non-positive values, the program should keep asking for corrected input.

But the "if" statement only checks this once. It says, if non-positive, ask again, then move on regardless of what's input the second time.

In contrast, a "while" statement will keep checking until the condition is met. It says, while non-positive, ask again, and don't move on until it's positive.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ok the code has now changed again, but it is still not quite right, there is 1 fault.
1.when you re-enter the maximum grade as 0 it does not tell you to enter a maximum grade above zero. If anyone can help me with this I would be most greatful.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Are you using "while(anInt < 0)" or "while(anInt <= 0)"?
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I'm using (anInt <= 0) for the first part. Maybe I'm going wrong at the line further down; if(achieved < 0) maybe I should change it to if(achieved <=0)

N that doesn't make any difference
[ January 20, 2005: Message edited by: Michael Munro ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Please post the exact code you're using now.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ok my code is now like this:

class KbTester
{
// a test program
public static void main(String [ ] args)
{

System.out.println("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
if(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
if(achieved >= anInt)
{
System.out.println("Please enter a number less than " + anInt);
achieved = Keyboard.getInteger( );
}
if(achieved <= 0)
{
}
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");

}

}



I'm going to try changing 'if' to 'while'
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I hope you've found that changing the "if" to "while" solved that problem, but you need to understand why this is.

if(condition){ //code } checks the condition once and only once. If the condition is true, then the code executes. If the condition is false, then it doesn't execute. Either way, execution then moves on to the next line -- whether or not the new input is valid.

while(condition){ //code } also checks the condition. As above, if the condition is true, then the code executes; and if the condition is false, then it doesn't execute. But the difference with "while" is that the condition will continue to be checked after each execution of the code. As long as it stays true, the code will keep executing. This way, the input is guaranteed to be valid.

So I think you'll see that "while" is the solution here, and you'll also want to use "while" in checking the next input, "achieved."

But consider this: We want to check the input for "achieved" in 2 ways. It has to be less than or equal to anInt and it can't be negative. The way the code is written now, we first check to make sure that it's less than or equal to anInt. (And we can ensure this by making the "if" statement a "while" loop.) Then after that, we check to make sure that it's not negative. BUT what happens if the user inputs a number greater than anInt in the second check? That second "while" loop is only checking the non-negative condition -- it doesn't know anything about being less than or equal to anInt.

To solve this, ask yourself: Is there a way to check both conditions at the same time (in the same statement)?
[ January 22, 2005: Message edited by: marc weber ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ye thats what I did and it solved the problem.:

class KbTester
{
// a test program
public static void main(String [ ] args)
{

System.out.println("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
while(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
if(achieved >= anInt)
{
System.out.println("Please enter a number less than " + anInt);
achieved = Keyboard.getInteger( );
}
if(achieved <= 0)
{
}
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");

}

}
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Marc,

Looks like we've got our hands full with these students! I'll take Rose, you look like you're having quite a discussion over here. Rose needs keyboard input too. We'll be over to look at your code later!


Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
O code made in a k the above code does definatley work. I now need to combine a code I made in the previous post (pt1) with the KB class and the above code. Basically I need to join them togeter.
What I am now posting is the 'Marks Calculator code' followed by the keyboard class and t get code to get the final mark. They both work seperatly, I just need help to get them to work together.

class MarksCalculator{


char computeLetterGrade(float inv){
char ch ' ';
if(inv <50.0)
ch = 'F';
else if(inv < 60.0)
ch = 'D';
else if(inv < 70.0)
ch = 'C';
else if(inv < 80.0)
ch = 'B';
else if(inv < 100.0)
ch = 'A';
return ch;
}

float computePercentage(float a1, float a2, float m, float f,){
float weightAssigns = (a1 + a2)/4;
float weightMidterm = (m)/4;
float weightExam = (a)/2;
}

public static void main(String args[]){

MarksCalculator m = new MarksCalculator();

float percentage = m.computePercentage(50f, 70.0f, 70.0f, 80.0f);
char grade = m.computeLettergrade(percentage);

System.out.println("The corresponding grade is: " + grade);
}
}

// 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("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
while(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
if(achieved >= anInt)
{
System.out.println("Please enter a number less than " + anInt);
achieved = Keyboard.getInteger( );
}
if(achieved <= 0)
{
}
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");

}

}THIS POST IS WRONG IGNORE
[ January 30, 2005: Message edited by: Michael Munro ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Check my post from the 22nd. I think there's still a potential problem with validating the achieved grade.

As for putting all of this together: Try explaining (in words) what each of the classes do, and how these will work together. Specifically, where exactly in the process do we want to use each of these classes, and what type of information do we want to pass back and forth between them?
[ January 30, 2005: Message edited by: marc weber ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
No, there is no problem with validating the grade, there was but I have fixed that now. I need to join together the code called 'MarksCalculator' at the top(which finds the grade letter and the percentage) with the KbTester class and the code which finds the actual mark.
Im not sure but maybe the 'MarksCalculator' code should go at the end instead of at the begining. There seems to be a problem getting that to connect with the KbTester class which begins with the lines import...
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I'll post the codes seperatly: The first is the 'Marks Calculator' when the marks gained are enterd it will ouput the grade (A,B or C etc) and the percentage, this works perfectly:

class MarksCalculator{
//method definition

char computeLetterGrade(float inv){
char ch = ' ';
if(inv <50.0)
ch = 'F';
else if(inv < 60.0)
ch = 'D';
else if(inv < 70.0)
ch = 'C';
else if(inv < 80.0)
ch = 'B';
else if(inv < 100.0)
ch = 'A';
return ch;
}
float computePercentage(float a1, float a2, float m, float f){
float weightAssigns = (a1 + a2)/8;
float weightMidterm = (m)/4;
float weightExam = (f)/2; //calculation of results
return (a1/8 + a2/8 + m/4 + f/2 ); //returns the method round
}

public static void main(String args[]){
//creates a new class
MarksCalculator m = new MarksCalculator();
//the method and results to be calculated
float percentage = m.computePercentage(50f, 70.0f, 70.0f, 80.0f); //students results enterd here
char grade = m.computeLetterGrade(percentage);

System.out.println("The corresponding grade is: " + grade);
System.out.println("The corresponding percentage is: " + percentage);
}
}



The next is the code which it need to somehow join. This is the KbTester code follwed by the code which finds the actual Mark gained. This also works perfectly:

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("Please enter the maximum grade: ");
int anInt = Keyboard.getInteger( );
while(anInt <= 0)
{
System.out.print("The maximum grade must be greater than zero");
System.out.println("\nPlease enter the maximum grade: ");
anInt = Keyboard.getInteger( );
}
System.out.println("Please enter the achieved grade: ");
int achieved = Keyboard.getInteger( );
if(achieved >= anInt)
{
System.out.println("Please enter a number less than " + anInt);
achieved = Keyboard.getInteger( );
}
if(achieved <= 0)
{
}
float result = (float)achieved/anInt;
System.out.println("Mark is " + (result*100) + "%");

}

}


Now I need to get the two codes to work together.
[ January 30, 2005: Message edited by: Michael Munro ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Sorry I posted the wrong Marks Calculator code, that was an old one. I am going to edit the post. Ok the above code is now correct.
[ January 30, 2005: Message edited by: Michael Munro ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Michael Munro:
Now I need to get the two codes to work together.
I'm just jumping in now, and I don't quite know what this means. Do you want the KbTester program to use the MarksCalculator class to calculate the mark for the scores the user enters? I'll run with that since it makes sense to my brain.

Before I address your question, please bear with me for a few tips.

In the future, please use code tags to keep the indentation so we have a chance to read it in the forum. Otherwise we have to copy-and-paste-and-reformat it into our editor of choice. Adding any number of steps, no matter how small, will lessen your chances of getting an answer.

Since the computeLetterGrade() and computePercentage() methods of MarksCalculator use only their parameters and nothing else to perform their work, they make great candidates for static methods. Like the main() method, static methods do not require an instance of the class in order to be called.Static methods are often used for utility methods such as you've created. Check out java.util.Math for a prime example of a utility class. Also see your Keyboard class for another example.

In your code to read in the score achievedand comparing it to the while loop you used to read in the maximum score immediately above, I suspect you already saw the problem you created: how do you handle two boundary (validation) conditions? Here's a simple pattern that can help:I leave it to you to add the other tests and merge it with your code if you want. While loops are perfect for repeating a process until it passes some set of tests, like user input.

So you have a maximum score and the student's actual score. Now what? You want to use MarksCalculator to calculate the letter grade, correct? The main() method in MarksCalculator does exactly this, so you need to copy in the lines that do it into the main() method for KbTester.

All three classes are in the same package, right? In this case, that's the default "no package" package (not generally a good idea, but okay for small programs like this). This is important since Keyboard and MarksCalculator are not "public" classes -- they can only be accessed by other classes in that package. If this isn't something you've heard of before, let it go for now, but make sure all three classes are in the same directory.

Hmm, the only hint I can think to give you isn't a hint -- it's the solution. How have you tried to solve this problem? What errors did you get? KbTester already uses the Keyboard class, and it can use the MarksCalculator in almost the same way. "Almost" because Keyboard uses static methods while MarksCalculator uses instance methods. Change that as I suggested above and you can use MC in exactly the same manner.
[ January 30, 2005: Message edited by: David Harkness ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Hey No worries Project complete Now I've got it to work! Thanks for the help, forever in debt to your priceless advice!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Michael Munro:
... Project complete Now I've got it to work! ...

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Michael Munro:
I've got it to work! Thanks for the help, forever in debt to your priceless advice!
Isn't that a great feeling? Good job! Just remember to help the new people out when it's your turn.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I'll do my best, but I'd say I'm still relatively cluess but not as much as I was!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Keyboard.Class/Anyone with brains.pt2
 
Similar Threads
How to read "0000 0001" from keyboard?
Lowest, Highest, Average
check values in a csv file in all files in a directory
accept input about patients, write it to a file, display the file's contents
Need help extending a program.