This website is preserved for historical and scholarly reference and is no longer actively maintained.
CpSc 210, Section 1
Assignment 2
September 3, 2002
Due: Wednesday, September 11 at 11:59 p.m.
Assignment: Write a method that takes a String containing the digits of a non-negative integer and returns the integer value. Two other methods are required in order to validate the String. Do not call any methods other than those covered in the chapter on Strings
(i.e., Do not call any Java method to accomplish these tasks. Write the code yourself.)
Total point value of assignment: 50 points
Not accepted late.
This assignment is to be done INDIVIDUALLY.
Assignment: Design and implement a class, StringToInt, which must be stored
in a file named StringToInt.java. (3 point deduction from grade for
failure to name file correctly!) You may assume that the string will
not contain leading zeros. i.e., You do not need to concern yourself
with strings of the form "00025".
public class StringToInt
{
public static String removeSpaces ( String target )
// post: returns target String with all blanks removed
{
// code here
}
public static boolean isValid ( String target )
// post: returns true if target String consists entirely of
// digits between 0 and 9 inclusive; otherwise, returns false
// (Note that minus sign is not allowed.)
{
// code here
}
public static int intEquivalent ( String target )
// pre: target contains no spaces or other non-numeric characters.
// String target is not empty.
// post: returns int equivalent of target String
{
// code here
}
public static boolean testResult ( String target, int number )
// post: returns true if number is int representation of target String
// otherwise, returns false
{
return (target.compareTo(""+number) == 0);
}
public static void main ( String args [ ] )
{
// print your name, assignment #, description of problem
// test program goes here -- one example shown
int number;
String str = "12345";
String save = str;
str = removeSpaces (str);
if ( isValid (str) )
{
number = intEquivalent (str);
if ( ! testResult (str,number) )
{
System.out.println (save + " and " + number
+ " do not test as equivalent.");
}
}
// more test cases here
System.out.println ("End of test code.");
}
}
Example 1. If the String contains " 1 234 ", remove spaces will
return "1234", isValid will return true, and intEquivalent will
return the int 1234.
Example 2. If the String contains "" (the empty String),
removeSpaces will return "" and isValid will return false.
Example 3. If the String contains "- 3 ", removeSpaces will return "-3"
and isValid will return false.
Grading Rubric:
5 pts. reasonable attempt to solve problem as assigned
5 pt. also compiles
5 pts. style standards (includes clarity)
(1) Your name must be printed as a comment at the beginning of file.
(2) A description of what this program does must immediate follow name.
(3) Indent exactly 3 or 4 spaces. Be consistent. See handout examples.
(4) Every Java statement must begin on a separate line.
(5) The first operation of your program must be to print your name,
followed by "CPSC 210", the assignment number, and a description
of what program does.
(6) A comment must immediately follow all while, do, or for loops
giving the condition that is true after loop exit.
The following 35 points are available only for code that compiles
and runs without error.
5 pts. Example 1 gives correct result
5 pts. Example 2 gives correct result
5 pts. Example 3 gives correct result
15 pts. program works correctly (with our code).
5 pts. overall quality of program (i.e., ease of reading and
understanding code, choice of code statements, etc.)
Other requirements:
Follow the style standards to be found on your instructor's
web page for CPSC 210.
Submit your program using the handin command. There is a link to
the handin description on the CPSC 210 lab page (www.cs.clemson.edu/~lab210).