CSU Banner

CS 163/164, Spring 2017

Lab 10 - Pseudo String Class

Tues-Wed, Feb. 27-28, 2017


Objectives of this Lab

  1. Write a class that implements a number of methods,
  2. declare instance data inside your class,
  3. instantiate an object of your class and call its methods, and
  4. test your own class with the main method,

Description

In this lab you will create your own class and write the methods in it. The class you write is called Pseudo and it mimics some of the attributes of the String class. After you write the methods in the Pseudo class, you can test them by comparing the return values from methods in your class to the return values from the String class.

Instructions

Create a project called R10 and a class named Pseudo in a file called Pseudo.java, then follow the instructions below exactly:
  1. Inside the class, but outside any method, declare a private instance (non-static) array of type char that holds up to 80 characters.
  2. Inside the class, but outside any method, declare a private instance (non-static) variable of type int that holds the length of the string.
  3. Declare a public (non-static) void method called setString that takes a parameter of type String and stores the characters in the instance array and length.
  4. Declare a public (non-static) method called getString that takes no parameters and builds and returns a String with the contents of your character array.


  5. NOTE:To implement the methods below, you cannot use the String methods indexOf, toUpperCase, substring, or equals. All of the methods below must use the contents of the character array stored in the Pseudo class:

  6. Declare a public (non-static) method called charAt that takes an index parameter and returns the character from the array at the index specified. You can assume that the index is valid, i.e. between 0 and String.length()-1.
  7. Declare a public (non-static) method called indexOf that takes a character parameter and returns the index in the character array of the first occurrence of that character or -1 if the character is not found.
  8. Declare a public (non-static) method called toUpperCase that constructs and returns a String with the contents of the character array converted to upper case. Do not change the contents of the character array!
  9. Add a main method with the usual signature that instantiates the Pseudo class and tests its methods as follows (the output from Pseudo should match the output from String):
    public static void main(String[] args) {

        // Instantiate class        
        Pseudo pseudo = new Pseudo();

        // Verify class
        System.out.println("Verifying Pseudo:");
        pseudo.setString("1234567890 Computer Science !(*@&#)*");
        System.out.println("pseudo = " + pseudo.getString());
        System.out.println("pseudo.charAt(5) = " + pseudo.charAt(5));
        System.out.println("pseudo.indexOf('C') = " + pseudo.indexOf('C'));
        System.out.println("pseudo.toUpperCase() = " + pseudo.toUpperCase());
        
        // String equivalent (should match!)
        System.out.println("\nComparing String:");
        String string = "1234567890 Computer Science !(*@&#)*";
        System.out.println("string = " + string);
        System.out.println("string.charAt(5) = " + string.charAt(5));
        System.out.println("string.indexOf('C') = " + string.indexOf('C'));
        System.out.println("string.toUpperCase() = " + string.toUpperCase());
    }

Testing

Based on the provided test code in the main method, you should see the following output if your methods are correct:
Verifying Pseudo:
pseudo = 1234567890 Computer Science !(*@&#)*
pseudo.charAt(5) = 6
pseudo.indexOf('C') = 11
pseudo.toUpperCase() = 1234567890 COMPUTER SCIENCE !(*@&#)*

Comparing String:
string = 1234567890 Computer Science !(*@&#)*
string.charAt(5) = 6
string.indexOf('C') = 11
string.toUpperCase() = 1234567890 COMPUTER SCIENCE !(*@&#)*

Specifications

Your program must meet the following specifications:

Grading

Show your class to the TA with the test run for credit.


CS Banner
CS Building