Lesson 1.14
CSA Unit 1.14
Popcorn Hack #1
// Work by [Your Name Here]
public class Dog {
private String name;
private String breed;
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
public void introduce() {
System.out.println("My dog " + name + " is a " + breed + "!");
}
}
// To use it:
public class DogDemo {
public static void main(String[] args) {
Dog d = new Dog("Buddy", "Golden Retriever");
d.introduce();
}
}
My dog Buddy is a Golden Retriever!
Popcorn Hack 2
public class Counter {
private int count;
public void add(int x) {
count += x;
}
public void subtract(int x) {
count -= x;
}
public void multiply(int x) {
count *= x;
}
public void divide(int x) {
if (x != 0) {
count /= x;
} else {
System.out.println("Cannot divide by zero!");
}
}
public int getCount() {
return count;
}
public static void main(String[] args) {
Counter c = new Counter();
c.add(10); // count = 10
c.subtract(2); // count = 8
c.multiply(3); // count = 24
c.divide(4); // count = 6
System.out.println("Final count: " + c.getCount());
}
}
Counter value: 6
Homework Hack
import java.util.ArrayList;
import java.util.Collections;
public class StudentGradeTracker {
private String name;
private ArrayList<Integer> grades;
public StudentGradeTracker(String name) {
this.name = name;
this.grades = new ArrayList<>();
}
// Adds a grade with error checking
public void addGrade(int points) {
if (points < 0 || points > 100) {
System.out.println("Error: Invalid grade. Must be between 0 and 100.");
return;
}
grades.add(points);
System.out.println("Adding grade: " + points + " points");
}
public double getAverage() {
if (grades.isEmpty()) return 0.0;
int total = 0;
for (int g : grades) total += g;
return (double) total / grades.size();
}
public String getLetterGrade() {
double avg = getAverage();
if (avg >= 90) return "A";
else if (avg >= 80) return "B";
else if (avg >= 70) return "C";
else if (avg >= 60) return "D";
else return "F";
}
public int getHighestGrade() {
if (grades.isEmpty()) return 0;
return Collections.max(grades);
}
public int getLowestGrade() {
if (grades.isEmpty()) return 0;
return Collections.min(grades);
}
public void dropLowestGrade() {
if (!grades.isEmpty()) {
int lowest = getLowestGrade();
grades.remove(Integer.valueOf(lowest));
System.out.println("Dropped lowest grade: " + lowest);
}
}
public void reset() {
grades.clear();
System.out.println("All grades have been reset for " + name + ".");
}
public int compareTo(StudentGradeTracker other) {
return Double.compare(this.getAverage(), other.getAverage());
}
public void printReport() {
System.out.println("Student Name: " + name);
System.out.println("Grades: " + grades);
System.out.printf("Average: %.2f\n", getAverage());
System.out.println("Highest Grade: " + getHighestGrade());
System.out.println("Lowest Grade: " + getLowestGrade());
System.out.println("Letter Grade: " + getLetterGrade());
System.out.println();
}
public static void main(String[] args) {
System.out.println("=== Student Grade Tracker (Bonus Version) ===\n");
StudentGradeTracker emma = new StudentGradeTracker("Emma Rodriguez");
emma.addGrade(95);
emma.addGrade(88);
emma.addGrade(92);
emma.addGrade(85);
emma.printReport();
emma.dropLowestGrade();
System.out.println("--- After Dropping Lowest Grade ---");
emma.printReport();
StudentGradeTracker james = new StudentGradeTracker("James Wilson");
james.addGrade(78);
james.addGrade(82);
james.addGrade(75);
james.printReport();
int comparison = emma.compareTo(james);
if (comparison > 0)
System.out.println(emma.name + " has a higher average than " + james.name);
else if (comparison < 0)
System.out.println(james.name + " has a higher average than " + emma.name);
else
System.out.println("Both students have the same average!");
emma.reset();
emma.printReport();
}
}
=== Student Grade Tracker (Bonus Version) ===
Adding grade: 95 points
Adding grade: 88 points
Adding grade: 92 points
Adding grade: 85 points
Student Name: Emma Rodriguez
Grades: [95, 88, 92, 85]
Average: 90.00
Highest Grade: 95
Lowest Grade: 85
Letter Grade: A
Dropped lowest grade: 85
--- After Dropping Lowest Grade ---
Student Name: Emma Rodriguez
Grades: [95, 88, 92]
Average: 91.67
Highest Grade: 95
Lowest Grade: 88
Letter Grade: A
Adding grade: 78 points
Adding grade: 82 points
Adding grade: 75 points
Student Name: James Wilson
Grades: [78, 82, 75]
Average: 78.33
Highest Grade: 82
Lowest Grade: 75
Letter Grade: C
Emma Rodriguez has a higher average than James Wilson
All grades have been reset for Emma Rodriguez.
Student Name: Emma Rodriguez
Grades: []
Average: 0.00
Highest Grade: 0
Lowest Grade: 0
Letter Grade: F
Self-Grade
| Category | Score | Comment |
|---|---|---|
| Functionality | 39/40 | My program runs perfectly and calculates averages and grades correctly for both students. |
| Method Implementation | 29/30 | I implemented all required methods and used instance variables properly. |
| Code Quality | 18/20 | Code is well-organized and uses clear variable names. I could add more comments next time. |
| Output & Presentation | 9/10 | Output is neatly formatted and matches the example provided. |
Final Score: 95/100
Image Proof (Link to issue)
https://github.com/shriya1401/shriyap_2025/issues/14