join the ap-comp-sci-a course

๐ŸŽ–๏ธfiveable certificate upon completion

โฑ๏ธ 4 min read

๐Ÿ“ย ย 

ap comp sci a

ย โ€ขย 

study guide

ย โ€ขย 

Unit 5

5.7 Static Variables and Methods


written by

Peter Cao

peter cao

published on september 20, 2020

Last updated on September 20, 2020

We are almost done with our classes, with only two more rounds of edits to make, one in this topic and the next in Topic 5.9. First, we will need to learn more about static variables and methods.

Introduction to Static Variables and Methods

Static variables and methods are marked by the keyword static, which means that these are properties of the entire class and not just of one particular object. For example, static variables and methods are for things that are true for every student, such as boundaries for certain letter grades which we will implement in Topic 5.9, and also the school that the student goes to which we will implement in this Topic.

Another example of static variables and methods is the Math class we learned back in Unit 2. There are no separate "math objects," but every method is just a general math method doing a calculation.

Fiveable

join more students

create an account on fiveable

Adding Static Variables and Methods

We will add static variables and methods to our Student class. Here is a summary of what we will add: grade boundaries for letter grades, a method to get the letter grade for a student, the school name of all students, and also a method to change this school name.

/** Represents an assignment that a student will complete */ public class Assignment { private boolean correctAnswer; // represents the answer to an assignment, either T/F /** Makes a new assignment with one True/False question and sets the correct answer */ public Assignment(boolean answer) { correctAnswer = answer; } /** Prints details about the assignment */ @Override public String toString() { return "This is an assignment with correct answer " + answer; } /** Grades an assignment, returns true if correct, false if incorrect */ public boolean gradeAssignment(boolean studentAnswer) { return studentAnswer == correctAnswer; } } /** Represents a high school student */ public class Student { private int gradeLevel; // a grade between 9-12 private String name; // the students name in the form "FirstName LastName" private int age; // the student's age, must be positive private Assignment assignment; // the current assignment the student is working on private int assignmentsComplete; // numbers of assignments completed private int correctAssignments; // number of correct assignments private static final int A_BOUNDARY = 0.9; private static final int B_BOUNDARY = 0.8; private static final int C_BOUNDARY = 0.7; private static final int D_BOUNDARY = 0.6; private static String school = "The Fiveable School"; /** Makes a new student with grade gradeLev, name fullName, and age ageNum */ public Student(int gradeLev, String fullName, int ageNum) { gradeLevel = gradeLev; name = fullName; age = ageNum; assignment = null; // There is no active assignment at the moment assignmentsComplete = 0; // no assignments complete yet correctAssignments = 0; } /** Returns the student's grade level */ public int getGradeLevel() { return gradeLevel; } /** Returns the student's name */ public String getName() { return name; } /** Returns the current assignment the student is working on */ public Assignment returnCurrentAssignment() { return assignment; } /** Prints details about the student */ @Override public String toString() { return name + ", a " + gradeLevel + "th grade high school student has an average grade of " + averageGrade + "."; } /** Changes the student's name */ public void setName(String fullName) { name = fullName; } /** Changes the student's grade level */ public void setGradeLevel(int gradeLev) { gradeLevel = gradeLev; } /** Submits an assignment */ public void submitAssignment() { boolean grade = assignment.gradeAssignment(); assignmentsComplete++; if grade { correctAssignments++ } } /** Calculates the student's grade as a decimal */ public double getGradeDecimal() { return (double) correctAssignments / assignmentsComplete; } /** Changes the school that the students go to */ public static void setSchool(String schoolName) { school = schoolName; } }
Fiveable Logo

2550 north lake drive
suite 2
milwaukee, wi 53211

92% of Fiveable students earned a 3 or higher on their 2020 AP Exams.

*apยฎ and advanced placementยฎ are registered trademarks of the college board, which was not involved in the production of, and does not endorse, this product.

ยฉ fiveable 2020 | all rights reserved.