Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
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
1.
Steps:
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?