Definition:
The notion of classes corresponds to a blueprint.
What is a blueprint?
Drawing of a structure
Plan
Instructions
Layout of design
Map
Where have you seen the notion of a blueprint?
Architecture
When you create buildings
When need to solve or maintain a solution
Coding
Machine design
City Hall
The ultimate goal of the blueprint is to duplicate or access information that is already been stated. Duplication in the context of generating multiple items of the same type. E.g., houses, apartments, stores.
The notion of blueprint in the book is called cookie cutter. The reason remains the same – to create multiple items of the same type.
In Object Oriented Programming (OOP), the notion of a class corresponds to the blueprint for a specific object that is desired to create. The classes are composed of the following items:
Fields: are the characteristics of the object. What makes an object unique. The more
Constructors: are in charge of initializing all the fields with actual values.
Methods: are the functionalities of the object by performing the operations with the fields.
To simplify the notion of object, we will discuss through the learning process the design of the Dollar Object.
We will design an object called Dollar. This dollar is the same dollar that you have in your wallet.
As shown in the figure, the dollar bill is composed of many characteristics (the fields). Let us brainstorm some of them:
Fields: the more characteristics you provide for the object, the more precise it becomes.
Face
Color
shape
Denomination
Serial number
Symbols
Verification seal
Year
Material
The next question is, how do you represent the fields in Java according to their data types? For simplicity purposes, let us pick four of them:
Face: String, e.g., “Washington,” “Jefferson,” “Lincoln,” “Hamilton,” “Jackson,” “Grant,” “Franklin.”
Denomination: int, e.g., 1, 2, 5, 10, 20, 50, 100, 1000
Serial number: String, e.g., F54931740C
Year: int, e.g., 2017
Constructors: are in charge of initializing all the fields. Java Provides the ability to create a default constructor and a customized set of constructors. Constructors have the following characteristics:
They have the same name as the class
They start with the public keyword
They DO NOT have a return type
They have (), some of them have parameters
You can have as many customized constructors as you want (polymorphism) – a.k.a. method overloading. Polymorphism provides flexibility in the usage of the methods/constructors.
Default Constructor
The default constructor does not take any argument, and it helps to initialize the fields to default values according to Java data types' default values[1]. In our particular object Dollar, the default values for:
denomination: 0
face: “” (empty string), OR null
serial: “” (empty string), OR null
year: 0
public Dollar(){
denomination = 0;
face = null;
serial = null;
year = 0;
}
Customized Constructors
The customized constructors provide you the flexibility to initialize the fields by providing a different amount of parameters. We will provide two customized constructors for the class Dollar.
Customized constructor #1: The first customized constructor typically takes the same amount of arguments as the total number of fields in the class. What will be the values for the fields on Dollar?
Therefore, the customized constructor may look as follows:
public Dollar(int newDenomination, String newFace,
String newSerial, int newYear){
denomination = newDenomination;
face = newFace;
serial = newSerial;
year = newYear;
}
Customized Constructor #2: For the second customized constructor, we will only provide 3 values:
public Dollar(int newDenomination, String newSerial, int newYear){
denomination = newDenomination;
serial = newSerial;
year = newYear;
}
Notice that based on the number of parameters, we initialized only 3 fields. But, we need to initialize ALL the fields. The remaining field to be initialized is the face. We can make a decision based on the denomination provided and initialize the field. E.g.,
public Dollar(int newDenomination, String newSerial, int newYear){
denomination = newDenomination;
serial = newSerial;
year = newYear;
if(denomination == 1)
face = "Washington";
else if(denomination == 2)
face = "Jefferson";
else if(denomination == 5)
face = "Lincoln";
else if(denomination == 10)
face = "Hamilton";
else if(denomination == 20)
face = "Jackson";
else if(denomination == 50)
face = "Grant";
else if(denomination == 100)
face = "Franklin";
}
Notice that regardless of the lack of a parameter for the face, we were able to initialize ALL the fields.
Let us test the object Dollar. We will be creating a class named DepOfTreasury (since at the department of treasury, there is machinery to create dollars for the nation).
When you create an instance of object Dollar, you must invoke the constructors. E.g.,
Dollar d1 = new Dollar(1,"Washington","F54931740C",2006);
You have seen this format on how to create an instance of an object for the past weeks. For example:
Scanner input = new Scanner(System.in);
int[] array = new int[5];
PrintWriter pr = new PrintWriter(“output.txt”);
JOptionPane p = new JOptionPane(“”);
File f = new File(“input”);
String s = new String("hello");
In the example, we are using the second constructor, the one that takes an int, String, String, and int as parameters of the constructor. The DepOfTreasury looks as follows:
1. public class DepOfTreasury{
2. public static void main(String [] args){
3. // instances of the object
4. // using the 2nd constructor
5. Dollar d1 = new Dollar(1,"Washington","K75719608D",2013);
6. // using the 3rd constructor
7. Dollar d2 = new Dollar(5,"ML12944865C",2002);
8. System.out.println("\t\tFor the first object");
9.
10. System.out.println(“$”+d1.denomination);
11. System.out.println(d1.face);
12. System.out.println(d1.serial);
13.
14. System.out.println("\t\t For the second object");
15. System.out.println(“$”+d2.denomination);
16. System.out.println(d2.face);
17. System.out.println(d2.serial);
18. }
19. }
By running the program, we obtained the following output.
For the first object
$1
Washington
K75719608D
For the second object
$5
Lincoln
ML12944865C
This demonstrates the method invocation from class to class.
The methods are the functionality of the objects. They perform operations based on the fields of the objects. In particular, we will discuss two types of methods: getters/accessors and setters/mutators
Getters: are responsible for retrieving information for the fields
Setters: are responsible for change the information for the fields
Characteristics of the Getters:
They are public methods
They have a return type based on the field you want to retrieve
They start with the word get
They do not have parameters
Use camel case style
Characteristics of the Setters:
They sometimes start with "public"[4]
They are void methods
They start with the word set
They have parameters. The parameters represent the new value for the field
Use camel case style
The setters take the parameter provided and assign to the current field.
What other methods can we define?
The type of functionality helps us to identify this object more precisely.
E.g.,
Create a method called getAge(). This method will calculate the age of the current dollar. The method will return an int representing the age of the dollar. Hint: the age can be calculated by subtracting the year of creation from the current year. E.g., 2017 – year.
Create a method called printInfo(). This method will print the general description of the object Dollar as follows:
$<denomination> face Year: ####, and is ## years old!
Notice that these methods:
· Do not require any parameters. The reason is that we have the fields as “local values” in the class Dollar. Since the variables belong to the scope of the class, they are visible in all the class methods.
· Do not have the static keyword anymore. The reason is that the blueprint is independent, from a static context. E.g., the main method. When you create an instance of the class in the main, you will create an object that contains all the fields and methods and can work independently from other Dollar objects[5].
If we go to the DepOfTreasury, and remove the previous lines of code that access each field independently of the class Dollar and substitute by:
System.out.println("\t\t For the first object");
d1.printInfo();
System.out.println("\t\t For the first object");
d2.printInfo();
Object-orientation helps us to reduce lines of code on the main and keep the high cohesion[6] of the class.
AP CS A
[Using Methods]
MOD-3.B Create an inheritance relationship from a subclass to the superclass.
MOD-3.B.1 A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass.
MOD-3.B.2 Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without repeating these in the code.
MOD-3.B.3 Extending a subclass from a superclass creates an “is-a” relationship from the subclass to the superclass.
MOD-3.B.4 The keyword extends is used to establish an inheritance relationship between a subclass and a superclass. A class can extend only one superclass.
MOD-3.C.3 If S is a subclass of T, then a reference of type T can be used to refer to an object of type T or S.
MOD-3.C.4 Declaring references of type T, when S is a subclass of T, is useful in the following declarations— ● Formal method parameters ● arrays — T[] var ArrayList<T> var.
MOD-3.D Call methods in an inheritance relationship.
MOD-3.D.1 Utilize the Object class through inheritance.
MOD-3.D.2 At compile time, methods in or inherited by the declared type determine the correctness of a non-static method call.
MOD-3.D.3 At run-time, the method in the actual object type is executed for a non-static method call.
MOD-3.E Call Object class methods through inheritance.
MOD-3.E.1 The Object class is the superclass of all other classes in Java.
MOD-3.E.4 Subclasses of an Object often override the equals and toString methods in class-specific implementations
MOD-3.E.2 The Object class is part of the java.lang package.
MOD-3.E.3 The following Object class methods and constructors—including what they do and when they are used—are part of the Java Quick Reference— ● boolean equals(Object other) ● boolean equals(Object other)
[Superclass Constructor and Methods]
MOD-3.B.5 Constructors are not inherited.
MOD-3.B.6 The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters.
MOD-3.B.7 The actual parameters passed in the call to the superclass constructor provide values that the constructor can use to initialize the object’s instance variables.
MOD-3.B.8 When a subclass’s constructor does not explicitly call a superclass’s constructor using super, Java inserts a call to the superclass’s no-argument constructor.
MOD-3.B.9 Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.
MOD-3.B.10 Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass.
MOD-3.B.11 Any method that is called must be defined within its own class or its superclass.
MOD-3.B.12 A subclass is usually designed to have modified (overridden) or additional methods or instance variables.
MOD-3.B.13 A subclass will inherit all public methods from the superclass; these methods remain public in the subclass.
MOD-3.B.14 The keyword super can be used to call a superclass’s constructors and methods.
MOD-3.B.15 The superclass method can be called in a subclass by using the keyword super with the method name and passing appropriate parameters.
MOD-3.C Define reference variables of a superclass to be assigned to an object of a subclass in the same hierarchy.
MOD-3.C.1 When a class S “is-a” class T, T is referred to as a superclass, and S is referred to as a subclass.
MOD-3.C.2 If S is a subclass of T, then assigning an object of type S to a reference of type T facilitates polymorphism.
[Random Numbers]
CON-1.D.4 The values returned from Math.random can be manipulated to produce a random int or double in a defined range
[Documentation Comments]
MOD-2.C.3 A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
MOD-2.C.4 A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
MOD-2.C.5 Programmers write method code to satisfy the postconditions when preconditions are met.
Explain the importance of algorithms in the problem-solving process.
Demonstrate how a problem may be solved by multiple algorithms, each with different properties.
Read a given program, and explain what it does
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Reading and explaining code
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Apply basic programming style guidelines to aid readability of programs such as comments, indentation, proper naming of variables, etc.
Basic testing (perhaps using suitable frameworks) including test case design
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Key modularity constructs such as functions (and methods and classes, if supported in the language) and related concepts like parameter passing, scope, abstraction, data encapsulation, etc.
When and how to use standard data structures
Structured data types available in the chosen programming language like sequences (e.g., arrays, lists), associative containers (e.g., dictionaries, maps), others (e.g., sets, tuples) and when and how to use them.