Homework for Calling Instance Methods
- Popcorn Hack #1
- Popcorn Hack #2
- MC Hacks
- Homework: Assignment Overview
- Requirements
- Homework Hack: Student Grade Tracker
- Sample Output
- Submission Instructions
- Grading Rubric
- Tips for Getting Started
- Tips for Success
- Bonus Challenges (Optional for Extra Credit)
Example Class:
// Suppose we have a class:
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.println(name + " says: Woof!");
}
}
// To use it:
Dog d = new Dog("Fido"); // create an instance
d.bark(); // call the instance method on d
Popcorn Hack #1
- Add another property to the dog class for
breed. - Add another method to the dog class that prints “My dog
nameis abreed!” - Create a new object of this class and call the method.
// Work by [Your Name Here]
public class Dog {
// ADD CODE HERE
}
// To use it:
// ADD CODE HERE
Example Class 2:
public class Counter {
private int count;
public void add(int x) {
count += x;
}
public int getCount() {
return count;
}
}
// Use it:
Counter c = new Counter();
c.add(5);
int val = c.getCount(); // returns 5
Popcorn Hack #2
-
Add on to the previous counter class and add three more methods for subtraction, multiplication, and division.
-
Call all five methods outside of the class.
// Work by [Your Name Here]
public class Counter {
private int count;
// ADD CODE HERE
}
// Use it:
// ADD CODE HERE
MC Hacks
Question 1
Consider the following class:
public class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
Which of the following code segments will compile without error:
A)
BankAccount.deposit(50.0);
double total = BankAccount.getBalance();
B)
BankAccount account = new BankAccount(100.0);
account.deposit(50.0);
double total = account.getBalance();
C)
BankAccount account = new BankAccount(100.0);
double total = account.deposit(50.0);
D)
BankAccount account;
account.deposit(50.0);
double total = account.getBalance();
E)
double total = getBalance();
BankAccount account = new BankAccount(total);
Question 2
What is printed as a result of executing the following code?
public class Rectangle {
private int length;
private int width;
public Rectangle(int l, int w) {
length = l;
width = w;
}
public int getArea() {
return length * width;
}
public void scale(int factor) {
length *= factor;
width *= factor;
}
}
Rectangle rect = new Rectangle(3, 4);
rect.scale(2);
System.out.println(rect.getArea());
A) 12
B) 24
C) 48
D) 96
E) Nothing is printed; the code does not compile
Question 3
Which of the following best describes when a NullPointerException will occur when calling an instance method?
A) When the method is declared as void
B) When the method is called on a reference variable that has not been initialized to an object
C) When the method’s parameters do not match the arguments provided
D) When the method is called from outside the class
E) When the method attempts to return a value but is declared as void
Question 4
Which of the following code segments will NOT compile?
public class Temperature {
private double celsius;
public Temperature(double c) {
celsius = c;
}
public double getFahrenheit() {
return celsius * 9.0 / 5.0 + 32;
}
public void setCelsius(double c) {
celsius = c;
}
}
A)
Temperature temp = new Temperature(0);
System.out.println(temp.getFahrenheit());
B)
Temperature temp = new Temperature(100);
temp.setCelsius(25);
C)
Temperature temp = new Temperature(20);
double f = temp.getFahrenheit();
D)
Temperature temp = new Temperature(15);
int result = temp.setCelsius(30);
E)
Temperature temp = new Temperature(0);
temp.setCelsius(100);
double f = temp.getFahrenheit();
Question 5
Consider the following class:
public class Book {
private String title;
private int pages;
public Book(String t, int p) {
title = t;
pages = p;
}
public String getTitle() {
return title;
}
public int getPages() {
return pages;
}
public void addPages(int additional) {
pages += additional;
}
}
Assume that the following code segment appears in a class other than Book:
Book novel = new Book("Java Basics", 200);
novel.addPages(50);
/* missing code */
Which of the following can replace /* missing code */ so that the value 250 is printed?
A) System.out.println(pages);
B) System.out.println(novel.pages);
C) System.out.println(Book.getPages());
D) System.out.println(novel.getPages());
E) System.out.println(getPages());
Homework: Assignment Overview
Due: 10/16/2025
Points: 1
In this assignment, you’ll demonstrate your understanding of instance methods by creating a Student class that tracks grades and calculates statistics. You’ll practice creating student objects, adding grades, calculating averages, and generating grade reports—all using instance methods that operate on each student’s individual data.
Requirements
Your program must include:
- A
Studentclass with at least 3 instance variables (name, total points, number of assignments) - A constructor that initializes the student’s name and sets initial values
- At least 2 void instance methods that modify the student’s grades (
addGrade,printReport) - At least 2 non-void instance methods that return calculated values (
getAverage,getLetterGrade) - A main method that creates at least 2 Student instances and demonstrates all methods working independently
Homework Hack: Student Grade Tracker
Create a Student class that manages a student’s grades throughout a semester.
Required Instance Variables:
String name- the student’s nameint totalPoints- sum of all grade points earnedint numAssignments- count of assignments completed
Required Instance Methods:
addGrade(int points)- void method that adds a grade and updates totalsgetAverage()- returns the student’s current average as a doublegetLetterGrade()- returns a String letter grade (A, B, C, D, F) based on averageprintReport()- void method that displays a formatted report of the student’s performance
Grading Scale:
- A: 90-100
- B: 80-89
- C: 70-79
- D: 60-69
- F: below 60
Sample Output
Here’s an example of what your output should look like:
=== Student Grade Tracker System ===
Creating student: Emma Rodriguez
Student created successfully!
--- Adding Grades for Emma ---
Adding grade: 95 points
Adding grade: 88 points
Adding grade: 92 points
Adding grade: 85 points
--- Emma's Grade Report ---
Student Name: Emma Rodriguez
Total Points: 360
Assignments Completed: 4
Current Average: 90.0
Letter Grade: A
Status: Excellent work!
========================================
Creating student: James Wilson
Student created successfully!
--- Adding Grades for James ---
Adding grade: 78 points
Adding grade: 82 points
Adding grade: 75 points
--- James's Grade Report ---
Student Name: James Wilson
Total Points: 235
Assignments Completed: 3
Current Average: 78.33
Letter Grade: C
Status: Keep working hard!
========================================
Final Summary:
Emma Rodriguez - Average: 90.0 (A)
James Wilson - Average: 78.33 (C)
Submission Instructions
Create a section on your personal blog documenting your completed Student Grade Tracker. Insert the rubric provided below and self-grade yourself with comments as to why you think you deserved that grade. Add screenshots/proof of your code as proof that you fulfilled all the rubric requirements in your blog along with a screenshot of the output (similar to the one provided above).
After updating your blog, please submit a personal blog link of your completed assignment to the google form link inserted in the ‘CSA Sign Up Sheet - Team Teach - Trimester 1’ spreadsheet.
Grading Rubric
| Criteria | Percent Weight | Description |
|---|---|---|
| Functionality | 40% | Program runs without errors, creates multiple Student objects, correctly adds grades, calculates averages, and assigns letter grades |
| Method Implementation | 30% | Includes all required methods (addGrade, getAverage, getLetterGrade, printReport) that properly access/modify instance variables |
| Code Quality | 20% | Clean code with meaningful variable names, proper constructor, and helpful comments explaining method purposes |
| Output & Presentation | 10% | Clear, well-formatted output showing multiple students with different grades and formatted reports |
Tips for Getting Started
- Review the Counter example from the lesson notes—your Student class will work similarly
- Start by creating the Student class with the three instance variables
- Write your constructor to initialize the name and set totalPoints and numAssignments to 0
- Implement
addGrade()first—it should add to totalPoints and increment numAssignments - Test each method before moving to the next one
- Remember: Test your hack multiple times and add updates in small increments in case you run into an error
Tips for Success
- Start simple - Get your constructor and
addGrade()working first before tackling calculations - Watch for division - When calculating average, make sure to cast to double:
(double)totalPoints / numAssignments - Test with multiple students - Create at least 2 Student objects with different grades to show independence
- Comment your code - Explain what each method does, especially your letter grade logic
Bonus Challenges (Optional for Extra Credit)
Want to go above and beyond? Try adding:
- A
getHighestGrade()method that tracks and returns the best individual grade (you’ll need to store individual grades, not just the total) - A
getLowestGrade()method for the lowest individual grade - A
dropLowestGrade()method that removes the lowest grade from calculations - A
compareTo(Student other)method that compares two students’ averages - Weighted grades—allow some assignments to be worth more points than others
- A
reset()method that clears all grades and starts fresh - Error checking to prevent negative grades or invalid input
Good luck!