Class Object120
- java.lang.Object
-
- Object120
-
public class Object120 extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
DEFAULT_BITS_TOLERANCE
-
Constructor Summary
Constructors Constructor Description Object120(java.lang.Object... args)
Automated constructor: Initialize each field of the subclass with the provided args, in the order the fields are declared within the subclass' file.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static void
assertEquals(java.lang.Object expectedResult, java.lang.Object actualResult)
Check that two things are `equals`; if not report it to System.err.static char
charAt(java.lang.String _this, int i)
Select a single character from a string, by index (starting at 0).static <T,S extends T>
intcompareTo(java.lang.Comparable<T> _this, S that)
Check whether one value is greater, equal, or lesser than another.static boolean
contains(java.lang.String _this, java.lang.CharSequence target)
Return whether or not one String contains another.boolean
equals(java.lang.Object oth)
static boolean
equals(java.lang.Object _this, java.lang.Object that)
Are two values equal? Works on any values (non-null).static boolean
equalsApprox(double a, double b)
static boolean
equalsApprox(double a, double b, int bitsTolerance)
static boolean
equalsIgnoreCase(java.lang.String a, java.lang.String b)
Are two Strings equal (up to case, but including punctuation).static java.lang.String
getVersion()
Return the version number of this library (since it might get updated through the semester).int
hashCode()
static boolean
hasNext()
Return whether there another token is available from from the keyboard.static boolean
hasNextDouble()
Return whether the next token of input from the keyboard is a double.static boolean
hasNextInt()
Return whether the next token of input from the keyboard is an int.static boolean
hasNextLine()
Return whether there is another line of input from the keyboard.static int
indexOf(java.lang.String _this, java.lang.String target)
Return where one string is contained inside another.static boolean
isEmpty(java.lang.String _this)
Return whether or not a String is the empty string "" (0 letters long).static int
length(boolean[] data)
Return the length of the array `data`.static int
length(byte[] data)
Return the length of the array `data`.static int
length(char[] data)
Return the length of the array `data`.static int
length(double[] data)
Return the length of the array `data`.static int
length(float[] data)
Return the length of the array `data`.static int
length(int[] data)
Return the length of the array `data`.static int
length(long[] data)
Return the length of the array `data`.static int
length(short[] data)
Return the length of the array `data`.static int
length(java.lang.String _this)
Return the number of characters in a string.static <T> int
length(T[] data)
Return the length of the array `data`.static java.lang.String
next()
Return the next token(word) of input from the keyboard.static double
nextDouble()
Return the next double from the keyboard.static int
nextInt()
Return the next int from the keyboard.static java.lang.String
nextLine()
Return the next line of input from the keyboard.static void
printTestMsg(java.lang.String msg)
Print a message (about testing perhaps), and make sure it doesn't abut any '.' characters printed by assertEquals.static void
printTestSummary()
Print a summary of how many tests ran & how many passed.static void
printVersion()
Print version information to the console window.static int
randomInt(int n)
Return a random integer in [0,n).static void
setSeed(long s)
(Re)set the random-number generator to a specific seed (used by Object120#randomInt).static java.lang.String[]
split(java.lang.String src, java.lang.String delimiterPattern)
Split a string into substrings, dlimited by a regular expression.static java.lang.String
substring(java.lang.String _this, int from)
Return a substring of the given string from index `from` up through the last character.static java.lang.String
substring(java.lang.String _this, int from, int to)
Return a substring of the given string, from index `from` up to but not including index `to`.static char
toChar(int n)
Return character corresponding to a particular the unicode value.static char
toChar(java.lang.String s)
Convert a String-of-length-1 to a char.static double
toDouble(int n)
Convert an int to a double.static double
toDouble(java.lang.String s)
Convert a String to a double.static int
toInt(char c)
Return the unicode value of a character.static int
toInt(double x)
Convert a double to an int (truncating towards zero).static int
toInt(java.lang.String s)
Convert a String into an int.static java.lang.String
toLowerCase(java.lang.String _this)
Return a lower-case version of the given string.java.lang.String
toString()
java.lang.String
toString(boolean includeFieldNames)
Return a String which looks like a constructor call:static java.lang.String
toString(java.lang.Object _this)
Convert any value into a String.static java.lang.String
toUpperCase(java.lang.String _this)
Return an upper-case version of the given string.
-
-
-
Field Detail
-
DEFAULT_BITS_TOLERANCE
static final int DEFAULT_BITS_TOLERANCE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Object120
public Object120(java.lang.Object... args)
Automated constructor: Initialize each field of the subclass with the provided args, in the order the fields are declared within the subclass' file. Remember that if a use writes their own constructor for a subclass, this constructor will still get called first with 0 args, unless the explicitly called `super` with more args. Alas, we can't check here whether they actually manage to initialize their fields correctly...
-
-
Method Detail
-
getVersion
public static java.lang.String getVersion()
Return the version number of this library (since it might get updated through the semester).
-
printVersion
public static void printVersion()
Print version information to the console window.
-
randomInt
public static int randomInt(int n)
Return a random integer in [0,n).- Parameters:
n
- the top end of the range.- Returns:
- a pseudorandom integer in [0,n). E.g. randomInt(100) might return 0 or 1 or 99, but never 100.
- See Also:
Random.nextInt(int)
-
setSeed
public static void setSeed(long s)
(Re)set the random-number generator to a specific seed (used by Object120#randomInt). (Resetting the seed will cause the sequence of random numbers to repeat, which can be helpful for testing and for reproducing bugs).- Parameters:
s
- The new seed for the random-number-generator.
-
equals
public static boolean equals(java.lang.Object _this, java.lang.Object that)
Are two values equal? Works on any values (non-null). A static version of Object.equals(Object), for use in CS1 before we introduce objects.- Parameters:
_this
- the first value to compare. (Must be non-null.)that
- the second value to compare. (Must be non-null.)- Returns:
- true iff `_this` equals `_that`.
For example:
equals( "hello", "howdy" ) = false equals( "hello", "hello" ) = true equals( 23, 22+1 ) = true equals( 'A', 65 ) = false equals( new int[]{2,3}, new double[]{2.0,3.0} ) = true
- See Also:
Object.equals(Object)
-
toString
public static java.lang.String toString(java.lang.Object _this)
Convert any value into a String. A static version of Object.toString(), for use in CS1 before we introduce objects.- Parameters:
_this
- The value to stringify. Cannot be null.- Returns:
- a String representation of `_this`.
For example:
toString(43) = "43" toString(true) = "true"
- See Also:
Object.toString()
-
compareTo
public static <T,S extends T> int compareTo(java.lang.Comparable<T> _this, S that)
Check whether one value is greater, equal, or lesser than another. A static version of compareTo(Object), for use in CS1 before we introduce objects.- Parameters:
_this
- The first value to compare.that
- The second value to compare.- Returns:
- a positive number if `_this` is greater than `that`, zero if `_this` is equal to `that`, or a negative number if `_this` is less than `that`.
- See Also:
Comparable.compareTo(Object)
-
equalsIgnoreCase
public static boolean equalsIgnoreCase(java.lang.String a, java.lang.String b)
Are two Strings equal (up to case, but including punctuation). A static version of String.equalsIgnoreCase(String), for use in CS1 before we introduce objects.- Parameters:
a
- The first string to compare. Cannot be null.b
- The second string to compare. Cannot be null.- Returns:
- true iff a and b are the same (ignoring case).
For example:
equalsIgnoreCase( "hi", "HI" ) = true equalsIgnoreCase( "hi", "hi " ) = false equalsIgnoreCase( "", "" ) = true
- See Also:
String.equalsIgnoreCase(String)
-
length
public static int length(java.lang.String _this)
Return the number of characters in a string. A static version of String.length(), for use in CS1 before we introduce objects.- Parameters:
_this
- The string to find the length of. Cannot be null.- Returns:
- The number of characters in `_this`.
For example:
length( "hi ho" ) = 5 length( "z" ) = 1 length( "" ) = 0
- See Also:
String.length()
-
substring
public static java.lang.String substring(java.lang.String _this, int from, int to)
Return a substring of the given string, from index `from` up to but not including index `to`. A static version of `String.substring(int,int)`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to take a substring from. Cannot be null.start
- The index of the first character of the substring.stop
- The index of the first character after the substring.- Returns:
- A String consisting from characters at indices [`start`,`stop`) from `_this`.
- See Also:
String.substring(int,int)
-
substring
public static java.lang.String substring(java.lang.String _this, int from)
Return a substring of the given string from index `from` up through the last character. A static version of `String.substring(int)`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to take a substring from. Cannot be null.start
- The index of the first character of the substring.- Returns:
- A String consisting from characters at indices [`start`,`length(_this)`) from `_this`.
- See Also:
String.substring(int)
-
indexOf
public static int indexOf(java.lang.String _this, java.lang.String target)
Return where one string is contained inside another. A static version of `String.indexOf(String)`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to look inside. Cannot be null.target
- The String to try to find occuring inside `_this`. Cannot be null.- Returns:
- The index of `_this` where `target` starts, or -1. That is: if int i=indexOf(s1,s2), then i==-1 || equals(s2,substring(s1,i,i+length(s2))) (equivalently, i==-1 || s2.equals(s1.substring(i,i+s2.length())))
- See Also:
String.indexOf(String)
-
toLowerCase
public static java.lang.String toLowerCase(java.lang.String _this)
Return a lower-case version of the given string. A static version of `String.toLowerCase()`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to take a substring from. Cannot be null.- Returns:
- A lower-case version of `_this`.
- See Also:
String.toLowerCase()
-
toUpperCase
public static java.lang.String toUpperCase(java.lang.String _this)
Return an upper-case version of the given string. A static version of `String.toUpperCase()`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to convert to upper case. Cannot be null.- Returns:
- An upper-case version of `_this`.
- See Also:
String.toUpperCase()
-
split
public static java.lang.String[] split(java.lang.String src, java.lang.String delimiterPattern)
Split a string into substrings, dlimited by a regular expression.- Returns:
- An array of substrings of `src`, delimited by `delimiterPattern` (a regular expression).
- See Also:
String.split(String)
-
next
public static java.lang.String next()
Return the next token(word) of input from the keyboard. @see java.util.Scanner#next()
-
nextInt
public static int nextInt()
Return the next int from the keyboard. @see java.util.Scanner#nextInt()
-
nextDouble
public static double nextDouble()
Return the next double from the keyboard. @see java.util.Scanner#next()
-
nextLine
public static java.lang.String nextLine()
Return the next line of input from the keyboard. @see java.util.Scanner#nextLine()
-
hasNext
public static boolean hasNext()
Return whether there another token is available from from the keyboard. @see java.util.Scanner#hasNext()
-
hasNextInt
public static boolean hasNextInt()
Return whether the next token of input from the keyboard is an int. @see java.util.Scanner#hasNextInt()
-
hasNextDouble
public static boolean hasNextDouble()
Return whether the next token of input from the keyboard is a double. @see java.util.Scanner#hasNextDouble()
-
hasNextLine
public static boolean hasNextLine()
Return whether there is another line of input from the keyboard. @see java.util.Scanner#hasNextLine()
-
length
public static <T> int length(T[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(byte[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(short[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(int[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(long[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(float[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(double[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(boolean[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
length
public static int length(char[] data)
Return the length of the array `data`.- Returns:
- `data.length`.
- See Also:
the field `.length` of array-objects.
-
toInt
public static int toInt(java.lang.String s)
Convert a String into an int.- Parameters:
s
- A string which is a valid representation of an int; cannot be empty.- Returns:
- the int represented by `s`.
For example:
toInt("2") = 2 toInt("007") = 7
- Throws:
java.lang.NumberFormatException
- if `s` does not represent a valid double. stringToDouble("2+3") throws NumberFormatException
-
toDouble
public static double toDouble(java.lang.String s)
Convert a String to a double.- Parameters:
s
- A string which is a valid representation of a double.- Returns:
- the double represented by `s`.
For example:
toDouble("43.2") = 43.2 toDouble("2") = 2.0 toDouble("007") = 7.0
- Throws:
java.lang.NumberFormatException
- if `s` does not represent a valid double. For example:toDouble("2+3") -> NumberFormatException
-
toDouble
public static double toDouble(int n)
Convert an int to a double. (Same as casting to an int, but use the regular syntax for calling-a-method.)- Parameters:
n
- the int to convert.- Returns:
- n as a double.
For example:
intToDouble(0) = 0.0 intToDouble(-2) = -2.0 intToDouble(2000000000) = 2e9 intToDouble(2000000001) = 2.000000001e9
-
toInt
public static int toInt(double x)
Convert a double to an int (truncating towards zero). (Same as casting to an int, but use the regular syntax for calling-a-method.)- Parameters:
x
- The double to convert to an int.- Returns:
- The int nearest to x, but between 0 and x.
For example:
doubleToInt(0.0) = 0 doubleToInt(3.1) = 3 doubleToInt(3.9) = 3 doubleToInt(-3.1) = -3.0 doubleToInt(-3.9) = -3.0
- See Also:
Double.intValue()
,Math.floor(double)
,Math.ceil(double)
,Math.round(double)
-
toChar
public static char toChar(int n)
Return character corresponding to a particular the unicode value. For "ordinary" characters, this is the ascii value ('A'=43,'B'=44','a'=97,...)- Parameters:
n
- The unicode(ascii) value to convert to a char. Must be valid as a short -- in [0,65536).- Returns:
- the character corresponding to the unicode value `n`.
-
toInt
public static int toInt(char c)
Return the unicode value of a character. For "ordinary" characters, this is the ascii value ('A'=43,'B'=44','a'=97,...)- Parameters:
c
- The character to get the unicode value of.- Returns:
- the unicode value of `c`.
-
toChar
public static char toChar(java.lang.String s)
Convert a String-of-length-1 to a char.- Parameters:
s
- A string of length 1.- Returns:
- s as a char.
- See Also:
String.charAt(int)
-
charAt
public static char charAt(java.lang.String _this, int i)
Select a single character from a string, by index (starting at 0). A static version of `String.charAt(int)`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to select a character from. Cannot be null.i
- The index of the character to select from s; 0 <= i < length(s).- Returns:
- The `i`th character of `_this`.
- See Also:
String.toLowerCase()
-
contains
public static boolean contains(java.lang.String _this, java.lang.CharSequence target)
Return whether or not one String contains another. A static version of `String.contains(CharSequence)`, for use in CS1 before we introduce objects.- Parameters:
_this
- The `String` to look inside of. Cannot be null.target
- The String to look for in `_this`.- Returns:
- whether or not `target` occurs in `_this`.
- See Also:
String.contains(CharSequence)
,indexOf(String,String)
,String.indexOf(String)
-
isEmpty
public static boolean isEmpty(java.lang.String _this)
Return whether or not a String is the empty string "" (0 letters long). A static version of `String.isEmpty()`, for use in CS1 before we introduce objects.- Parameters:
_this
- The String to compare to "". Cannot be null.- Returns:
- whether or not equals(_this,"") (equivalently, length(_this)==0).
- See Also:
String.isEmpty()
-
assertEquals
static void assertEquals(java.lang.Object expectedResult, java.lang.Object actualResult)
Check that two things are `equals`; if not report it to System.err. Intended for unit-testing (as an ersatz JUnit method).- Parameters:
expectedResult
- The desired/expected result of calling a function.actualResult
- The actual result of calling a function.- See Also:
printTestMsg(java.lang.String)
,printTestSummary()
,org.junit.jupiter.api.Assertions#assertEquals(Object,Object)
-
printTestMsg
public static void printTestMsg(java.lang.String msg)
Print a message (about testing perhaps), and make sure it doesn't abut any '.' characters printed by assertEquals.
-
printTestSummary
public static void printTestSummary()
Print a summary of how many tests ran & how many passed. Intended to be called after testing is complete.- See Also:
printTestMsg(java.lang.String)
-
equalsApprox
public static boolean equalsApprox(double a, double b, int bitsTolerance)
- Returns:
- whether `a` and `b` are approximately equal. That is, whether they are the same up to the last `bitsTolerance` bits (default @value{DEFAULT_BITS_TOLERANCE}). So `bitsTolerance`=0 is exactly-equal (aka `Double.equals`). There are 52-bits of precision in a double, so `bitsTolerance`=52 always passes. DISCLAIMER: this function is NOT exhaustively tested, and I'd actually be mildly surprised if there were NOT weird cases where it fails.
-
equalsApprox
public static boolean equalsApprox(double a, double b)
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object oth)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(boolean includeFieldNames)
Return a String which looks like a constructor call:- Parameters:
includeFieldNames
- If true, include the field names along with their values.- Returns:
- a String which looks just like a constructor call
when `includeFieldNames` is false,
or similar to one when `includeFieldNames` is true.
For example:
class Foo extends Object120 { int n; String s } new Foo(7,"hi").toString(false) = "new Foo( 7, \"hi\" )" new Foo(8,"ho").toString(true) = "new Foo( n=8, s=\"ho\" )"
Bug: Depending on the Java compiler, the field names might not be given in the same order they are declared in the class. (This is because java.lang.reflect doesn't provide access to the declared order; however, many implementations happen to to use that order.)
-
-