CS1101: Programming Methodology Recitation 2 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS1101: Programming Methodology Recitation 2

Description:

Convert user-specified date from American to standard format. import java.io. ... Converts a date from American format (e.g. August 12, 2004) to international ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 18
Provided by: leemo4
Category:

less

Transcript and Presenter's Notes

Title: CS1101: Programming Methodology Recitation 2


1
CS1101 Programming MethodologyRecitation 2
Java Basics
2
Problem Solving Process
Determine problem features e.g. input, output
Analysis
Describe objects and methods
Design
Produce the classes and code
Implementation
Testing
Examine for correctness
3
Case Study 1
  • Objective Develop a program AverageFive.java
    that computes the average of 5 integer inputs.
  • Analysis and Design
  • Constants
  • LIST_SIZE int //number of input values(5)
  • Variables
  • currentInput int //variable to hold current
    input value
  • runningTotal int //variable to hold sum of input
    value
  • //initialize to 0
  • average int //variable to hold ave of input
    values
  • Objects
  • stdin references a BufferedReader object
    associated with the standard input stream
    System.in

4
Analysis and Design 4. Methods method main()
step 1. define necc data structures.
step 2. process inputs to determine input
total. step 3. compute average.
step 4. display average.
5
// Purpose Average five user integer
inputs import java.io. public class
AverageFive // main() application entry
point public static void main(String args)
throws IOException // define necessary data
structures (Step 1) final int LIST_SIZE 5
// number of inputs to process int
currentInput // current input
value int runningTotal 0 // sum of
inputs processed so far BufferedReader stdin
// input stream new BufferedReader(new
InputStreamReader(System.in))
6
// process inputs to determine input total
(Step 2) System.out.print("Enter an integer
number ") currentInput Integer.parseInt(std
in.readLine()) runningTotal
currentInput System.out.print("Enter an
integer number ") currentInput
Integer.parseInt(stdin.readLine()) runningTota
l currentInput System.out.print("Enter
an integer number ") currentInput
Integer.parseInt(stdin.readLine()) runningTota
l currentInput System.out.print("Enter
an integer number ") currentInput
Integer.parseInt(stdin.readLine()) runningTota
l currentInput System.out.print("Enter
an integer number ") currentInput
Integer.parseInt(stdin.readLine()) runningTota
l currentInput
7
// compute average (Step 3) int average
runningTotal / LIST_SIZE // display
average (Step 4) System.out.println("The input
average is " average)
8
Use of Iterative Constructs // Compute average
of a list of positive numbers import
java.io. public class NumberAverage //
main() application entry point public static
void main(String args) throws IOException
// set up the list processing BufferedRea
der stdin new BufferedReader( new
InputStreamReader( System.in ) ) int
valuesProcessed 0 // no values processed so
far double valueSum 0 // running
total
9
Use of Iterative Constructs // prompt user
for values System.out.println("Enter positive
numbers one per line. " "Indicate the end
of the list with a negative number.") //
get first value double value
Double.parseDouble(stdin.readLine()) //
process values one-by-one while (value gt 0)
// add value to running total valueSum
value // processed another
value valuesProcessed // prepare for
next iteration --- get next value value
Double.parseDouble(stdin.readLine())
10
Use of Iterative Constructs // display
result if (valuesProcessed gt 0) //
compute and display average double average
valueSum / valuesProcessed System.out.println
("Average " average) else
System.out.println("No list to
average")
11
Case Study 2
Objective Develop an interactive program that
converts a data in American format (August 12,
2004) to the international format
(2004-August-12) Analysis and Design
Objects stdin BufferedReader //standard
input stream buffer String //represent input
date in American formatmonth String //represent
the month day String //represent the day of
the month year String //represent the
year standardDate String //represent the date
in standard format
12
Analysis and Design Algorithm step 1.
display purpose of program. step 2. issue
prompt for user to enter date in American
format step 3. get a String representation of
the date from input and assign to variable
buffer. step 4. echo buffers date
representation step 5. examine buffers date
representation and extract a String
representation of the input month. step 6.
examine buffers date representation and extract
a String representation of the input
day. step 7. examine buffers date
representation and extract a String
representation of the input year. step 8.
assign to variable standardDate the String
concatenation of year, month, and day
with separating hyphens step 9. display
standardDate
13
// Convert user-specified date from American to
standard format import java.io. class
DateTranslation // main() application
entry point static public void main(String
args) throws IOException // produce a
legend (Step 1) System.out.println("Date
format translator") System.out.println("
Converts a date from an American" " format
(e.g., July 4, 1776)") System.out.println("
to standard format (e.g.," "
1776-July-4).") System.out.println() //
prompt the user for a date in American format
(Step 2) System.out.print("Enter a date in
American format ")
14
// acquire the input entered by the user
(Step 3) BufferedReader stdin new
BufferedReader( new InputStreamReader(System.i
n) ) String buffer stdin.readLine().trim()
// echo the input back (Step 4)
System.out.println("The translation
of") System.out.println(" "
buffer) // get month entered by the user
(Step 5) int m buffer.indexOf("
") String month buffer.substring(0,
m) buffer buffer.substring(m1).trim()
// get day entered by the user (Step 6) int
n buffer.indexOf(",") String day
buffer.substring(0, n) buffer
buffer.substring(n1).trim() // get year
entered by the user (Step 7) String year
buffer
15
// create standard format version of
input (Step 8) String standardDate year
"-" month "-" day // display the
translation (Step 9) System.out.println("to
standard format is") System.out.println(" "
standardDate)
16

buffer buffer.trim() //trim any leading and
trailing white space
Int m buffer.indexOf ( ) //find the
first blank String month buffer.substring(0,m)
//copy the string from position 0 to m-1
in buffer to month
buffer buffer.substring(m1).trim()
month
Int n buffer.indexOf (, ) //find the
comma String day buffer.substring(0,m)
//copy the string from position 0 to n-1
in buffer to day
buffer buffer.substring(n1).trim()
day
String year buffer //get year
year
17
Case Study 2
Sample Output Date format translator Converts
a date from American format (e.g. August 12,
2004) to international format (e.g.
2004-August-12) Enter date in American format
September 15, 2000 The translation of
September 15, 2000 to standard format
is 2000-September-15
Write a Comment
User Comments (0)
About PowerShow.com