



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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