knowt ap exam guide logo

Writing Classes

Intro to Classes

  • Classes help out a lot! They will help to make your code a lot less complex to read and understand, and keeps it organized! It’s a key tool for a programmer to write code efficiently.

  • Class- When a group of statements, such as control structures, are all put together to be referred to this.

  • A class is stored on your computer in a file.

  • Note: We all write amazing code, but sometimes the compiler just doesn’t understand, and can’t compile it into machine code. Make sure to review your code carefully!!

  • The name of your class should refer to the purpose and function of the class, similar to how you name your variables.

    • Ex: A class that calculated GPA may be called GradePointAvg,  or GPA.

public class GradePointAvg

{

}
  • Note: Remember your indentations!! It needs to be correctly indented for the code to be considered a part of the class.

  • In classes, variable declarations are found at the top of the code right after the header.

  • It helps to set up the rest of the class.

  • The class above would be saved as GradePointAverage.java

  • The .java extension lets the compiler recognize the file as containing java code. If the code compiles and there are no errors that occur there are other files created in the same class which contains all the machine language for the computer to execute it.

  • The .java file is called the source code for a program because it defines the program’s actions and functions.

    Methods

  • Method- A group of code that performs a specific task.

  • To make your code more readable in a class, try to write out different methods to keep it more organized.

  • Say you have a list of tasks that you want to complete over the weekend your class in pseudocode would look something like this:

    public class Saturday

    {

    wake up method;//includes alarm times

    eat breakfast method; // includes the ingredients, and calories

    shower method;

    }
  • This makes the class more structured and a lot cleaner to read. A class that is created to control a larger program is called a driver class.

  • Since it drives the program through its structure to help execute the smaller commands.

  • Object class-This class houses the “guts” of the methods that the driver class calls.

  • While the driver class shortens, the object class expands.

  • The object class defines all of the aspects of an object, and it represents what the object has and what the object does.

  • The object class in this section shouldn’t be confused with the Object class in Java(that’s considered to be the parent class of all classes)!

  • Every class in Java is a descendent of the object class in the Java language.

  • Header- It’s used to define the function of the method.

  • Constructor- They set an object's initial values for instance variables.

  • The programmer needs to write attributes in a class, instance variables, or fields in order for the code to function correctly.

  • An object class defines what an object has, and what it does.

  • Structure for a method:

    • Visibility returnType methodName(param1, param2)

    • The visibility is if the method is public or private.

    • The return type, specifies the type of data that will be returned from the methods after its commands are executed.

    • The method name, is the name of the method.

    • Parameters- (these are optional) They are data that the method needs to function properly.

  • Let’s go back to our breakfast example for this for what a class would look like: public void routine()

  • To make it more specific we just need to include parameters: public void routine(int minutes)

  • Remember: A method can have any number of parameters, it is up to the programmer, and how they design their program.

  • The purpose of a method is to perform some sort of task with regards to the object.

  • Sometimes we end up writing multiple methods that perform the same task, but require different information, or parameters to to something. This is considered overloading.

    Preconditions and Postconditions

  • Precondition- A comment that is intended to inform the user more about the condition of the method and guarantees it to be true.

  • Note: On the AP Exam, the precondition shows up as a comment above the method. It’s the program's job that calls the method not to pass parameters that violate the precondition.

    • Ex(Precondition):

/** precondition- a and b are positive integers*

*/

public int sum(int a, int b)

{

}
  • Postcondition- A condition that must always be true just after the execution of a section of code, or after an operation in a formal specification.

  • Note: On the AP Exam, postcondition is also written as a comment before or within the method. The method designer is responsible for making sure that these conditions are being met.

    • Ex(Postcondition):

public int sum100(int a, int b)

{

<code>

//postcondition- returns 100, if sum if greater than 100, or the value of the sum

if( sum < 100)

Return sum;

else

Return 100;

}
  • Note: A method can accept any number of parameters, but may return only one data value or object.

  • Tip: Plan out how you are trying to construct your classes based on your needs. It makes it a lot easier to write the code in the most efficient way possible!

  • Note: The AP Exam tests the planning of a class by giving a lengthy requirements list that comes along with the other classes that interact in either their multiple choice questions, or on the FRQ’s.

Composition

  • In order for the classes to come together and have the program function properly, they need to work together properly.

  • Remember: the driver and object classes must be in the same folder on the computer.

  • Note: The driver depends on the object class in order for the code to compile. The object class depends on the driver to make it do anything.

  • To create an object, you need to instantiate it using the corresponding class.

  • Every time we instantiate an object, we need to assign it an object reference variable, or in other words, a variable name.

  • Each item has to have its own separate object class when its instance data and methods are different than that of the other types of data.

  • The programmer has to take on the responsibility of deciding which objects to instantiate and to which types and when to use them, and how they are used based on the needs of the class.

  • Aggregate class- Made up of other data, and instances of other classes.

  • Note: The FRQs on the AP Exam will give you content behind the functions of the classes and methods that you need to write.

References & this Keyword & Static Modifier

  • This is useful for when you need data, and information from another class to use in another class. You would need to create a copy of the object so that it can be used in the specific class.

  • Note: When received as a parameter, primitive data is actually copied, while object data will receive a new reference to the object itself. Primitives are copied by value, objects are copied by reference.

    this Keyword

  • It might be necessary for the calling object to refer to itself.

  • When an object calls a method, a reference is assigned to the calling object. The name of this reference is this.

  • this is a keyword used to reference the current calling object and may be used as an object variable.

    Static Modifier

  • A static variable is an attribute that is shared among all instances of a class.

  • When the value of this variable is changed, the alteration is reflected by all of the objects instantiated through that specific class.

  • Think of it like a high-score list on a video game. There is a list of high scores, and the old ones get thrown out as a new high score emerges.

  • A non-constructor method that is designed to access or modify a static variable is a static method.

Writing Classes

Intro to Classes

  • Classes help out a lot! They will help to make your code a lot less complex to read and understand, and keeps it organized! It’s a key tool for a programmer to write code efficiently.

  • Class- When a group of statements, such as control structures, are all put together to be referred to this.

  • A class is stored on your computer in a file.

  • Note: We all write amazing code, but sometimes the compiler just doesn’t understand, and can’t compile it into machine code. Make sure to review your code carefully!!

  • The name of your class should refer to the purpose and function of the class, similar to how you name your variables.

    • Ex: A class that calculated GPA may be called GradePointAvg,  or GPA.

public class GradePointAvg

{

}
  • Note: Remember your indentations!! It needs to be correctly indented for the code to be considered a part of the class.

  • In classes, variable declarations are found at the top of the code right after the header.

  • It helps to set up the rest of the class.

  • The class above would be saved as GradePointAverage.java

  • The .java extension lets the compiler recognize the file as containing java code. If the code compiles and there are no errors that occur there are other files created in the same class which contains all the machine language for the computer to execute it.

  • The .java file is called the source code for a program because it defines the program’s actions and functions.

    Methods

  • Method- A group of code that performs a specific task.

  • To make your code more readable in a class, try to write out different methods to keep it more organized.

  • Say you have a list of tasks that you want to complete over the weekend your class in pseudocode would look something like this:

    public class Saturday

    {

    wake up method;//includes alarm times

    eat breakfast method; // includes the ingredients, and calories

    shower method;

    }
  • This makes the class more structured and a lot cleaner to read. A class that is created to control a larger program is called a driver class.

  • Since it drives the program through its structure to help execute the smaller commands.

  • Object class-This class houses the “guts” of the methods that the driver class calls.

  • While the driver class shortens, the object class expands.

  • The object class defines all of the aspects of an object, and it represents what the object has and what the object does.

  • The object class in this section shouldn’t be confused with the Object class in Java(that’s considered to be the parent class of all classes)!

  • Every class in Java is a descendent of the object class in the Java language.

  • Header- It’s used to define the function of the method.

  • Constructor- They set an object's initial values for instance variables.

  • The programmer needs to write attributes in a class, instance variables, or fields in order for the code to function correctly.

  • An object class defines what an object has, and what it does.

  • Structure for a method:

    • Visibility returnType methodName(param1, param2)

    • The visibility is if the method is public or private.

    • The return type, specifies the type of data that will be returned from the methods after its commands are executed.

    • The method name, is the name of the method.

    • Parameters- (these are optional) They are data that the method needs to function properly.

  • Let’s go back to our breakfast example for this for what a class would look like: public void routine()

  • To make it more specific we just need to include parameters: public void routine(int minutes)

  • Remember: A method can have any number of parameters, it is up to the programmer, and how they design their program.

  • The purpose of a method is to perform some sort of task with regards to the object.

  • Sometimes we end up writing multiple methods that perform the same task, but require different information, or parameters to to something. This is considered overloading.

    Preconditions and Postconditions

  • Precondition- A comment that is intended to inform the user more about the condition of the method and guarantees it to be true.

  • Note: On the AP Exam, the precondition shows up as a comment above the method. It’s the program's job that calls the method not to pass parameters that violate the precondition.

    • Ex(Precondition):

/** precondition- a and b are positive integers*

*/

public int sum(int a, int b)

{

}
  • Postcondition- A condition that must always be true just after the execution of a section of code, or after an operation in a formal specification.

  • Note: On the AP Exam, postcondition is also written as a comment before or within the method. The method designer is responsible for making sure that these conditions are being met.

    • Ex(Postcondition):

public int sum100(int a, int b)

{

<code>

//postcondition- returns 100, if sum if greater than 100, or the value of the sum

if( sum < 100)

Return sum;

else

Return 100;

}
  • Note: A method can accept any number of parameters, but may return only one data value or object.

  • Tip: Plan out how you are trying to construct your classes based on your needs. It makes it a lot easier to write the code in the most efficient way possible!

  • Note: The AP Exam tests the planning of a class by giving a lengthy requirements list that comes along with the other classes that interact in either their multiple choice questions, or on the FRQ’s.

Composition

  • In order for the classes to come together and have the program function properly, they need to work together properly.

  • Remember: the driver and object classes must be in the same folder on the computer.

  • Note: The driver depends on the object class in order for the code to compile. The object class depends on the driver to make it do anything.

  • To create an object, you need to instantiate it using the corresponding class.

  • Every time we instantiate an object, we need to assign it an object reference variable, or in other words, a variable name.

  • Each item has to have its own separate object class when its instance data and methods are different than that of the other types of data.

  • The programmer has to take on the responsibility of deciding which objects to instantiate and to which types and when to use them, and how they are used based on the needs of the class.

  • Aggregate class- Made up of other data, and instances of other classes.

  • Note: The FRQs on the AP Exam will give you content behind the functions of the classes and methods that you need to write.

References & this Keyword & Static Modifier

  • This is useful for when you need data, and information from another class to use in another class. You would need to create a copy of the object so that it can be used in the specific class.

  • Note: When received as a parameter, primitive data is actually copied, while object data will receive a new reference to the object itself. Primitives are copied by value, objects are copied by reference.

    this Keyword

  • It might be necessary for the calling object to refer to itself.

  • When an object calls a method, a reference is assigned to the calling object. The name of this reference is this.

  • this is a keyword used to reference the current calling object and may be used as an object variable.

    Static Modifier

  • A static variable is an attribute that is shared among all instances of a class.

  • When the value of this variable is changed, the alteration is reflected by all of the objects instantiated through that specific class.

  • Think of it like a high-score list on a video game. There is a list of high scores, and the old ones get thrown out as a new high score emerges.

  • A non-constructor method that is designed to access or modify a static variable is a static method.