Recitation - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Recitation

Description:

Solution to Assignment 3. A brief review of the mid-term. 2. Assign.3. UML ... Assign.3. Class Name: Statistics - fieldGoalsMade (int) fieldGoalAttempts (int) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 28
Provided by: qizh
Category:

less

Transcript and Presenter's Notes

Title: Recitation


1
Recitation
Qi ZhangOct 22
COMP 110 Introduction to Programming
2
Todays Lecture
  • Solution to Assignment 3
  • A brief review of the mid-term

2
3
Assign.3
  • Class Name Statistics
  • fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double
  • UML

3
4
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
UNCStats
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

5
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
UNCStats
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

6
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

7
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
692
UNCStats
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

8
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • Int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

9
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

398
10
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

398
11
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

398
12
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

398
13
Assign.3
  • Class Name Statistics
  • - fieldGoalsMade (int)
  • fieldGoalAttempts (int)
  • freeThrowsMade (int)
  • freeThrowAttempts (int)
  • - threePintersMade (int)
  • - threePointAttempts (int)
  • setFieldGoalsMade(int fieldMade)void
  • setFieldGoalAttempts(int fieldAttempts)void
  • setFreeThrowsMade(int freeMade)void
  • setFreeThrowAttempts(int freeAttempts)void
  • set3PointersMade(int threeMade)void
  • set3PointersAttempt(int threeAttempts)void
  • getTotalPoints() int
  • getFieldGoalPercent() double
  • getFreeThrowPercent() double

841
1753
524
UNCStats
692
145
  • unc.setFieldGoalsMade(841)
  • unc.setFieldGoalAttempts(1753)
  • unc.setFreeThrowsMade(524)
  • unc.setFreeThrowAttempts(692)
  • unc.set3PointersMade(145)
  • unc.set3PointAttempts(398)
  • int points unc.getTotalPoints()
  • double field unc.getFieldGoalPercent()
  • double free unc.getFreeThrowPercent()
  • double three unc.get3PointPercent()

398
14
Defining Data Members
  • private int fieldGoalsMade
  • private int fieldGoalAttempts
  • private int freeThrowsMade
  • private int freeThrowAttempts
  • private int threePintersMade
  • private int threePointAttempts

Access modifier
14
15
Defining Data Members
  • private int fieldGoalsMade
  • private int fieldGoalAttempts
  • private int freeThrowsMade
  • private int freeThrowAttempts
  • private int threePintersMade
  • private int threePointAttempts

Access modifier
type
15
16
Defining Data Members
  • private int fieldGoalsMade
  • private int fieldGoalAttempts
  • private int freeThrowsMade
  • private int freeThrowAttempts
  • private int threePintersMade
  • private int threePointAttempts

Access modifier
variable names
type
16
17
Defining Data Members
  • private int fieldGoalsMade
  • private int fieldGoalAttempts
  • private int freeThrowsMade
  • private int freeThrowAttempts
  • private int threePintersMade
  • private int threePointAttempts

not double !!!!!
Do typecasting later!! double fieldGoalPercent
(double) fieldGoalsMade / (double)
fieldGoalsAttempts
17
18
Private or Public Instance Variables?
  • Public any other class or program can directly
    access and change the instance variables
  • unc.fieldGoalsMade
    1000000
  • Private the instance varaibles name is not
    accessible outside of the class definition.
  • unc.fieldGoalsMade 1000000
    illegal
  • Normally, we define the instance variables as
    private
  • force the programmer who uses the class to access
    the instance variables only via methods.
  • Define accessor methods and mutator methods for
    accessing the private variables
  • unc.setFieldGoalsMade(1000000)

18
19
Other variables?
  • int totalPoints
  • double fieldGoalPercent
  • double freeGoalPercent
  • double threePointerPercent
  • We do not need to define them as instance
    variables
  • They should be defined as local variables in
    the methods where we compute them

19
20
Mutator Methods
  • public void setFieldGoalsMade(int fieldMade)
  • public void setFieldGoalAttempts(int
    fieldAttempts)
  • public void setFreeThrowsMade(int freeMade)
  • public void setFreeThrowAttempts(int
    freeAttempts)
  • public void set3PointersMade(int threeMade)
  • public void set3PointersAttempt(int
    threeAttempts)

Public! Void! Requires parameter!
20
21
Mutator Methods
841
  • public void setFieldGoalsMade(int fieldMade)
  • fieldGoalsMade fieldMade

parameter
Instance variable
21
22
Mutator Methods
  • public void setFieldGoalsMade(int fieldGoalsMade)
  • this.fieldGoalsMade fieldGoalsMade

parameter
Instance variable
Current object
Parameter and instance variable are having the
same name
22
23
Accessor Methods
  • Public int getTotalPoints()
  • public double getFieldGoalPercent()
  • public double getFreeThrowPercent()
  • public double get3PointPercent()

Public! Has return value! Never void! Does not
require parameter!
23
24
Accessor Methods
  • public double getFieldGoalPercent()
    return ((double)fieldGoalsMade/(double)fieldGoalAt
    tempts)100

24
25
Accessor Methods
  • public double getFieldGoalPercent()
    return ((double)fieldGoalsMade/(double)fieldGoalAt
    tempts)100

Instance variable (int)
typecasting
25
26
Accessor Methods
  • public double getFieldGoalPercent()
    return ((double)fieldGoalsMade/(double)fieldGoalAt
    tempts)100
  • Define local variable fieldGoalsPercent
  • public double getFieldGoalPercent()
    double fieldGoalsPercent
  • (double)fieldGoalsMade/
    (double)fieldGoalAttempts
  • return fieldGoalsPercent100

26
27
Some other problems
  • Meaningful names!!
  • fieldGoalsMade Good
  • fgm Bad
  • Do not need to import any packages
  • Better to define each instance variable in a
    separate line
  • getMisses() ???

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