Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © Texas Education Agency, 20131 Computer Programming Class Basics.

Similar presentations


Presentation on theme: "Copyright © Texas Education Agency, 20131 Computer Programming Class Basics."— Presentation transcript:

1 Copyright © Texas Education Agency, 20131 Computer Programming Class Basics

2 A Little Programming History In the early days programmers mixed data and procedures all in the same program. As programs got longer and more complicated, they also were very difficult to understand and maintain. Programmers found that they often repeated similar blocks of code throughout a program. This type of programming is sometimes referred to as ‘procedural’. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]2

3 A new way of looking at programming Eventually programmers began to define the code they were writing in more modular ways, creating ‘classes’ with particular actions and characteristics. Classes could be more easily defined and re-used in other applications. Different classes interact with each other using their actions and characteristics. The better the classes are defined, the less code programmers have to write to accomplish a task. This type of programming is called ‘object oriented’. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]3

4 A Comparison Procedural programming could be compared to writing a novel. It starts from the beginning; the characters, actions, and plot develop throughout the book; and, most parts of the book are not made to stand on their own. Object oriented programming could be compared to writing a recipe book, in which measurements, ingredients, cooking techniques, and utensils are described separately; then pulled together into individual menus. Each menu in the book does not need to re-define how to stir-fry or how much a cup is. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]4

5 Java files Most Java files are classes. The name of the class is the same as the file name. The Monkey class is defined in the file Monkey.java. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]5

6 How are Classes and Objects related? A Class is the definition of an Object, in the same way a blueprint is the definition of a car. An Object is a specific instance of a Class, in the same way that your car is specific instance of the blueprint. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]6

7 A Class only contains two things: Methods -- the actions the class can do Fields -- characteristics of the class (also called variables) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]7

8 Consider the ‘Car’ Class… Car methods -- honk, run, turn… Car fields -- color, weight, length… Note: methods and fields are class ‘members’ Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]8

9 In the next slide… Car methods are defined in red Car fields are defined in blue Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]9

10 public class Car { public String color; public int weight; public void honk() { System.out.println(“Beeep”); } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]10

11 Creating an Object Most, but not all Classes have constructors Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]11

12 Constructor Characteristics Constructors are like methods, and are used to create a specific instance of the class. Constructors are instructions on how to create an instance of the class. Constructors always have the same name as their class. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]12

13 The next slide shows a class with a constructor and the call to that constructor. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]13

14 public class Car { public String color; public static void main(String args []) { Car c1 = new Car(“red”); } public Car(String c) { color = c; } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]14

15 In the code on the previous slide… An instance named c1 is created The color of c1 is “red” How many instances are created on the next slide? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]15

16 public class Car { public String color; public static void main(String args []) { Car c1 = new Car(“blue”); Car c2 = new Car(“green”); } public Car(String c) { color = c; } public void honk() { System.out.println(“Beeep”); } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]16

17 Notice in the previous slide… The definition of a constructor does not show a return data type. The constructor returns the data type of its class. The value for color field of c1 is “blue”. The value of the color field of c2 is “green”. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]17

18 Using methods and fields Fields allow an object to store its own data. Methods allow an object to interact with other objects. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]18

19 How do you access an Object’s field? Look at the code on the next slide. What should be printed when the code runs? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]19

20 public class Car { public String color; public static void main(String args []) { Car c1 = new Car(“blue”); System.out.println(c1.color); } public Car(String c) { color = c; } public void honk() { System.out.println(“Beeep”); } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]20

21 How do you access an Object’s method? Look at the code on the next slide. What should be printed when the code runs? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]21

22 public class Car { public String color; public static void main(String args []) { Car c1 = new Car(“blue”); c1.honk(“bleeeep”); } public Car(String c) { color = c; } public void honk(String s) { System.out.println(s); } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]22

23 Can instance fields be accessed using methods? Look at the next slide. The method call is in red. What instance names are created? Inside the getColor method, are instance names used? How does the getColor method know whether to return the color field for c1 or c2? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]23

24 public class Car { public String color; public static void main(String args []) { Car c1 = new Car(“blue”); Car c2 = new Car(“red”); System.out.println(c1.getColor()); } public Car(String c) { color = c; } public String getColor() { return color; } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]24

25 Understanding the keyword ‘this’ The keyword ‘this’ works as a substitute for the instance name. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]25

26 When an instance name is used to call a method, the method knows, but does not explicitly use, the instance name. public class person { int age; Person p1 = new Person(); p1.setAge(12); // The 2 lines above are in the main method. public void setAge( int x){ age = x; // age belongs to the instance that called } } // The line age=x; means p1.age=x; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]26

27 There are two kinds of variables inside a class Local variables are defined inside of a method. They only pertain to that method and the variable name is not recognized outside of the method. Class or instance variables are defined inside the class, but not inside of any method. These variables are recognized throughout the entire class. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]27

28 The keyword ‘this’ is used to refer to the instance name from inside a non-static method public class person { int age; public void setAge( int age){ this.age = age; } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]28

29 The keyword ‘this’ helps to distinguish local variables from instance variables of the same name public class person { int age; // this ‘age’ is defined inside the class. public void setAge( int age){ // defined in a method this.age = age; } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]29

30 Anonymous Objects (or Instances) Anonymous Objects are instantiated without a name. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]30

31 If a method requires an object to be passed in as an argument, like… Food f = new Food(“chicken”); // create object getCalories(f); // call method, passing object named f public void getCalories(Food x) { // code goes here. Method definition } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]31

32 An anonymous object is a shortcut when a name is not required // The calling line does not use a name getCalories(new Food(“chicken”)); public void getCalories(Food x) { // The method names the object ‘x’ } Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]32

33 Review What do you call the thing that is used to create a new instance of a class? What 2 things can a class contain? How do you access fields that belong to an instance? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Class Basics]33


Download ppt "Copyright © Texas Education Agency, 20131 Computer Programming Class Basics."

Similar presentations


Ads by Google