Announcements - PowerPoint PPT Presentation

About This Presentation
Title:

Announcements

Description:

Announcements – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 24
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Newsgroup cornell.class.cs100
  • Assignment W1 due tomorrow at the beginning of
    class
  • Assignment P1 can be picked up at the end of
    class

2
Todays Topics
  • Review
  • Classes
  • Instance of a Class (an object)
  • Fields
  • Declaration of class variables
  • The new operator
  • Class methods

3
Review
  • Whats the difference between a variable and a
    constant?
  • When might you want to use an if statement?
  • What is the purpose of block statements?

4
Review Example 1
  • final int THRESHOLD 65
  • System.out.println(Enter temperature )
  • int temperature Integer.parseInt(stdin.readLine(
    ))
  • if (temperature lt THRESHOLD)
  • System.out.println(Its cold.\n)

5
Review Example 2
  • final boolean FOURLEGS true
  • final boolean BARKS true
  • final boolean MEOWS false
  • if (FOURLEGS false)
  • System.out.println(not four-legged)
  • else if (BARKS)
  • System.out.println(4 legs, probably a dog)
  • else if (MEOWS)
  • System.out.println(4 legs, probably a cat)
  • else
  • System.out.println(4 legs, no idea what.)

6
Rewrite of Previous example
  • final boolean FOURLEGS true
  • final boolean BARKS true
  • final boolean MEOWS false
  • if (FOURLEGS false)
  • System.out.println(not four-legged)
  • if ( FOURLEGS BARKS (MEOWS false))
  • System.out.println(4 legs, probably a dog)
  • if (FOURLEGS MEOWS (BARKS false))
  • System.out.println(4 legs, probably a cat)
  • if (FOURLEGS (MEOWS false) (BARKS
    false))
  • System.out.println(4 legs, no idea what.)

7
Review Formatting Output
  • What does the following produce?
  • \tCourse\tStudents\n\tCS100\tMany
  • Course Students
  • CS100 Many

8
Why Classes?
  • Recall a variable is a named box into which one
    can place values
  • Suppose we want boxes (variables) that contain
    more than one value?
  • For exampleCoordinate c

x
-532
x -532
y 25
9
More examples
  • Coordinate c1
  • Coordinate c3
  • Coordinate is a class. Its like a type, except
    that you (the programmer) define it.
  • c1 and c3 are instances of the class we call
    them objects
  • c1.x, c1.y, c3.x, c3.y are fields of objects c1
    and c3 respectively.

x 3 y 4
10
Declaring a class in Java
  • public class Coordinate
  • public int x
  • public int y
  • keywords public and class
  • name of the class -- naming convention
    capitalize all words in class names, e.g.
    StringBuffer
  • body of the class (within and )
  • field declarations (normal type and class
    declarations)
  • methods and constructors (later in the course)

11
Type declarations -- Class declarations
  • int x// x
  • Coordinate w// w
  • null is a Java constant -- it denotes the absence
    of an object
  • Just as x hasnt been assigned (so defaults to
    0), w hasnt been assigned, it has only been
    declared

0
null
12
Take a breath
  • So, objects (like c, as declared in Coordinate c)
    have a type -- the type is the objects class
    (here Coordinate)
  • Classes have methods and fields
  • fields are data variables associated with a class
    and its objects
  • methods contain the executable code of a class

13
Creating a class instance
  • To create a new instance of a class, use the
    operator new
  • Coordinate w// w
  • w new Coordinate()// w

null
x ? y ?
14
Referencing fields
x 3 y 5
// w
6
// x
x w.x
x 3 y 5
// w
3
// x
15
Assigning to fields
// w
w.x 7
// w
16
Instance1 Instance2 ??
  • Assigning one instance to another creates aliases
    -- 2 names refer to the same object.

// w
// h
h w
// w
// h
17
What if you want to copy?
// w
// h
  • h.x w.x
  • h.y w.y

// w
x 3 y 5
// h
18
Methods of Classes
  • Classes can have methods that operate on fields
    of the class

public class Coordinate public int x public
int y // Set field x to pp public void
setX(int p) x pp // Set field y to
q public void setY(int q) y q //
Return the sum of the squares of the
fields public int sumSquares() return xx
yy
19
Return types of methods
  • // Return the sum of the squares of the fields
  • public int sumSquares()
  • return xx yy
  • sumSquares should return an integer to wherever
    it is called from

20
Returns
  • return ltexpressiongt
  • terminates execution of the method in which it
    appears
  • returns value of ltexpressiongt to the place of the
    call to the method
  • NB ltexpressiongt must match the return type in
    the method header
  • If the type is not void, Java insists that a
    return statement exist
  • Can also have plain return with no expression
    if void

21
Method calls
  • Suppose we want to call some of the methods of
    class Coordinate
  • Use object name followed by a dot . followed by
    method name
  • c.setX(3) // x field of c becomes 9
  • a c.sumSquares() // a becomes . . . 81?

22
Example
  • c.setX(3)
  • c.setY(2)
  • // Store 85 in ss c.sumSquares()

x 9 y 2
// c

23
Another example
  • d new Coordinate()
  • d.setX(c.sumSquares())
  • d.setY(c.x)

x 7225 y 9
// d
Write a Comment
User Comments (0)
About PowerShow.com