Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3.

Similar presentations


Presentation on theme: "Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3."— Presentation transcript:

1 Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3

2 Week 22 Notes and Questions Pixar talk at UMD on September 30, 3:00 pm –Any interest in going? Note from syllabus: Quiz on Friday AP Java Docs – subset of Java API –http://www.cs.duke.edu/csed/ap/subset/doc/http://www.cs.duke.edu/csed/ap/subset/doc/ Any questions on reading –Chapter 1, Deitel and Deitel

3 Week 23 What do these words mean in OO programming? Classes Objects/Instances Methods Statements Let’s explore…

4 Week 24 OO Programming as Modeling The parts of the model are objects in the problem domain What are the properties of objects? –Objects can be organized into categories –Objects do things –Objects know things

5 Week 25 Classes A class describes (abstractly) a category of objects –Think of a class as a blueprint for an object – it describes the boundaries of what an object can know or can do How would we model/program a dog?

6 Week 26 Programming a Dog How would you describe a dog? Would a dog be a class or an object? –Dogs in general –A bulldog named Bully Image from: www.cowpainters.com/ custfab_main.html Image from: www.irishfieldsports.com/ gundogs/golden.htm

7 Week 27 public class Dog{ // This is a comment, used to describe code } Creating a Dog class in Java So everyone can access it Tell java that this is a class Name the class Curly brace starts the class This indicates the start of a one-line comment Curly brace to end the class CODE GOES HERE

8 Week 28 Object/Instance A specific description, an instance of a class –A specific dog that we are modeling An object knows things and does things –In D&D book, called attributes and behaviors Things an object knows are variables –Data that is unique for an object of a given type Things an object does are methods Notice that the variables and methods are defined as part of the class –Variable values are set only for a specific instance

9 Week 29 Dog Class public class Dog{ String breed; //this is a variable String name; int size; // in pounds void bark(); }

10 Week 210 My Dog Instance Breed: Bulldog Name: Bully Size: 24 Color: Purple This dog can bark (invoke the method)

11 Week 211 What’s a Method? Things an object does void bark() { System.out.println(“Ruff! Ruff”); } The method doesn’t return anything The name of the method The empty parenthesis means there are no parameters in the method Brackets again! This is a statement; this tells the computer what you want to do in this method

12 Week 212 What can methods do? When we experimented with JavaEyes, we altered the code to change the program’s behavior –But we had to recompile each time Methods allow you to change what an object does or knows without recompiling

13 Week 213 What if you want to teach your dog to bark differently? Make the dog say “Grrruffff”, “Bow-wow”, or “Ruff Ruff” –Could change the Dog program each time and recompile like we did Friday –Or we could have the method take a parameter (a String of the sound we want the Dog to make)

14 Week 214 What’s a Statement? A line of code that gives a specific instruction Must end with a semicolon int x; /* a declaration is a type of statement */ x = 5; // an assignment statement System.out.println(“Ruff! Ruff”); /* a print statement */

15 Week 215 Your Turn to Explain… What’s a class? What’s an object? What’s an instance? What’s a method?

16 Week 216 For tomorrow Homework Worksheet Lab Day Also, individual discussion of goals for course with me

17 Week 217 Lab Day: Tuesday Sep 14 Picture and Shapes BlueJ Lab

18 Week 218 Introduction to Programming: Ms. Knudtzon C Period Thursday Sep 16 - Lecture 4

19 Week 219 Shapes Lab and Car Homework Any questions about shapes and picture lab? Any questions about Homework?

20 Week 220 Car Homework Discussion of what I saw in this assignment Good job exploring language –Students working ahead and teaching themselves things we haven’t even discussed yet.

21 Week 221 Programming Mistakes What happens if you forget a semi-colon? Forget parenthesis? Forget opening or closing brackets? Don’t use the “right” capitalization? Programming languages are very picky! –Why do you think that is?

22 Week 222 Understanding an Object’s State Look at variables (and their values) in BlueJ Look at the Inspect function in the object popup menu –Show the object’s data types and their values (aka the state of the object of the object’s variables) –What happened in to the state of the object when you call circle  moveLeft ? –What happens when you set a variable in a method?

23 Week 223 Elements of a method Signature of the method –public void changeColor(String newColor) –Public/private/protected is optional –The return type –The name of the method Good programming practice: Make it descriptive –Parameter declarations (if any) Body Comment –Good Programming Practice: Place comments before methods that describe what the method does

24 Week 224 Methods Two special kinds of methods Accessor methods get the value of a variable –getName() Mutatormethods change or set the value of a variable –setName (String newName) –changeName(String newName)

25 Week 225 Variables Variables need a data type and a name. Think of a variable as a cup. A container. It holds something. –The type tells Java the size of the container. –The name is so you know which container is yours. –(Maybe at a party you have a big red cup of soda – if you put your name on it, you can find it if you put it down)

26 Week 226 Primitive Data Types What is an int ? (Think back to math…) What might other data types be? –What other kinds of information might an object need to know? True or False values ( boolean ) Decimal numbers ( double ) Character ( char ) A variable's data type determines the values that the variable can contain and the operations that can be performed on it Primitive Data Types are built-in data types (8 of them) – boolean, char, byte, short, int, long, float, double

27 Week 227 Pre-Defined Data Types Pre-defined data types are just objects that Java has provided you that store information What is a String ? –Stores a section of text –Always in double quotes (what happens if you forget those?) Java has several other data structures to store data in useful ways –We will be learning many of them this semester and next

28 Week 228 Naming Practices in Programming Class names start with a capital letter Method names don’t, but use capital letters for subsequent words –Like changeMyOil Instance variables start with a lower-case letter Constant variables (that’s an oxymoron…) –In ALL_CAPS, like MAX_INT_SIZE

29 Week 229 Variable Names Can be almost anything But not Java reserved words… –abstract do if package synchronized boolean double implements private this break else import protected throw byte extends instanceof public throws case false int return transient catch final interface short true char finally long static try class float native strictfp void const for new super volatile continue goto null switch while default Luckily, BlueJ colors these words a different color so you will know there is something special about them before you know what they all mean


Download ppt "Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3."

Similar presentations


Ads by Google