CS 214 Lab 3: Java Exercise


Begin by using a text editor to edit the file YearCodes.java. Take a moment to study it, to see how it implements our basic algorithm.

To build method yearCode(), we will begin by reviewing the (simplified) BNF for a Java method definition:

   MethodDef       ::=   MethodHeading MethodBody
   MethodHeading   ::=   Type identifier (ParameterDecs)
   ParameterDecs   ::=   ParameterDec MoreParams | Ø
   ParameterDec    ::=   Type identifier DefaultArg
   DefaultArg      ::=   = Expression | Ø
   MoreParams      ::=   , ParameterDec MoreParams | Ø
   MethodBody      ::=   { StatementList }

Method declarations alone are not used in Java.  The body of the method is coded when the declaration is made, creating the entire method definition.  Java programmers differentiate between classes and methods by capitalizing the first letter of class names and not capitalizing the first letter of method names.

From a procedural point of view, our yearCode() method has this specification:

Receive: year, a string. Precondition: year is one of {freshman, sophomore, junior, senior}. Return: The integer code corresponding to year (1, 2, 3 or 4).

We can place the following stub (method declaration) for our method after our main method but still within the YearCodes class:

public static int yearCode(String year)
{
}

To fill in the stub, we need the syntax of the Java if construct, which is a statement:

   IfStmt          ::=   if (Expression) Statement ElsePart
   ElsePart        ::=   else Statement | Ø

where Statement is any valid Java statement (including another if statement), and Expression is usually (but not limited to) a boolean expression:

   Expression      ::=   BoolExpr
   BoolExpr        ::=   RelationalExpr BoolCondition
   BoolCondition   ::=   BoolOp RelationalExpr | Ø
   BoolOp          ::=   && | || | !
   RelationalExpr  ::=   Expression RelationalCond
   RelationalCond  ::=   RelationalOp Expression | Ø
   RelationalOp    ::=   == | != | < | > | <= | >=

Our algorithm requires the equality relation, but the "==" operator applied to Strings only compares if the two strings are the same object.  We need to use the equals() method of the String class to check whether or not the values of two String objects are the same.  So we can use the rest of the preceding BNF productions to fill in our method stub:

public static int yearCode(String year)
{
   if (year.equals("freshman"))

   else if (year.equals("sophomore"))

   else if (year.equals("junior"))

   else if (year.equals("senior"))

   else

}

To complete the stub, we must understand how Java methods return a value to their caller. This is accomplished by a return statement:

   ReturnStmt     ::=   return Expression ;

When executed, such a statement evaluates Expression and returns control to the caller of the method, with

public static int yearCode(String year)
{
   if (year.equals("freshman"))
      return 1;
   else if (year.equals("sophomore"))
      return 2;
   else if (year.equals("junior"))
      return 3;
   else if (year.equals("senior"))
      return 4;
   else
      return 0;
}

Note that while this if-else-if structure is very readable, it is actually a bit misleading, since the structure makes it appear as if we have written a single statement. In actuality, each if after the first one is the separate Statement given in the ElsePart BNF production. A definition like the following would be less readable:

public static int YearCode(String year)
{
   if (year.equals("freshman"))
      return 1;
   else
      if (year.equals("sophomore"))
         return 2;
      else
         if (year.equals("junior"))
            return 3;
         else
            if (year.equals("senior"))
               return 4;
            else
               return 0;
}

but it more accurately reflects the grammar rules of the Java if.

Compile and test your code for correctness; then use script to create a file in which you cat your file, compile it, and show that it works correctly for valid and invalid values.

Follow the steps from the end of lab01 to create a script file script.java in which you (i) use cat to list your source file, (ii) show that it compiles with no errors, and (iii) show that it runs correctly, producing the correct output for each valid input and one invalid input.

That concludes the Java part of the lab.


Calvin > CS > 214 > Labs > 03 > Java


This page maintained by Joel Adams