Lab TA: Nicholas CS Year 3 Email: nicholaslum@u.nus.edu Website: http://comp.nus.edu.sg/~lum76 Consultation hours: By appointment 2
Guidance for take-home labs Issues with vim Issues with sunfire/unix Feedback for lab submissions Consultations 3
Attempt take-home labs before Thursday Ask for help early; don t wait until last minute Code on vim/unix to build confidence Sleep early before Sit-In Lab! 4
Unix Environment Input Output 5
Change Directory (cd) cd [directory name] Use [Tab] button for auto-completion List Content (ls) ls (list current directory) ls all (include hidden files/folders) Remove Directory Entries (rm) rm [file/folder name] Java Compile (javac) javac *.java (shortcut to compile all java files) 6
Edit File in VIM (vim) vim [file name] Running Java Program (java) java [program name without extension] java [class name] < [input file] > [output file] (input/output redirection) Checking for Difference (diff) diff [file name] [file name] Difference in a single space or an extra line will be noted Manual (man) man [command name] When you are not sure what the command does, or what parameter it expects 7
Command Mode: (to get into command mode, press ESC key) 1. dd or :d Delete the current line 2. :q! Exit without saving changes 3. :wq or ZZ Save and Exit 4. /[pattern] Search for the pattern n Search next in the same direction N Search next in the opposite direction More commands (copy, cut, paste, end of line, etc): http://www.keyxl.com/aaa8263/290/vim-keyboard-shortcuts.htm 8
Unix Environment Input Output 9
Scanner Class Initialization: Scanner sc = new Scanner (System.in); hasnext() and variants Check if there exists another input next() and variants Get the next input Variants: Int, Line, Double example: hasnextint() and nextline() There is NO nextchar() and hasnextchar() variant Type of Input: (from Lab 0 Problem 1) o Type 1: Number of Operations is specified o Type 2: Read until Terminating Special Character o Type 3: Read until End of File 10
public static void main(string[] args) { // Code Section Omitted // Other Initialization Scanner sc = new Scanner(System.in); int numops = sc.nextint(); for (int i=0; i<numops; i++) { // Read Other Inputs // Code Section Omitted 11
Steps: 1. Initialize the Scanner Class Scanner sc = new Scanner(System.in); 2. Read the First Input String tempinput = sc.next(); 3. Loop until Terminating Special Character encountered while (!tempinput.equals(terminating_char)) { // Read Other Input (if exist) // Step 4 Here 4. Read into tempinput again tempinput = sc.next(); 12
public static void main(string[] args) { // Code Section Omitted // Other Initialization Scanner sc = new Scanner(System.in); String tempinput = sc.next(); while (!tempinput.equals(terminating_char)) { // Read Other Input tempinput = sc.next(); // Code Section Omitted 13
Steps: 1. Initialize the Scanner Class Scanner sc = new Scanner(System.in); 2. Loop until End of File while (sc.hasnext()) { // Read Other Input 14
public static void main(string[] args) { // Code Section Omitted // Other Initialization Scanner sc = new Scanner(System.in); while (sc.hasnext()) { // Read Other Inputs // Code Section Omitted 15
Combines two or more different input types into a single program e.g.: Type 3 Input on the Outside, Type 1 Input on the Inside. public static void main(string[] args) { // Code Section Omitted // Other Initialization Scanner sc = new Scanner (System.in); while (sc.hasnext()) { int numops = sc.nextint(); for (int i=0; i<numops; i++) { // Read Other Inputs // Code Section Omitted 16
Unix Environment Input Output 17
With Extra Line at the End System.out.println("Output String Here"); System.out.print("Output String Here\n"); Without Extra Line at the End System.out.print("Output String Here"); 18
One per Line, Terminate with End Line for (int i=0; i<numoutput; i++) { System.out.println(outputString); One per Line, Terminate without End Line System.out.print(outputString); for (int i=1; i<numoutput; i++) { System.out.println(); System.out.print(outputString); 19
Matrix Type, End Space for (int i=0; i<numline; i++) { for (int j=0; j<numout; j++) { System.out.print (output + " "); System.out.println(); Matrix Type, End Not Space for (int i=0; i<numline; i++) { System.out.print (output); for (int j=1; j<numout; j++) { System.out.print (" " + output); System.out.println(); 20
Questions?