28
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00- 12:15 Peabody Hall 218

COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

COMP 14Introduction to Programming

Mr. Joshua StoughFebruary 2, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218

Page 2: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Announcements

• Assignment simpler than I thought (darn).

• Office hours (I’m lonely).• Precedence of % operator.• Why reference variables?

Page 3: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Review

Assignment Operators

count += 5; // count = count + 5;

count -= 5; // count = count - 5;

count *= 5; // count = count * 5;

count /= 5; // count = count / 5;

Page 4: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Review

• Primitive Variables– primitive data types (int, double, ...)– stores the data in the memory

location

• Reference Variables– stores an address in the memory

location– "points to" another memory location

Page 5: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Today in COMP 14

• Input/Output using GUI

• String Tokenization

• Format Floating-Point Output

• Read from and Write to Files

Page 6: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Using Dialog Boxes for I/O• Use a graphical user interface (GUI)

• class JOptionPane– Contained in package javax.swing– showInputDialog

• allows user to input a string from the keyboard

– showMessageDialog• allows the programmer to display results

• Program must end with System.exit(0);

Page 7: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

JOptionPane Methods

• showInputDialogstr = JOptionPane.showInputDialog(strExpression);

– stores what the user enters into the String str

• showMessageDialogJOptionPane.showMessageDialog(parentComponent,

strExpression, boxTitleString,

messageType);

Page 8: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

showMessageDialog

• parentComponent– parent of the dialog box– we'll use null

• StrExpression– what you want displayed in the box

• boxTitleString– title of the dialog box

• messageType– what icon will be displayed

Page 9: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

messageType

• JOptionPane.ERROR_MESSAGE– error icon

• JOptionPane.INFORMATION_MESSAGE– information icon

• JOptionPane.PLAIN_MESSAGE– no icon

• JOptionPane.QUESTION_MESSAGE– question mark icon

• JOptionPane.WARNING_MESSAGE– exclamation point icon

Page 10: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

JOptionPane Example

JOptionPane.showMessageDialog (null, "Hello World!", "Greetings",

JOptionPane.INFORMATION_MESSAGE);

Page 11: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

UsingGUI.java example

Page 12: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

User Input

• BufferedReader– reads everything as a string

• Integer.parseInt– only handles one integer in the string

• How to handle?

Enter 3 numbers: 34 15 75

Page 13: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The StringTokenizer Class• tokens

– elements that comprise a string

• tokenizing– process of extracting these elements

• delimiters– characters that separate one token from

another• StringTokenizer class

– defined in the java.util package– used to separate a string into tokens

Page 14: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The StringTokenizer ClassTokens and Delimiters"Four score and seven years ago"

"Bart:Lisa:Homer:Marge"

delimiter: ' 'tokens: "Four" "score" "and" "seven" "years" "ago"

delimiter: ':'tokens: "Bart" "Lisa" "Homer" "Marge"

Page 15: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The StringTokenizer Class• Default delimiters:

– space, tab, carriage return, new line

• Methods– StringTokenizer (String str)– StringTokenizer (String str, String delimits)

– String nextToken()– boolean hasMoreTokens()– int countTokens()

Page 16: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Tokenize.java example

• separated by spaces

• separated by commas– gotcha

Page 17: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Formatting the Output of Decimal Numbers• float: defaults to 6 decimal places• double: defaults to 15 decimal

places

Page 18: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

class DecimalFormat

• Import package java.text• Create DecimalFormat object and initializeDecimalFormat fmt = new DecimalFormat (formatString);

• FormatString– "0.00" - limit to 2 decimal places, use 0 if there's

no item in that position– "0.##" - limit to 2 decimal places, no trailing 0

• Use method format– rounds the number instead of truncating

• Result of using DecimalFormat is a String

Page 19: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

ExamplesDecimalFormat twoDecimal =

new DecimalFormat("0.00");DecimalFormat fmt =

new DecimalFormat("0.##");

System.out.println (twoDecimal.format(56.379));System.out.println (fmt.format(56.379));

System.out.println (twoDecimal.format(.3451));System.out.println (fmt.format(.3451));

System.out.println (twoDecimal.format(.3));System.out.println (fmt.format(.3));

56.38

0.35

0.30

56.38

0.35

0.3

Page 20: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Reading From Text Files• Similar to reading from the keyboard• Create a BufferedReader object, but

use a FileReader object instead of InputStreamReader

• Create BufferedReader object inside the main method instead of outside

• Substitute the name of the file for System.in

• When finished reading from the file, we need to close the file:– BufferedReader close() method

Page 21: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Exceptions

• FileNotFoundException– if the file specified to open was not

found

• IOException– some other I/O exception

public static void main (String[] args) throws FileNotFoundException,IOException

Page 22: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Reading From Text FilesString file = "data.dat";

BufferedReader inFile = new BufferedReader (new FileReader (file));

String line = inFile.readLine();

inFile.close();

Page 23: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Writing To Text Files• Similar to reading from text files

• Use FileWriter and PrintWriter instead of FileReader and BufferedReader

• PrintWriter– methods include print() and println(), which we

use just like those in System.out

• Like reading, we need to close the file when we're done– PrintWriter close() method

Page 24: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Writing To Text Files

String file = "outfile.dat";

PrintWriter outFile = new PrintWriter(new FileWriter (file));

outFile.print ("Hi");outFile.println(" There!");outFile.close();

Page 25: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

FileGUI.java Example

Page 26: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Summary

• JOptionPane– showInputDialog– showMessageDialog

• StringTokenizer– tokens are separated by a delimiter

• DecimalFormat– pattern tells how many digits after the

decimal point– 0 - fill in with trailing 0s– # - don't fill in trailing 0s

Page 27: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Summary

• Reading Data from File– BufferedReader– FileReader

• readLine()

• Writing Data to a File– FileWriter– PrintWriter

• print(StrExpression)• println(StrExpression)

• Close files after using them

Page 28: COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Next Time in COMP 14

Reading AssignmentChapter 4 (pgs. 147-164)

• Relational and Logical Operators and Expressions