Unix Environment Input Output 2 List Content (ls) ◦ ls (list current directory) ◦ ls –all...
-
Upload
madeleine-lawrence -
Category
Documents
-
view
236 -
download
0
Transcript of Unix Environment Input Output 2 List Content (ls) ◦ ls (list current directory) ◦ ls –all...
CS1020 Lab1Introduction
2
Unix Environment
Unix EnvironmentInputOutput
3
List Content (ls)◦ ls (list current directory)◦ ls –all (include hidden files/folders)
Make directory (mkdir)◦ mkdir[directory name]
Change Directory (cd)◦ cd [directory name]◦ Use [Tab] button for auto-completion
Remove Directory Entries (rm)◦ rm [file/folder name]
Java Compile (javac)◦ javac *.java (shortcut to compile all java files)◦ Similar effect as make command
Important UNIX Commands (1/2)
4
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
Important UNIX Commands (2/2)
5
http://vim.rtorr.com/ 3 main modes
◦ Normal/Command mode (default) For navigation and manipulation of text Everything the user types is interpreted as commands e.g. “h” to move left, “x” to remove a character
◦ Insert mode (i, I, a, A, o, O) For inserting new text Exit Insert mode to normal mode using Esc
◦ Visual mode For highlighting texts (useful for copy and paste)
Vim
6
Cursor movement (Only in command mode!)◦ h: move cursor left◦ j: move cursor down◦ k: move cursor up◦ l: move cursor right◦ 0: jump to start of line◦ $ jump to end of line◦ G: end of file◦ 5G: go to line 5
Important Vim Commands
7
Insert mode – insert/append text From command mode
◦ i: insert before cursor◦ I: insert at the beginning of the line◦ a: append after cursor◦ A: append at the end of line◦ o: open a new line below the current line◦ O: open a new line above the current line◦ ea: append at end of word
Important Vim Commands
8
Editing◦ s: delete character and substitute text◦ cc: replace(change) entire line◦ cw: replace(change) to end of word◦ c$: replace(change) to end of line◦ x: cut character◦ u: undo◦ r: replace one character◦ R: replace all character until ESC
Important Vim Commands
9
Visual mode◦ v: start visual mode, mark lines, then yank (y)◦ V: linewise ◦ Ctrl-v: visual block mode
Copy and Pasting◦ Copy/yank: y
Copy one line: yy, 2 lines: 2yy, to end of line: y$, one word: yw
◦ Cut/delete: d Cut one line: dd, 2 lines: 2dd, to end of line: d$, one word:
dw◦ Paste: p
Important Vim Commands
10
Search◦ /[pattern] → Search for the pattern
◦ n → Search next in the same direction◦ N → Search next in the opposite direction
Saving and quitting◦ Save (write): :w◦ Save and quit: :wq◦ Quit without saving: :q!
Tip◦ Open 2 windows, one to code, other to compile◦ gg=G : indent all lines
Important Vim commands
11
Write a Java program that prints “Hello World” out to the console
In Vim Compile and execute the program
vim [name of file].java
Show me your Vim skills!
12
Unix EnvironmentInputOutput
Input
13
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
Reading Standard Input
14
Finds and returns the next complete token from this scanner.
A complete token is preceded and followed by input that matches the delimiter pattern.
By default: delimiter pattern is whitespace
next()
15
next()◦ reads the next token as a string
nextInt()◦ reads the next token as an integer. If it is not: error
nextLine()◦ Advance scanner, read and return all in current line (until
next newline character)
Difference between next(), nextInt(), nextLine()
16
Input: 1 fish 2 fish 3 fish 4 fish /n end next():
◦ 1 (as a string) nextInt():
◦ error next():
◦ fish nextInt():
◦ 2 (as an integer) nextLine():
◦ fish 3 fish 4 fish
Example
17
o Type 1: Number of Operations is specifiedo Type 2: Read until Terminating Special
Charactero Type 3: Read until End of File
Different Input Reading Styles
18
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
}
1. Number of operations specified
19
Steps:1. Initialize the Scanner Class
2. Read the First Input
3. Loop until Terminating Special Character encountered
4. Read into tempInput again
2. Read until Terminating Special Character
Scanner sc = new Scanner(System.in);
String tempInput = sc.next();
while (!tempInput.equals(TERMINATING_CHAR)) {// Read Other Input (if exist)
// Step 4 Here}
tempInput = sc.next();
20
2. Read until Terminating Special Characterpublic 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}
21
Steps:
1. Initialize the Scanner Class
2. Loop until End of File
3. Read until End of File
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {// Read Other Input
}
22
3. Read until End of File (Example)
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}
23
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.
Hybrid Input
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}
24
Unix EnvironmentInputOutput
Output
25
With Extra Line at the End
Without Extra Line at the End
Printing Standard Output
System.out.println("Output String Here");
System.out.print("Output String Here");
System.out.print("Output String Here\n");
26
One per Line, Terminate with End Line
One per Line, Terminate without End Line
Typical Output Methods (1/2)
for (int i=0; i<numOutput; i++) {System.out.println(outputString);
}
System.out.print(outputString);for (int i=1; i<numOutput; i++) {
System.out.println();System.out.print(outputString);
}
27
Matrix Type, End Space
Matrix Type, End Not Space
Typical Output Methods (2/2)
for (int i=0; i<numLine; i++) {for (int j=0; j<numOut; j++) {
System.out.print (output + " ");}System.out.println();
}
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();
}
END OF FILE