Lab 9 In-Class: Classes

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

  1. Program 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 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 for two calls to getBalance that were not in the prelab.) Compile and run your program.
    3. Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You can either store it in a variable and then print the value of the variable, or embed the method call in a println statement.

    Print and

  2. Program 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).
      • 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 contains a shell program that declares two Student objects. Fill in statements to do the following for each student:
      • 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 the average with the student's name, e.g., "The average for Joe Brown is 87". Use the printName method to print the student's name.
      Test your program.
    3. The printName method is rather cumbersome for producing the output described in (b). Add a method getName to your Student class that instead of printing the name, returns it as a string. Now modify your Grades program to use getName instead of printName in printing each student's average.
    4. 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.
    5. 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.
      • In the constructor, after you store the name that is passed in, you can extract the initials and store them in the initials variable:
        • Initialize variable initials to be just the first letter in the name. 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)
          When you find a space (' '), add the next character to the initials. (Remember that you can concatenate a character to a String.) 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 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.
    6. Add statements to your Grades program that print the values of your Student variables directly, e.g.:
          System.out.println("Student 1: " + student1);
      This should compile, but notice what it does when you run it -- 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 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 and

  3. The Coin class from Listing 4.2 in the text is in the file 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 You will add to the skeleton as follows (see the comments in the program):
Print to hand in.

Hand in: