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.
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:
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.
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.
/** precondition- a and b are positive integers*
*/
public int sum(int a, int b)
{
}
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;
}
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.
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.
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.
\