CS 160, Summer 2016
Programming Assignment P6
Creating a Pseudo String Class
Programming due Monday, Jul. 18 at 6:00pm; late deadline 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 P6 and a class named Pseudo in a file called Pseudo.java,
then follow the instructions below exactly:
- Declare two private instance variables. One of type char array that holds up to 80
characters and the other of type int that will hold the length of the string.
- Create a constructor that takes a parameter of type String and calls the
setString method that you will be implementing next.
- Create a setString method that takes a parameter of type String and doesn't return anything.
This method should and use a loop to store the characters into the instance array and then stores
the size into the instance variable length.
- Create a toString method that takes no parameters and returns a String. This method should
use a loop to build and returns a String with the contents of your character array.
WARNING: 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:
- Declare a public 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.
- Declare a public 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.
- Declare a public 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!
- Declare a public method called substring with two integer parameters
start and end that builds and returns a substring (from the character array) 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 substring with one integer parameter called start
that returns the substring from the character array in a manner identical to the String method
of the same name. You can assume the starting index is valid.
- This is called method overloading, even though the name of the method is the same, having a
different number of parameters makes it a different method. You may want to consider calling
the method you just created instead of re-writing the code again! What would you need to use
as the argument for the ending index?
- Declare a public method called pseudoEquals that takes a String
parameter and compares the contents of it with the string stored in the character array.
Return true if they exactly match character for character, and false if not.
- Declare a public method called pseudoEquals that takes a Pseudo parameter
and compares the contents of it with the string stored in the character array.
- This is called method overloading, even though the name of the method is the same, if the
parameters are different types, then the method is different. You may want to
consider calling the method you just created instead of re-writing the code again!
What would you need to provide as an argument?
- 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):
Testing
Here are some sample test cases and the corresponding output. Type this code into your main method to test your class out.
Output
output if your methods are correct:
Verifying Pseudo methods:
pseudo = 1234567890 Computer Science !(*@)*&
pseudo.charAt(5) = 6
pseudo.indexOf('C') = 11
pseudo.toUpperCase() = 1234567890 COMPUTER SCIENCE !(*@)*&
pseudo.substring(11, 17) = Comput
pseudo.substring(5) = 67890 Computer Science !(*@)*&
Verifying Pseudo equals method with a String object:
pseudo.pseudoEquals("Compare") = true
pseudo.pseudoEquals("Compare!") = false
Verifying Pseudo equals method with a Pseudo object:
pseudo.pseudoEquals("Compare") = true
pseudo.pseudoEquals("Compare!") = false
Comparing to the String Methods:
string = 1234567890 Computer Science !(*@)*&
string.charAt(5) = 6
string.indexOf('C') = 11
string.toUpperCase() = 1234567890 COMPUTER SCIENCE !(*@)*&
string.substring(11, 19) = Computer
string.equals("Compare") = true
string.equals("Compare!") = false
You may want to do some additional testing on your own!
Specifications
Your program must meet the following specifications:
- Your Pseudo class must be capable of storing a String as an array of characters.
- 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 or 1.8.
- 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.
- Preliminary Tests
- compileTest: checks that code in the Pseudo class compiles. (6 points)
- test1: tests that another object can instantiate the Pseudo class. (9 points)
- test2: tests toString to make sure the constructor worked properly (5 points)
- test3: tests setString and toString in the Pseudo class. (10 points)
- test4: Tests the charAt method with an arbitrary string. (10 points)
- test5: Tests the indexOf method with an arbitrary string. (10 points)
- test6: Tests the toUpperCase method with an arbitrary string. (10 points)
- Final Tests
- test7: Tests the substring method with two parameters (10 points)
- test8: Tests the substring method with one parameter (10 points)
- test9: Tests the equals method with arbitrary strings (10 points)
- test10: Tests the equals method on other Pseudo objects (10 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.
© 2016 CS160 Colorado State University. All Rights Reserved.