unix environment input output 2 list content (ls) ◦ ls (list current directory) ◦ ls –all...

28
CS1020 Lab1 Introduction

Upload: madeleine-lawrence

Post on 14-Dec-2015

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

CS1020 Lab1Introduction

Page 2: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

2

Unix Environment

Unix EnvironmentInputOutput

Page 3: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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)

Page 4: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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)

Page 5: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 6: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 7: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 8: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 9: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 10: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 11: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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!

Page 12: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

12

Unix EnvironmentInputOutput

Input

Page 13: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 14: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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()

Page 15: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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()

Page 16: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 17: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 18: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

Page 19: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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();

Page 20: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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}

Page 21: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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

}

Page 22: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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}

Page 23: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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}

Page 24: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

24

Unix EnvironmentInputOutput

Output

Page 25: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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");

Page 26: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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);

}

Page 27: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

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();

}

Page 28: Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

END OF FILE