comp 14 introduction to programming
Download
Skip this Video
Download Presentation
COMP 14 Introduction to Programming

Loading in 2 Seconds...

play fullscreen
1 / 28

COMP 14 Introduction to Programming - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' COMP 14 Introduction to Programming' - trevet


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

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

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
ad