COMP 110 More about classes - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 More about classes

Description:

Class: a definition of a kind of object ... Confined to the class ... Confined to the method. Can only be used inside the method that declares the variable ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 17
Provided by: luv
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | classes | confined | more

less

Transcript and Presenter's Notes

Title: COMP 110 More about classes


1
COMP 110More about classes
  • Luv Kohli
  • October 3, 2008
  • MWF 2-250 pm
  • Sitterson 014

2
Announcements
  • Midterm exam currently scheduled for Monday,
    October 20

3
Change exam date?
  • Options
  • Monday, October 13
  • Wednesday, October 22
  • Keep it on Monday, October 20

4
Questions?
5
Classes, Objects, and Methods
  • Class a definition of a kind of object
  • Object an instance of a class
  • Contains instance variables (data) and methods
  • Methods
  • Methods that return a value
  • Methods that return nothing

6
Today in COMP 110
  • Local variables and instance variables
  • Brief introduction to methods with parameters
  • In-class exercise

7
Local/Instance variables
  • Instance variables
  • Declared in a class
  • Confined to the class
  • Can be used anywhere in the class that declares
    the variable, including inside the class methods
  • Local variables
  • Declared in a method
  • Confined to the method
  • Can only be used inside the method that declares
    the variable

8
Simple example
  • public class Student
  • public String name
  • public int classYear
  • // ...
  • public void printInfo()
  • String info name classYear
  • System.out.println(info)
  • public void increaseYear()
  • classYear
  • public void decreaseYear()
  • classYear and name are instance variables
  • can be used in any method in this class
  • info is a local variable declared inside method
    printInfo()
  • can only be used inside method printInfo()

9
Simple example
  • public class Student
  • public String name
  • public int classYear
  • // ...
  • public void printInfo()
  • String info name classYear
  • System.out.println(info)
  • public void increaseYear()
  • classYear
  • info My info string // ERROR!!!
  • public void decreaseYear()

The compiler will not recognize the variable info
inside of method increaseYear()
10
More about local variables
  • public static void main(String args)
  • Student jack new Student()
  • jack.name Jack Smith
  • jack.major Computer Science
  • String info Hello there!
  • System.out.println(info)
  • System.out.println(jack.name is majoring
    in jack.major)
  • Student apu new Student()
  • apu.name Apu Nahasapeemapetilon
  • apu.major Biology
  • System.out.println(apu.name is majoring
    in apu.major)

Variable info in main method not affected by
variable info in printInfo method in class Student
11
Methods with parameters
  • Compute the square of this number
  • 5
  • 10
  • 7
  • I could give you any number, and you could tell
    me the square of it
  • We can do the same thing with methods

12
Methods with parameters
  • Parameters are used to hold the value that you
    pass to the method
  • Parameters can be used as (local) variables
    inside the method
  • public int square(int number)
  • return number number

Parameters go inside parentheses of method header
13
Calling a method with parameters
  • public class Student
  • public String name
  • public int classYear
  • // ...
  • public void setName(String studentName)
  • name studentName
  • public void setClassYear(int year)
  • classYear year

14
Calling a method with parameters
  • public static void main(String args)
  • Student jack new Student()
  • jack.setName(Jack Smith)
  • jack.setClassYear(3)
  • More details next week

Arguments
15
In-class exercise
16
Monday
  • Even more about classes

16
Write a Comment
User Comments (0)
About PowerShow.com