CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.

Slides:



Advertisements
Similar presentations
Introduction to Objects and Input/Output
Advertisements

Chapter 2 - Introduction to Java Applications
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
Fundamental Programming Structures in Java: Strings.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Chapter 2 - Introduction to Java Applications
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 3: Introduction to Objects and Input/Output.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
I/O in Java Dennis Burford
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Lecture 2 Objectives Learn about objects and reference variables.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
CiS 260: App Dev I. 2 Introduction to Methods n A method (or ________) is a segment of code that performs a specific task. n Advantages of modular program.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Chapter 3: Introduction to Objects and Input/Output
File Input / Output.
Java Programming Lecture 2
Outline Creating Objects The String Class Packages Formatting Output
2.5 Another Java Application: Adding Integers
Dialogues and Wrapper Classes
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
Streams and File I/O Chapter 14.
Chapter 3: Introduction to Objects and Input/Output
Reading and Writing Text Files
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Getting Started with Java
F II 2. Simple Java Programs Objectives
Presentation transcript:

CIS 260: App Dev I

2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer —for defining and manipulating integers  Double —for defining and manipulating _________. n Primitive integers and Integer objects  int aNumber = 78; aNumber is a variable of the __________ data type int aNumber is a variable (a memory location, say, 2500) that actually holds the value 78  Integer aNumber = new Integer(78); aNumber is a __________ variable (a memory location) that stores the memory location where the value 78 is stored aNumber is called an _________ of the Integer class or an Integer _________

3 The Operator new Integer aNumber = new Integer(78); The operator _____ does the following:  Assigns the memory location for the integer 78  Stores 78 in that memory location  Stores the address of that memory location in the reference variable (object) called aNumber aNumber is an _______ of the Integer class Integer aNumber = new Integer(50); creates a new object called aNumber, but aNumber points to a different address Java’s automatic _______ collector will remove the 78 eventually, or you can use System.gc();

4 Predefined Classes and Methods n A ________ is a set of instructions designed to accomplish a specific task. The method main() runs automatically. n Java has many _________ that contain classes that contain methods (all are predefined). How to call the pow() method in the class Math :  result = Math.pow(2,3);  // result is 8 = 2 to the 3 rd power  // the 2 and 3 are called arguments n ____ notation is used to access class members.

5 The class String n In Java, a string is not a variable, but an _____. n The following two statements are equivalent:  String name = “Lisa Johnson”;  name = new String(“Lisa Johnson”); name is actually a reference variable that contains the address of the String ________. n A string contains ___ or more characters enclosed in double quotes. n The ________ of the first character in a string is 0, of the second character is 1, and so on.

6 Methods in the class String The method substring() is a member of the class String. Using String name = “Lisa Johnson”; :  name.substring(0,4) yields _______  name.substring(5,12) yields “Johnson”  name.indexOf(‘J’) yields ___  name.charAt(4) yields ‘ ’  name.equals(“Lisa Johnson”) yields true  name.equals(“Luke Johnson”) yields ______  name.length() yields ____  name.replace('i','e') yields “Lesa Johnson” The String object name has access to the String methods using the ____ operator.

7 Input/Output n You can input data into a Java program using a simple GUI (____________ user interface). The class JOptionPane has the method showInputDialog() and is used as follows: name = JOptionPane.showInputDialog(“Enter your name”); A message can be displayed to the user using the showMessageDialog() method: JOptionPane.showMessageDialog(null,“Hello World!”,”Greetings”,JOptionPane.PLAIN_MESSAGE);

8 More Input/Output The class JOptionPane is in the package javax.swing, which must be imported. A program that uses a GUI must end with the statement System.exit(0) to terminate the GUI “________”. n It is better to create an output string within the program, then use that string in the GUI. JOptionPane.showMessageDialog(null, outputString, guiTitle, JOptionPane.INFORMATION_MESSAGE);

9 Tokenizing a String A string can be broken up into parts called ________ using the class StringTokenizer, which is in the package java.util. n Example: StringTokenizer tokenizer = new StringTokenizer(“Richard Johnson”); String firstName = tokenizer.nextToken(); String lastName = tokenizer.nextToken(); The above example will store “Richard” in firstName and “Johnson” in lastName.

10 Formatting Output The class DecimalFormat can format numbers to a specific number of decimal places. The following statement creates the DecimalFormat object: DecimalFormat twoDecimal = new DecimalFormat("0.00"); The following statement uses the DecimalFormat object: formattedNumber = twoDecimal.format(number); If number contains then formattedNumber contains ________ (rounding is performed)

11 File Input/Output n It is often more efficient to get input from and send output to a file on a disk (instead of using the keyboard or ________). n A file is an area in __________ storage that holds information. To input data from a file use the class FileReader. To output data to a file use the classes FileWriter and PrintWriter (in the package _________).

12 More on File Input This statement creates a FileReader object associated with a specific file on a disk, then reads an entire line of data from the file, storing the line of data in a BufferedReader object. BufferedReader inFile = new BufferedReader(new FileReader(“a:\\prog.dat”)); If the line contains different data segments, use StringTokenizer to break it up and store data in __________.

13 Writing to a File n First, create a __________ object and associate it with the destination file. Then, create a ___________ object using the FileWriter object. n This can be done in a single statement: PrintWriter outFile = new PrintWriter(new FileWriter(“a:\\prog.out”); n You write data to the file with a statement like outFile.println(“The paycheck is: $” + pay); You _______ the file with outFile.close(); n If the output file doesn’t exist, Java will create it. n If it does exist, Java will overwrite it.

/* * Chapter3Examples.java * Created by Richard Johnson * 9/15/04 * Demonstrates topics in Chapter 3 of Malik and Nair */ import javax.swing.*; // for GUIs import java.util.*; // for tokenizing import java.text.*; // for formatting import java.io.*; // for input/output public class Chapter3Examples { public static void main (String[] args) throws IOException, FileNotFoundException { System.out.println("Welcome to the Chapter 3 Examples\n\n"); // demonstrate simple GUIs String name = JOptionPane.showInputDialog("Enter your first and last name only:"); String message = "Thank you " + name; JOptionPane.showMessageDialog(null,message,"Greetings",JOptionPane.PLAIN_MESSAGE); // demonstrate tokenizing StringTokenizer tokenizer = new StringTokenizer(name); String firstName = tokenizer.nextToken(); // get first token in string String lastName = tokenizer.nextToken(); // get next token in string System.out.println("Your name is: " + firstName + " " + lastName); // demonstrate formatting double firstNumber = ; DecimalFormat twoDecimal = new DecimalFormat("0.00"); System.out.println(twoDecimal.format( " $ " + firstNumber));

// demonstrate file output PrintWriter outFile = new PrintWriter(new FileWriter("prog.out")); outFile.println("My name is: " + lastName + ", " + firstName); outFile.close(); // close the file // demonstrate file input BufferedReader inFile = new BufferedReader(new FileReader("prog.out")); StringTokenizer aTokenizer = new StringTokenizer(inFile.readLine()); String junk1 = aTokenizer.nextToken(); // gets the text 'My' String junk2 = aTokenizer.nextToken(); // gets the text 'name' String junk3 = aTokenizer.nextToken(); // gets the text 'is:' String nameLastWithComma = aTokenizer.nextToken(); // gets name w/ comma String nameLast = nameLastWithComma.substring(0, nameLastWithComma.indexOf(",")); // gets name without comma String nameFirst = aTokenizer.nextToken(); System.out.println("My name is: " + nameFirst + " " + nameLast); System.out.println("\nEnd of program\n"); System.exit(0); // terminate GUI thread }