CS 112 Introduction to Programming - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CS 112 Introduction to Programming

Description:

Location: DL 220 7-9 PM. Office hours today. 2-3pm (AKW 202) ... loop-and-a-half idiom. 6. Classes and Methods. Using a class to create objects ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 10
Provided by: Richar9
Category:

less

Transcript and Presenter's Notes

Title: CS 112 Introduction to Programming


1
CS 112 Introduction to Programming
  • Lecture 22
  • Review
  • Http//zoo.cs.yale.edu/classes/cs112/

2
Admin.
  • Midterm one this evening
  • Location DL 220 7-9 PM
  • Office hours today
  • 2-3pm (AKW 202)
  • Another time slot before the exam to be posted
    on the class page

3
C Data Types
count
t4
  • Value (simple) type
  • int, char, double, etcA value type variable
    stores a value of the type of the variableint x
    45double y 45.12
  • Reference type
  • Class or arrayA reference variable stores a
    reference to an object or arrayA reference is a
    location in computers memory where the object
    or array itself is storedTime3 t1t1 new
    Time3(11, 45, 59)
  • Time3 t2 t1Time3 t4 new Time3(1, 2,
    3)t2.SetTime(2, 2, 2)Time3.Count()int a1
    1, 2, 3int a2 a1

x
45
y
45.12
t1
t2
a1
a2
4
Simple Data Types
  • range and precision of data types
  • know what data type to use, e.g., assignment 1
    problem 1
  • input/output of simple data types Parse()
    methods
  • arithmetic operations
  • Result depend on type
  •  4/5 0 while 4.0/5 0.8 and (double)4/5 0.8
    4/(double)5 0.8 int x (int)Math.Round(
    (double)4/ 5 )
  • data conversion
  • implicit conversion, cast, see examples of
    lecture 5
  • boolean operations
  • operator precedence
  • a summary of all operators can be found at Fig.
    7.2 or slides of lecture 18.

5
Flow of Control
  • conditional statements
  • if and if/else statements
  • C ternary conditional operator
  • switch statement
  • loop statements
  • while, do/while, and for statements
  • break/continue statements
  • writing, choosing, and understanding loop
    programs, e.g.
  • loop-and-a-half idiom

6
Classes and Methods
  • Using a class to create objects
  • an object has state and behavior
  • A class definition includes variable and method
    definitions
  • A variable or method can be static or non-static
  • A non-static variable also called an instance
    variable
  • A non-static method is also called an instance
    method
  • See StaticTest and other samples.
  • A variable or method can be public or private
    (default is private)
  • enforce encapsulation

7
Defining a Method
  • Overloading and signature
  • Compiler try the best match
  • If multiple matches, find the best match
  • If no direct match, if implicit conversion is
    allowed to match a method, use it.
  • Variables that can be used in a method
  • A static method
  • class scope static variables
  • Local variables (we also consider formal
    arguments as local variables)
  • An instance method
  • Class scope variables (instance or static)
  • Local variables

8
Calling a Method
  • Each time a method is called, the actual
    arguments in the invocation are copied into the
    formal arguments
  • If a value type, then it is the value that is
    copied
  • If a reference type, then it is the reference
    that is copied
  • The formal argument and the actual argument are
    different variables, with different memory
    locations
  • However, if an argument of a method is ref (or
    out), then the formal argument and the actual
    argument are aliases of each other, i.e. they
    share the same memory cell

9
Arrays
  • array declaration and initialization
  • Some examples of arrays command line, string
  • some typical designs of using arrays, e.g., using
    array elements as counters
Write a Comment
User Comments (0)
About PowerShow.com