Overview for Introduction to Computing Using Java - Spring 2008 | CS 1110, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-pq2-1
koofers-user-pq2-1 🇺🇸

10 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
CS 211 - Spring 2008
Books etc.
- at java.sun.com
/reference/api
/docs/books/tutorial
- IDE from www.eclipse.org
- The Java Language
Specification (Gosling et al)
- Data Structures and Algorithm
Analysis in Java (Weiss)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Overview for Introduction to Computing Using Java - Spring 2008 | CS 1110 and more Study notes Computer Science in PDF only on Docsity!

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

CS 211 - Spring 2008

Books etc.

  • at java.sun.com /reference/api /docs/books/tutorial
  • IDE from www.eclipse.org
  • The Java Language Specification (Gosling et al)
  • Data Structures and Algorithm Analysis in Java (Weiss)

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”.

  • i /o 4

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”.

  • i /o 5

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 ...

  • i /o 7

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

  • i /o 8

CS 211 Java overview

Arithmetic

  • Also, for those who like calculating ... Math.sin(1.78) ; Math.atan(72.4) ; etc. all do the obvious. Math is a repository of lots of useful stuff!
  • Now for an example ... 10
  • plus 3 + 4 ; 7
  • minus 3 - 4 ; -
  • times 3 * 4 ; 12 / divide 3 / 4 ; 0 % remainder 3 % 4 ; 3 What do you think (-3) % 4 ; evaluates to? As an aside, it’s worth noting that there is a non-primitive type String which carries a string of characters, and String tut = “Methought I was,” ; String um = “ there is no man ...” ; tut = tut + um ; gives tut the updated value of Methought I was, there is no man ... So for strings, + appends the second string to the end of the first string.
  • 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

  • arithmetic

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

  • if ( ............. ) • if ( ............. ) { ------------ } { ------------ } else { ------------ } 13 A && B AND A & B A || B OR A | B ! A NOT Branching processes
  • logic and control

CS 211 Java overview

  • -----^?^ -----^ :^ -----^ ;^ •^ switch (^ name^ ) { case value : ----- break ; case value : ----- break ; case value : ----- break ; default : ----- }
  • while (^ .............^ )^ •^ do { ------------ } { ------------ } while ( ............. ) ;
  • for (^ -----^ ;^ .........^ ;^ -----^ ) { ------------ } We can now broaden our previous example ... 14 More branching processes Looping processes
  • control

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

  • methods This is all playing with the thread of control, rather like passing the baton back and forth in a relay race.

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

  • reference types 17

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

  • reference types 19

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 !!!

  • reference types 20