



|
|
CS1 Course Site
|
Programming Challenge Archive
String Thing
StringThingApp
|
|
|
JavaApplication --
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 = "Becker, Kara";
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(10);
String sculptorFirst = sculptor.substring(9);
String selfFirst = self.substring(8);
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,6);
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);
IO.println(sculptorPos);
IO.println(selfPos);
// 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
static private String firstName(String name)
{
int firstNamePos = name.indexOf(",");
String nameOne = name.substring(firstNamePos + 2);
return nameOne;
}
// Point G2
// operator-style to compute last name
static private String lastName(String name)
{
int lastNamePos = name.indexOf(",");
String nameTwo = name.substring(0,lastNamePos);
return nameTwo;
}
// Point H2
// operator-style to compute full name
static private String fullName(String name)
{
int fullNamePos = name.indexOf(",");
String nameThree = (firstName(name) + " " + lastName(n+
ame));
return nameThree;
}
}
// Demo
// --------------------------------------------
/*
$ javac StringThingApp.java
$ java StringThingApp
Holliday, Billie
Claudel, Camille
Becker, Kara
Lengths ...
16
16
12
First names ...
Billie
Camille
Kara
Last names ...
Holliday
Claudel
Becker
Comma positions ...
8
7
6
First names ...
Billie
Camille
Kara
Last names ...
Holliday
Claudel
Becker
Full names ...
Billie Holliday
Camille Claudel
Kara Becker
$
*/
|
|
|