476b4c7027ec459726f49e7e44593804.ppt
- Количество слайдов: 31
Introduction to Programming Ms. Knudtzon C Period Monday Sep 27 Week 4 1
Introduction to Commenting Code • Comments before signature of methods – To tell what the method does • Comments for variables – To explain what the variable holds, what they are used for • Comments within methods – To explain what is going on; used when it is not immediately clear from looking at the code • Also, this allows me to see what you are trying to do, even if your code doesn’t work! – Make the comments be pseudocode if you can’t get your code to work! Week 4 2
Objects as Data Types • Object data types are not the same as primitive data types – They are references to the object, instead of containers holding the object – A reference is like a pointer or an address of an object • A good way to think of this object reference is as a remote control Week 4 3
The Dot Operator • A Dog remote control would have buttons to do something (to invoke the dog’s methods) Eat Wag Bark Dog my. Dog = new Dog(); my. Dog. bark(); DOG Think of the dot operator like pushing a button on a remote control Week 4 Imagine this is a Remote control 4
Objects starting other objects • Objects can create other objects • Why would they want to do this? • How do they do this? DMV dmv = new DMV(); dmv. get. License. Plate(); Week 4 5
Grades Homework • Handout: Let’s look at this • How would we model the classes we need to make? • How do we start? Week 4 6
Introduction to Programming: Review for Test Ms. Knudtzon C Period Tuesday Sep 28 Week 4 7
Introduction to Programming: Test 1 Ms. Knudtzon C Period Wednesday Sep 29 Week 4 8
Introduction to Programming Ms. Knudtzon C Period Thursday Sep 30 Week 4 9
Conditional Statement • Used for checking if a certain condition has been met int amount; //this is set somewhere else if (amount > 0){ /*in () is a statement that gives a true or false result */ //do something } else{ //do something else } Week 4 10
What if there are several conditions you want to check? int amount; //this is set somewhere else if (amount == 0){ //notice the == //do something } else if (amount < 0) { //do something else } else { // amount must be > 0… //do something different } Week 4 11
What if two things have to be true? if (amount > 0 && open > 0){ //do something } • && means “and” – it joins together the two boolean conditional statements and IF both are true, then the entire statement is true, if only one is true, then the statement is false Week 4 12
What if one of two things could be true but both don’t have to be? if (amount > 0 || open > 0){ //do something } • || means “or” Week 4 13
What if you already have a boolean value? boolean flag; if (flag){ //if flag is true this happens } else { //flag is false so this happens } Week 4 14
Tricking you… • The brackets in the “if” and “else” statements are optional in Java – BUT only if you want to execute ONE statement after the if or else statement if (amount == 0) System. out. println(“okay”); else System. out. println(“nonzero amount”); if(amount == 0) amount = 500; System. out. println(“amount equal to 0”); else amount = 200; System. out. println(“amount was not equal to 0”); Week 4 15
More Nesting if (grade > = 90){ System. out. println(“A”); } else if (grade > = 80) { System. out. println(“B”); } else if (grade > = 70){ System. out. println(“C”); } else if (grade > = 60){ System. out. println(“D”); } else { System. out. println(“F”); } Week 4 16
while Loop while (amount > 0){ /* do something and only stop doing it if the amount becomes 0 or less */ } Week 4 17
Reading • D&D : Chapter 4: Conditional Statements: Part 1 (Can read or skim – covers if/else and while) – Note: The reading introduces the code without brackets first… (I think it’s a bad programming practice, especially for beginners see Good Programming Practice 4. 4) – Also note: D&D puts everything in a main method – there are good reasons why we have not learned to program this way • D&D : Chapter 5: Conditional Statements: Part 2 (Sections 5. 1 -5. 4 – covers for loops) Week 4 18
Introduction to Programming Ms. Knudtzon C Period Friday Oct 1 Week 4 19
Funny Pseudo. Code Dave writes: if(you're around at the right time && georg does not do it instead) {i'm there} else {i sit in my cubicle and cry. } Georg writes: while(I don't like showing off && !kids_saw_the_printer) { Dave shows off the printer; kids_saw_the_printer = true; } we go to the pixar talk; we go to cake love; return to home; Week 4 20
Getting Input From A User • I’ve made a class for you to use to get input from the terminal window – Input. Reader • What is input? • How is different from calling a method and typing in a parameter? Week 4 21
Input • Input is something a non programmer might input to your computer program. • It could be anything – Text: • • • Inputting a password Filling out forms Doing math equations Asking the computer questions Telling the computer your feelings – But Also: Mouse/ Joystick Input • Clicking buttons to do something • Commands to a video game • etc Week 4 22
Can Machines Think? • Turing (1950) describes the following kind of game. Suppose that we have a person, a machine, and an interrogator. The interrogator is in a room separated from the other person and the machine. The object of the game is for the interrogator to determine which of the other two is the person, and which is the machine. The interrogator knows the other person and the machine by the labels ‘X’ and ‘Y’ -but, at least at the beginning of the game, does not know which of the other person and the machine is ‘X’ -- and at the end of the game says either ‘X is the person and Y is the machine’ or ‘X is the machine and Y is the person’. The interrogator is allowed to put questions to the person and the machine of the following kind: “Will X please tell me whether X plays chess? ” Whichever of the machine and the other person is X must answer questions that are addressed to X. The object of the machine is to try to cause the interrogator to mistakenly conclude that the machine is the other person; the object of the other person is to try to help the interrogator to correctly identify the machine. Week 4 23
Turing Test • http: //en. wikipedia. org/wiki/Turing_test • Eliza – The Computer Therapist (better than my simple one) – http: //www. manifestation. com/neurotoys/eliza. php 3 • Loebner Prize – Contest each year, up to a $100, 000 award Week 4 24
What else can input do? • Two Simple Demos: – Math program – Bad Turing test contender Week 4 25
Pop-up windows Like the javax. swing. JOption. Pane String input = JOption. Pane. show. Input. Dialog(“Enter first number”); int num = Integer. parse. Int(input); //Display Final Results String result = "program got input: " + num; JOption. Pane. show. Message. Dialog(null, result, “Program Results”, JOption. Pane. INFORMATION_MESSAGE); But this program does not check to make sure the user actually inputs an integer can get a run-time error! (The Input. Reader program forces the user to keep entering input until they input the expected type) Week 4 26
Beyond while loops… • What happens if you want to do the same thing a number of times (5 times, 10 times, etc) • Also what if you want to do some sort of calculation a certain number of times • Use a “FOR” loop for(int x = 0; x < 5; x++){ System. out. println(“hello”); } Week 4 27
What’s ++ • ++ is a shortcut (unary increment operator) • x++ x = x+1 • Other math shortcuts • x-- (unary decrement operator) • x+=5 • x-=10 • x*=3 Week 4 28
Let’s examine this for(int x = 0; x < 5; x++){ //the body of the loop } • Loop conditions: – 1 st: declare a starting value for the loop – 2 nd: define when the loop will stop (any conditional statement) – 3 rd: declare how the loop variable is incremented Week 4 29
Class Exercises • Write a for loop that prints the numbers 10 to 100 (in increments of 10) Week 4 30
Weekend Homework • Reading (Deitel & Deitel) • Journal – Explore your progress thus far and your thoughts about the class. Are we moving too fast? Too slow? Do you think the material is explained clearly? What more could I do to help you learn better in this course? Week 4 31