A Procedure for Exercising Your Brain
We are given a positive integer n, the number of exercises
in a set, and a non-negative integer i, the number of the
current exercise. (Exercise numbers range from 0 to n - 1.)
Try to perform the following steps when you are wide awake.
If you catch yourself beginning to nod off, then sleep;
when you feel refreshed, return to where you were at when
you wisely took a break.
Step 1. RTFM. Read the fascinating manuals. In other words,
do some of the recommended reading. You don't have
to read everything. Be judicious. Don't hesitate to
read good stuff more than once.
Step 2. Try. Attempt exercise i.
Step 3. Done? If you think you have finished exercise i,
then peruse any hints or solutions that you have
not already studied. Double check your answer.
Step 4. Happy? If you are happy with your answer, then
divide i + 1 by n, let r be the remainder, and
set i <- r. (We will have 0 <= i < n.)
Step 5. Stuck? If you feel stuck and there are hints you
haven't peeked at yet, then peek at the next hint.
Remember that Google is your so-called "friend".
Step 6. Continue. Go to step 1 of exercise i.
/* Hints for warm up drill A-1.
*/
public class ValueRangesForTypes {
/* Create a subroutine that prints values of two objects
* referred to by the parameter variables min and max.
*/
public static void printRange(Object min, Object max) {
}
/** A program that prints the smallest and largest values
* that can be represented by the primitive numeric data
* types of the Java programming language.
*/
public static void main(String[] args) {
}
}
/* An answer for warm up drill A-1.
*
* The following Java program prints the smallest and largest
* finite numbers that can be represented using each of Java's
* six primitive numeric types: byte, short, int, long, float,
* and double. The source code for the program demonstrates
* several basic Java constructs and programming techniques
* including:
*
* 0. Defining a class with methods.
* a) public class ValueRangesForTypes { [...] }
* c) public static void main(String[] args) { [...] }
* b) [...] printRange(Object min, Object max) { [...] }
*
* 1. Using standard Java classes, e.g.
* a) System (System.out.println).
* b) Byte (Byte.MIN_VALUE, Byte.MAX_VALUE).
* c) Object (Object min).
*
* 2. Calling/invoking methods.
* a) min.getClass()
* b) System.out.println(maxLabel + ": " + max);
*
* 3. Passing arguments to a method.
* a) printRange(0x8000000000000000L, 0x7fffffffffffffffL);
*
* 4. Declaring and initializing variables.
* a) String minLabel = "min finite " + [...] ;
*
* 5. Using literals of various types, e.g.
* a) -32768 (a negative number of type int).
* b) 0x80000000 (a hexadecimal number of type int).
* c) 0x8000000000000000L (a hex number of type long).
* d) "min finite " (a String literal).
*
* 6. Explicitly casting values from one type to another.
* a) (short)32767
*
* 7. Using operators.
* a) +, -, ~, >>, >>>, (), etc.
*
* 8. Using parentheses to control order of evaluation.
* a) ((~0) >>> 1) >> 0
*
* 9. The use of comments to describe code.
* a) End-of-line comments (//).
* b) Multi-line comments.
*/
public class ValueRangesForTypes {
/* A subroutine that prints values of two objects
* referred to by the parameter variables min and max.
*/
public static void printRange(Object min, Object max) {
String minLabel = "min finite " + min.getClass().getSimpleName();
String maxLabel = "max finite " + max.getClass().getSimpleName();
// We can call the object's toString method explicitly.
System.out.println(minLabel + ": " + min.toString());
// Or we can just pass the object to the println method
// and that method will invoke the toString method.
System.out.println(maxLabel + ": " + max);
}
/** A method that prints the smallest and largest finite
* values that can be represented by the six primitive
* numeric types of the Java programming language.
*/
public static void main(String[] args) {
/************/
/* Integers */
/************/
// Use built-in constant properties of the class
// that represents the type as arguments.
printRange(Byte.MIN_VALUE, Byte.MAX_VALUE);
// Use literal values as arguments. We might want
// to cast the literal values to the primitive type
// if the literal is of a different type. Here
// casting is applicable, because -32768 and 32767
// are interpreted by Java to be values of type int
// as opposed to values of type short. Casting
// converts the int values to short values. This
// might help us spot our mistake if we happen to
// pass an argument value that is out of range.
// For example, try changing 32767 to 32768. What
// happens?
printRange((short)-32768, (short)32767);
// Use hexadecimal literals or bitwise operators
// as arguments. Remember that the first bit in the
// base 2 representation of integers encoded using
// two's complement is a sign bit that indicates
// whether the number is positive (sign bit is 0) or
// negative (sign bit is 1). Also remember that the
// minimum value (the negative value with the
// largest magnitude) is one more than the largest
// positive value in this case. Finally, remember
// that he four Java integer types use two's
// complement to encode integers, whereas floating
// point numbers are encoded using a completely
// different encoding system.
printRange(0x80000000, ((~0) >>> 1) >> 0);
// Use hexadecimal long values. Note the terminal
// letter L indicating that these are long values.
printRange(0x8000000000000000L, 0x7fffffffffffffffL);
/*********************************/
/* Finite Floating Point Numbers */
/*********************************/
// Be careful if you use the built-in constant
// properties, since the Float.MIN_VALUE and
// Double.MIN_VALUE values are the smallest POSITIVE
// values that can be represented, not the negative
// values with the largest magnitude. Floating point
// numbers in Java are represented using an encoding,
// The IEEE Standard for Floating-Point Arithmetic
// (IEEE 754), that is very different from the way
// integers are represented. In contrast to the
// integer data types, the minimum finite floating
// point number has the same magnitude as the largest
// finite floating point number for a given type.
printRange(-Float.MAX_VALUE, Float.MAX_VALUE);
printRange(-Double.MAX_VALUE, Double.MAX_VALUE);
}
}
/* Hint for the main exercise. */
public class Terrific
{
/** outputBytes */
public static int outputBytes(String[] set, int[] seq)
{
}
/** ttt */
public static int ttt()
{
}
/** main */
public static void main(String[] args)
{
}
}
/* Hint for the main exercise. */
public class Terrific
{
/** outputBytes */
public static int outputBytes(String[] set, int[] seq)
{
String s = "";
final int n = set.length;
for (int i = 0; i < n; i++)
{
// Replace s with the concatenation of s and the
// the next string in the sequence of strings
// that is defined by the indices of seq.
}
// Print the value of s.
// Return the space cost of the inputs.
}
/** ttt */
public static int ttt()
{
final String[] set = {};
final String[] seq = {};
return outputBytes(set, seq);
}
/** main */
public static void main(String[] args)
{
}
}
/* Hint for the main exercise, part 1. */
/* Created by Holden Hutley and Mr. Spurgeon
* during the 5th period of the day (Holden's
* study all period) on 22 Oct 2018.
*/
public class Foo {
public static int sumLengthsOfStrings(String[] strings) {
int tally = 0;
for (int i = 0; i < strings.length; i++) {
tally = tally + strings[i].length();
}
return tally;
}
public static int outputBytes(String set[], int seq[]) {
int sumOfLengthsOfStringsInSet = sumLengthsOfStrings(set);
int numberOfElementsInSeq = seq.length;
int numberOfElementsInSet = set.length;
// The output of (i.e. the value returned by) outputBytes
// should be the sum of:
// 2 × the combined lengths of the strings referred to by
// the elements of set;
// 4 × the number of elements of seq;
// 8 × the number of elements of set.
return 2 * sumOfLengthsOfStringsInSet +
4 * numberOfElementsInSeq +
8 * numberOfElementsInSet;
}
public static void main(String[] args) {
String[] testSet = {"hello, world\n"};
int[] testSeq = {0};
int est = outputBytes(testSet, testSeq);
System.out.println(est);
}
}
The goose always repeated everything she said because . . . she was a mom.
Perhaps she had been repeating herself to her goslings so long, she began to talk that way perpetually.
— HEATHER C. KING, “‘TERRIFIC, TERRIFIC, TERRIFIC’
and Why the Goose Repeats Herself” (2012)
and Why the Goose Repeats Herself” (2012)
§ 1. The Exercise
In this programming exercise, you will get practice using several basic programming constructs found in most high-level programming languages including arithmetical and logical operations and operators, variables, arrays, conditionals, loops, and subroutines. Warm up drills provide the opportunity to focus on subsets of these skills in isolation. The main exercise introduces an interesting data compression problem and guides you through the process of creating several subroutines related to the problem. More challenging exercises follow the main exercise.