Objects+and+Classes

Welcome to the class notes on Objects and Classes compiled by our AP Computer Science Students from AISC

=5 steps to a 5 - Sitara=

Class: A programing unit that captures an abstraction, the state and behavior of an abstract object it defines. A class may represent a tangible object or a concept. The first line of code in a class is called the class heading or a signature.

Instance variables: variables that represent the state of the object. They are declared private and hold the data about each individual instance of the class.

Constants: constants are identified with the keyword final. The value of a constant is defined when it is declared and cannot be changed during execution of the program. Constants are usually declared public and static. Public allows the constant to be used by the client method and static means there is one constant value used by all objects of the class rather than a separate constant for each instance. Constants must be in all upper case letters

Method signature: the fist line of a method is known as the method signature. There are four singnificant parts in a method signature. In which the parts are the public or private modifier, the return type, the method name, and the parameter list.

Constructors: methods whose job is to build an instance of a class. Its signature is distinguished by the fact that there is no specified return type. The name of the constructor must match the name of the class exactly.

Accessors: accessor method allows the client to retrieve information about the state of an object.

Modifiers (mutators): modifier methods allow the client to change the state of an object. These methods are usually void and have one or more parameters.

Helper methods: private methods that help with the implementation of other methods in the same class

Parameters: parameters are values required by a method in order to execute a task

Types of Parameters: Actual parameters: specific values sent to the method in the method call Explicit parameters: Parameters that are specified by the method. If a method inside the class needs to refer to the implicit parameter, it uses the term “this.”

The print and println methods Println prints the information sent to it and then moves to the beginning of the next line. Println does not go to the next line when completed

String literals String concatenation (+), used to connect two strings Escape Sequences


 * Sequence || Meaning ||
 * \n || new line ||
 * \” || Double quote ||
 * \\ || backslash ||
 * \b || backspace ||
 * \t || tab ||
 * \\r || Carriage return ||
 * \’ || Single quote ||

Variables and Assignment A Variable is a name for a location in memory used to hold a data value.

Local Variable Declaration Final à Type à Variable Declarator

Variable Declarator Identifier à = à Expression, Array initializer

Assignment Statement Identifier à = à Expression

Integers and floating points: int and double are used to indicate numerical values. Int is for whole numbers and double is for decimals

Boolean: Boolean indicates true or false

Arithmetic Expressions


 * Expression || Meaning ||
 * (+) || Add ||
 * (-) || Subtract ||
 * (*) || Multiply ||
 * (/) || Divide ||
 * (%) || Remainder ||

Operator Precedence Multiply, Divide, and Remainder are done before addition and subtraction, otherwise, expressions are done left to right. Any expression in parenthesis is done first.

Conversion Assignment Conversion: money = dollars; Arithmetic Promotion: result = sum/count; Casting: dollars = (int) money; Cast returns the value in money and cuts off the fractional part. Can be used to round a floating point number to the nearest hundredth.

Constants Constants are values which do not change, usually they begin with final Ex: final int numbers;

Random Class Random : Creates a new pseudorandom number generator Double nextDouble : returns a random number between 0 and 1 inclusive Int nextInt (int num): Returns a random in the range of 0 to num-1

Math Class Static int abs (int num) Static double abs (double num) Return the absolute value

Static double pow Returns the number raised to a specific power

Static double sqrt (double num) Returns the square root

Number Format String format (double number) Static NumberFormat getCurrencyInstance Returns a NumberFormat object that represents a currency format Static NumberFormat getPercentInstance: Gets the percentage format

Operators Parenthesis Exponents Multiplication, Division, Mod Addition, Subtraction

Note: Mod takes the remainder of dividing op1 by op2

**Blue Pelican Java - Sruti ** A **class** is like a cookie cutter and the “cookies” it produces are the **objects**. When building a class the following parts must be included: A cookie-cutter creates a cookie while a class instantiates an object. A object is instantiated in the main method of a different class, usually the //Tester// class in the following way: Circle cir1 = new Circle(10); Data can the object can be taken and stored as a variable: int rad = cir1.radius; The signature, also called the method declaration, states the access control, returned data type, method name and parameters. public double area Access control: public and private Public gives access from outside the class Private access is limited to the class. Public and private access can also be given to variables.
 * __State variable__- Also known as instance field and data member, it is information available to code in ALL the methods of the class.
 * __Method__- It performs some action.
 * __Constructor__- A specific method that allows certain properties of the object to be specified in the class.

Void can be the returned data type. In this case, nothing is returned. Conventially, all methods begin with a small latter. Parameters are data inputed for use in the method. Formal parameters are the variable used as parameters. Actual paramenters or arguments are the values passed in the parameter.

The purpose of the constructor is to set values for some of the static variables of an object at its time of construction. It has rules different from those of other methods. Objects can be declared and instantiated separately or together. Circle cir1; cir1 = new Circle(3.0);
 * 1) public is always specified
 * 2) The name of the constructor method is always the same as the name of the class
 * 3) It is a void method but the //void// specifier is omitted.
 * 4) It may or may not have parameters. A constructor with no parameters is called the default constructor.

It is possible for objects to be instantiated without a name. This is done by passing the object as the parameters for a method. In the method, the object will have a name but does not carry the name outside of the method.

To determine if objects are equal: cir1 and cir2 are two circle objects. They are the same object.
 * Case 1: **

System.out.println(cir1==cir2); //true

Circle c1 = new Circle(11); Circle c2 = new Circle(11); System.out.println(c1==c2); //false System.out.println(c1.equals(c2));
 * Case 2: **

Various objects of the same class must refer to the same object in order to be judged equal using = = or .equals. However, the .equals method can be overridden by the programmer so that the content within the object is checked to determine if the objects are equal.

The String class behaves like other objects but also has exceptions. Therefore when comparing String objects it is always better to use either the //equals// or the //compareTo// method not the == operand.

The name of an object is just a reference to it and can be changed to refer to a different object.

If the access control for a method or variable is not given, then it will be set to public by default.

Initialization is usually done in the constructor method but can also be done when the variable is declared. If the variable is no initialized it is automatically set to 0.

However the rules are different when the variable is declared in the body of a method. In this case, a variable that is not initialized will result in a compile time error.

When calling a constructor the parameters must match exactly with one of the constructors in the class except for the default constructor with lacks any parameters. The class must have a default constructor if it is called when making the object.