Transcript Week2
CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101/
Week 2: Java Basics
Last week:
Introduction
Writing algorithms (pseudo-codes)
This week:
Chapter 1: Intro to OOP and Software Development
Chapter 2: Getting Started with Java
DrJava
CourseMarker
Next week:
Chapter 3: Numerical Data
Chapter 4: Defining Your Own Classes – Part 1
© CS1101 (AY2009-2010 Semester 1)
Week2 - 2
Reminder
Have you read the relevant chapters and
PowerPoint files BEFORE coming to this
lecture?
© CS1101 (AY2009-2010 Semester 1)
Week2 - 3
Chapter 1 Intro to OOP and S/W Dev
Let’s go to Chapter 1.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 4
Chapter 2 Getting Started with Java
Let’s go to Chapter 2.
We will skip the Date and SimpleDateFormat
classes.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 5
Example on String methods (1/2)
/*
Program to test out some String methods in Chapter 2
File: Ch2TestString.java
*/
class Ch2TestString {
public static void main (String[] args) {
String text = "I'm studying CS1101.";
//or String text = new String("I'm studying CS1101.");
//We’ll explain the difference next time.
System.out.print("text: " + text);
int length = text.length();
System.out.println("text.length(): " + length);
© CS1101 (AY2009-2010 Semester 1)
Week2 - 6
Example on String methods (2/2)
System.out.println("text.substring(5,8): " +
text.substring(5,8));
System.out.println("text.indexOf(\"in\"): " +
text.indexOf("in"));
// why are there 2 backslashes (\) above?
String newText = text + "How about you?";
System.out.print("newText: " + newText);
}
}
Download this program from the CS1101 website
(“Resources – Lectures”).
What is the output of this program?
© CS1101 (AY2009-2010 Semester 1)
Week2 - 7
CourseMarker (CM)
You are going to use CourseMarker (CM) for
submitting your lab assignments.
Password already emailed to you (@nus.edu.sg)
Refer to the following:
CourseMarker website
http://www.comp.nus.edu.sg/~cmarker/
“Using CourseMarker” PowerPoint file
Available on CS1101 website
http://www.comp.nus.edu.sg/~cs1101/3_ca/labs.html
© CS1101 (AY2009-2010 Semester 1)
Week2 - 8
Example: Initials
This problem is given in the Chapter 2 PowerPoint
file, but the program uses JOptionPane class for
input. We prefer to use the standard input (Scanner
class) and show it here.
Problem statement:
Write a program that asks for the user’s first, middle,
and last names and replies with their initials.
Example:
input:
output:
© CS1101 (AY2009-2010 Semester 1)
Andrew Lloyd Weber
ALW
Week2 - 9
Initials: Overall Plan
Identify the major tasks the program has to
perform
We need to know what to develop before we
develop!
Tasks:
Get the user’s first, middle, and last names
Extract the first letter from each name and create
the initials
Output the initials
© CS1101 (AY2009-2010 Semester 1)
Week2 - 10
Initials: Development Steps
We will develop this program in two steps:
1. Start with the program template and add code to
get input
2. Add code to compute and display the initials
© CS1101 (AY2009-2010 Semester 1)
Week2 - 11
Initials: Step 1 Design
The program specification states “get the user’s
name” but doesn’t say how.
We will consider “how” in the Step 1 design.
We will use standard input (Scanner class) for input.
We have two choices for input style:
Choice 1: Input first, middle and last names separately
Choice 2: Input the full name at once
We choose choice 2 because it is easier and quicker
for the user to enter the information.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 12
Initials: Step 1 Code
/*
Chapter 2 Sample Program: Displays the Initials
File: Ch2Initials.java
*/
import java.util.*;
Download this program from the CS1101 website.
What is the output of this program?
class Ch2Initials {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your full name "
+ " (first, middle, last): ");
String name = scanner.nextLine();
System.out.println("Full name entered: " + name);
}
}
© CS1101 (AY2009-2010 Semester 1)
Week2 - 13
Initials: Step 1 Test
In the testing phase, we run the program and
verify that…
we can enter the name
the name we enter is displayed correctly
Question:
What if we had used scanner.next() instead of
scanner.nextLine()? Test it out!
© CS1101 (AY2009-2010 Semester 1)
Week2 - 14
Initials: Step 2 Design
Our programming skills are limited, so we will
make the following assumptions:
input string contains first, middle, and last names
first, middle, and last names are separated by
single blank spaces
Examples:
John Quincy Adams
(okay)
John Kennedy
(not okay)
Harrison,William Henry
(not okay)
© CS1101 (AY2009-2010 Semester 1)
Week2 - 15
Initials: Step 2 Design (cont’d)
Given a valid input, we can generate the
initials by
breaking the input name into first, middle and last
extracting the first character from each of them
concatenating the three first characters
“Aaron Ben Cosner”
“Aaron”
“Ben Cosner”
“Ben”
“Cosner”
“ABC”
© CS1101 (AY2009-2010 Semester 1)
Week2 - 16
Initials: Step 2 Code
/*
Chapter 2 Sample Program: Displays the Initials
File: Ch2Initials.java
*/
import java.util.*;
class Ch2Initials {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your full name "
+ " (first, middle, last): ");
String name = scanner.nextLine();
String space = " ";
© CS1101 (AY2009-2010 Semester 1)
Week2 - 17
Initials: Step 2 Code (cont’d)
String first = name.substring(0, name.indexOf(space));
name = name.substring(name.indexOf(space)+1,
name.length());
String mid = name.substring(0, name.indexOf(space));
String last = name.substring(name.indexOf(space)+1,
name.length());
// Compute the initials
String initials = first.substring(0,1) +
mid.substring(0,1) +
last.substring(0,1);
// Output the initials
System.out.println("Your initials: " + initials);
}
}
© CS1101 (AY2009-2010 Semester 1)
Week2 - 18
Initials: Step 2 Test
In the testing phase, we run the program and
verify that, for all valid input values, correct
initials are displayed.
We run the program numerous times. Seeing
one correct answer is not enough. We have
to try out many different types of (valid) input
values.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 19
Initials: Program Review
The work of a programmer is not done yet.
Once the working program is developed, we
perform a critical review and see if there are
any missing features or possible
improvements.
One possible improvement
Improve the initial prompt so the user knows the
valid input format requires single spaces between
the first, middle and last names.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 20
Summary for Today
You should have learned how to use the
following:
DrJava
CourseMarker
Basic Java features
Some standard (built-in) classes
String
Scanner
© CS1101 (AY2009-2010 Semester 1)
Week2 - 21
Announcements/Things-to-do (1/2)
CM password
Already emailed to you @ nus.edu.sg
Trial Lab
Released on 15 August 2009, Saturday.
Read Module website, “CA – Labs”.
No hard deadline, but try to submit by the end of this week to
ensure that you know how to use CM.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 22
Announcements/Things-to-do (2/2)
To prepare for next week’s lecture:
Read Chapters 3 and 4 and their PowerPoint files before you
come for lecture.
To prepare for next week’s discussion session:
Download “week3_discussion_qns.pdf” from module website,
“CA – Discussion”.
Do the questions before you attend your discussion session.
© CS1101 (AY2009-2010 Semester 1)
Week2 - 23
End of File
© CS1101 (AY2009-2010 Semester 1)
Week2 - 24