Problem Solving and Computing (CSC-103 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Journal] [Problems] [Computing] [SamR Docs] [Tutorial] [API]


Computing Assignment 6: Objects

Assigned: Tuesday, March 3, 1998
Assigned: Thursday, March 12, 1998

Purpose: In this assignment, you will investigate another perspective on Java's programming model. Hopefully, this will resolve some of the problems some of you have been having.

Background

Recall that there are three basic steps to building a Java program (assuming that SamR has helped you make the change that allows this shorthand).

Of course, when creating a program from scratch, you should begin by sketching the design of the program: what it does and how it does it.

Calculations with Strings

I've created a simple class that provides a number of simple "calculations on strings". If you take a look at the source code for the class, you'll observe that there is no main routine. This is because the class is intended only as a utility class.

For now, you only need to understand what the various methods in the class do and now how they do them.

Using the StringManipulator Object

How do we use this class? We create a separate class (StringStuff.java) that contains only a main method. That method needs to

Copy StringManipulator.java and StringStuff.java to files, compile both files, and then execute. You should only execute StringStuff.java (although you might see what happens if you try to exectue StringManipulator.java).

% jc StringManipulator.java
% jc StringStuff.java
% ji StringStuff.java

Extensions

Write and test a palindrome method that turns a string into a palindrome by appending the reverse of the string to itself. For example, palindrome("zebra") should produce zebraarbez.

Turning it In

You do not need to turn in your answers to this assignment, but you should make sure it runs correctly.


Code

StringManipulator.java



/**
 * Compute various attributes and variations of strings.  Constructed
 * as part of an example for Math/CS103.
 * <p>
 * Copyright (c) 1998 Samuel A. Rebelsky.  All rights reserved.
 *
 * @version 1.0 of March 1998
 * @author Samuel A. Rebelsky
 */
public class StringManipulator {

  // +---------+-----------------------------------------------------------
  // | Methods |
  // +---------+

  /**
   * Compute the string given by removing the first letter in 
   * a string.
   */
  public String deleteFirst(String str)
  {
    return str.substring(1);
  } // deleteFirst

  /**
   * Compute the string generated by concatenating a string
   * with itself.
   */
  public String duplicate(String str)
  {
    return str + str;
  } // Duplicate(String)

  /**
   * Compute the reversal of a string (the letters in reverse
   * order).
   */
  public String reverse(String str)
  {
    String rev = "";
    // Append characters, one by one
    for (int pos = str.length()-1; pos >= 0; pos = pos - 1) {
      rev = rev + str.charAt(pos);
    } // for
    // That's it
    return rev;
  } // reverse

} // StringManipulator




StringStuff.java



import StringComputer;		// We'll compute with strings
import rebelsky.io.SimpleInput;	// We'll read strings
import rebelsky.io.SimpleOutput;// We'll write strings

/**
 * Manipulate a string in a few different ways.
 */
public class StringStuff 
{
  public static void main(String[] args)
  {
    // A string the user inputs
    String str;
    // Something for reading input
    SimpleInput in = new SimpleInput();
    // Something for writing output
    SimpleOutput out = new SimpleOutput();
    // Something that can manipulate strings
    StringManipulator manip = new StringManipulator();

    // Get a string
    out.print("Enter a string: ");
    str = in.readLine();

    // Print out various modified versions of that string
    out.println("Doubled: " + manip.duplicate(str));
    out.println("Reversed: " + manip.reverse(str));
    
    // Repeatedly remove the first element until we run out of
    // letters.
    while (!str.equals("")) {
      out.println(str);
      str = manip.deleteFirst(str);
    } // while
  } // main(String[])

} // StringStuff




[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Journal] [Problems] [Computing] [SamR Docs] [Tutorial] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Tue Mar 3 13:40:22 1998.

This page generated on Thu Apr 2 13:50:01 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu