Docsity

Java Programming Fundamentals: I/O, Classes, and Objects, Study notes of Computer Science

Toggle sidebar
bg1
CS 100
Intro to programming … mostly Java
some Matlab
knowing one language well Æ easy to pick up another one
Java … powerful, portable
Matlab … nice for simulations and graphing
Java basics (common to most languages)
• I/O … messy, but points to underlying ethos
• if/then, loops, arrays, etc.
• ‘methods’ (aka functions, subroutines) Æ delegation
• classes/objects … organisation + modularity + reuse
• GUIs … interface design
• threads … juggling processes
• recursion, efficiency, sorting
• LOTS of programming!!!!
Java Æ uses classes and objects = hyperorganised!
A class Car is like a manufacturer who can only construct individual new
cars.
A class Bucket will only construct individual new buckets.
Cars + buckets have natural things which belong to every car or bucket,
although of course cars have different colours, etc…
pf3
pf4
pf5

Partial preview of the text

Download Java Programming Fundamentals: I/O, Classes, and Objects and more Study notes Computer Science in PDF only on Docsity!

CS 100

Intro to programming … mostly Javasome Matlab knowing one language well Æ easy to pick up another one JavaMatlab …… powerful, portablenice for simulations and graphing Java basics (common to most languages) • I/O … messy, but points to underlying ethos

  • • if/then, loops, arrays, etc.‘methods’ (aka functions, subroutines) Æ delegation
  • • classes/objects … organisation + modularity + reuseGUIs … interface design
  • • threads … juggling processesrecursion, efficiency, sorting
  • LOTS of programming!!!!

Java Æ uses classes and objects = hyperorganised!

Acars. class Car is like a manufacturer who can only construct individual new

A class Bucket will only construct individual new buckets. Cars + buckets have natural things which belong to every car or bucket,although of course cars have different colours, etc…

To build a red car called Ferrari, we might write Car Ferrari = new Car(red);

  • I’m not promising that this will work!!! and to build a yellow car called ccbb, we might write Car ccbb = new Car(yellow); Of course, Bucket rollsroyce = new Bucket(huge); will only give you a peculiarly named huge bucket.

If you want to access stuff in your car, then the dot“genitive case”, so. is the

Ferrari.colour would be red, and ccbb.colour would be yellow. This is also like a path ; looking into the Ferrari or the ccbb to find the individual colours.

More on this later…

Java is a language of “let’s pretend!”, so keyboard which has the ability (among other skills) of grab is a virtual readLine() between “make believe” and “reality.”—all the other stuff is there to establish a connection

We can do the same thing with files: FileReader secret = new FileReader(“spy.oops”); BufferedReader james = new BufferedReader(secret); constructs a BufferedReader called james so that james.readLine(); reads a whole line of input fromcommon courtesy, you should spy.oops. As a matter of

secret.close(); close the ‘file’ when you’ve finished with it!

(If you need to specify a path for your file, you can have = new FileReader(“c:/money/penny/spy”); or whatever is appropriate for your system.)

Actually, you should also nab.close() any InputStreamReaders you’ve opened when finished.

Similarly, FileOutputStream plop = new FileOutputStream(‘meow.t”); PrintWriter scribble = new PrintWriter(plop); allows scribble.println(“What big teeth you have!”); to write 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’sactually write a program.