// CIS 790 JAVA PROF. AUCIELLO // this document contains 12 Java Applications that run on a DOS platform. // the goal of these is to develop student skills in interactive designing, testing, // debugging, and improving programs. Java applications form an excellent // set of examples to work with -- easy to compile, quick to debug, and // have a powerful set of library functions. Get into this project. Take them // apart -- reverse engineer them -- reassamble, improvise, transfer concepts from // one program to the other! Show off what you can do! Acknowledgement to // Prof. Susan Silvera for inspiring this assignment. // APPLICATION 01 public class Hello { public static void main(String args[]) { System.out.println("Hello "+args[0]+"!"); } } // APPLIC 02 (BOOLEAN) public class QuickTest { public static void main(String args[]) { System.out.println("5 is greater than 6:"+(5>6)); System.out.println("6 is greater than or equal to 3:"+(6>=3)); System.out.println("8 is less than 10:"+(8<10)); } } // APPLIC 03 // all class name and file name will be in lower case. class p011 { public static void main (String args[]) { System.out.println("Hello World!"); System.out.println("Is this worth all the trouble?"); System.out.println("I sure hope and think so!"); } } // APPLIC 04 (toString) class LiteralTest { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Innocuous Display Message"); System.out.println(sb.toString()); } } /* sb = name of instance of new StringBuffer class. to.string method converts buffered string to string. */ .. APPLIC 05 (do ... while) /* CIS 790 790q3.htm */ // import javaio.* class loop { public static void main (String args[]) { int x = 1; do { System.out.println("Hello World, " + x); x++; } // ends do while (x <= 10); } } // ends class // APPLIC 06 (sequentially reads a file) import java.io.*; class read { public static void main(String args[]) { byte buffer[] = new byte[2056]; try { FileInputStream fileIn = new FileInputStream("read.java"); int bytes = fileIn.read(buffer,0,2056); try{ String str = new String(buffer,0,bytes, "Default"); System.out.println(str); }catch (UnsupportedEncodingException e){ System.out.println("The encoding \"Default\"was not found :"+e); } } catch (Exception e) { String err = e.toString(); System.out.println(err); } } } // APPLIC 07 (input and output of a single character) // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO // TOPICS: /* . input - output libraries . try ... catch (error trapping) . KB input and Display output. */ import java.io.*; class kb1a { public static void main(String args[]) { char c; try { System.out.println("Prints just the 1st char."); System.out.print("Type a Character: "); c = (char)System.in.read(); System.out.println("You typed ......: " + c); } catch (Exception e) { String err = e.toString(); System.out.println(err); } // end try-catch. } // end main procedure. } // end class // APPLIC 08 (reads any file) import java.io.*; class f8 { public static void main(String args[]) { byte buff2[] = new byte[64]; byte buffer[] = new byte[2056]; try { FileInputStream fileIn = new FileInputStream("f8.java"); int bytes = fileIn.read(buffer,0,2056); System.out.println(bytes); String str = new String(buffer,0,bytes, "Default"); System.out.println("Length of str = " + str.length()); System.out.println("Press any key to start."); System.in.read(buff2, 0, 64); System.out.println(str); } catch (Exception e) { String err = e.toString(); System.out.println(err); } // end try-catch. } // end main procedure. } // end class /* line 1 2 3 4 5 6 7 8 9 */ // APPLIC 09 (input & output of chars and strings) // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO // TOPICS: /* . input - output libraries . creating array of bytes . try ... catch (error trapping) . KB input and Display output. */ import java.io.*; class kb1 { public static void main(String args[]) { byte buff2[] = new byte[64]; byte buffer[] = new byte[2056]; char c; // System.out.println(" Enter File name: "); StringBuffer buff1 = new StringBuffer(); try { System.out.println("This exercise will print out"); System.out.println("the 1st char entered, then 'echo'"); System.out.println("the entire string up to the key"); c = (char)System.in.read(); System.out.println("1st Char in Char format = " + c); buff1.append(c); while ( (c = (char)System.in.read()) != '\n' ) buff1.append(c); System.out.println(buff1); } catch (Exception e) { String err = e.toString(); System.out.println(err); } // end try-catch. } // end main procedure. } // end class // APPLIC 10 (i/o of strings [2nd method]) // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO // TOPICS: /* . input - output libraries . creating array of bytes . try ... catch (error trapping) . Opening a file . counting # of bytes in file. . KB input and Display output. */ import java.io.*; import java.lang.String; import java.io.InputStream; class kb4 { public static void main(String args[]) { byte buff2[] = new byte[64]; byte buffer[] = new byte[2056]; char c; String charin; try { // 1st try: System.out.print ("Enter String: "); System.in.read(buff2, 0, 64); String str5 = new String(buff2,0); System.out.println("You typed: " + str5); // 2nd try: (for character only.) System.out.print("Type a Single Character "); c = (char)System.in.read(); System.out.println("You typed ....... :" + c); } catch (Exception e) { String err = e.toString(); System.out.println(err); } // end try-catch. } // end main procedure. } // end class // APPLIC 11 (reading a file) // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO // TOPICS: /* . input - output libraries . creating array of bytes . try ... catch (error trapping) . Opening a file . counting # of bytes in file. . KB input and Display output. */ import java.io.*; class f10 { public static void main(String args[]) { byte buff2[] = new byte[64]; byte buffer[] = new byte[2056]; try { FileInputStream fileIn = new FileInputStream("f10.java"); int bytes = fileIn.read(buffer,0,2056); System.out.println("Press any key to start to read file.."); System.in.read(buff2, 0, 64); fileIn.read(buffer,0,64); String str1 = new String(buffer,0); System.out.println(" Length of bytes field + bytes " + " Length of str1 field = " + str1.length()); System.out.println(" File has been read."); System.out.println(" Press any key to start to print file.."); System.in.read(buff2, 0, 64); System.out.println(str1); } catch (Exception e) { String err = e.toString(); System.out.println(err); } // end try-catch. } // end main procedure. } // end class // APPLIC 12 (string to integer) // method works .. inputs a string convert to integer. // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO // TOPICS: /* . input - output libraries . try ... catch (error trapping) . KB input and Display output. . String input. . Convert String to Integer . Try ... Catch */ import java.io.*; import java.lang.String; class kb5 { public static void main(String args[]) { byte buff2[] = new byte[64]; try { System.out.print ("Enter Integer Value: "); System.in.read(buff2, 0, 64); String str5 = new String(buff2,0); System.out.println("You typed - str5 = : " + str5); int l1 = str5.length(); System.out.println("Length of str5 (before trimming) = " + l1); String str6 = str5.trim(); System.out.println("Len of str5(trimmed) = " + str6.length()); System.out.println(" "); System.out.println("Example of converting string to Integers"); int i5 = Integer.parseInt(str6); int i6 = i5 + 1; System.out.println(i6); } // end try. catch (Exception e) { String err = e.toString(); System.out.println(err); } // end catch. } // end main procedure. } // end class