CS 160, Spring 2015
Programming Assignment P7
Creating a Pseudo String Class

Programming due Monday, Mar. 23 at 6:00pm; late deadline Mar. 23 at 11:59pm.


This lab has the goal of teaching you how to:
  1. Write a class that implements a number of methods.
  2. Declare private instance data inside your class.
  3. Instantiate an object of your class and call its methods.
  4. Test your own class with the main method.

Description

In this assignment 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 P7 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 variable of type String initialized to "", with a name you choose.
  2. Declare a public void method called setString that takes a parameter of type String and stores it in your instance variable.
  3. Declare a public method called getString that takes no parameters and returns a String with the contents of your instance variable.

    NOTE: You can only use the String methods charAt and length, in addition to String concatenation with the '+' operator. You cannot use any other String or Character methods in your class!

    All of the methods below operate on the instance variable of type String stored in the Pseudo class:

  4. Declare a public method called charAt that takes an index parameter and returns the character in the stored string at the index. You can assume that the index is valid, i.e. between 0 and String.length()-1.
  5. Declare a public method called indexOf that takes a character parameter and returns the index in the stored string of the first occurrence of that character or -1 if the character is not found.
  6. Declare a public method called substring with two integer parameters start and end that returns the substring in a manner identical to the String method of the same name. You can assume that both indices are valid
  7. Declare a public method called equals that takes a String parameter and compares the contents of it with the stored string. Return true if they exactly match character for character, and false if not.
  8. 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.substring(11, 19) = " + pseudo.substring(11, 19));
		pseudo.setString("Compare");
		System.out.println("pseudo.equals(\"Compare\") = " + pseudo.equals("Compare"));
		System.out.println("pseudo.equals(\"Compare!\") = " + pseudo.equals("Compare!"));
		
		// 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.substring(11, 19) = " + string.substring(11, 19));
		string = "Compare";
		System.out.println("string.equals(\"Compare\") = " + string.equals("Compare"));
		System.out.println("string.equals(\"Compare!\") = " + string.equals("Compare!"));
	}

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.substring(11, 19) = Computer
pseudo.equals("Compare") = true
pseudo.equals("Compare!") = false

Comparing String:
string = 1234567890 Computer Science !(*@&#)*&
string.charAt(5) = 6
string.indexOf('C') = 11
string.substring(11, 19) = Computer
string.equals("Compare") = true
string.equals("Compare!") = false

Specifications

Your program must meet the following specifications:

Grading Criteria

Submit your Pseudo.java file to the Checkin tab on the course website, as you were shown in the recitation, and read the syllabus for the late policy.

© 2015 CS160 Colorado State University. All Rights Reserved.