Docsity

CS 357: Data Structures Class Notes - Week 1, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS 357: Data Structures
Class 03
January 14, 2001
Fall 2001 CS 357 Class Notes Page 2
Announcements
• Finish chapter 1 for Wednesday, sections 1.4-
1.8 (1.9 and 1.10 couldn’t hurt, but are not
required for Wednesday).
• No class on Monday, January 21 in
observance of Martin Luther King, Jr.’s
birthday.
• Discussion group to be up this afternoon.
• Homework due in class on Friday.
Fall 2001 CS 357 Class Notes Page 3
Basics – Primitives
• boolean true or false
• char 16-bit Unicode character
• byte 8-bit signed two’s complement integer
• short 16-bit signed two’s complement integer
• int 32-bit signed two’s complement integer
• long 64-bit signed two’s complement integer
• float 32-bit floating-point number
• double 64-bit floating-point number
Two’s complement?
See
http://www.duke.edu/~twf/cps104/twoscomp.html
pf3
pf4
pf5

Partial preview of the text

Download CS 357: Data Structures Class Notes - Week 1 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 357: Data Structures

Class 03

January 14, 2001

Fall 2001 CS 357 Class Notes (^) Page 2

Announcements

  • Finish chapter 1 for Wednesday, sections 1.4-

1.8 (1.9 and 1.10 couldn’t hurt, but are not

required for Wednesday).

  • No class on Monday, January 21 in

observance of Martin Luther King, Jr.’s

birthday.

  • Discussion group to be up this afternoon.
  • Homework due in class on Friday.

Fall 2001 CS 357 Class Notes Page 3

Basics – Primitives

  • boolean true or false
  • char 16-bit Unicode character
  • byte 8-bit signed two’s complement integer
  • short 16-bit signed two’s complement integer
  • int 32-bit signed two’s complement integer
  • long 64-bit signed two’s complement integer
  • float 32-bit floating-point number
  • double 64-bit floating-point number

Two’s complement? See http://www.duke.edu/~twf/cps104/twoscomp.html

Fall 2001 CS 357 Class Notes (^) Page 4

Basics – Literals

  • Any “constant” value that can be used in an

assignment or other expression.

  • Boolean: true and false
  • Integer: 38, 46, -32 (32-bit by default)
    • Long: 38L, 46L, -32L
  • Double: 3.1415, 6.322 (64-bit by default)
    • Float: 3.1415F, 6.322f
    • Exponentials allowed: 3.14E2 or 3.14E2f
  • Strings: Typically multiple characters in double quotes
    • “quick brown fox”
  • Object: null object reference

Fall 2001 CS 357 Class Notes (^) Page 5

Basics – Literals (cont.)

  • Character: typically single character in single quotes - ‘a’, ‘b’, ‘c’ - Special: - ‘\n’ (newline) - ‘\b’ (backspace) - ‘\f’ (form feed) - ‘\ ’ ’ (single quote) - ‘\t’ (tab) - ‘\r’ (return) - ‘\’ (backslash) - ‘\ ” ’ (double quote)

Fall 2001 CS 357 Class Notes Page 6

Basics – Operators

  • = (Assignment)
  • +,-,*,/,% (Arithmetic Binary)
  • ++,-- (Arithmetic Unary)
  • <,<=,==,!=,>=,>,&&,|| (Logical Binary) -! (Logical Unary)
  • &,|,^,<<,>>,>>> (Bitwise Binary)
  • ~ (Bitwise Unary)
    • (String concatenation)

Fall 2001 CS 357 Class Notes (^) Page 10

Basics – Casting (cont.)

  • Implicit int iresult, i=3; double dresult,d=3.2; dresult=i/d; // Computes dresult = 0. iresult=i/d; /* Does not compile. Gives a “possible loss of precision” error. Needs an explicit cast of either d or the result of i/d. */ float y=4.5; // Also gives an error. Why?
  • Implicit/Explicit with String Objects
    • Cannot explicitly cast: String t = (String)5;
    • Must do something like: String s = “X=“ + 34; String y = Integer.toString(99); String z = s; z += y;

Fall 2001 CS 357 Class Notes (^) Page 11

Number Objects

  • Byte
  • Integer Integer n; n = new Integer(35);
  • Float
  • Short
  • Long
  • Double

Fall 2001 CS 357 Class Notes Page 12

Basics – Arrays

< type >[] < name > [={< val0 >,< val1 >,…,< valN-1 >}];

  • Numbered 0..N-
  • N obtained with .length
  • If no initialization, must use new operator.
    • new operator allocates space & creates objects.

Fall 2001 CS 357 Class Notes (^) Page 13

Quick Quiz

Code Blocks

  1. int[] x = {1,2,3,4};
  2. int[] x = {}; x[0] = 3;
  3. int[] x = new int [0]; x[0]=4;
  4. int[] x; x = {1,2,3,4};
  5. int[] x; x = new int [4]; x = {1,2,3,4};

Describe what happens when each of the code blocks to the right are inserted into the code below.

class foo { public static void main(String[ ] args) { // Insert block here System.out.println (x[0]); } }

Fall 2001 CS 357 Class Notes (^) Page 14

Arrays (cont.)

public class ArrayOfArraysDemo { public static void main(String[] args) { String[ ][ ] cartoons = { { "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" }, { "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" }, { “S.D. Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } }; for (int i = 0; i < cartoons.length; i++) { System.out.print(cartoons[i][0] + ": "); for (int j = 1; j < cartoons[i].length; j++) { System.out.print(cartoons[i][j] + " "); } System.out.println(); } } }

Fall 2001 CS 357 Class Notes Page 15

Quick Quiz

  1. Rewrite each of the following to achieve the intended result: a) String s=49.5; b) String t=“Pi = “ + (String) 3.1415; c) String u=22;
  2. What is the result of the following code? What changes must be made to have the intended result? public static void Link (Node fCurrent, Node fNext) { Node k = new Node(fCurrent.data,null); fCurrent = k; k.next = fNext; }