Assignment • FAQ • Development Guide • Code • Checklist • |
You must turn in your assignment by Wednesday, October 6th, 2021 at
11:59am at NOON.
In this assignment, you are asked to write methods that read strings and integers from the user, processes them, and displays them.
You will be writing these methods in the hw2.java file. Your programs should terminate when the user enters ^D (Ctrl-d).
The methods to write in hw2.java:
void clrbuf (int character);
long decin ();
long getaline (char message[], int maxlength);
The methods needed from your hw1:
baseout, decout, hexout, newline, writeline
You must write debug statements in the following methods:
getaline, writeline
In this assignment, portions of the method headers are provided for you. Refer to these to get a better understanding of the methods you will be writing.
clrbuf:
Removes any characters in the System.in buffer by repeatedly calling fgetc until a newline character is detected. The method is implemented by calling fgetc until the buffer is empty. You know the buffer is empty when the last character you removed was a a newline character '\n'.
decin:
Reads a decimal number from System.in and converts it from a sequence of ASCII characters into a decimal integer. This converted value is returned. When an overflow occurs or non-digit characters are entered, the user is prompted to reenter the number using the digiterror method (provided). The best way to check for overflow is after you perform a math operation, perform the opposite operation to see if you get the original value back. As for non-digit characters, just check if the character you grabbed using fgetc is outside the values of the '0' and '9' characters. You should be using a loop to keep grabbing characters from System.in until you hit the newline character '\n'. If it's a valid character, convert it to a decimal number by subtracting the '0' character from it ( look at an ASCII table to see why that works ). Finally, to add this to the sum you're returning, you just use the algorithm of multiplying your current sum by 10 and adding the converted digit so it falls in the one's place. Don't forget to check for EOF!
decout:
Displays number in base 10 to the filestream stream.
getaline:
Reads characters from System.in or System.in and stores them into the message array. The number of characters stored into message must not exceed maxlength (including NULL character). Any extra characters entered to System.in are discarded using clrbuf. Add a debug statement which prints the number of characters stored in message. The method should return the number of characters stored. Don't forget to check for EOF!
hexout:
Displays number in hexadecimal with a preceding 0x. The number will always be printed with 16 digits (ex. 0x00000000000ABC12, 0x0000000000000000).
writeline:
In hw2.java, update the writeline code you wrote from hw1 by adding a debug statement to display the length of the string printed.
Add code to return the number of characters printed.
To implement these methods, you may not make calls to ANY methods you did not author except for fputc and fgetc. For more information about fputc and fgetc, look at the man page by typing man fputc or man fgetc into the command prompt or into Google. You can also use fprintf for error and debug messages using System.err , and you can use System.err.print or System.err.println for error and debug messages.
Copy and past the following commands into your terminal
Assignment • FAQ • Development Guide • Code • Checklist • |