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:
- Write a class that implements a number of methods.
- Declare private instance data inside your class.
- Instantiate an object of your class and call its methods.
- 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:
- Inside the class, but outside any method, declare a private instance
variable of type String initialized to "", with a name you choose.
- Declare a public void method called setString that takes
a parameter of type String and stores it in your instance variable.
- 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:
- 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.
- 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.
- 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
- 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.
- 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:
- Your Pseudo class must be capable of storing a String as an instance variable
- None of the methods and data in the Pseudo class are static, except main.
- The methods in your class should match the behavior of methods in the String class.
- However, the methods in your class never need throw exceptions.
- Work on your own, as always.
- The name of the source code file must be exactly Pseudo.java.
- Name the file exactly - upper and lower case matters!
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java 1.5 or 1.6 or 1.7.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- -80 points for using any String method other than charAt and length!
- Preliminary Tests
- compileTest: checks that code in the Pseudo class compiles. (15 points)
- test1: tests that another object can instantiate the Pseudo class. (15 points)
- test2: tests setString and getString in the Pseudo class. (15 points)
- Final Tests
- test3: Tests the charAt method with an arbitrary string. (10 points)
- test4: Tests the indexOf method with an arbitrary string. (10 points)
- test5: Tests the substring method with an arbitrary string (20 points)
- test6: Tests the equals method with arbitrary strings (15 points)
- Final grading includes the preliminary tests.
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.