Comp 14 introduction to programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

COMP 14 Introduction to Programming PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on
  • Presentation posted in: General

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?. Review. Assignment Operators

Download Presentation

COMP 14 Introduction to Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Comp 14 introduction to programming

COMP 14Introduction to Programming

Mr. Joshua Stough

February 2, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218


Announcements

Announcements

  • Assignment simpler than I thought (darn).

  • Office hours (I’m lonely).

  • Precedence of % operator.

  • Why reference variables?


Review

Review

Assignment Operators

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

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

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

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


Review1

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


Today in comp 14

Today in COMP 14

  • Input/Output using GUI

  • String Tokenization

  • Format Floating-Point Output

  • Read from and Write to Files


Using dialog boxes for i o

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);


Joptionpane methods

JOptionPane Methods

  • showInputDialog

    str = JOptionPane.showInputDialog(strExpression);

    • stores what the user enters into the String str

  • showMessageDialog

    JOptionPane.showMessageDialog(parentComponent, strExpression,

    boxTitleString,

    messageType);


Showmessagedialog

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


Messagetype

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


Joptionpane example

JOptionPane Example

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

"Greetings",

JOptionPane.INFORMATION_MESSAGE);


Usinggui java example

UsingGUI.java example


User input

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


The stringtokenizer class

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


The stringtokenizer class tokens and delimiters

The StringTokenizer ClassTokens and Delimiters

"Four score and seven years ago"

delimiter: ' '

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

"Bart:Lisa:Homer:Marge"

delimiter: ':'

tokens: "Bart" "Lisa" "Homer" "Marge"


The stringtokenizer class1

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()


Tokenize java example

Tokenize.java example

  • separated by spaces

  • separated by commas

    • gotcha


Formatting the output of decimal numbers

Formatting the Output of Decimal Numbers

  • float: defaults to 6 decimal places

  • double: defaults to 15 decimal places


Class decimalformat

class DecimalFormat

  • Import package java.text

  • Create DecimalFormat object and initialize

    DecimalFormat 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


Examples

Examples

DecimalFormat 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

56.38

0.35

0.35

0.30

0.3


Reading from text files

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


Exceptions

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


Reading from text files1

Reading From Text Files

String file = "data.dat";

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

String line = inFile.readLine();

inFile.close();


Writing to text files

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


Writing to text files1

Writing To Text Files

String file = "outfile.dat";

PrintWriter outFile = new PrintWriter

(new FileWriter (file));

outFile.print ("Hi");

outFile.println(" There!");

outFile.close();


Filegui java example

FileGUI.java Example


Summary

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


Summary1

Summary

  • Reading Data from File

    • BufferedReader

    • FileReader

      • readLine()

  • Writing Data to a File

    • FileWriter

    • PrintWriter

      • print(StrExpression)

      • println(StrExpression)

  • Close files after using them


Next time in comp 14

Next Time in COMP 14

Reading Assignment

Chapter 4 (pgs. 147-164)

  • Relational and Logical Operators and Expressions


  • Login