Algorithms and OOD (CSC 207 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Summary: In this laboratory, you will extend your knowledge of some basic types in Java: Numbers, Strings, and such.
Some Useful Classes:
Create a new project for this lab. Create a new package called
taojava.labs.basics.
a. Read the documentation for the various class forms of integers and
write a main class that prints out the largest and smallest legal
value for int and long. For example, to
print out the largest int, you would use something like:
pen.print("Largest int: ");
pen.println(Integer.MAX_VALUE);
b. Determine experimentally what happens when you add 1, 2, 5, and 100 each largest legal value. Note that you may get some strange results because, well, it doesn't make a lot of sense to add beyond the largest value.
c. Determine experimentally what happens when you subtract 1, 2, 5, and 100 from each smallest legal value.
a. Determine experimentally the smallest positive non-zero float value you can represent.
b. Read the documentation for java.lang.Float
and see if it provides further guidance on what values are
representable.
You may recall that in a previous course, you answered the following question when you first explored numeric computation:
Have DrRacket compute the square of the square root of 2 and subtract 2 from the result. Ideally, the difference should be 0. Why isn't it? How big is the difference? [Davis, Rebelsky, Stone, Walker, and Weinman. Functional Problem Solving]
Redo this exercise in Java, using doubles for the computation.
Note that java.lang.Math.sqrt is useful for computing
square roots.
What happens when int or long is not big
enough, but you want exact numbers? In C, you're stuck making your
own representation for arbitrary-precision integers. In Java, there's
a wonderful utility class, java.math.BigInteger
Read the documentation for java.math.BigInteger and answer the following questions.
a. What is the largest possible BigInteger?
b. What constructors does BigInteger provide? Why
do you think the designers decided to include all of them?
c. What standard arithmetic operations do you see and how are they called?
d. Are there any methods you find surprising? If so, which ones?
a. Create a new main class, StringManipulation, for this exercise.
b. As you may note from the documentation for java.lang.String,
Java strings have a concat operation.
What do you expect the result of the following instructions to be?
PrintWriter pen = new PrintWriter(System.out, true);
String greeting = "Hello ";
pen.println(greeting.concat("World"));
pen.println(greeting);
c. Check your answer experimentally. What does the result suggest about strings in Java?
d. Strings in Java also contain a few replace methods.
What do you expect the result of the following instructions to be?
(We'll assume you still have the PrintWriter from the
earlier subexercise.)
String word = "alphabet";
pen.println(word.replace('a', 'A'));
pen.println(word);
e. Check your answer experimentally. What does the result suggest about strings in Java?
In addition to java.lang.String, Java also provides
an alternative called java.lang.StringBuffer. (You should
be able to guess why after the previous exerciss.) StringBuffer
objects provide some similar methods to those we just explored, although
with slightly different semantics or names (or both).
You can either create a new main class for this exercise, or reuse one from the previous exercise.
a. What do you expect the result of the following instructions to be?
StringBuffer buffer = new StringBuffer("This is a StringBuffer");
pen.println(buffer);
b. Check your answer experimentally. What does the result suggest about string buffers in Java?
c. What do you expect the result of the following instructions to be?
StringBuffer hello = new StringBuffer("Hello ");
pen.println(hello.append("World"));
pen.println(hello);
d. Check your answer experimentally. What does the result suggest about string buffers in Java?
e. Let's explore the replace method.
What do you expect the result of the following instructions to be?
StringBuffer text = "alphabet"; pen.println(text.replace(0, 0, "A")); pen.println(text); pen.println((text.replace(2, 3, "A"))); pen.println(text); pen.println(text.replace(8, 9, "A")); pen.println(text);
f. Check your answer experimentally. What does the result suggest about string buffers in Java?
You can either create a new main class for this exercise, or reuse one from a previous exercise.
a. What do you expect the result of the following instructions to be?
PrintWriter pen = new PrintWriter(System.out, true);
String str = "Hello ";
StringBuffer buf = new StringBuffer("Hello ");
// Create another name for str
String alt = str;
pen.println("alt: " + alt);
// Update str
str = str + "world";
pen.println("str: " + str);
pen.println("alt: " + alt);
// Create another name for buf
StringBuffer stf = buf;
pen.println("stf: " + stf);
// Update the buffer.
buf.append("world");
pen.println("buf: " + buf);
pen.println("stf: " + stf);
// Extract info from the buffer
alt = buf.toString();
// Update the buffer.
buf.append(". Are we done yet?");
pen.println("buf: " + buf);
pen.println("alt: " + alt);
b. Check your answer experimentally.
c. Reflect with your partner or the people around you on the implications of this exercise.
You can either create a new main class for this exercise, or reuse one from a previous exercise.
a. What do you expect the result of the following instructions to be?
PrintWriter pen = new PrintWriter(System.out, true);
String str = "Alphabetical";
pen.println(str.substring(0,5));
pen.println(str.substring(0,1));
pen.println(str.substring(1,5));
pen.println(str.substring(2,2));
b. Check your answer experimentally.
c. What do you expect to have happen if the second parameter to
substring is less than the first?
d. Check your answer experimentally.
e. What do you expect to have happen if the first or second parameter
to substring is equal to the length of the string?
f. Check your answer experimentally.
Read the documentation for java.lang.Math
and make a note of one or two particularly interesting methods that
are available.
Read through the documentation for java.lang.String and
java.lang.StringBuffer and make a note of at least two
interesting and useful procedures that we have not yet covered in this
lab.
Programmers often find it convenient to store compound data in a text file with one line per entry. To separate the components of the entry, they use some designated symbol, such as a colon. For example, we might store information on ratings of movies with a format like
rater-lname:rater-fname:movie:rating:maxrating
with sample entries of
Ebert:Roger:Bedazzled:5:5 Siskel:Gene:Bedazzled:4:5 Rebelsky:Samuel:Satyricon:0:5 Rebelsky:Samuel:Playtime:5:5 Rebelsky:Samuel:Bedazzled:6:5
Write a method that takes one line of the given form, segments it into its components, and returns a string in human-readable form. For example, for the first line above, it might return the string “Roger Ebert gave Bedazzled 5 on a 5-point scale”.
In approaching this, you may find it easiest to see if you can build a string with just the last name, then add the first name, then the movie, and so on and so forth.
Note that you should use indexOf to find the index of the
colon and then substring to extract the appropriate portion.