Algorithms and OOD (CSC 207 2014S) : Labs

# Laboratory: Some Basic Types

Summary: In this laboratory, you will extend your knowledge of some basic types in Java: Numbers , Strings, and such.

Some Useful Classes:

## Preparation

Create a new project for this lab. Create a new package called taojava.labs.basics.

## Exercises

### Exercise 1: Limits on Integer Values

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.

### Exercise 2: Limits on Real Values

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.

### Exercise 3: Square Roots and Approximations

You may recall that in a previous course, you answered the following question when you first explored numeric computation:

Have DrScheme 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?

Redo this exercise in Java, using doubles for the computation.

Note that java.lang.Math.sqrt is useful for computing square roots.

### Exercise 4: Other Cool Math Functions

Read the documentation for java.lang.Math and summarize for yourself what methods are available.

### Exercise 5: Exceeding Limits with BigInteger

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?

### Exercise 6: Manipulating Strings

a. Create a new main class, Exercise5, 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);

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);

### Exercise 7: String Buffers

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);

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);

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);

### Exercise 8: Assigning Strings

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?

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);

### Exercise 9: String Methods

Read through the documentation for java.lang.String and java.lang.StringBuffer and make a note of at least three interesting and useful procedures that we have not yet covered in this lab.

### Exercise 10: Segmenting Strings

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.

Copyright (c) 2013-14 Samuel A. Rebelsky.