CS 112 Lab 2: More I/O, and using the Math and String libraries

In this lab we will continue our study of the Java platform, building essential skills for using Java in flexible and interesting applications. Most of these applications will involve some kind of Input and output (I/O), and will access some of the build-in libraries of Java. Two of the most useful of these are the Math library and the String library.

NOTE: You should work on these problems in the hour lab, and then finish them to submit as problem B.1 in H1, due Friday. You do NOT have to submit anything today in the lab.

Problem One (I/O with for loops)

We will write a version of the Statistics program from HW 1 in such a way that it accepts an unlimited number of doubles from the user, first asking the user for how many doubles to input, then inputting exactly that many, and then calculating the maximum, the minimum, the sum, and the mean (average) of the numbers. These numbers should be output to the console using System.out.println(...). Here is a template showing how to input integers: StringTest2.java. You must rewrite this to create your solution.

You must do the following:

You may NOT use the Math library for this problem. Write your program as ArrayStatistics.java and hand it in with HW1.

 

Problem Two (String Library)

For this problem, we will explore the String library. Review the PDF of the lecture from today about Strings if you are unsure what to do.

Now download StringTest3.java and run it; you will see that it inputs a sequence of strings and simply prints them out between double quotes. For this problem, you must read in lines which are assumed to be English sentences ending in a period (no other punctuation). For each line you must:

  1. Split the line into an array of Strings (which will be the words); this would be accomplished by using split("\\s+"), which uses any white space as a delimiter;
  2. Convert each word to all lower case, using the appropriate method from the String library (look at my slides!);
  3. Capitalize the word by extracting the first letter, capitalize it using explicit arithmetic on chars and ints, and then concatenate it with the rest of the word; you may NOT use a library method to do this, but must do it as shown in lecture using arithmetic on chars; s
  4. Print out the sentence, with one space between each word, and an exclamation point at the end.

You may NOT use an explicit capitalize method from any library, but must use the method described here. You do not have to use every method given above, but these are the most useful ones.

Hand in your code as a file StringPractice.java with your other HW1 files.

Here is an example of what my solution prints out: