JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of...

21
JAVA Classes Review

Transcript of JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of...

Page 1: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

JAVA Classes

Review

Page 2: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Definitions

Class – a description of the attributes and behavior of a set of computational objects

Constructor – a method that is run when an object is instantiated, usually to initialize that object’s instance variables

Object – a collection of data and operations in which the data can be accessed and modified only by means of the operations.

Page 3: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Definitions

Instance variable - storage for data in an object or an instance of a variable.

Local variable – a name of a variable that is only valid and usable within a method or nested block of code.

Оbject variable – a label or name of the object or one of its attributes.

Page 4: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Types of Methods

Mutator method - is a method used to change the value of an attribute of an object

Accessor method name is a method used to EXAMINE an attribute of an object without changing it.

Page 5: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Definitions

Argument – a value or expression passed in a method call.

Parameter list – a list of the arguments used in a method call (actual parameters) or in the method heading (formal parameter).

Data type – a description of the set of values and the kind of data that a variable can have.

Page 6: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Definitions

Actual Parameter – (or argument) a variable or expression contained in a method call and passed to that method.

ex. S.setScore(1, testScore)

Formal Parameter – the names of the variables in the method.

ex. public void setScore (int i, int Score)

Page 7: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Parameter list of a Method

<visibility modifier> <return type><method name> <parameter list>

data type variable

EXAMPLES:

public void checkGPA(double gpa)public int getGPA(double grade)private string getStudent(string nm)

Page 8: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Structure of a Method Definition

<visibility modifier> <return type><method name> <parameter list>

EXAMPLES:

public void checkGPA(double gpa)

public int getGPA(double grade)

private string getStudent(string nm)

Page 9: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Definition of Method items

visibility modifier – used in the method definition to specify who should be able to see and use the method. Private specifies that is should be used only be this class (helper method). Public is used when the method should be available to clients of the defining class.

T return type – the data type of variable returned by a method. It is specified in the method definition. Void is used when nothing is being returned by the method.

Page 10: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

//Beginning of class Student

public class Student { private String name; private double gpa;

public Student(){ name = ""; gpa = 0.0; }

public Student(String nm, double theGpa) {

name = nm; gpa = theGpa; }

//part 1 class Student

Example of a CLASS

Page 11: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

public void setName(String nm){ name = nm; }

public void setGpa(double theGpa){ gpa = theGpa; }

public String getName(){ return name; }

public double getGpa(){ return gpa; } }

//part 2 of class Student

Page 12: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

public String toString(){

String str;

str = "Name: "+name+"\n";

str+= "GPA: " + getGpa()+ "\n";

return str; }

}//end of class Student

Page 13: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

public class StudentTest {

public static void main(String [] args){

Student s1 = new Student();

s1.setName("Bill");

s1.setGpa (4.0);

Student s2 = new Student ("Carrie", 3.56); System.out.println(s1); System.out.println(s2);

}

}

Page 14: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

KEY (use for all classes)∆ instance variableC constructor nameО object variableX class nameT return type P formal parameterM mutator method nameA accessor method name□ argument (actual parameter)

$ data type* local variable

Mark EVERY occurance of each class part

Page 15: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Page 16: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Page 17: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Additional information

Overloading: The process of using the same operator symbol or identifier to refer to many different functions.

Encapsulation: The hiding of data within an object so that it cannot be directly accessed by clients of that object.

Page 18: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Additional information

Identity: The property of an object that it is the same thing at different points in time, even though the values of its attributes might change.

Instantiation: The process of creating an new object.

Page 19: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Additional information

Scope: The largest area of program text in which an identifier is available.

Lifetime: The time during which a data object or method call exists.

Behavior: The set of actions that a class of objects supports.

State: The set of all the values of the variables of a program at any point during its execution.

Page 20: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Additional information

Garage Collecting: The automatic process of reclaiming memory when the data of a program is no longer needed.

Information Hiding: A condition in which the user of the module does not know the details of how it is implemented, and the implementer of a module does not know the details of how it is used.

Page 21: JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.

Tonight’s homework (due Friday @ beginning of class)

TO BE HAND WRITTEN, not done in BlueJ or NetBeans, etc.

Pick an object and write a class that defines the object. The class needs to have

at least 3 instance variables (use at least 2 different data types)

2 constructors (one “default” constructor with no parameters, and one constructor with at least 1 parameter),

“get” methods for each instance variable (accessor methods)

“set” methods for each instance variable (mutator methods)

a toString( ) method that displays the state of the object (the values of the instance varibles) with appropriate labels.

You DO NOT need to make a “main” class to test this…just write the Standard class.