Lesson 5 Homework - Writing Classes
FRQ homework assignment
public class StepTracker {
private int lowerBound;
private int steps;
private int totalDays;
private int activeDays;
public StepTracker(int minSteps) {
lowerBound = minSteps;
steps = 0;
totalDays = 0;
activeDays = 0;
}
public void addDailySteps(int newSteps) {
steps += newSteps;
totalDays += 1;
if (newSteps >= lowerBound) {
activeDays += 1;
}
}
public int activeDays(){
return activeDays;
}
public double averageSteps() {
if (totalDays == 0) {
return (double) totalDays;
}
else {
return (double) steps / totalDays;
}
}
public static void main(String[] args){
StepTracker tr = new StepTracker(10000);
System.out.println(tr.activeDays());
System.out.println(tr.averageSteps());
tr.addDailySteps(9000);
System.out.println(tr.averageSteps());
tr.addDailySteps(23000);
System.out.println(tr.activeDays());
}
}
StepTracker.main(null);
Notes
- Classes are blueprints to create objects and define attributes
- Instances: attributes, constructors, methods, objects
- Public classes: no restricted access - constructors
- Private classes: restricted access - instance variables
- Accessor method (getter): allows other objects to obtain values of instance variables or static variables
- Non void methods return a single value