Download presentation
Presentation is loading. Please wait.
Published byLiane Gerber Modified over 5 years ago
1
CS 1054: Lecture 2, Chapter 1 Objects and Classes
2
Objects and Classes Objects Classes
represent ‘things’ from the real world, or from some problem domain (example: “the red car down there in the car park”) Classes represent all objects of a kind (example:“car”)
3
Methods and Parameters
Objects have operations which can be invoked (Java calls them methods) Methods may have parameters to pass additional information needed to execute
4
Return Values Methods may return a result via a return value.
public int getPrice()
5
Signature The header of a method is called its signature. It provides information needed to invoke that method and its return type.
6
Note: Many instances can be created from a single class
An object has attributes: values stored in fields. The class defines what fields an object has, but each object stores its own set of values (the state of the object)
7
State State of circle object
8
Data Types byte short int long float double char boolean User defined
9
Source Code Each class has source code (Java code) associated with it that defines its details (fields and methods).
10
Understanding class definitions
CS1054: Lecture 3, Chapter 2 Understanding class definitions 1.0
11
Basic Elements Here are three basic elements of class definitions:
public class ClassName { Fields Constructors Methods } The contents of a class
12
Fields, Constructors and Methods
Classes have a wrapper around them. An example for the TicketMachine Class public class TicketMachine { Inner part of the class omitted } The outer wrappings of different classes look pretty much the same, with only the name changing to match the class
13
The inner workings The inner part of the class is where the fields, constructors and methods for the class It is these fields, constructors and methods that give a class its uniqueness
14
Fields Fields store values for an object.
They are also known as instance variables. Use the Inspect option to view an object’s fields. Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } visibility modifier type variable name private int price;
15
Constructors Constructors initialize an object.
They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; }
16
Passing data via parameters
17
Methods Methods have two parts: Here is an example header /**
a header and a body Here is an example header /** * Return the price of a ticket */ public int getPrice()
18
Types of Methods Accessor Methods Mutator Methods
Methods that simply return values from the object Mutator Methods Methods that change the state of the object
19
Accessor methods public int getPrice() { return price; } return type
visibility modifier method name parameter list (empty) public int getPrice() { return price; } return statement start and end of method body (block)
20
Mutator methods public void insertMoney(int amount) {
return type (void) visibility modifier method name parameter public void insertMoney(int amount) { balance += amount; } assignment statement field being changed
21
Method vs. Constructor You can tell a method apart from a constructor from the signature for each public TicketMachine( int ticketCost ) public int getPrice() The constructor can have no return type The constructor must match the name of the class
22
Scope The scope of a variable means where the variable can be accessed
The scope of a parameter is only in the method that it has been passed to The scope of an instance variable is the entire class
23
Lifetime The lifetime of a variable means how long a variable can be accessed The lifetime for a parameter to a method is only the duration of the method. The lifetime for an instance variable is the same as for the object
24
Assignment Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketCost; The type of the expression and the type of the variable must match A variable stores a single value, so any previous value is lost.
25
Special Assignemt There are some types of assignments that occur so frequently that the designers of Java gave them a shortcut notation variable = variable + expression; Can be shortened to: variable += expression;
26
Output Computer programs would be very boring if you never had any output So we need a way to print stuff to the screen System.out.println(“This is what I want to say”); Will print out “This is what I want to say” to the screen when it is put in a method of a class and that method is called.
27
Output of Variables Suppose you have a variable, myVariable and it contains the value 8. You can output the value in myVariable like this System.out.println( myVariable ); This would print out: 8 But what if you want to say something before you print out the value System.out.println(“This is the value stored in myVariable: ” + myVariable ); Would print out: This is the value stored in myVariable: 8
28
Making Choices A conditional statement allows the program to choose between two possibilities if ( amount > 0 ) { balance += amount; } else System.out.println(“Use a positive amount: ” + amount);
29
Boolean Expression The tests used in the conditional statement are Boolean expressions Boolean expressions result in one of two values: true or false The conditional statement uses the result of the Boolean expression to determine which action to take
30
Local Variables Local variables are declared inside of a method block
They have scope and lifetime that is limited to that block They differ from class fields in that they are defined only in the method block You do not use private or any other modifier to declare a local variable Local variables are used as temporary storage locations to help methods do their job
31
Example Class TicketMachine { … public int refundBalance()
int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; }
32
Fields, Parameters and Local Variables
All three kinds of variables are able to store a value that is appropriate to their defined type Fields are defined outside constructors and methods Fields are used to store data that persists throughout the life of an object. Fields have class scope Fields cannot be accessed from outside their class
33
Fields, Parameters and Local Variables
Parameters and local variables persist for only the time period in which the method or constructor they are in is executing Formal parameters are defined in the header and receive their values from outside the method Formal parameters have scope that is limited to their method call. Local variables are defined within a method Local variables have scope that is limited to the block they are defined in.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.