



|
|
Byron's CSC212 Web Site
|
Programming Challenge Archive
Program Potpouri ( Lab Programming )
String Thing App
|
|
|
This program from Lab # 9 provides an opportunity to write conditional constructs as a ``Program Maintenance'' task.
JavaApplication --
String Thing
// 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 strings which represent names
// Point A
String singer = "Holliday, Billie";
String sculptor = "Claudel, Camille";
String self = "Bahr, Byron";
IO.println(singer);
IO.println(sculptor);
IO.println(self);
// Compute 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);
// Compute and print first names
// Point C
String singerFirst = singer.substring(10);
String sculptorFirst = sculptor.substring(9);
String selfFirst = self.substring(6);
IO.println(); IO.println("First names ...");
IO.println(singerFirst);
IO.println(sculptorFirst);
IO.println(selfFirst);
// Compute 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);
// Compute 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);
// Compute 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);
// Compute 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);
// Compute 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 dsName)
{
// Compute the position of the comma
int whereIsComma = dsName.indexOf(",");
// Compute the location of the First Name
String firstName = dsName.substring(whereIsComma+2);+
// Return the First Name
return firstName;
}
// Point G2
// operator-style to compute last name
// <?last name opertor method definition>
static private String lastName(String dsName)
{
// Compute the position of the comma
int whereIsComma = dsName.indexOf(",");
// Compute the location of the Last Name
String lastName = dsName.substring(0,whereIsComma);
// Return the Last Name
return lastName;
}
// Point H2
// operator-style to compute full name
// <?full name opertor method definition>
static private String fullName(String dsName)
{
// Compute the position of the comma
int whereIsComma = dsName.indexOf(",");
// Compute the location of the First Name
String firstName = dsName.substring(whereIsComma+2)+
;
// Compute the location of the Last Name
String lastName = dsName.substring(0,whereIsComma);+
// Combine firstName and lastName
String fullName = firstName + " " + lastName;
// Return the Full Name
return fullName;
}
}
// Demo
// --------------------------------------------
/*
Holliday, Billie
Claudel, Camille
Bahr, Byron
Lengths ...
16
16
11
First names ...
Billie
Camille
Byron
Last names ...
Holliday
Claudel
Bahr
Comma positions ...
8
7
4
First names ...
Billie
Camille
Byron
Last names ...
Holliday
Claudel
Bahr
Full names ...
Billie Holliday
Camille Claudel
Byron Bahr
*/
|
|
|