



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
Java programming basics, including creating and running java programs, controlling program flow with operators and conditional statements, working with arrays, and handling input and output streams. It includes examples and instructions for compiling and running java code.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Java Basics - Part I
(lectures programs)
Sun OnLine Documentations
Everything is an Object
First Example: HelloDate
Run as applet
public class HelloDate { public static void main(String[] args) { System.out.println("Hello, it's: "); System.out.println(new Date()); } }
% make OR % javac HelloDate.java
% java HelloDate
Controlling Program Flow
¾ Operators, precedence, assignment. ¾ If-else ¾ While ¾ Do-while ¾ For ¾ Switch
Example: VowelsAndConsonants
public class VowelsAndConsonants { public static void main(String[] args) { for (int i = 0; i < 100; i++) { char c = (char)(Math.random() * 26 + 'a'); System.out.print(c + ": "); switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("vowel"); break; case 'y': case 'w': System.out.println( "Sometimes a vowel"); break; default: System.out.println("consonant"); } } } }
Run as applet
Initilization & Cleanup
System.out.println ( "After loop:\n" + "total created = " + Chair.created + ", total finalized = " + Chair.finalized ); } } Run as applet
The Java IO System
Example: IOStreamDemo.java
public class IOStreamDemo { public static void main(String[] args) throws IOException {
// Finding file length File file = new File(args[0]); long length = file. length(); System.out.println("file lenght is: " + length);
// Transfer bytes from in to out String infile = args[0]; String outfile = args[1]; InputStream fin = new FileInputStream( infile ); OutputStream fout = new FileOutputStream( outfile );
byte[] buf = new byte[1024]; int len; while ((len = fin. read(buf)) > 0) { fout. write(buf, 0, len); } fin.close(); fout.close();
// Reading standard input: BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in )); System.out.print("Enter a line:");
System.out.println( stdin. readLine());
// Reading input by lines: BufferedReader in = new BufferedReader( new FileReader( infile )); String s, s2 = new String(); while((s = in. readLine())!= null) s2 += s + "\n"; in.close();
// File output try { BufferedReader in2 = new BufferedReader( new StringReader( s2 )); PrintWriter out1 = new PrintWriter( new BufferedWriter( new FileWriter(" IODemo.out "))); int lineCount = 1; while((s = in2 .readLine()) != null ) out1 .println(lineCount++ + ":" + s); out1.close(); } catch(EOFException e) { System.err.println("End of stream"); }
} }
To execute:
% java IOStreamDemo InputFile OutFile
and look at: IODemo.out and OutFile