





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Study notes
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















CS 211 Java overview
very quick overview of Java basics
actually use inheritance and interfaces
intro to threads
GUIs
recursion/induction, efficiency, sorting
data structures (lists, queues, stacks, trees, etc)
graph algorithms
applications
LOTS of programming! 1
Books etc.
CS 211 Java overview
Java uses classes and objects = hyperorganised!
A class Car is like a manufacturer who only constructs individual new cars.
A class Bucket will only construct individual new buckets.
Cars and buckets have natural things which belong to every car or bucket, although of course cars have different colours, etc.! 2
CS 211 Java overview
Enough of such generalities! How do we write a simple program in Java?
First we need to be able to get stuff in to and out from the computer! System.out.println ( “Once upon a time ...” ) ; looks into the System where it finds an out, and looks into System’s out where it finds a method (or function or routine) which can print a String of characters onto a fresh line on the standard output screen. System.out.print ( “Golly gosh” ) ; does exactly the same, except the method print doesn’t finish with a “new line”.
CS 211 Java overview
To read in a String of characters from the standard input keyboard, InputStreamReader nab = new InputStreamReader(System.in); BufferedReader grab = new BufferedReader(nab); constructs a BufferedReader called grab so that grab.readLine( ); reads a whole line of input.
Java is a language of “let’s pretend!”, so grab is a virtual keyboard which has the ability (amongst other skills) of readLine( ) - all the other stuff is there to establish a connection between “make believe” and “reality”.
CS 211 Java overview
Similarly, FileOutputStream plop = new FileOutputStream ( “meow.t” ) ; PrintWriter scribble = new PrintWriter ( plop ) ; allows scribble.println( “What big teeth you have!” ) ; to write to the file meow.t , which again should be closed by plop.close( ) ; when finished with.
Of course, we could have done the same thing when writing to the screen, PrintWriter tube = new PrintWriter ( System.out , true ) ; tube.println ( “How time flies!” ) ; Here, tube is the name of the make believe computer screen.
Now that we can get stuff into and out of the computer, let’s actually write a program ...
CS 211 Java overview
import java.io.* ; // so that i/o stuff is available public class PlayTime { public static void main (String [ ] args) throws Exception { InputStreamReader ca = new InputStreamReader(System.in) ; BufferedReader va = new BufferedReader(ca) ; PrintWriter bon = new PrintWriter(System.out , true) ; int x , y = 2 ; bon.println( “Enter an integer.” ) ; x = Integer.parseInt( va.readLine( ) ) ; y = y * x - x / 2 ; bon.println( “x was ” +x+ “ and y is ” +y ) ; ca.close( ) ; } // end of main method } // end of class PlayTime
This whole file would be called PlayTime.java and ‘compiled’ and run as relevant to your computer system. Now for some routine details ... action starts HERE in case of silliness flushes the pipe
CS 211 Java overview
Arithmetic
CS 211 Java overview
import java.io.* ; public class Multiplier { public static void main ( String [ ] args ) throws Exception { InputStreamReader isr = new InputStreamReader ( System.in ) ; BufferedReader comingIn = new BufferedReader ( isr ) ; PrintWriter goingOut = new PrintWriter ( System.out , true ) ; int x , y ; // space to store input long z = 0 ; // bigger space for answer! String ask = “Please enter an integer.” ; // may as well be polite! goingOut.println ( ask ) ; x = Integer.parseInt ( comingIn.readLine( ) ) ; goingOut.println ( ask ) ; y = Integer.parseInt ( comingIn.readLine( ) ) ; z = x * y ; goingOut.print ( “The value of ” +x+ “ times ” +y ) ; goingOut.println ( “ is ” +z ) ; goingOut.println ( “Thanks for using \“Multiplier\”. Do come again!” ) ; comingIn.close( ) ; } // end of main method } // end of class Multiplier 11 setting up the i/o getting the first number getting the next number actually PERFORM the multiplication!!! unremitting courtesy! giving out the answer
CS 211 Java overview
Logic So for example, A != B and !(A == B) are equivalent. The difference between & and && (similarly for | and || ) relies on “short-circuiting”. ( 3 == 7 ) && ( 2 == 3/0 ) evaluates comfortably to false, since failure occurred in the first term there was no need to go further, but ( 3 == 7 ) & ( 2 == 3/0 ) is a disaster, since the single ampersand has no short-circuit provision. Typically we use the ‘single’ flavour when we need to ensure that each term of the expression is evaluated, such as when incrementing variables.
Control
CS 211 Java overview
CS 211 Java overview
Delegation - There are times when it’s sensible to delegate certain tasks within a program. Javaspeak for this is methods (alias functions or subroutines). For example ... public class Stats { public static void main ( String [ ] args ) { int a = 7 , b = 12 , c = 24 ; double d ; d = avg ( a , b ) ; System.out.println ( “The average of ” +a+ “ and ” +b+ “ is ” +d+ “.” ) ; System.out.println ( “Including” +c+ “ changes this to” + avg( a , b , c ) + “.” ) ; } // end of main method public static double avg ( int x , int y ) { return ( x + y ) / 2.0 ; } // end of two-variable avg method public static double avg ( int x , int y , int z ) { return ( x + y + z ) / 3.0 ; } // end of three-variable avg method } // end of class Stats 16 unambiguous ‘overloading’ values copied
CS 211 Java overview
Types of types - For the primitive types, actual values are copied and passed. For any other ‘types’, since Java doesn’t really know how big they might be, instead of copying/passing whatever constitutes value, Java copies/passes the address/reference to the object, leaving the object wherever it happens to sit in memory. Indeed ... Car ferrari = new Car(red) ; does not make ferrari the actual car; it makes instead ferrari the address of the actual car. Car bus = new Car(green) ;
CS 211 Java overview
This raises the obvious question of what address a reference holds if it’s not currently referencing any particular object. The answer is null If this situation is not picked up by the compiler, but only noticed at run- time, then we can easily get the common error message NullPointerException if we attempt to access that object’s properties, i.e., a reference had been declared but not assigned the address of an actual existent object.
It’s also worth noting that once an object is no longer being referenced, as in cheap after the assignment cheap = riding ; Java allows the memory allocated to the object cheap to be written over (whenever that may happen) ... this is called automatic garbage collection.
A reference cannot reference a primitive variable (hence the existence of wrapper classes like Integer, Double, Character, Boolean, etc.).
CS 211 Java overview
As a final comment in this vein, suppose yummy ( rhubarb ) ; is a call to a method yummy with declaration public int yummy ( ------ custard ) { ............................. } then if ------ is a primitive type the variable custard copies the value of rhubarb, but if ------ is a reference type then custard points to the same object that rhubarb does.
We should observe that in Car ferrari = new Car (red) ; the word new creates an anonymous object according to the manufacturer class Car which has been told explicitly to make it red; the phrase Car ferrari makes ferrari an allowable reference to a Car-like object; finally the = assigns the address of the anonymous car to the name ferrari !!!