cs1020
Download
Skip this Video
Download Presentation
CS1020

Loading in 2 Seconds...

play fullscreen
1 / 28

CS1020 - PowerPoint PPT Presentation


  • 211 Views
  • Uploaded on

CS1020. Week 3: 30 th January 2014. Contents. Part 1: UNIX Environment, vim, Input/Output Part 2: Discussion on Lab # 0 Part 3: Discussion on Lab #1. Part 1. UNIX environment vim Input/output. Important UNIX Commands (1/2). Change Directory ( cd ) cd [ directory name ]

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS1020' - osric


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs1020

CS1020

Week 3: 30thJanuary 2014

contents
Contents
  • Part 1: UNIX Environment, vim, Input/Output
  • Part 2: Discussion on Lab #0
  • Part 3: Discussion on Lab #1

Week 3

part 1
Part 1

UNIX environment

vim

Input/output

Week 3

important unix commands 1 2
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
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

important vim commands
Important VIM Commands
  • Command Mode: (to get into command mode, press ESC key)
  • dd or :d → Delete the current line
  • :q! → Exit without saving changes
  • :wq or ZZ → Save and Exit
  • /[pattern] → Search for the pattern
    • n → Search next in the same direction
    • N → Search next in the opposite direction
  • See “Editor” in CS1020 Online web page:
    • http://www.comp.nus.edu.sg/~cs1020/2_resources/online.html

Week 3

input reading standard input
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, Doubleexample: 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
Input: Type 1 Input (Example)

public static void main(String[] args) {

// … Code Section Omitted

// … Other Initialization

Scanner sc = new Scanner(System.in);

intnumOps = sc.nextInt();

for (int i=0; i<numOps; i++) {

// Read Other Inputs

}

// … Code Section Omitted

}

Week 3

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

input type 3 input
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
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

input hybrid input
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()) {

intnumOps = sc.nextInt();

for (inti=0; i<numOps; i++) {

// Read Other Inputs

}

}

// … Code Section Omitted

}

Week 3

output printing standard output
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

part 2
Part 2

Discussion on Take-home Lab #0

Week 3

exercise 1 area of triangle

Exercise 1: Area of Triangle

Objectives:

Input/output in Java

Using Math class

Writing user-defined methods

Methods:

Determine whether it is a valid triangle.

Compute area of the triangle if it is a valid triangle.

Main method

Week 3

exercise 1 1 3
Exercise 1 (1/3)
  • Method 1: Determine whether it is a valid triangle
  • Read the lengths of the sides as inputs.
  • For any two sides, the sum of their lengths must be greater than the third side.
  • Return a boolean
  • // if a, b, c represent lengths of the sides of a triangle,
  • // return true, otherwise return false.
  • public static booleanvalidTriangle(double a,
  • double b, double c) {
  • return (a + b > c) && (a + c > b) && (b + c > a);
  • }

Week 3

Week 3

exercise 1 2 3
Exercise 1 (2/3)
  • Method 2: Compute area of the triangle
  • Read the lengths of the sides as inputs.
  • Use the the Heron’s formula to compute the area
  • Return a double value as the area
  • // To return area of a triangle of sides a, b, and c
  • // Using Heron\'s formula
  • public static double area(double a, double b, double c) { double p = (a + b + c)/2;
  • return Math.sqrt(p * (p-a) * (p-b) * (p-c));
  • }

Week 3

exercise 1 3 3
Exercise 1 (3/3)
  • Main method
  • Instantiate a Scanner object and read the inputs
  • If the triangle is valid, compute the area and print it out.
  • Otherwise print “Invalid triangle!”.
  • public static void main(String[] args) {
  • double a, b, c; // lengths of sides of triangle
  • Scanner sc = new Scanner(System.in);
  • System.out.print("Enter 3 lengths: ");
  • a = sc.nextDouble();
  • b = sc.nextDouble();
  • c = sc.nextDouble();
  • if (validTriangle(a, b, c)) System.out.printf("Area = %.2f\n", area(a, b, c));
  • else
  • System.out.println("Invalid triangle!");
  • }

Week 3

exercise 2 date conversion 1 3
Exercise 2: Date Conversion (1/3)
  • Date format:

UK MM DD, YYYY

US DD MM YYYY

  • Convert from UK dateto US date
  • Not allowed to use the date formatter class

Week 3

exercise 2 date conversion 2 3
Exercise 2: Date Conversion (2/3)
  • How to transfer the two date?
  • eliminate the comma
  • use substring/split to find the month and day
  • swap the day with month

Week 3

exercise 2 date conversion 3 3
Exercise 2: Date Conversion (3/3)
  • Leap year?
  • It is divisible by 400; or
  • It is divisible by 4 but not by 100

Year%400==0 || (year%4==0 && year%100!=0)

  • String to Integer:

int year = Integer.valueOf(yearStr);

Week 3

part 3
Part 3

Discussion on Take-home Lab #1

Week 3

ex 1 overlapping rectangles 1 4
Ex 1: Overlapping Rectangles (1/4)
  • Observe the overlapping area:

X2,Y2

X2,Y4

X4,Y4

X3,Y1

X1,Y1

X3,Y3

Week 3

ex 1 overlapping rectangles 2 4
Ex 1: Overlapping Rectangles (2/4)
  • The overlapping rectangle bottom left point’s x coordinate and y coordinate are the maximumof the other two rectangles bottom left points’ x and y
  • MAX(X1,X3)  X3; MAX(Y1,Y3)  Y1

X2,Y2

X2,Y4

X4,Y4

X3,Y1

X1,Y1

X3,Y3

Week 3

ex 1 overlapping rectangles 3 4
Ex 1: Overlapping Rectangles (3/4)
  • The overlapping rectangle top right point’s x coordinate and y coordinate are the minimumof the other two rectangles top right points’ x and y
  • MIN(X2,X4)  X2; MIN(Y2,Y4)  Y4

X2,Y2

X2,Y4

X4,Y4

X3,Y1

X1,Y1

X3,Y3

Week 3

ex 1 overlapping rectangles 4 4
Ex 1: Overlapping Rectangles (4/4)
  • But….
  • The two points given may not lie in the same direction
  • You need to transfer to the same direction

X2,Y2

X2,Y4

X4,Y4

X1,Y1

X3,Y1

X3,Y3

Week 3