cse 1341 honors principles of computer science i n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 1341 - Honors Principles of Computer Science I PowerPoint Presentation
Download Presentation
CSE 1341 - Honors Principles of Computer Science I

Loading in 2 Seconds...

play fullscreen
1 / 16

CSE 1341 - Honors Principles of Computer Science I - PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on

CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 15. Note Set 19 Overview. Intro to ArrayLists File and Streams. ArrayList. ArrayList <String> list = new ArrayList <String>(); list.add (“Southern”); list.add (“Methodist”);

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 'CSE 1341 - Honors Principles of Computer Science I' - saburo


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
cse 1341 honors principles of computer science i

CSE 1341 - HonorsPrinciples of Computer Science I

Mark Fontenot

mfonten@engr.smu.edu

Note Set 15

note set 19 overview
Note Set 19 Overview
  • Intro to ArrayLists
  • File and Streams
arraylist
ArrayList

ArrayList <String> list = new ArrayList<String>();

list.add(“Southern”);

list.add(“Methodist”);

list.add(“University”);

  • One of Java’s Container Classes
    • Containers hold other things
  • Can hold unlimited number of object references of any type (all need to be the same type – but remember about inheritance)
  • No need to worry about running out of places (unless you run out of memory)
arraylist useful methods
Arraylist Useful Methods

ArrayList<String> v = new ArrayList<String>();

v.add(“one”);

v.add(“two”);

v.add(“three”);

for(inti = 0; i < v.size(); i++)

System.out.println(v.get());

size() – returns the size of the arraylist

get (inti) – returns the reference stored at the specified position i

remove( inti)– removes the object references stored at position i and moves the ones after forward

arraylists
Arraylists

ArrayList<Integer> l = new ArrayList<Integer>();

l.add(1);

l.add(2);

l.add(3);

for (Integer x: l){

System.out.println(x * 2);

}

-adding ints, not Integers

-They are automatically converted to Integer

ojbects

Automatically converted from

Integers to ints

  • Can only hold object references
    • Can’t doArrayList<int> v = new ArrayList<int> ();
currently
Currently
  • Data in an executing program
    • Stored in RAM while program is in execution
    • When program ends, data is no longer accessible
    • RAM is volatile storage – must have powered to maintain state (retain data)
persistent storage
Persistent Storage
  • Non-volatile memory – doesn’t need power to maintain state
  • Data can survive a power cycle
  • Examples
    • hard-drive
    • CDs
    • USB drive (flash memory)
file processing
File processing
  • Part of nearly all languages
  • Stream – ordered data read or written
    • abstraction in Java
  • Two types of file processing in Java for us
    • Text-based file i/o
    • Binary File I/O - Object serialization (storing objects directly to file)
      • Object serialization/deserialzation
data hierarchy
Data Hierarchy
  • Lowest level – 1s & 0s - The bit
    • “easy” to create computer hardware that only had to maintain two states
    • 2 bits – 4 combinations
      • 00, 01, 10, 11
    • 3 bits – 8 combination
      • 000, 001, 010, 011, 100, 101, 110, 111
    • 4 bits – 16 combinations
    • n bits – 2n combinations
  • 8 bits –> 1 byte
  • Java uses 2 bytes to represent a character (16 bytes) – Unicode character set
    • Unicode contains characters from many of the worlds languages
data hierarchy1
Data Hierarchy
  • Can group characters to make fields
    • i.e. string of characters to make name
  • Group of fields makes a record
    • record is implemented as a class in java
    • think about just the data stored in the data members of one instance of an object
    • Name, Id, Address, etc…
  • Group of records can be stored in a file
    • Employee file
    • Customer file
    • Inventory file
java and files
Java and Files
  • Each file – sequential stream of bytes
    • How that stream/sequence is interpreted is up to the programmer who reads/writes the file
  • How do we know when we’re done reading?
    • OS provides some mechanism to know that the complete contents of the file have been read (sentinel value)
    • programmer knows exactly how many “things” to read from the file
  • 2 Types of File I/O
    • Character File I/O
      • Writing text to files
    • Binary File I/O
      • Writing byte data or complete objects
sample class
Sample class

public class Account {

private String acctNum;

private String fName;

private double balance;

public Account (String num, String name, double bal) {

acctNum = num;

fName = name;

balance = bal;

}

//Assume Accessors and Mutators for each data member

}

sample output
Sample Output

import java.util.*;

import java.io.*;

public class FileOutput {

public static void main (String [] args) {

//Create an arraylist to store customers

ArrayList<Account> customers = new ArrayList<Account> ();

//create some sample customers and add to list

Account a = new Account ("123", "Sam", 123.34);

customers.add(a);

a = new Account("234", "Sue", 223.33);

customers.add(a);

a = new Account("144", "Mark", 322.22);

customers.add(a);

sample output1
Sample Output

//open a file

Formatter output = null;

//must open the file in a try catch block

try {

output = new Formatter("data.txt");

} catch (IOException e) {

System.out.println("There is an error - exiting");

System.exit(1);

}

//write everything from list to output

for (Account x: customers)

output.format("%s %s %.2f\n",

x.getAcctNum(),

x.getFName(),

x.getBalance());

output.close();

}

}

This is text-based or character-based

file I/O.

sample read
Sample Read

import java.util.*;

import java.io.*;

public class FileInput {

public static void main (String [] args) {

//List to put the objects we're going to read from the file

ArrayList <Account> list = new ArrayList<Account> ();

//We'll use a scanner object to read from the file.

//Are there other ways? Of course...

Scanner s = null;

try {

s = new Scanner (new File ("data.txt"));

} catch (IOException e) {

System.out.println("Error opening file");

System.exit(1);

}

sample read1
Sample Read

//Temp variables to store values read from file

String aNum, fName;

double bal;

while (s.hasNext()) {

aNum = s.next();

fName = s.next();

bal = s.nextDouble();

Account a = new Account(aNum, fName, bal);

list.add(a);

}

//print the customers out

for(Account x: list)

System.out.println(x);

}

}

could be replaced with:

Account a = new Account (s.next(), s.next(), s.nextDouble());