×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

by: Taylor Kahl

8

0

6

# Week 3 Notes - CSC 2720 CSC 2720

Taylor Kahl
GSU
GPA 4.21

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

Notes from class on Tuesday 9/6 and Thursday 9/8 and corresponding textbook material from chapter 1
COURSE
Data Structures
PROF.
Jaman Bhola
TYPE
Class Notes
PAGES
6
WORDS
CONCEPTS
switch, exceptions, files, iteration, Input, output, java, serialization, data structures
KARMA
25 ?

## Popular in Computer science

This 6 page Class Notes was uploaded by Taylor Kahl on Sunday September 11, 2016. The Class Notes belongs to CSC 2720 at Georgia State University taught by Jaman Bhola in Fall 2016. Since its upload, it has received 8 views. For similar materials see Data Structures in Computer science at Georgia State University.

×

## Reviews for Week 3 Notes - CSC 2720

×

×

### What is Karma?

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/11/16
Notes from Tuesday 9/6 and Thursday 9/8 Switch statements:  Used to choose between more than 2 options; choice is made according to value of a number  Example: code to print the number of days in a month: switch (month) { case 4: case 6: case 9: case 11: //April, June, Sept, Nov daysInMonth = 30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: //Jan, March, May, July, Aug, Oct, Dec daysInMonth = 31; break; case 2: //Feb daysInMonth = 28; break; default: System.out.println(“Incorrect value for month.”); o “case” refers to the number value of “month” o A case must be terminated with a “break” or “return” statement o If no case matches “month,” the default code executes  if there’s no default statement, the switch statement ends Iteration statements:  while(expression) { o executes if the expression is true; continues to execute if expression is still true  for(initialize, test, update) { o executes while the initialized counter variable meets the test requirements; the counter is updated each time the loop executes  do { } while(expression) o executes the “do” statement at least once; executes again while the expression is true The Object class:  Every class pre-written in Java inherits the Object class & its methods  Common methods from the Object class: o boolean equals(Object obj)  compares the references of 2 objects (not the value stored in the objects) o protected void finalize()  used by java to destroy objects a program is no longer using o int hashCode()  returns the hash code of an object as an integer  hash code – unique identifier associated with each object o String toString()  returns a String representation of an object The String classes:  String, StringBuffer, StringTokenizer o StringBuffer – makes Strings changeable (unlike regular String class) o StringTokenizer – Can separate a String into individual tokens  token – an individual word or set of characters, separated by a space, tab or new line Java Exceptions:  Exception – handles an error during execution  try-catch blocks: o place statement that might throw exception in the try block o try block must be followed by 1 or more catch blocks, each to handle a different type of exception o when statement in try block throws exception, rest of statements in try block are ignored – execution goes to catch block o catches allow program to continue; otherwise, errors will cause program to terminate Syntax: try { statements; } catch (exceptionClass identifier) { statement(s); } finally { statements; } o “exceptionClass” is the type of exception being caught; “identifier” is a variable name o finally block – may be placed after the last catch. The statements in this block will execute whether or not an exception was thrown  Exceptions are either checked or unchecked/runtime o checked – must be explicitly handled by the programmer  Example: FileNotFoundException must be written when trying to open a file, otherwise program will not compile o unchecked/runtime – less serious, may be handled by the program  Example: ArrayIndexOutOfBoundsException thrown during runtime if the program tries to access an array index which does not exist  Call an exception in your program with the throw keyword: o throw new exceptionClass(StringArgument);  “StringArgument” is a message that will appear when the exception is thrown – you can write it to specify what the error might be File input:  You can use the contents of a file by opening the file in java using the Scanner class (have to implement java.util.Scanner)  Scanner fileinput = new Scanner(inFile); o “inFile” must be a String  Scanner allows you to retrieve tokens from a file and convert them into data types: o next() – scans next token as a String o nextLine() – scans next line of input as a String o nextInt() – scans next token as an int o Example: storing the next int from a file in a new variable “num” :  int num = fileInput.nextInt();  Must test for a FileNotFoundException when reading a file with the Scanner class for the program to compile  To test for end of the file: o while(fileInput.hasNext())  this statement will evaluate to true if there are more tokens left to scan or false if all of the file has been scanned  Always need to close a file to prevent it from being corrupted o fileInput.close(); File output:  You can save information to an output file using the FileWriter and PrintWriter classes  PrintWriter output = new PrintWriter(new FileWriter(“results.txt”)); o this will overwrite the file “results.txt”  Add text to a file with print/println statements o Example: output.println(“Title of new file”);  If you want to append a file, instead of overwriting it: o PrintWriter output = new PrintWriter(new FileWriter(“results.txt”, true)); o this retains the old contents of “results.txt” and allows you to add to it  Close the output stream when finished using the file o output.close();  Copying a file: o open input and output files o read line by line from input, copy to output o close both files Object serialization:  Serialization – transform object into bytes representing the object  Deserialization – transforming bytes back into object  Objects can be serialized and stored in a file, then deserialized and read back later  To serialize an object, that object’s class must implement the Serializable interface o Example: public class Ticket implements Serializable {  To use serialization, must import java.io.Serializable;  Serialize using the writeObject method of the ObjectOutputStream class o Example: ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream (“FileName”)); oos.writeObject(obj);  this saves the serialized version of object “obj” in the file “FileName”  Deserialize using the readObject method of the ObjectInputStream class o Example: ObjectInputStream ios = new ObjectInputStream(new FileInputStream(“FileName”)); object = ios.readObject();

×

×

### BOOM! Enjoy Your Free Notes!

×

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

Jim McGreen Ohio University

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Kyle Maynard Purdue

#### "When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the material...plus I made \$280 on my first study guide!"

Bentley McCaw University of Florida

Forbes

#### "Their 'Elite Notetakers' are making over \$1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!
×

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com