public final class Rhul
extends java.lang.Object
Rhul
class features:
Rhul
class masks some more advanced Java constructs that are
not relevant in the context of a first programming course. These advanced
features will be uncovered in due time. The names in the Rhul
class have been chosen uniformly to ease programming.
The inputs are always from the keyboard and the outputs are always shown in the terminal.
The methods
print(type)
and
println(type)
allow to print any data type
(println
adds a newline at the end).
The input methods are of the form
readType()
for all the Java primitives data types. In
case of an error, all these methods will update a flag that can be accessed
using the isOk()
and getStatus()
.
An example of graphical program is the following:
Rhul.startDrawings(200,200); for (i=0; i < 200; i+=4) { Rhul.setGray(i); Rhul.drawLine(0,i,199,199-i); Rhul.drawLine(i,0,199-i,199); } Rhul.stopDrawings();It produces:
typeToString()
and
stringTotype()
where (type can be:
boolean
, int
, long
, float
ou double
). These methods provide the expected conversions from
and to String
.
The following comparison methods are also included: equals()
,
compare()
,
subString()
,
charAt()
et
length()
.
Modifier and Type | Field and Description |
---|---|
static int |
ARG_ERROR
The arguments is invalid (ex:
==null ). |
static int |
FORMAT_ERROR
The conversion of the character string that was read to the needed data
type was not possible.
|
static int |
NO_ERROR
The last I/O operation went well.
|
static int |
STREAM_ERROR
Reading the input stream was impossible.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
booleanToString(boolean value)
Converts a boolean to a character string (
String ). |
static char |
charAt(java.lang.String aString,
int position)
Extracts the character at a given position in a character string.
|
static java.lang.String |
charToString(char value)
Converts a character to a character string (
String ). |
static void |
clearArea()
Clears the diplay area completely.
|
static int |
compare(java.lang.String aString,
java.lang.String anotherString)
Compares two character strings.
|
static java.lang.String |
doubleToString(double value)
Converts a double-precision floating point number to a character string (
String ). |
static void |
drawArc(int x,
int y,
int r,
int angleInit,
int angleCouvert)
Draws an arc in the display zone.
|
static void |
drawCircle(int x,
int y,
int r)
Draws a circle in the display zone.
|
static void |
drawLine(int x0,
int y0,
int x1,
int y1)
Draws a line between two points in the drawing area.
|
static void |
drawPoint(int x,
int y)
Draws a point in the drawing zone.
|
static void |
drawRect(int x,
int y,
int w,
int h)
Draws an empty rectangle in the display zone.
|
static boolean |
equals(java.lang.String aString,
java.lang.String anotherString)
Tests the equality between two character strings.
|
static void |
exit()
Terminates the program.
|
static void |
fillArc(int x,
int y,
int r,
int angleInit,
int angleCouvert)
Draws an filled arc in the display zone.
|
static void |
fillCircle(int x,
int y,
int r)
Draws a filled circle in the display zone.
|
static void |
fillRect(int x,
int y,
int w,
int h)
Draws a filled rectangle in the display zone.
|
static java.lang.String |
floatToString(float value)
Converts a single-precision floating point number to a character string (
String ). |
static int |
getStatus()
Returns the error code of the last I/O operation.
|
static java.lang.String |
intToString(int value)
Converts an integer to a character string (
String ). |
static boolean |
isOk()
This method return the error state of the last I/o operation.
|
static int |
length(java.lang.String aString)
Computes the length of a given character string.
|
static java.lang.String |
longToString(long value)
Converts a long integer to a character string (
String ). |
static void |
print(boolean b)
Prints a boolean (
boolean ) |
static void |
print(char c)
Prints a character.
|
static void |
print(double d)
Prints a double-precision flooating point number (
double ) |
static void |
print(float f)
Prints a single-precision floating point number (
float ) |
static void |
print(int i)
Prints an integer (
int ) |
static void |
print(long l)
Prints a long integer (
long ) |
static void |
print(java.lang.Object o)
Prints the
String representation of an object |
static void |
print(java.lang.String s)
Prints a character string (
String ) |
static void |
println()
Prints a new line.
|
static void |
println(boolean b)
Prints a boolean (
boolean ) followed by a newline. |
static void |
println(char c)
Prints a single character (
char ) followed by a newline. |
static void |
println(double d)
Prints a double-precision floating point number (
double )
followed by a newline. |
static void |
println(float f)
Prints a single-precision floating point number (
float )
followed by a newline. |
static void |
println(int i)
Prints an integer (
int ) followed by a newline. |
static void |
println(long l)
Prints a long integer (
long ) followed by a newline. |
static void |
println(java.lang.Object o)
Prints the
String representation of an object
followed by a newline. |
static void |
println(java.lang.String s)
Prints a character string (
String )
followed by a newline. |
static char |
readChar()
Reads a character from the keyboard and returns its value.
|
static double |
readDouble()
Reads a lexeme from the input and converts it into a
double . |
static float |
readFloat()
Reads a lexeme from the input and converts it into a
float . |
static int |
readInt()
Reads a lexeme from the input and converts it into an
int . |
static java.lang.String |
readLine()
Read a character string until the next carriage return (end of line).
|
static long |
readLong()
Reads a lexeme from the input and converts it into a
long . |
static java.lang.String |
readString()
Reads a lexeme from the input.
|
static void |
setColor(int r,
int g,
int b)
Sets the color of the subsequent drawing operations.
|
static void |
setGray(int c)
Sets the color of the subsequent drawing operations to be grey.
|
static void |
startDrawings()
Prepares a drawing zone of size 200x200.
|
static void |
startDrawings(int width,
int height)
Prepares a drawing zone of arbitrary size.
|
static void |
stopDrawings()
Closes the drawing window.
|
static boolean |
stringToBoolean(java.lang.String s)
Converts a character string into a
boolean . |
static byte |
stringToByte(java.lang.String s)
Converts a character string into a
byte . |
static double |
stringToDouble(java.lang.String s)
Converts a character string into a
double . |
static float |
stringToFloat(java.lang.String s)
Converts a character string into a
float . |
static int |
stringToInt(java.lang.String s)
Converts a character string into an
int . |
static long |
stringToLong(java.lang.String s)
Converts a character string into a
long . |
static java.lang.String |
subString(java.lang.String aString,
int begin,
int end)
Extracts a substring from a given character string.
|
public static final int NO_ERROR
public static final int STREAM_ERROR
public static final int FORMAT_ERROR
public static final int ARG_ERROR
==null
).public static boolean isOk()
true
false
getStatus()
.getStatus()==NO_ERROR
.public static int getStatus()
public static char readChar()
The read bytes come from the standard input.
<nul>
it is advised to look if the I/O operation was
actually successful or not (isOk()
).public static void exit()
public static java.lang.String readLine()
The characters are read one after the other using the
readChar()
method.
public static boolean stringToBoolean(java.lang.String s)
boolean
.s
- the character stringboolean
) true
s
is equal to
"true"
false
public static int stringToInt(java.lang.String s)
int
.
In case of an error, the error state is updated.
s
- the character stringint
)
Warning: if the returned value is 0
, it is
necessary to consult the error status of the operation.public static byte stringToByte(java.lang.String s)
byte
.
In case of an error, the error state is updated.
s
- the character stringbyte
)
Warning: if the returned value is 0
, it is
necessary to consult the error status of the operation.public static double stringToDouble(java.lang.String s)
double
.
In case of an error, the error state is updated.
s
- the character stringdouble
)
Warning: if the returned value is 0.0
, it is
necessary to consult the error status of the operation.public static float stringToFloat(java.lang.String s)
float
.
In case of an error, the error state is updated.
s
- the character stringfloat
)
Warning: if the returned value is 0.0
, it is
necessary to consult the error status of the operation.public static long stringToLong(java.lang.String s)
long
.
In case of an error, the error state is updated.
s
- the character stringlong
)
Warning: if the returned value is 0
, it is
necessary to consult the error status of the operation.public static int readInt()
int
.
In case of an error, the error state is updated.
int
)public static long readLong()
long
.
In case of an error, the error state is updated.
long
)public static float readFloat()
float
.
In case of an error, the error state is updated.
float
)public static double readDouble()
double
.
In case of an error, the error state is updated.
double
)public static java.lang.String readString()
In case of an error, the error state is updated.
null
in case of failure).public static void print(char c)
c
- the character to print.public static void print(int i)
int
)i
- the integer to printpublic static void print(boolean b)
boolean
)b
- the boolean to printpublic static void print(long l)
long
)l
- the long integer to printpublic static void print(float f)
float
)f
- the float to printpublic static void print(double d)
double
)d
- the double to printpublic static void print(java.lang.Object o)
String
representation of an objecto
- the object to printpublic static void print(java.lang.String s)
String
)s
- the string to printpublic static void println(char c)
char
) followed by a newline.
A call to this method is equivalent to
print(c); println();
.c
- the character to printpublic static void println(boolean b)
boolean
) followed by a newline.
A call to this method is equivalent to
print(c); println();
.b
- the boolean to printpublic static void println(int i)
int
) followed by a newline.
A call to this method is equivalent to
print(c); println();
.i
- the integer to printpublic static void println(long l)
long
) followed by a newline.
A call to this method is equivalent to
print(c); println();
.l
- the long integer to printpublic static void println(float f)
float
)
followed by a newline.
A call to this method is equivalent to
print(c); println();
.f
- the single-precision float to printpublic static void println(double d)
double
)
followed by a newline.
A call to this method is equivalent to
print(c); println();
.f
- the double-precision float to printpublic static void println(java.lang.Object o)
String
representation of an object
followed by a newline.o
- the object to printpublic static void println(java.lang.String s)
String
)
followed by a newline.s
- the string to printpublic static void println()
public static char charAt(java.lang.String aString, int position)
aString
- the character stringposition
- the index of the character to extractpublic static java.lang.String subString(java.lang.String aString, int begin, int end)
aString
- the character stringbegin
- index of the first character of the substringend
- index of the first character excluded from the substringpublic static java.lang.String booleanToString(boolean value)
String
).value
- the boolean (boolean
)public static java.lang.String charToString(char value)
String
).value
- the character (char
)public static java.lang.String floatToString(float value)
String
).value
- the single-precision floating number (float
)public static java.lang.String doubleToString(double value)
String
).value
- the double-precision floating number (double
)public static java.lang.String intToString(int value)
String
).value
- the integer (int
)public static java.lang.String longToString(long value)
String
).value
- the integer (long
)public static boolean equals(java.lang.String aString, java.lang.String anotherString)
aString
- the first character stringanotherString
- the second character stringtrue
if the two are equal,
false
otherwise.public static int compare(java.lang.String aString, java.lang.String anotherString)
aString
- the first character stringanotherString
- the second character stringpublic static int length(java.lang.String aString)
aString
- the character stringpublic static void startDrawings()
public static void startDrawings(int width, int height)
width
- the width of the displayheight
- the height of the displaypublic static void stopDrawings()
public static void drawPoint(int x, int y)
x
- the x-coordinate (abscissa)y
- the y-coordinate (ordinate)public static void setColor(int r, int g, int b)
0,0,0
for black to 255,255,255
for white.
Red is therefore with 255,0,0
, green with 0,255,0
and blue with 0,0,255
.r
- the red levelg
- the green levelb
- the blue levelpublic static void setGray(int c)
0
for black to 255
for white.
An average grey would be done with parameter 127
.c
- the grey-levelpublic static void drawLine(int x0, int y0, int x1, int y1)
x0
- the x-coordinate of the first point.y0
- the y-coordinate of the first point.x1
- the x-coordinate of the second point.y1
- the y-coordinate of the second point.public static void drawRect(int x, int y, int w, int h)
The top-left corner is at coordinates x
and y
.
x
- the x-coordinate of the top-left corner.y
- the y-coordinate of the top-left corner.w
- the width of the rectangle.h
- the height of the rectangle.public static void fillRect(int x, int y, int w, int h)
The top-left corner is at coordinates x
and y
.
x
- the x-coordinate of the top-left corner.y
- the y-coordinate of the top-left corner.w
- the width of the rectangle.h
- the height of the rectangle.public static void drawCircle(int x, int y, int r)
x
- the x-coordinate of the centre of the circle.y
- the y-coordinate of the centre of the circle.r
- the circle radius.public static void fillCircle(int x, int y, int r)
x
- the x-coordinate of the centre of the circle.y
- the y-coordinate of the centre of the circle.r
- the circle radius.public static void drawArc(int x, int y, int r, int angleInit, int angleCouvert)
The angles are in degrees.
The direction is counter-clockwise.
The initial angle is angleInit
and the terminating angle is angleInit + angleCouvert
.
x
- the x-coordinate of the centre of the circle.y
- the y-coordinate of the centre of the circle.r
- the circle radius.angleInit
- the initial angle.angleCouvert
- the angle covered by the arc.public static void fillArc(int x, int y, int r, int angleInit, int angleCouvert)
The angles are in degrees.
The direction is counter-clockwise.
The initial angle is angleInit
and the terminating angle is angleInit + angleCouvert
.
x
- the x-coordinate of the centre of the circle.y
- the y-coordinate of the centre of the circle.r
- the circle radius.angleInit
- the initial angle.angleCouvert
- the angle covered by the arc.public static void clearArea()