what about alice?

21
What About Alice? In Alice, we have seen that we have: Objects (skater, snowman, snowwoman, camera) Methods (move, turn, roll, move toward) Properties (color, opacity) In addition, although not as obvious, Alice has classes.

Upload: elda

Post on 04-Jan-2016

54 views

Category:

Documents


0 download

DESCRIPTION

In Alice, we have seen that we have: Objects (skater, snowman, snowwoman, camera) Methods (move, turn, roll, move toward) Properties (color, opacity) In addition, although not as obvious, Alice has classes. What About Alice?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: What About Alice?

What About Alice?

In Alice, we have seen that we have: Objects (skater, snowman, snowwoman, camera) Methods (move, turn, roll, move toward) Properties (color, opacity)

In addition, although not as obvious, Alice has classes.

Page 2: What About Alice?

Classes and Objects

Snowman is both a class and an object. As a class, it refers to a generic snowman. The class allows us to create many snowmen (snowman, snowman1, snowman2, etc.). Each snowman may be customized, e.g., the hats of the snowwomen were different colors, and the snowwomen were in different locations. Changing the value of a property of one snowwoman, doesn't change the value for another snowwoman.

Page 3: What About Alice?

Classes (cont'd)

The snowman class says what methods and property a snowman can have. It is a blueprint for making actual snowmen.

Page 4: What About Alice?

Objects

Objects, in Alice, are created by dragging and dropping the prototype from the Gallery, or from copying an existing object, using the copy button. Although two objects can be made from the same blueprint, they can be customized differently.

Page 5: What About Alice?

Properties

The class specifies what properties its objects can have. Each object has its own set of values. For example, color is a property of all snowwomen, but each snowwoman may have a different color.

Page 6: What About Alice?

Methods

The class also specifies the methods. We can add new methods, modify existing methods, and call methods from other methods. One object can invoke the method of another object.

Page 7: What About Alice?

Java Classes

In Java, a class is a blueprint for objects. A class describes the fields (properties) and methods (methods, functions) that the objects have. Each object has its own values for each field. Fields are also called instance variables. Fields and methods together are called members of the class, and are sometimes called components of the class.

Page 8: What About Alice?

Class Definition

A class is created by a class definition. The form is:

class Snowman {

<field1>

<field2>

...

<method1>

<method2>

...

}

Page 9: What About Alice?

Classes as Types

In Java, classes are types. We can create objects of that type and give them names, just as we can create ints and give them names. We can name a object using a variable. The statement:

Snowman snowman1;

declares the variable snowman1 to be of type Snowman, but it does not create a Snowman object!

Page 10: What About Alice?

Creating Objects

To create an object, we must use the new operator. The operator new creates and returns a new object of the given class. The statement:

Snowman snowman1 = new Snowman();

declares snowman1 to be an object of type Snowman, creates a new Snowman object, and initializes snowman1 to have this new object as its value.

Page 11: What About Alice?

Example

We've seen this statement before:

Scanner sc = new Scanner(System.in);

declared sc to be a variable of type Scanner, created a new Scanner object (based on reading in from the keyboard) and initialized sc to be this new object.

Page 12: What About Alice?

Instance

An object of a class is called an instance of that class, and creating the object is called instantiation.

Page 13: What About Alice?

Example Snowman

This might be the start of a Snowman class:

public class Snowman {

private Color color;

private int opacity;

private double xPosition;

private double yPosition;

public void move() { ... }

public void turn() { ... }

}

Page 14: What About Alice?

Classes

“ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as object-oriented programming.”

Page 15: What About Alice?

Procedural Programming

In procedural programming, we break down a program in terms of functions. Top-down design principles start with the problem at hand and finds a solution by breaking down the problem into steps that can be used to solve it. We begin with the major steps, and then break those down into smaller steps. This process is called stepwise-refinement.

Page 16: What About Alice?

Result

The result is a program in which functions play the major role. Data is merely passed to and from functions. Data may be structured but is secondary to the flow of control of the program. This is the procedural programming paradigm.

Page 17: What About Alice?

Object-Oriented Design

In object-oriented design, instead of thinking in terms of functions, we think in terms of objects. Objects are collections of related data along with the functions that apply to that data. Objects occur naturally in many problems, and so by thinking about what objects are needed to solve a problem, we often can create better solutions.

Page 18: What About Alice?

OOD - Example

Let's take a simple example: A clock.What is the data associated with a clock?What operations do we normally perform on that

data?

Page 19: What About Alice?

OOD – Another Example

A second example is a bank account. Let's take a simple form of a bank account – a checking account with interest.

What is the data?balance, APR

What are the operations?deposit, withdrawal, check balance, modify APR,

check APR

Page 20: What About Alice?

Exercise

Write a class declaration for a bank account class. A bank account should have a balance and an APR. In your main method, create three bank accounts.

Page 21: What About Alice?

Object-Oriented Design

In object-oriented design the problem is decomposed into different objects that are inherent in the problem. The program relies on the interaction between the objects. The objects can call each others' methods. This is called message passing. A message is the name (and parameters) of the method to be called. Different types of objects may have methods with the same name, so which method is actually run depends on the object and the message.