CSE1020 Lab 09: Regular Expressions and Traversing a Collection

Please submit before 11:59PM Friday, November 11.

Using Regular Expressions


One common use of regular expressions is to split a string into individual substrings where the splitting occurs at some pattern of characters. For example, suppose you wanted to count the number of words in a string where the individual words are separated by whitespace; copy-and-paste the following program into your editor, save it, compile it, and run it, and observe its output.

import java.io.PrintStream;

public class Split
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      
      String s = "Harry Potter was a very unusual boy in many ways.";
      
      String regex = "\\s";
      String[] word = s.split(regex);
      int numWords = word.length;
      for (int i = 0; i < numWords; i++)
      {
         out.printf("%2d : %s%n", i, word[i]);
      }
   }
}


The program uses the regular expression "\\s" to match whitespace, and the String method split to split a string wherever the regular expression is matched. The result is returned in a Java array, and you can find the number of words in the array using the array attribute length.

Problem 7.19


Problem 7.19 from the textbook is:

The file "essay.txt" contains multiple lines of text comprising an English essay. Write an application that reads this file and outputs the average number of words per sentence in the essay. A word is defined as any sequence of non-whitespace characters, and a sentence is defined as any sequence of words whose last word ends with one of the following characters:

. ; : ? !

Note, however, that if the last word in a line ends with a hyphen, it means it is continued on the next line, and hence, should not be counted more than once. For example, consider the essay:

The population of Canada is about 30.5 million. Ontar-
io is one of the provinces in Canada, the most populous.
I live in Toronto; do you? Do you like it?


Your application should output:

Average number of words per sentence = 5.8.

In this example, the essay contains 29 words and 5 sentences. Note that 30.5 is a word because it contains non-whitespace characters. Note also that the decimal point in 30.5 did not end the sentence because it did not occur at the end of the word. Note also that "Ontar" and "io" were not counted as two words.

You should start with the following program (which will read in the essay one line at a time):

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class WordAverage
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      
      // first command line argument is a filename
      String fileName = "/cse/course/1020/essay.txt";
      File inputFile = new File(fileName);
      
      // creating the Scanner for a file might
      //   fail with an exception
      Scanner in = null;
      try
      {
         in = new Scanner(inputFile);
      }
      catch (IOException ex)
      {
         out.println("Error opening file named " + fileName);
         System.exit(1);
      }
      
      while (in.hasNextLine())
      {
         String line = in.nextLine();

      }

   }
}
 

Submit

Submit your program using the command:

submit 1020 L09 WordAverage.java

 

Searching a Collection

Student API

A Student is a collection that records all of the CSE courses and corresponding grades for a single student. Both courses and grades are stored as strings. The following program shows you how to use a for-each loop to retrieve every course and its corresponding grade taken by the student, as well as how to compute the grade point average (GPA) for the student.

import type.lib.Student;
import type.lib.ITstudent;
import java.io.PrintStream;

public class Grades
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      
      int seed = 0;
      if (args.length == 1)
      {
         seed = Integer.parseInt(args[0]);
      }
      
      Student.setSeed(seed);
      Student stu = Student.getRandom();

      // this code is here because ITstudents are evil
      if (stu instanceof ITstudent)
      {
         out.println("Try a different seed");
         return;
      }
      
      for (String course : stu)
      {
         String grade = stu.getCourseGrade(course);
         out.printf("course : CSE%s, grade : %s%n", course, grade);
      }
      double gpa = stu.getGpa();
      out.println("GPA is " + gpa);
   }
}


Here are some sample runs (*The optional command line argument must be an integer value; here, we are using it as something called a seed value. The seed value is used to create a random number generator with a known starting state; thus, every time you run the program with the same seed value, you get the same sequence of random numbers. This ensures that everyone who runs the program shown above can generate the same output as long as they use the same seed value.)

java Grades 8
course : CSE3829, grade : B
course : CSE2755, grade : D
course : CSE1711, grade : F
course : CSE1768, grade : B
course : CSE4007, grade : F
GPA is 2.0

java Grades 260
course : CSE2338, grade : A
course : CSE4259, grade : C
course : CSE4589, grade : A
GPA is 4.333333333333333

java Grades 354
course : CSE2542, grade : F
course : CSE4111, grade : F
course : CSE2389, grade : E
course : CSE1461, grade : D
course : CSE3188, grade : E
GPA is 0.8
 

Modify the program shown above so that it outputs the minimum number of courses that need to be retaken so that the student's GPA increases by 1.0; assume that the student will get an A for every course retaken. Note that it may not be possible to raise the GPA by 1.0; in this case, the student should not retake any courses.

Your output should look like the following:

java Grades
course : CSE4445, grade : F
course : CSE1384, grade : F
course : CSE1032, grade : A
course : CSE1557, grade : F
course : CSE3253, grade : E
course : CSE4055, grade : C
course : CSE3146, grade : E
course : CSE1976, grade : A
course : CSE4687, grade : D
course : CSE1785, grade : A
course : CSE3718, grade : E
course : CSE2827, grade : F
course : CSE2072, grade : B
GPA is 2.076923076923077
Upgrading CSE4445 from F to A, GPA is 2.4615384615384617
Upgrading CSE1384 from F to A, GPA is 2.8461538461538463
Upgrading CSE1557 from F to A, GPA is 3.230769230769231

java Grades 8
course : CSE3829, grade : B
course : CSE2755, grade : D
course : CSE1711, grade : F
course : CSE1768, grade : B
course : CSE4007, grade : F
GPA is 2.0
Upgrading CSE1711 from F to A, GPA is 3.0

java Grades 260
course : CSE2338, grade : A
course : CSE4259, grade : C
course : CSE4589, grade : A
GPA is 4.333333333333333
GPA cannot be increased by 1

java Grades 354
course : CSE2542, grade : F
course : CSE4111, grade : F
course : CSE2389, grade : E
course : CSE1461, grade : D
course : CSE3188, grade : E
GPA is 0.8
Upgrading CSE2542 from F to A, GPA is 1.8
 

Submit

Submit your program using the command:

submit 1020 L09 Grades.java