CS1020 Week 3: 29th January 2015.

Slides:



Advertisements
Similar presentations
UNIX Overview. 2 UNIX UNIX is a multi-user and multi-tasking operating system. Multi-tasking: Multiple processes can run concurrently. Example: different.
Advertisements

CS1020 Data Structures and Algorithms I Lecture Note #0 Course Admin (AY2014/5 Semester 2)
CS1010 Programming Methodology
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Cosc 4750 Getting Started in UNIX Don’t be afraid of the prompt, in linux it can be your best friend. In some cases, the only way to do certain things.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
CS1020: Intro Workshop. Topics CS1020Intro Workshop Login to UNIX operating system 2. …………………………………… 3. …………………………………… 4. …………………………………… 5. ……………………………………
Starting Vi Opening an existing file vi filename Creating a new file vi filename In your workshop directory, create a new file called mysong vi mysong.
L INUX C OMMAND L INE I NTERFACE G UNAANBAN.G
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
1 Unix Editors (ee, ed, ex, vi, vim) and Compilers (g77, gcc) Speaker: Li-Wen Chen Date:
Help session: Unix basics Keith 9/9/2011. Login in Unix lab  User name: ug0xx Password: ece321 (initial)  The password will not be displayed on the.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
© Crown copyright Met Office An Introduction to Linux PRECIS Workshop, University of Reading, 23rd – 27th April 2012.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Guide To UNIX Using Linux Fourth Edition
Unix Basics Chapter 4.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction Unix-like system is everywhere Linux Android for smartphones Google Chrome OS for Chromebook Web.
The UNIX development environment CS 400/600 – Data Structures.
JAVA (NPRG013) LABS 2012/2013 Jaroslav Keznikl,
Vim Editor and Unix Command gcc compiler Computer Networks.
1 Editing a C Program 01/16/15. 2 Objective Use Linux to edit, compile and execute a C program.
Carnegie Mellon Linux Boot Camp Jenna MacCarley, Peter Pearson, Shashank Goyal 9/19/2015.
1 Lab 2 “Hello world” in Unix/Linux #include "std_lib_facilities_4.h" int main(){ cout
COP3502: Introduction to Computer Science Yashas Shankar.
BIF713 Basic Unix/Linux Commands Getting Help with Commands.
Introduction to UNIX Geraint Vaughan. What is UNIX? Command-line operating system (not point- and click) Designed for ‘experts’ Lots of different variants.
Unix Session IV.
Text Editing February 2 nd, 2004 Class Meeting 3.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
1May 16, 2005 Week 2 Lab Agenda Command Line FTP Commands Review More UNIX commands to learn File name expansion - * Introduction of vi.
Introduction to Programming Using C An Introduction to Operating Systems.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
More on Using onyx 8/28/13. Program 1 Due a week from today. See website for details.
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Workshop III - Part B Shell Commands Professional Training.
Unix Fundamentals CS 127. File navigation cd - change directory cd /var/log cd /etc/apache2 cd ~/Desktop ~ is a shortcut for the home directory.
ICE UNIX TUTORIAL. File System Commands cd – change directory cd – change directory ls – list contents ls – list contents rm – remove/delete rm – remove/delete.
 Simple UNIX commands  I/O techniques in C++  Solutions to Lab#0 problems  Solutions to Lab#1 problems 1.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Learning Unix/Linux Based on slides from: Eric Bishop.
GRID COMPUTING.
UNIX To do work for the class, you will be using the Unix operating system. Once connected to the system, you will be presented with a login screen. Once.
Tutorial of Unix Command & shell scriptS 5027
CS1010: Intro Workshop.
Development Environment Basics
Introduction to programming in java
Getting started with CentOS Linux
Commands Basic syntax of shell commands UNIX or shell commands have a basic structure command -options target command comes first (such as cd or ls) any.
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
Some Linux Commands.
The Command Prompt Commands are the way to “do things” in Unix
Lecture 3 More on editors: emacs and vi COP 3344 Introduction to UNIX.
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Practice #0: Introduction
CSE 374 Programming Concepts & Tools
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Web Programming Essentials:
Tutorial of Unix Command & shell scriptS 5027
Run Java file with Window cmd
Getting started with CentOS Linux
Command line.
Module 6 Working with Files and Directories
Java Tutotrial for [NLP-AI] 2
Lab 2: Terminal Basics.
Introduction to Linux and Code Editing
Presentation transcript:

CS1020 Week 3: 29th January 2015

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

Introduction Week 3

Introduction Lab TAs, please customise this slide. I’m ________ *SoC background* *interesting things about yourself?* *some basic expectations of your lab sessions* *email/preferred mode of contact for students* etc. Lab TAs, please customise this slide. Week 3

Lab Schedules (Tentative) Plan is tentative. Refer to module website for the most up-to-date plan. Assessments Week Date Type Topics 1 15th Jan Special Intro Workshop -- Take-home #0 Basic Java 3 29th Jan Take-home #1 OOP 4 5th Feb Sit-in #1 5 12th Feb Take-home #2 Array and OOP 6 19th Feb Chinese New Year (no lab) 7 5th Mar Sit-in #2 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 13 16TH Apr Revision [CS1020 Lecture 0: Course Admin]

UNIX environment UNIX commands Vim Week 3

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

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

Some VIM Commands Command Mode: (to get into command mode, press ESC key) :q! → Exit without saving changes :wq or ZZ → Save and Exit (don’t press ctrl-z!) dd → Delete the current line (ndd → delete n lines) yy → Yank (copy) the current line (nyy → yank n lines) p → paste after current line (P → paste before current line) :n or nG → Go to line number n (Example: 12G) w → go to next word (b → go back to previous word) /[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

Input/Output Practice Exercise 9 Week 3

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) Type 1: Number of Operations is specified Type 2: Read until Terminating Special Character Type 3: Read until End of File Week 3

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 } Week 3

Input: Type 2 Input Steps: Instantiate a Scanner object Read the First Input Loop until Terminating Special Character encountered 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

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(); } Week 3

Input: Type 3 Input Steps: Instantiate a Scanner object Loop until End of File Scanner sc = new Scanner(System.in); while (sc.hasNext()) { // Read Other Input } Week 3

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 } Week 3

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 } Week 3

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\n"); System.out.print("Output String Here"); Week 3

Take-home Lab #1 Exercise 1 – Temperature Conversion Week 3

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

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

Ex 1: Temperature Conversion (3/5) Sample run Enter temperature: 300.0 Choose source scale: 1. Celsius 2. Fahrenheit 3. Kelvin Enter your choice: 3 Choose destination scale: Enter your choice: 1 300.0 degrees Kelvin = 26.85 degrees Celsius Week 3

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/Decimal Format.html Week 3

Ex 1: Temperature Conversion (5/5) Create DecimalFormat object with appropriate pattern Week 3

Take-home Lab #1 Exercise 2 – Palindromes Week 3

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

Ex 2: Palindromes (2/5) Write a program to… Sample runs 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  Enter text: Race-car 7... Enter text: I like CS1020 Enter text: 12 noon Enter text: Madam, I'm Adam Enter text: (user pressed ctrl-d here) Number of palindromes = 3 Enter text: Don't nod Enter text: Never odd or even Enter text: Too bad – I hid a boot Enter text: Madam, in Eden I'm Adam Enter text: Oozy rat in a sanitary zoo Enter text: (user pressed ctrl-d here) Number of palindromes = 5        Week 3

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

Ex 2: Palindromes (4/5) Remove all non-letters Convert all letters to lower case Week 3

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

Take-home Lab #1 Exercise 3 – Overlapping Rectangles Week 3

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 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 Week 3

Ex 3: Overlapping Rectangles (2/5) Observe the overlapping area: X2,Y2 X1,Y1 X4,Y4 X3,Y3 X2,Y4 X3,Y1 Week 3

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 X2,Y2 X1,Y1 X4,Y4 X3,Y3 X2,Y4 X3,Y1 Week 3

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 X2,Y2 X1,Y1 X4,Y4 X3,Y3 X2,Y4 X3,Y1 Week 3

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 X2,Y2 X4,Y4 X3,Y3 X2,Y4 X3,Y1 X1,Y1 Week 3

END OF FILE