Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.

Slides:



Advertisements
Similar presentations
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Advertisements

Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
COMS W1004 Introduction to Computer Science May 30, 2008.
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Lecture 9a Instructor: Craig Duckett CALL ME. Assignment 2 Revision DUE THIS Wednesday, August 5 th Assignment 3 DUE Monday, August 10 th Assignment 3.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Lecture 2: Classes and Objects, using Scanner and String.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Overloading Methods 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.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
BIT115: Introduction to Programming
Lecture 13 Instructor: Craig Duckett CALL ME. Assignment 3 DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of a team.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Classes - Intermediate
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
OOP Basics Classes & Methods (c) IDMS/SQL News
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Lecture 11 Instructor: Craig Duckett Instance Variables.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Monday, October 10 th I will double dog try to have.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.
CompSci 230 S Programming Techniques
BIT 115: Introduction To Programming
BIT 115: Introduction To Programming
User-Written Functions
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Review for Midterm 3.
Presentation transcript:

Lecture 6a Instructor: Craig Duckett

Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by midnight Assignment 2 Assignment 2 is due NEXT Monday, July 27 th, by midnight Mid-Term is due NEXT Wednesday, July 29 th It is scheduled for the entire class period, but generally takes between 1.5 to 2 hours to complete. Once you are finished with your exam, turn it in, and you are free to go for the rest of the class time. I'll have the exam graded and results posted to StudentTracker by Friday morning (hopefully sooner), and return the exam to go over it on Monday, August 3 rd. Mid-Term Exam

Assignment 1 Assignment 1 GRADED Assignment 2 Assignment 2 DUE Lecture 7, Monday, July 27 th, by midnight Assignment 1 Revision Assignment 1 Revision DUE Lecture 8, Wednesday, July 29 th, by midnight Assignment 2 Revision Assignment 2 Revision DUE Lecture 10, Wednesday, August 5 th, by midnight Assignment 3 Assignment 3 DUE Lecture 11, Monday, August 10 th, by midnight Assignment 3 Revision Assignment 3 Revision DUE Lecture 13, Monday, August 17 th, by midnight Assignment 4 Assignment 4 DUE Lecture 15, Monday, August 24 th, by midnight NO REVISION AVAILABLE Extra Credit 01 Extra Credit 01 DUE Lecture 15, Wednesday, August 26 th, by midnight 3 Assignment Announcements

4 Assignment 2: Walk-Through

But First… The Quiz!

A Quick Look at What We've Learned So Far Class that has the just the main method: public class someName extends Object [class name has to match file name] Class that holds Robot extension and the main method public class someName extends Robot [class name has to match file name] File that holds two classes, one for the constructor and methods, one for main class someName extends Robot public class otherName extends Object [class name has to match file name] When using more than one class in a file, the CLASSES NEED DIFFERENT NAMES SEE EXAMPLE COMPARISON ON NEXT SLIDE  Constructor – lining up the new class with the super class Methods – public void someMethod() Curly Braces – Opening { and Closing } "Squiggles" properly indented this keyword – a generic placeholder for whatever robot will be using the method if statement, while statement, if…else statement Trace and Debug Tables Counters, including increment and decrement +, -,, >=, ==, != Robot methods: pickThing(), putThing(), move(), turnLeft(), frontisClear(), getDirection(), getStreet(), getAvenue(), canPickThing(), countThingsInBackpack(), Output – System.out.print ("String"); or System.out.println("String"); Java Library - import java.util.*; Input – Scanner Keyboard = new Scanner(System.in); hasNextInt(), nextInt(), nextLine()

S We Are Nearing the END of the FIRST HALF OF THE QUARTER--- S We Are Nearing the END of the FIRST HALF OF THE QUARTER (one more week remaining) --- WHAT DOES THIS MEAN IN THE COSMIC SCHEME OF THINGS? In the SECOND HALF of the Quarter there will be: 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!

What We're Going Over Today First Half today we're going to have a closer look at parameters and arguments Second half today, we're going to look at "instance variables" FYI: You should have enough coding behind you by the end of the first half lecture today to successfully complete the navigational part of the Assignment 3 Maze (i.e., to move through the Maze from start to finish using logic) After today's second half lecture on "instance variables" you should have the coding wherewithal 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 whole shebang at the end of the program.

More Flexible Methods

The Way We Originally Learned (“Hard Coded”) public void move2() { this.move(); this.move(); } A More Flexible Way (“Argument Coded”) karel.howManyMoves(2); karel.howManyMoves(3); karel.howManyMoves(4); karel.howManyMoves(12); 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  or  public void move3() { this.move(); this.move(); } public void move4() { this.move(); this.move(); }

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

Chapter 4.6 Chapter 4.6: Using Parameters We need a way to pass information to a service. rob.moveMultiple(3); or rob.moveMultiple(7); or even Scanner keyboard = new Scanner(System.in); int howFar = keyboard.nextInt(); rob.moveMultiple(howFar); We’d like to be able to say: The actual info being passed to the service is called an ARGUMENT The service 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 numberOfIntersections)

Chapter 4.6 Chapter 4.6: Using Parameters For integers ints, this creates (within moveMultiple) a COPY of whatever we put inside the parentheses in main. Inside of 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 method service to expect some info is called a PARAMETER. Of course we’ve been doing this all along whenever we’ve been declaring a new instance of a Robot.

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); ARGUMENT PARAMETER counter moves howFar

Chapter 4.6 Chapter 4.6: Using Parameters Notice how the parameters that we declare are matched up against the “hard-coded” arguments that we give it: import becker.robots.*; public class MrRoboto extends Robot { public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.turnAround(); lisa.move(); lisa.turnAround(); } These, in turn, are passed through the superclass constructor.

Chapter 4.6 Chapter 4.6: Using Parameters Suppose we want a subclass of Robot that can easily tell us if it has gone past a particular Avenue, for example Avenue We could use a getAvenue method and “hard-code” compare it to 18: if(this.isPastAvenue == 18) {// what to do when the robot has strayed too far} but our code is more self-documenting with a predicate: if(this.isPastAvenue(18)) // The coder supplies 18 here {// what to do when the robot has strayed too far} // or better yet: if(this.isPastAvenue(anAvenue)) // The user supplies 18, or 22, or 66, etc. // using the Scanner class and System.in input {// what to do when the robot has strayed too far} The isPastAvenue method is written as follows: private boolean isPastAvenue(int anAvenue) // either way this is used { return this.getAvenue() > anAvenue; // the same and with any } // number being entered In main you’d call it this way: this.isPastAvenue(anAvenue );

Using a While Statement with a Parameter The following method moves a robot east to Avenue 18. public void moveToAvenue18() // <-- 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(18); In main you’d call it this way: this.moveToAvenue(destAve);

Chapter 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. public void moveTheBot() { int howFar = 2; // <-- temporary variable this.street = this.street + howFar; } Parameter Variables versus Temporary Variables public void moveTheBot(int howFar) // <-- parameter variable { this.street = this.street + howFar; } In main you’d call it this way: this.moveTheBot(); In main you’d call it this way: this.moveTheBot(2); In main you’d call it this way: this.moveTheBot(howFar);

public void moveMultiple() { this.move(); this.move(); this.move(); this.move(); } rob.moveMultiple(); // rob will move 5 times 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++; } } rob.moveMultiple(); // rob will move 5 times 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; } } rob.moveMultiple(); // rob will move 5 times, and only 5 times EXAMPLE Method Without a 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; } } 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 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; } } 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 this.moveMultiple(numMoves); } EXAMPLE Method with Parameter Argument Main LET’S HAVE A CLOSER LOOK 

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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 + "."); this.moveMultiple(numMoves); } EXAMPLE Method Main numberOfIntersections counter 0

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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 + "."); this.moveMultiple(numMoves); } EXAMPLE Method Main numMoves numberOfIntersections counter 0

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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 + "."); this.moveMultiple(numMoves); } EXAMPLE Method Main numberOfIntersections numMoves 5 counter 0

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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 + "."); this.moveMultiple(numMoves); } EXAMPLE Method Main numberOfIntersections numMoves 5 counter 0

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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 + "."); this.moveMultiple(numMoves); } EXAMPLE Method Main numMoves numberOfIntersections 5 5 counter 0

Chapter 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 - - -

Overloading 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

32 LECTURE 6a: ICE Part 1: