Lab 9 In-Class: Classes

As usual, create a lab9 subdirectory for today's lab, open up Mozilla and the Web version of this handout, and open emacs.

  1. Program Account.java contains the Account class from the pre-lab.

    1. Fill in your definitions for methods printSummary, chargeFee (returning the new balance) and changeName. Compile your class.

    2. Program ManageAccounts.java contains the shell program from the prelab that uses the Account class to create and manipulate bank accounts. Add code as indicated by the comments. Note that this program asks you to use getBalance to print the balance in two places that were not in the prelab. Compile and run your program.

    3. Although chargeFee returns the new balance, your ManageAccounts program currently throws that value away. Modify ManageAccounts so that each time it calls chargeFee it stores the returned balanced in a variable (you'll have to declare a new one). After each call to chargeFee, add a print statement that prints the stored value of the new balance (appropriately labeled).

    Print Account.java and ManageAccounts.java.

  2. Program Student.java contains the incomplete Student class declaration from the prelab.
    1. Complete the class declaration. You will need to do the following:
      • Declare the instance data (name, score for test1, and score for test2). Remember that these should be private.
      • Add the bodies for the constructor and the inputGrades method.
      • Add the headers and bodies for the printName and getAverage methods.
      Compile your Student class.

    2. Program Grades.java contains a shell program that declares two Student objects. Fill in statements to do the following for each student:
      • Prompt for and read in the student's name.
      • Create a Student object using the name you read in and store it in the appropriate Student variable.
      • Use the inputGrades method to read in the student's test scores.
      • Use the getAverage method to find the student's test average. Print this average with a simple label (but no name), e.g., "Average is 87.5".
      Test your program.

    3. It would be nicer to use the student's name in the label for the average ("Average for Joe is 87.5"), and you do have a printName method. Use it to add the name to the label for the first student -- you can do it, but it's a pain!

    4. As you just saw, methods that print values are often cumbersome to use. Add a method getName to your Student class that instead of printing the name, returns it as a string. Think: does it need any parameters? What is its return type? Be sure to give it a nice documentation header like the other methods have. Now modify your Grades program to use getName to print the second student's name along with the average. (The first student should still use printName.) Much easier!

    5. Modify the inputGrades method of the Student class so that it validates the grades it reads in. That is, if a grade entered is less than 0 or greater than 100, it should print a warning message and ask for another grade. This should be repeated (for each grade entered) until the grade is between 0 and 100.

    6. Enhance your Student class so that you figure out the student's initials. Initials are easy to extract from the full name; just use the first character plus any character that comes after a space. Proceed as follows:
      • First add an instance variable of type String called initials to the Student class. This will go with the variables that hold the name and test scores, and will also be private.
      • In the constructor, after you store the name that is passed in, extract the initials and store them in the initials variable:
        • Initialize variable initials to be just the first letter in the name. (Note that initials is a string and the first letter is a char. An easy way to convert a char to a string it to concatenate it to the empty string; e.g., "" + 'a' => "a".) Remember that String method charAt takes an integer and returns the character at that index, 0-based. For example, if name is "Mary", name.charAt(0) is 'M' and name.charAt(2) is 'r'.
        • Use a for loop to run through all of the characters in the name (still using charAt). Remember the syntax:
          for (init; condition; update)
             body
          
          You can get the number of characters in a String with its length() method. When you find a space (' '), add the next character to the initials. (Remember that you can concatenate a character to a String using +.) So the loop will look something like this:
          for each character in the name
             if this character is a space
                add the next character to the initials
          
          Of course, this will work only if there is exactly one space between first/middle/last names, but we'll assume that for now.
      • Now add a method getInitials to the Student class that returns the student's initials. It will just return the value of the initials variable, which was determined in the constructor. It should NOT print anything -- it should just return a string containing the initials. Think: Does this method need any parameters?
      • Modify Grades.java so that after printing the average for each student, it prints the student's initials.
      • You'll find that the initials come out in lower or upper case, depending on how the user entered the name. Modify the code in your constructor so that they are always upper case. This is easy; construct the initials string as before, then use the toUpperCase method of the String class.

    7. Add statements to the end of your Grades program that print the values of your Student variables directly, e.g.:
          System.out.println("Student 1: " + student1);
      
      This should compile and run, but notice what it does -- nothing very useful! When an object is printed, Java looks for a toString method for that object. This method must have no parameters and must return a string. If such a method exists for this object, it is called automatically -- you don't have to write the call in your program -- and the string it returns is printed. If no such method exists, a unique hexadecimal identifier for the object is printed (e.g., Student@3a56d7).

      Add a toString method to your Student class that returns a string containing the student's name and test scores, e.g.:

                        Name: Joe  Test1: 85  Test2: 91
      
      Note that the toString method does not call System.out.println -- it just returns a string.

      Recompile your Student class and the Grades program (you shouldn't have to change the Grades program -- you don't have to call toString explicitly). Now see what happens when you print a student object -- much nicer!

    Print Student.java and Grades.java.

  3. The Coin class from Listing 4.2 in the text is in the file Coin.java. Copy it to your directory, then write a program to find the length of the longest run of heads in 100 flips of the coin. A skeleton of the program is in the file Runs.java. You will add to the skeleton as follows (see the comments in the program):
Print Runs.java to hand in.

Hand in: