1 / 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. What About Alice?.

elda
Download Presentation

What About Alice?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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?

  2. 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. Classes and Objects

  3. The snowman class says what methods and property a snowman can have. It is a blueprint for making actual snowmen. Classes (cont'd)

  4. 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. Objects

  5. 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. Properties

  6. 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. Methods

  7. 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. Java Classes

  8. A class is created by a class definition. The form is: class Snowman { <field1> <field2> ... <method1> <method2> ... } Class Definition

  9. 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! Classes as Types

  10. 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. Creating Objects

  11. 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. Example

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

  13. 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() { ... } } Example Snowman

  14. “ 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.” Classes

  15. 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. Procedural Programming

  16. 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. Result

  17. 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. Object-Oriented Design

  18. 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? OOD - Example

  19. 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 OOD – Another Example

  20. 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. Exercise

  21. 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. Object-Oriented Design

More Related