CSE/IT 213 - Strings: New Mexico Tech
CSE/IT 213 - Strings: New Mexico Tech
Strings
A step up from character arrays
Tasks:
Get the users first, middle, and
last names
Extract the initials and create
the monogram
Output the monogram
8
Development Steps
We will develop this program in
two steps:
1. Start with the program template and add code to get input
2.
Step 1 Design
The program specification states
get the users name but doesnt
say how.
We will consider how in the
Step 1 code
10
12
13
Step 1 Code
import java.util.*; //Scanner reads data from std in
class Monogram {
public static void main (String[ ] args) {
String name;
Scanner in = new Scanner(System.in);
System.out.print("Enter Your Name: ");
name = in.nextLine(); //reads a line in delimiter = \n
System.out.println(name);
}
}
14
Step 1 Test
In the testing phase we run the
program and verify that we can
enter the name and the name
we enter is displayed correctly.
15
Examples
James Marshall Hendrix (okay)
Finn, Huckleberry (not okay)
John Kennedy (okay or not okay)
Is John Kennedy wrong, just because he doesnt use his middle
name (JFK)?
16
Step 2 Design
Our programming skills are limited, so we will make the following assumptions:
1. input string contains first,
middle, and last names
2. first, middle, and last names
are separated by single blank spaces
17
20
21
2. Which is correct?
For encapsulation,
A. private methods are usually declared.
B. public methods are usually declared.
C. private instance fields are usually declared.
D. public instance methods are usually declared.
23
3. True or False
Each class in java must contain
a main method.
24
Homework
Read Wu pages 52 - 76
Problems Chapter 2 Wu #20,
26, 29 (galapagos class will be
posted on course website), 32
25
Classpath
The class path tells the Java SDK
tools and applications where to
find third-party and user-defined
classes.
26
28
JAR Files
Java Archive Format files (jar)
are files that bundle and compress multiple files into one file.
A lot like tar.
30
Java packages
31
Example downloaded Galapagos package and placed in subdirectory of current directory where
source code file Triangle.java is
located.
javac -cp .:Galapagos/galapagos.jar Triangle.java
37
38
39
40