Download presentation
Presentation is loading. Please wait.
Published byJonathan Sanders Modified over 9 years ago
1
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah) bkweusijana@cascadia.edu http://edutek.net/cascadia/bit115
2
BIT 115: Introduction To Programming 2 Quiz 06 https://catalysttools.washington.edu/webq/s urvey/babaw/53908https://catalysttools.washington.edu/webq/s urvey/babaw/53908 Typo in ICE flowchart –Should be 1, 2, 3 and 4 (not 1, 2, 1 and 2) –Include putting down and picking up 2
3
BIT 115: Introduction To Programming Reading Reading for today: –( F.5 - Arguments to methods ) –2.2.3 - Constructors –6.6 - Printing expressions, Using a debugger –F.6 - Getting user input, printing output Reading for next class: –F.7 - Object Instance Variables –2.2, 5.3 - Extending classes, Nesting tatements –Review everything for midterm
4
BIT 115: Introduction To Programming 4 Note-taker Needed I need a student to volunteer to be a note taker for the quarter You will be paid $50 by the DSS office See me after class for a form 4
5
BIT 115: Introduction To Programming A2 –Due Wednesday –have you worked out meeting times w/ partner? Outcomes Journal #2 –Also due Wednesday HOMEWORKHOMEWORK
6
BIT 115: Introduction To Programming Today I/O - Input, Output Parameters Instance Variables –Giving robots memory
7
BIT 115: Introduction To Programming 7 Input / Output We've made programs that can adapt to changing situations –(while there's stuff to pick up, pick it up) However, everything is predictable –Given the same City, the Robot will do the same thing each time What if you wanted to make a game, "Drive The Robot"? 7
8
BIT 115: Introduction To Programming 8 I/O We need a way to ask the user what to do (info we get from the user is input) And to deliver information to the user (info we print to the user is output) We're going to: –Print text to the screen –Get text from the keyboard –We'll leave stuff involving windows, buttons, etc till (much) later…. 8
9
BIT 115: Introduction To Programming 9 Demo: ICE_06_IO.java –typing 2 moves the Robot 9
10
BIT 115: Introduction To Programming 10 Standard Java output mechanism System.out.println( "Whatever I want to print, surrounded by double-quotes" ); System.out : The name of the object we're asking to do the output println(...) : The method that actually does the output-ing "…": Everything in between the double-quotes is outputted to the user's screen (but not the quotes) –MS Word has ”Smart Quotes” that look better, but don't work as double-quotes. If you write code in Word (why?) watch out for them if you copy-and-paste code 10
11
BIT 115: Introduction To Programming 11 Special input mechanism Standard Java input is actually a bit more tricky, so we're going to use some old input classes –This software ONLY gets input from the keyboard –At the top of the file write: import becker.io.*; –let’s the compiler know about the classes in /becker/io/ 11
12
BIT 115: Introduction To Programming 12 Getting keyboard input 1. Create an object that'll get stuff from the keyboard for us: TextInput in = new TextInput(); 2. If you haven't done so already, you should tell Java that you'll need to remember this number: int menu; 3. Each time you want to get something from the keyboard, you need to tell it to get the thing you want For example, if I want to get a whole number (such as 1, 2, or 15 - an int ) menu = in.getInt(); 4. You can combine the declaration of a variable with an assignment statement, like so: int menu = in.getInt(); Remember that you can only use each name once Notice that we print out enough instructions to give the user a good shot at actually providing reasonable input 12
13
BIT 115: Introduction To Programming 13 ICE06: Get User input, based on the flowchart Do Part 1 now Skip Parts 2 & 3 until after class 13
14
BIT 115: Introduction To Programming 14 Parameters Most of our new services so far are not dynamic –Each method call will do the same thing We could have it ask for user input –But then the service would fill TWO roles: Doing Interacting with the user We'd like each service to have a single, well- defined, and easy to summarize role –Thus, methods like moveMultiple should the robot through multiple intersections –Methods like main should run the part of the program that relays instructions from the user to the Robot(s) 14
15
BIT 115: Introduction To Programming 15 Parameters We need a way to pass information to a service. We'd like to say – rob.moveMultiple(3); –or – rob.moveMultiple(7); –or even – TextInput in = new TextInput(); – int howFar = in.getInt(); – rob.moveMultiple(howFar); The actual info passed to the service is called an ARGUMENT (see p. 189 & 206) 15
16
BIT 115: Introduction To Programming 16 Parameters The service must be told to expect this information. So instead of public void moveMultiple() we'll write public void moveMultiple(int numberOfIntersections) For int s, this creates (within moveMultiple ) a COPY of whatever we put inside the parentheses in main. Inside moveMultiple, numberOfIntersections behaves just like any other int variable. We can print it out, assign new values to it, use it in a while loop, etc. The thing that tells the service to expect some info is called a PARAMETER 16
17
BIT 115: Introduction To Programming 17 Parameters Notice how the parameters that we declare are matched up against the arguments that we give it: We can add our own parameters, so long as we also add arguments when creating a new kind of Robot, such as MysteryRobot 17
18
BIT 115: Introduction To Programming 18 ICE06 Part 4: Parameters ~15 minutes! 18
19
BIT 115: Introduction To Programming 19 Local Variables, Instance/Object Variables All the local variables we've looked at so far will disappear at the end of the service. –This means that they're temporary. We want memory that won't go away between service calls –We want the Robot to remember something, not just get some temporary working space 19
20
BIT 115: Introduction To Programming 20 Permanent Memory Pattern 1. One (or more) instance/object variables (To Remember Things Permanently) 2. The instance variable must have a starting value – Normally, you should do this in the constructor 3. Use the memory in 2 or more methods – Either 2 separate methods, or else the same method, called twice, so that the memory won't go away between calls. 20
21
BIT 115: Introduction To Programming 21 Constructor class MysteryRobot extends Robot { MysteryRobot( City c, int ave, int st, int dir, int num) { super(c, ave, st, dir, num); – // your special code goes here AFTER the call to super! } This is called a constructor service, because it constructs an individual MysteryRobot object You can put (almost) any code in there that you can normally put inside main. You can have it print, create things, create other robots, and assign values to variables. –(Normally you'd only assign values to variables) All this stuff MUST go after the call to super. 21
22
BIT 115: Introduction To Programming 22 Constructor We can add our own parameters, so long as we also have the old ones when creating a new kind of Robot, such as MysteryRobot We pass the usual arguments to the constructor of the parent class (the superclass), in this case Robot's constructor class MysteryRobot extends Robot { MysteryRobot( City c, int ave, int st, int dir, int num, int HowMany) { super(c, ave, st, dir, num); System.out.println("Hey, HowMany is: " + HowMany); } The code for creating the new object is: MysteryRobot mary = new MysteryRobot(ForgetsVille, 0, 4,Directions.EAST, 0, 5); 22
23
BIT 115: Introduction To Programming 23 Constructor We've seen these (really really briefly), but it's important to understand them They're a command (a method) that gets run when the robot is constructed! –shortly after the new command gets memory Demo: –when the stuff gets printed –notice how we can even put a while loop in the constructor 23
24
BIT 115: Introduction To Programming 24 Constructors are methods with extra rules 1. Constructors’ names are identical to the class’s name 2. They can never return a value, so they don't have a return value. 3. Make them public ! Not making them public could be very bad, since that means other code might not be allowed to access them. 4. Constructor’s can be overloaded, just like normal methods can be. 5. Within the { and }, they're basically normal methods 6. What's special is that the JVM automatically calls one when a new object is created. 7. The Java compiler will automatically create a default one for you, if you didn't. When you create even a single constructor, javac stops auto-generating the default constructor, so you'll have to write that yourself. 24
25
BIT 115: Introduction To Programming 25 Tracing demo Example of how to trace: Program_Trace_Table_Params.xls –Add an extra column for each parameter –Put a dash (-) or leave when an object doesn’t exist 25
26
BIT 115: Introduction To Programming 26 ICE06 Part 5: Code Tracing: Instance Variables 26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.