Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.

Similar presentations


Presentation on theme: "AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example."— Presentation transcript:

1

2 AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example

3 AP Computer Science A – Healdsburg High School 2 Objects in the Dance Studio Program Dancer Foot Dance floor Control panel Go / Stop button Dance selection pulldown list Positioning button Dance Studio window Band Dance group Waltz, etc. Dance step

4 AP Computer Science A – Healdsburg High School 3 Classes and Source Files Each class is stored in a separate file The name of the file must be the same as the name of the class. public class Car {... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)

5 AP Computer Science A – Healdsburg High School 4 public class SomeClass Fields Constructors Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Procedures for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import... import statements

6 AP Computer Science A – Healdsburg High School 5 public class Foot { private Image picture; private CoordinateSystem coordinates; public Foot (int x, int y, Image pic) { picture = pic; coordinates = new CoordinateSystem (x, y, pic); } public void moveForward (int distance) { coordinates.shift (distance, 0); } public void moveSideways (int distance) { coordinates.shift (0, distance); }... } Fields Constructor Methods

7 AP Computer Science A – Healdsburg High School 6 Fields A.k.a. instance variables Constitute “private memory” of an object Each field has a data type (int, double, String, Image, Foot, etc.) Each field has a name given by the programmer

8 AP Computer Science A – Healdsburg High School 7 private [static] [final] datatype name ; Fields (cont’d) Usually private May be present: means the field is a constant int, double, etc., or an object: String, Image, Foot May be present: means the field is shared by all objects in the class private Foot leftFoot ;

9 AP Computer Science A – Healdsburg High School 8 Constructors Short procedures for creating objects of a class Always have the same name as the class Initialize the object’s fields May take parameters A class may have several constructors that differ in the number and/or types of their parameters

10 AP Computer Science A – Healdsburg High School 9 Constructors (cont’d) public class Foot {... public Foot (int x, int y, Image pic) {... }... } // FootTest.java... Image leftShoe =...;... Foot leftFoot = new Foot (5, 20, leftShoe);... An object is created with the new operator The number, order, and types of parameters must match Constructor

11 AP Computer Science A – Healdsburg High School 10 Methods Call them for a particular object: leftFoot.moveForward(20); amy.nextStep( ); ben.nextStep( ); go.setText("Stop");

12 AP Computer Science A – Healdsburg High School 11 Methods (cont’d) The number and types of parameters (a.k.a. arguments) passed to a method must match method’s parameters: g.drawString ("Welcome", 120, 50); public void drawString ( String msg, int x, int y ) {... }

13 AP Computer Science A – Healdsburg High School 12 Methods (cont’d) A method can return a value to the caller The keyword void in the method’s header indicates that the method does not return any value public void moveSideways(int distance) {... }

14 AP Computer Science A – Healdsburg High School 13 Ex. Using the Walker class as a prototype, create a new class, CrazyJumper. A CrazyJumper should move both feet forward together by stepLength in firstStep and all other steps. In addition, the feet should rotate 45 degrees in the first jump and rotate back and forth 90 degrees in following jumps.


Download ppt "AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example."

Similar presentations


Ads by Google