Lecture Notes, class 6 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Classes
Recall all Java programs are built from classes.In Java a class is used in several way's.
- To store a main method to launch an application.
- To create a data type from which objects are created.
- To store some static methods to be used by other classes.
- Some combination of the above.
- Servlets and Applets
Defining Classes to Create Objects.
- Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large programs using OOP.
- A class is instantiable if we can create instances of the class.
The
DecimalFormat
andString
classes are all instantiable classes, theMath
class is not. - An object is an entity that encapsulates one or more variables and methods.
Objects have three properties.
- State - The attributes of the object, defined by the variables of the class
- Behavior - The operations, defined by the methods of the class
- Identity
Write a class that can be used to create Bicycle objects. Note: the state (data) and operations (methods) are determined by the application that will use Bike objects.
- The State (data) - Two variables one to represent the owner of the Bike. The other to represent the model.
- The behaviors (operations) -
- Let users see the owner and/or model.
- Let users change the state, the owner and/or model.
See Bicycle.java
- A Template for designing classes used to create objects. (See Bicycle.java or BankAccount.java)
class comments import statements class CLASSNAME { declaration of data members. constructors methods }
- For now we will refer to the data members as instance variables and methods as instance methods (Except main). Instance variables are defined within the class AND outside any method or constructor.
- Visibility or access modifiers specify where the class member can be used. For now two types.
- public members can be accessed anywhere.
- private members can only be accessed within the class.
- Instance variables are tagged as private.
- For now methods will be public.
- A constructor is a special method that is executed when an object is created.
- One purpose of the constructor is to initialize an object to a valid state. Whenever an object is created, we should ensure that it is created in a valid state by properly initializing all data members in a constructor.
- The name of a constructor must be the same as the name of the class.
- If no constructor is defined for a class, then the Java compiler will include a default constructor.
- A constructor does not have a return type
Features of class Bicycle
- Two variable names -
ownerName
andmodel
. Every Bicycle object will have an instance of these two variables.private String ownerName; private String model;
- A constructor
- Used to construct new Bicycle objects.
- A Constructors name is identical to the name of the class.
- A Constructor has no return type.
public Bicycle() { ownerName = "Unassigned"; model = "Unassigned"; }
- Methods
These are the remaining members of the class. These are the operations(services) that can be performed on Bicycle objects. Clients of the Bicycle class will create Bicycle objects and use the services that the Object provides. These services are carried out by method calls.public void setOwnerName(String name){ ownerName = name; }
- All methods will have this form.
access_modifier returnType methodName( parameterList ) { // method body }
- The parameter list consists of local variable declarations used to pass data into a method.
- Some methods do not require a parameter list.
- To create a Bicycle object we use the operator
new
public String getOwnerName(){ return ownerName; }
new Bicycle()
Operator new
returns a reference to the constructed object. Once you have a reference to an Object
you can invoke methods on it.
Typically we store the reference to the object in a reference variable and use the reference variable to manipulate the object.
Bicycle bike = new Bicycle(); bike.setOwnerName("Sally"); System.out.println(bike.getOwnerName());
Code that uses the Bicycle class is often referred to as the client code . The Bicycle class can be thought of as server code . The Bicycle class provides services to other classes.
Once you are finished writing a class to produce objects you will write another class called the driver to test your class. See BicycleRegistration.
Three Types of Variables
- Local variables - declared inside a method.
- Instance variables - declared within a class AND outside of any method.
- Class variables(Static) - declared within a class AND outside of any method AND with the
static
keyword
Stack and the Heap
- The Stack - Where local variables live.
- The Heap - Where Objects live.
The Circle Class
Look at the driver class first
The PairOfDice Class
Look at the driver class first
The BankAccount Class
Look at BankAccountDriver.java first to see how the class is used then look at BankAcount.java
Word Guess Game
Write a program that plays a simple word scramble game with the user. The program will select a word from a list of words. All I/O will be done using the Standard Input using Scanner Class and Standard Output, using System.out.