



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Lab Challenge Archive
String Thing
|
|
Java Application --
StringThingApp
// General Information
// -------------------------------------------------------+
-----
// File: StringThingApp.java
// Type: java application file
// Date: November 1998
// Name: BLUE
// Line: Simple String Processing Application
// Application Description
// ------------------------------------------------
/*
Simple String processing application.
*/
// Required Packages
// -------------------------------------------------
import blue.io.*;
// Application Class
// ------------------------------------------------
class StringThingApp
{
// main method
static public void main (String args[])
{
// Create and print some strings which represent nam+
es
String singer = "Holliday, Billie";
String sculptor = "Claudel, Camille";
// Point A
String self = "Dunn, Patrick";
IO.println(singer);
IO.println(sculptor);
IO.println(self);
// Computer and print the lengths +
// Point B
int singerLength = singer.length();
int sculptorLength = sculptor.length();
int selfLength = self.length();
IO.println(); IO.println("Lengths ...");
IO.println(singerLength);
IO.println(sculptorLength);
IO.println(selfLength);
// Computer and print first names +
// Point C
String singerFirst = singer.substring(11);
String sculptorFirst = sculptor.substring(10);
String selfFirst = self.substring(7);
IO.println(); IO.println("First names ...");
IO.println(singerFirst);
IO.println(sculptorFirst);
IO.println(selfFirst);
// Computer and print last names +
// Point D
String singerLast = singer.substring(0,8);
String sculptorLast = sculptor.substring(0,7);
String selfLast = self.substring(0,4);
IO.println(); IO.println("Last names ...");
IO.println(singerLast);
IO.println(sculptorLast);
IO.println(selfLast);
// Computer and comma position within names +
// Point E
int singerPos = singer.indexOf(",");
int sculptorPos = sculptor.indexOf(",");
int selfPos = self.indexOf(",");
IO.println(); IO.println("Comma positions ...");
IO.println(singerPos+1);
IO.println(sculptorPos+1);
IO.println(selfPos+1);
// Computer and print first names +
// Point F1
singerFirst = firstName(singer);
sculptorFirst = firstName(sculptor);
selfFirst = firstName(self);
IO.println(); IO.println("First names ...");
IO.println(singerFirst);
IO.println(sculptorFirst);
IO.println(selfFirst);
// Computer and print last names +
// Point G1
singerLast = lastName(singer);
sculptorLast = lastName(sculptor);
selfLast = lastName(self);
IO.println(); IO.println("Last names ...");
IO.println(singerLast);
IO.println(sculptorLast);
IO.println(selfLast);
// Computer and print full names +
// Point H1
String singerFull = fullName(singer);
String sculptorFull = fullName(sculptor);
String selfFull = fullName(self);
IO.println(); IO.println("Full names ...");
IO.println(singerFull);
IO.println(sculptorFull);
IO.println(selfFull);
}
// Point F2
// operator-style method to compute first name
// <?first name opertor method definition>
static private String firstName(String theName)
{
// obtain where the comma is
int whereCommaIs = theName.indexOf(",");
// grab the first name by adding 2 to index because of+
, and
// the space
String firstName = theName.substring(whereCommaIs + 2)+
;
// send the first name back to the caller
return firstName;
}
// Point G2
// operator-style to compute last name
// <?last name opertor method definition>
static private String lastName(String theName)
{
// obtain where the comma is
// this separates the first & last names
int whereCommaIs = theName.indexOf(",");
// grab the last name by grabbing from 0 to
// the location of where the comma is
String lastName = theName.substring(0,whereCommaIs);
// send the last name back to the caller
return lastName;
}
// Point H2
// operator-style to compute full name
// <?full name opertor method definition>
static private String fullName(String theName)
{
String theFullName = firstName(theName) + " " + lastNa+
me(theName);
return theFullName;
}
}
// Demo
// --------------------------------------------
/*
<?DEMO>
*/
|
|
|