Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT115: Introduction to Programming

Similar presentations


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

1 BIT115: Introduction to Programming
Lecture 10 Instructor: Craig Duckett Second Half of BIT115 Quarter Begins

2 Announcements Assignment 1 Revision DUE TONIGHT in StudentTracker by midnight If you have not yet submitted an Assignment 1, this is your last chance to do so to earn points, otherwise you will get a zero, and I dislike doing that! I would want everyone in this class to be successful!

3 Assignment Dates (By Due Date)
Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday, January 31st Assignment 1 Revision (LECTURE 10) Section 1: Wednesday, February 7th Assignment 2 Revision (LECTURE 12) Section 1: Wednesday, February 14th Assignment 3 (LECTURE 13) Section 1: Wednesday, February 21st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5th Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12th The Fickle Finger of Fate

4 Great Job! Class Average is 125/150!
But First… No Quiz! Mid-Term Post-Mortem Great Job! Class Average is 125/150!

5 TODAY BEGINS THE SECOND HALF OF THE BIT115 QUARTER --
WHAT DOES THIS MEAN IN THE “COSMIC SCHEME OF THINGS”? Less Theory, More Hands-On Work (Less means Less, not No) Less Hand-Holding, More Trial-and-Error Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (on occassion) Aggravation. Grrrr! When You Do Get Aggravated: Remember to STEP AWAY from your code occasionally, take a break, walk around, go and eat, contemplate the great outdoors, then come back. If it is late at night, go to bed. You may discover that bizarre thing that sometimes can happen, where you DREAM IN CODE, and wake up in the morning refreshed and with the beginnings of a solution!

6 QUICK REVIEW The Scanner Class

7 INPUT: Scanner Class REVIEW
import java.util.*; or import java.util.Scanner; Scanner keyboard = new Scanner(System.in); nextInt() Assumes there is an int and does something with it hasNextInt() Checks to see if there is an int (boolean true or false) nextLine() Replaces the int in the keyboard buffer with a newline character (Enter) so the program won't use the int again

8 The Scanner Class INPUT
To read input from the keyboard we use the Scanner class. Like Random, the Scanner class is defined in the Java Library package called java.util, so we must add the following statement at the top of our programs that require input from the user: import java.util.*; // <-- I usually do this or import java.util.Scanner;

9 The Scanner Class Scanner objects work with System.in
To create a Scanner object: Scanner keyboard = new Scanner(System.in); NOTE: Like any other object, keyboard here is just a name “made up” by the coder and can be called anything instead—input, feedIine, keyIn, data, stuffComingFromTheUser, etc.—although it should represent a word most apt to its purpose. In this case I am using the name keyboard since it seems apt as I’ll be using the keyboard to enter data (i.e., do the input)

10 Variable Container Scanner keyboard = new Scanner(System.in);
In order to get data from the user using the Scanner object keyboard, you’ll first have to determine the type of data that is going to entered (whether an integer, or a floating-point number, or something else) and create a variable container to hold that data. You can do this in one of two different ways, as a separate declaration or tied directly into a method call. int someNum = 0; someNum = keyboard.nextInt(); int someNum = keyboard.nextInt(). or

11 New Scanner Methods These are for ints (integers). There are also Scanner methods available for floats, etc, which we'll see later on in the quarter  nextInt() Assumes there is an int and does something with it (integer value) hasNextInt() Checks to see if there is an int (boolean true or false) nextLine() Replaces the int in the keyboard buffer with a newline character (like “Enter”) so the program won't use the int again Okay: Let's have a look at the Basic_Keyboard_IO.java program from the previous lecture 

12 A CLOSER LOOK Parameters Arguments Method Overloading

13 What We're Going Over Today
Today we're going to have a closer look at: parameters and arguments We're also going to look at: method overloading You should have enough coding behind you by the end of the NEXT Lecture (LECTURE 12) to successfully complete the coding for Assignment 3 “The Maze”: Successfully Navigate through the Maze from start to finish using various logic After NEXT lecture, use "instance variables" to set up the counters and be able to collect the number of moves made, how many times the robot moved in any particular direction, and then print out the various totals at the end of the program.

14 More Flexible Methods

15 More Flexible Methods The Way We Originally Learned (“Hard Coded”)
public void move2() { this.move(); this.move(); } public void move3() { this.move(); this.move(); } public void move4() { this.move(); this.move(); } A More Flexible Way (“Argument Coded”) public void howManyMoves(int numMoves) { int counter = 0; while(counter < numMoves) { this.move(); counter++; } // Note: This method has no error handling }  New Flexible Method How it might be called in main  karel.howManyMoves(2); karel.howManyMoves(3); karel.howManyMoves(4); karel.howManyMoves(12); or  or  or 

16 Argument, Pass, Parameter

17 Chapter 4.6: Using Parameters
You’ll notice that every new method (service) we’ve created does something: Each time we call upon one of those services, it’ll always do the same thing public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); public void turnRight() { this.turnAround(); We could have asked for user input, but then the method would fill TWO ROLES Doing <something> Interacting with the user We'd like each service to have a single, well-defined, and easy to summarize role Thus, a method like moveMultiple() should move the robot through multiple intersections Things happening in main should run the part of the program that relays instructions from the user to the robot(s)

18 Chapter 4.6: Using Parameters
We need a way to pass information to a method or “service”. We’d like to be able to say: rob.moveMultiple(3); or rob.moveMultiple(7); or even Scanner keyboard = new Scanner(System.in); int howFar = keyboard.nextInt(); rob.moveMultiple(howFar); The actual data being passed to the method is called an ARGUMENT The method must also be told to expect this information AND make room for it to be stored somewhere in memory, so instead of: public void moveMultiple() we'll write public void moveMultiple(int someNumber)

19 Chapter 4.6: Using Parameters
For integers ints, this creates (within moveMultiple(someNumber) ) a COPY of whatever we put inside the parentheses in main. Inside of the moveMultiple() method someNumber 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 method service to expect some info is called a PARAMETER. Let’s have a look at the next slide

20 Arguments and Parameters
A method in new class up top: public void moveMultiple(int moves) { int counter = 0; while(counter < moves) move(); counter++; } In main below: Scanner keyboard = new Scanner(System.in); int howFar = keyboard.nextInt(); rob.moveMultiple(howFar); PARAMETER moves Now in the programming world the language tends to get a little loose, so these words are often interchanged since both things are dealing with values being passed into and out of parentheses. Google counter PASS howFar ARGUMENT No need to fear! We will look at this in step-by-step detail in a moment 

21 Using a While Statement with a Parameter
The following method moves a robot east to Avenue 18. public void moveToAvenue18( ) // <-- empty, no parameter { while(this.getAvenue() < 18) { this.move(); } This is very limited, and only useful to move the robot specifically to Avenue 18 since it was “hard-coded” to do so. With a parameter, however, it can be used to move the robot to any avenue east of its current location. public void moveToAvenue(int destAve) // <-- with parameter { while(this.getAvenue() < destAve) { this.move(); } In main you’d call it this way: this.moveToAvenue(20); //or 12 or 18 OR in main you’d call it this way: this.moveToAvenue(someVar);

22 Chapter 6.2.2: Reviewing Parameter Variables
In this section, we will show how parameter variables are closely related to temporary variables, explore using parameters with constructors, and discuss overloading. Parameter Variables versus Temporary Variables public void howFarToMove() { int howFar = 2; // <-- temporary variable System.out.println("You asked to move " + howFar + "times."); } In main you’d call it this way: this.howFarToMove(); public void howFarToMove(int howFar) // <-- parameter variable { System.out.println("You asked to move " + howFar + "times."); } In main you’d call it this way: this.howFarToMove(2); OR In main you’d call it this way: this. howFarToMove(someVar);

23 Putting it All Together
Let’s look at how arguments and parameters work with a demonstration using the Robot and RobotSE classes, before we do a walk-through how to use arguments and parameters with our own methods. First, we’ll have a look at the Robot and RobotSE classes in the Becker Robot Library: Then, we’ll look at some demonstration files: simple_argument.java scanner_argument_with_Robot_class.java scanner_argument_with_RobotSE_class.java simple_scanner_argument.java scanner_argument_with_method_parameter.java scanner_all_in_one_method.java

24 PARAMETERS: Step-by-Step

25 EXAMPLES: Step-by-Step
Method Without a Parameter Argument public void moveMultiple() { this.move(); this.move(); this.move(); this.move(); } Main rob.moveMultiple(); // rob will move 5 times

26 EXAMPLE Method Without a Parameter Argument Main
public void moveMultiple() { int counter = 0; while( counter < 5) { if(this.frontIsClear()) { this.move(); counter = counter + 1; // This is the same as counter++; } } Main rob.moveMultiple(); // rob will move 5 times

27 EXAMPLE Method Without a Parameter Argument Main
public void moveMultiple() { int counter = 0; while( counter < 5) // 5 is “hard-coded” here, so easier to change at { // this coded location but there is a better solution if(this.frontIsClear()) { this.move(); counter = counter + 1; } } Main rob.moveMultiple(); // rob will move 5 times, and only 5 times

28 EXAMPLE Method with Parameter Argument Main
public void moveMultiple(int numberOfIntersections) // declare and add parameter { int counter = 0; while( counter < numberOfIntersections) // replace 5 with the passed argument { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } Main rob.moveMultiple(5); // <-- 5 is placed within the method call as an argument // The 5 is still being “hard-coded” but this time as a parameter argument // instead of an integer defined inside the method. This makes it easier since // different numbers can now be called when the method is used, but why not // free up the method to call up ANY input actually entered by the user? That // way no number is being “hard-coded” before the program is compiled and run

29 EXAMPLE Method with Parameter Argument Main
public void moveMultiple(int numberOfIntersections) // declare and add argument { int counter = 0; while( counter < numberOfIntersections) // replace 5 with that argument { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } Main System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt()) // hasNextInt checks the input is an integer { int numMoves = keyboard.nextInt(); // nextInt puts it into memory container System.out.println ("You entered a " + numMoves + "."); // called numMoves rob.moveMultiple(numMoves); } LET’S HAVE A CLOSER LOOK  simple_scanner_argument.java

30 EXAMPLE Method ? public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter Main System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

31 EXAMPLE Method ? public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter ? Main numMoves System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

32 EXAMPLE 5 Method Main “…Let’s imagine the user types in a 5”
public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter 5 “…Let’s imagine the user types in a 5” Main numMoves System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

33 EXAMPLE Method public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter 5 Main numMoves System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); // nextInt actually gets the input System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

34 EXAMPLE Method 5 public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter 5 Main numMoves System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

35 Method Overloading Test() Test(int x) Test(int x, int y)

36 Chapter 6.2.2: Overloading In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. Polymorphism is the ability of an object to take on many forms, and uses the “is a” test determine multiple inheritance through from different classes, subclasses, etc. Method overloading is one of Java's most exciting and useful features. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Let’s have a look-see …

37 Overloading a: 10 a and b: 10, 20 double a: 5.5 Result: 30.25
public class MethodOverloading extends Object { public void test(int a) { System.out.println("a: " + a); } public void test(int a, int b) { System.out.println("a and b: " + a + "," + b); } public double test(double a) { System.out.println("double a: " + a); return a*a; } public static void main(String args[]) { MethodOverloading MethodOverloading = new MethodOverloading(); double result; MethodOverloading.test(10); MethodOverloading.test(10, 20); result = MethodOverloading.test(5.5); System.out.println("Result : " + result); } } a: 10 a and b: 10, 20 double a: 5.5 Result: 30.25

38 Assignment 3: The Maze Read the Directions !
You can work on Assignment 3 alone (one person) -or- You can work on Assignment 3 with a partner (two people) You can work on Assignment 3 as a team of three (three people) ONLY ONE PERSON HAS TO SUBMIT, BUT MAKE SURE EVERYONE’S NAMES ARE ON ALL THE FILES

39 HINT #1: Assignment 3: The Maze The Basic Move Logic:
Turn Left (or Right) While the front is NOT clear Turn Right (or Left) Move

40 A NOTE ABOUT THE ICE NUMBERS GOING FORWARD:
Today is Lecture 11, and the ICE number is ICE 11, but sometimes these will not always align with the lecture number. The ICE title in the browser tab may also show a different number. This is because some quarters have a different number of scheduled days than Fall quarter, so I include a buffer/study day right before the Mid-Term much like the Buffer/Study day this quarter right before the Final Exam. Thus, going forward, some of the ICE numbers may be “off-by-one” when compared to the Lecture number.


Download ppt "BIT115: Introduction to Programming"

Similar presentations


Ads by Google