Top Banner
CS1020 Week 3: 29 th January 2015
37

CS1020 Week 3: 29 th January 2015. Contents Introduction UNIX environment Input/Output: Practice Exercise #9 Take-home Lab #1 Exercise 1: Temperature.

Dec 14, 2015

Download

Documents

Garrett Loft
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

CS1020Week 3: 29th January 2015

Page 2: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

2

Contents

Introduction UNIX environment Input/Output: Practice Exercise #9 Take-home Lab #1

Exercise 1: Temperature Conversion Exercise 2: Palindromes Exercise 3: Overlapping Rectangles

Week 3

Page 3: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Introduction

Week 3

Page 4: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

4

Introduction

I’m ________ *SoC background* *interesting things about yourself?* *some basic expectations of your lab sessions* *email/preferred mode of contact for students* etc.

Week 3

Lab TAs, please customise this slide.

Page 5: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Week Date Type Topics

1 15th Jan Special Intro Workshop

1 -- Take-home #0 Basic Java

3 29th Jan Take-home #1 OOP

4 5th Feb Sit-in #1 OOP

5 12th Feb Take-home #2 Array and OOP

6 19th Feb Chinese New Year (no lab)

7 5th Mar Sit-in #2 Array and OOP

8 12th Mar Take-home #3 Linked List

9 19th Mar Take-home #4 Stack/Queue

10 26th Mar Sit-in #3 LL/Stack/Queue

11 2nd Apr Take-home #5 Recursion

12 9th Apr Sit-in #4 Recursion

13 16TH Apr Revision

Lab Schedules (Tentative)

5

Plan is tentative. Refer to module website for the most up-to-date plan.

Ass

ess

men

ts

[CS1020 Lecture 0: Course Admin]

Page 6: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

UNIX environment

UNIX commandsVim

Week 3

Page 7: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

7

Important UNIX Commands (1/2) Change Directory (cd)

cd [directory name] cd .. (to go back to parent directory) cd (to go to home directory)

List Content (ls) ls (list current directory) ls –al (include hidden files/folders, in long format)

Copy/remove/move (cp, rm, mv) mv command is also used for renaming files

Edit File in VIM (vim) vim [file name]

Auto-completion of filename/directory name Use <tab> button Week 3

Page 8: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

8

Important UNIX Commands (2/2) Java Compile (javac)

javac *.java (shortcut to compile all java files) Running Java Program (java)

java [program name without extension] java [class name] < [input file] > [output file]

(input/output redirection)

Checking for Difference (diff) diff [filename1] [filename2] 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 options it

has

Week 3

Page 9: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

9

Some VIM Commands

Command Mode: (to get into command mode, press ESC key)

1. :q! → Exit without saving changes

2. :wq or ZZ → Save and Exit (don’t press ctrl-z!)

3. dd → Delete the current line (ndd → delete n lines)

4. yy → Yank (copy) the current line (nyy → yank n lines)

5. p → paste after current line (P → paste before current line)

6. :n or nG → Go to line number n (Example: 12G)

7. w → go to next word (b → go back to previous word)

8. /[pattern] → Search for the pattern n → Search next in the same direction N → Search next in the opposite direction

See “Vim editor” in CS1020 Online web page: http://www.comp.nus.edu.sg/~cs1020/2_resources/online.html

Week 3

Page 10: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Input/Output

Practice Exercise 9

Week 3

Page 11: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

11

Input: Reading Standard Input

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 Practice Exercise 9)o Type 1: Number of Operations is specifiedo Type 2: Read until Terminating Special Charactero Type 3: Read until End of File

Week 3

Page 12: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

12

Input: Type 1 Input (Example)

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

}

Week 3

Page 13: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

13

Input: Type 2 Input Steps:1. Instantiate a Scanner object

2. Read the First Input

3. Loop until Terminating Special Character encountered

4. Read into tempInput again

Scanner sc = new Scanner(System.in);

String tempInput = sc.next();

while (!tempInput.equals(TERMINATING_CHAR)) {// Read Other Input (if exists)

// Step 4 Here}

tempInput = sc.next();

Week 3

Page 14: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

14

Input: Type 2 Input (Example)

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}

Week 3

Page 15: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

15

Input: Type 3 Input Steps:

1. Instantiate a Scanner object

2. Loop until End of File

Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {// Read Other Input

}

Week 3

Page 16: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

16

Input: Type 3 Input (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}

Week 3

Page 17: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

17

Input: Hybrid Input 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}

Week 3

Page 18: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

18

Output: Printing Standard Output

With newline character at the end

Without newline character at the end

Note that for CodeCrunch, your last output line should contain a newline character at the end

System.out.println("Output String Here");

System.out.print("Output String Here");

System.out.print("Output String Here\n");

Week 3

Page 19: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Take-home Lab #1

Exercise 1 – Temperature Conversion

Week 3

Page 20: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

20

Ex 1: Temperature Conversion (1/5)

Convert temperature of various scales

From Fahrenheit to Celcius: °C = (5/9) × (°F – 32)

From Celsius to Fahrenheit: °F = 1.8 × °C + 32

From Celsius to Kelvin: °K = °C + 273.15

Week 3

Page 21: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

21

Ex 1: Temperature Conversion (2/5)

Write a program to… Read a temperature and convert the value from

one scale to another Contains four conversion methods (three of

which must be from exercise pdf file) Output converted temperature in two decimal

places

Week 3

Page 22: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

22

Ex 1: Temperature Conversion (3/5)

Week 3

Enter temperature: 300.0

Choose source scale: 1. Celsius 2. Fahrenheit 3. Kelvin Enter your choice: 3

Choose destination scale: 1. Celsius 2. Fahrenheit Enter your choice: 1

300.0 degrees Kelvin = 26.85 degrees Celsius

Sample run

Page 23: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

23

Ex 1: Temperature Conversion (4/5)

Use DecimalFormat class (requirement) Print properly formatted output (2 decimal places)

Check out the API! http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Week 3

Page 24: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

24

Ex 1: Temperature Conversion (5/5)

Create DecimalFormat object with appropriate pattern

Week 3

Page 25: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Take-home Lab #1

Exercise 2 – Palindromes

Week 3

Page 26: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

26

Ex 2: Palindromes (1/5)

Palindrome: A string that reads the same backward as forward – ignoring case and non-letters

Examples raDAR aibohphobia Do you know what this means? Race-car 7… Madam, I’m Adam A man, a plan, a canal – Panama!

Week 3

Page 27: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

27

Ex 2: Palindromes (2/5)

Write a program to… Read in several lines of text For each line, check if it is a palindrome Count and output the number of palindromes read

Sample runs

Week 3

Enter text: Race-car 7...Enter text: I like CS1020Enter text: 12 noonEnter text: Madam, I'm Adam Enter text: (user pressed ctrl-d here)Number of palindromes = 3

Enter text: Don't nodEnter text: Never odd or evenEnter text: Too bad – I hid a bootEnter text: Madam, in Eden I'm AdamEnter text: Oozy rat in a sanitary zooEnter text: (user pressed ctrl-d here)Number of palindromes = 5

Page 28: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

28

Ex 2: Palindromes (3/5)

Preprocessing the string before checking if it is a palindrome: Remove all non-letters Convert all letters to lowercase

Check out the API! http://docs.oracle.com/javase/7/docs/api/

Week 3

Page 29: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

29

Ex 2: Palindromes (4/5)

Convert all letters to lower case

Week 3

Remove all non-letters

Page 30: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

30

Ex 2: Palindromes (5/5)

replaceAll(String regex, String replacement) regex: Regular Expression What is the regex for non-letters?

[^A-Za-z] (Explanation: ^ means not. Hence, not A-Z or a-z)

See IVLE “General” forum, “Regex (regular expression)” thread started by Aaron Contains a sample program on how to use replaceAll()

with regex

Week 3

Page 31: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

Take-home Lab #1

Exercise 3 – Overlapping Rectangles

Week 3

Page 32: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

32

Ex 3: Overlapping Rectangles (1/5)

Write a program to… Read in integer values for 4 points First two points are opposite vertices of one rectangle and

subsequent two are for second rectangle Compute the overlap area of the two rectangles

Sample run

Week 3

Enter 2 opposite vertices of 1st rectangle: 3 -1 15 6 Enter 2 opposite vertices of 2nd rectangle: 19 2 9 10 Overlap area = 24

Page 33: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

33

Ex 3: Overlapping Rectangles (2/5)

Observe the overlapping area:

X2,Y2

X1,Y1

X4,Y4

X3,Y3

X2,Y4

X3,Y1

Week 3

Page 34: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

34

Ex 3: Overlapping Rectangles (3/5) The overlapping rectangle bottom-left point’s x- and

y-coordinates are the maximum of the other two rectangles bottom-left points’ x- and y-coordinates

MAX(X1,X3) X3; MAX(Y1,Y3) Y1

Week 3

X2,Y2

X1,Y1

X4,Y4

X3,Y3

X2,Y4

X3,Y1

Page 35: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

35

Ex 3: Overlapping Rectangles (4/5) The overlapping rectangle top-right point’s x- and y-

coordinates are the minimum of the other two rectangles top-right points’ x- and y-coordinates

MIN(X2,X4) X2; MIN(Y2,Y4) Y4

Week 3

X2,Y2

X1,Y1

X4,Y4

X3,Y3

X2,Y4

X3,Y1

Page 36: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

36

Ex 3: Overlapping Rectangles (5/5) But…. The two points given may not lie in the same direction Need to rearrange coordinates to form suitable points

Week 3

X2,Y2

X4,Y4

X3,Y3

X2,Y4

X3,Y1X1,Y1

Page 37: CS1020 Week 3: 29 th January 2015. Contents  Introduction  UNIX environment  Input/Output: Practice Exercise #9  Take-home Lab #1  Exercise 1: Temperature.

END OF FILE