Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Ms. Knudtzon C Period Monday Sep 27 Week 4

2 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! Look at Circle Class with only has comments before signature of methods – but all the stuff Circle code does is straight-forward Look at Java Eyes – sometime there is no comment before a method (Bad Programming Practice)  look at the setPhysics method – we have no way when just looking at that method to know what the int we pass in is used for or what values work for the parameter But in that code there are comments that explain exactly what variables hold and what they are used for and there are several examples where the comments explain what a block of code does  these kind of comments help people who are reading this code figure out what it does, and it helps you when you go to change the code to understand where to begin Week 4

3 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 BlueJ gives you a good way of looking at the references too… Week 4

4 The Dot Operator A Dog remote control would have buttons to do something (to invoke the dog’s methods) Dog myDog = new Dog(); myDog.bark(); DOG Eat Bark Wag Built-in data type containers hold the actual value (Java has made the storage for them) But object data types (like Strings and objects we create) tell a way to get to the object so that you can tell the object what you want it to do. Dog myDog  Declare a reference variable new Dog()  creates the object (make space for this object to exist – “on the heap” Dog myDog = new Dog()  link the object and the reference together (Program the remote control) Think of the dot operator like pushing a button on a remote control Imagine this is a Remote control Week 4

5 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.getLicensePlate(); Talk about why in class Week 4

6 Grades Homework Handout: Let’s look at this
How would we model the classes we need to make? How do we start? Week 4

7 Introduction to Programming: Review for Test
Ms. Knudtzon C Period Tuesday Sep 28 Week 4

8 Introduction to Programming: Test 1
Ms. Knudtzon C Period Wednesday Sep 29 Week 4

9 Introduction to Programming
Ms. Knudtzon C Period Thursday Sep 30 Week 4

10 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

11 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

12 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

13 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

14 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

15 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”); amount = 200; System.out.println(“amount was not equal to 0”); AP tests tries to trick you with questions like this!! Week 4

16 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

17 while Loop while (amount > 0){ /* do something
and only stop doing it if the amount becomes 0 or less */ } Week 4

18 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 – covers for loops) Week 4

19 Introduction to Programming
Ms. Knudtzon C Period Friday Oct 1 Week 4

20 Funny PseudoCode Dave writes: Georg 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

21 Getting Input From A User
I’ve made a class for you to use to get input from the terminal window InputReader What is input? How is different from calling a method and typing in a parameter? Week 4

22 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

23 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

24 Turing Test http://en.wikipedia.org/wiki/Turing_test
Eliza – The Computer Therapist (better than my simple one) Loebner Prize Contest each year, up to a $100,000 award Loebner Prize $2,000 for the most human seeming of all bots for that year - awarded every year $25,000 for the first bot that judges cannot distinguish from a real human in a text-only based Turing Test (awarded once only) $100,000 to the first bot that judges cannot distinguish from a real human in a Turing Test that includes deciphering and understanding text, visual, auditory (and tactile?) input. The Loebner Prize dissolves once the $100,000 prize is won. Week 4

25 What else can input do? Two Simple Demos: Math program
Bad Turing test contender Week 4

26 Pop-up windows Like the javax.swing.JOptionPane
String input = JOptionPane.showInputDialog(“Enter first number”); int num = Integer.parseInt(input); //Display Final Results String result = "program got input: " + num; JOptionPane.showMessageDialog(null, result, “Program Results”, JOptionPane.INFORMATION_MESSAGE); But this program does not check to make sure the user actually inputs an integer  can get a run-time error! (The InputReader program forces the user to keep entering input until they input the expected type) From Java Docs, look up JOption Pane to see what it does showMessageDialog(Component parentComponent, Object message, String title, int messageType) parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used message - the Object to display title - the title string for the dialog messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE Week 4

27 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

28 What’s ++ Other math shortcuts
++ is a shortcut (unary increment operator) x++  x = x+1 Other math shortcuts x-- (unary decrement operator) x+=5 x-=10 x*=3 Note: x++ is different than ++x (Pre versus post operators) ++x -- Preincrement – increment x by 1 first then use the new value of x in anything that follows X++ - post increment – use current value of x in anything that follows and then increment it by 1 Week 4

29 Let’s examine this for(int x = 0; x < 5; x++){
//the body of the loop } Loop conditions: 1st: declare a starting value for the loop 2nd: define when the loop will stop (any conditional statement) 3rd: declare how the loop variable is incremented Keyword “for” starts out the loop () around the loop conditions 1st: declare a starting value for the loop 2nd: define when the loop will stop (any conditional statement)n  “Loop-continuation condition” 3rd: declare how the loop variable is incremented In loop conditions – declare a looping variable, in this case I am declaring a variable called x (looping variables are also ints – because you have to do something a specific integer number of times), then I initialize that value to 0 (you could initialize it to whatever you want, then set the stopping condition – this stops when x < 5 (which means that when x==5, the statement inside the loop won’t be called because x is no longer less than 5.) The last statement in the loop condition increments the loop variable (which actually happens after the loop body is executed) Note: the loop variable can be incremented by any int value you want) Week 4

30 Class Exercises Write a for loop that prints the numbers 10 to 100 (in increments of 10) for(int i = 10; i <=100; i= i+10){ System.out.println(i); } Week 4

31 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


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google