Lecture 10. Instructor: Craig Duckett

Size: px
Start display at page:

Download "Lecture 10. Instructor: Craig Duckett"

Transcription

1 Lecture 10 Instructor: Craig Duckett

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 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday, January 31 st Assignment 1 Revision (LECTURE 10) Section 1: Wednesday, February 7 th The Fickle Finger of Fate Assignment 2 Revision (LECTURE 12) Section 1: Wednesday, February 14 th Assignment 3 (LECTURE 13) Section 1: Wednesday, February 21 st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5 th Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12 th 3

4 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 INPUT The Scanner Class 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(); or int somenum = keyboard.nextint().

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 isan 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() public void move3() public void move4() A More Flexible Way ( Argument Coded ) public void howmanymoves(int nummoves) New Flexible Method int counter = 0; while(counter < nummoves) counter++; // Note: This method has no error handling How it might be called in main or or or karel.howmanymoves(2); karel.howmanymoves(3); karel.howmanymoves(4); karel.howmanymoves(12);

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() public void turnright() this.turnaround(); this.turnleft(); We could have asked for user input, but then the method would fill TWO ROLES 1. Doing <something> 2. Interacting with the user We'd like each service to have a single, welldefined, 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 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 ARGUMENT moves counter howfar PASS 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 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) 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() Main rob.movemultiple(); // rob will move 5 times

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

27 EXAMPLE Method Without a Parameter Argument 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()) counter = counter + 1; Main rob.movemultiple(); // rob will move 5 times, and only 5 times

28 EXAMPLE Method with Parameter Argument public void movemultiple(int numberofintersections) // declare and add parameter int counter = 0; while( counter < numberofintersections) // replace 5 with the passed argument if(this.frontisclear()) 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 public void movemultiple(int numberofintersections) // declare and add argument int counter = 0; while( counter < numberofintersections) // replace 5 with that argument if(this.frontisclear()) 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()) counter = counter + 1;? numberofintersections 0 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()) counter = counter + 1;? numberofintersections 0 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); nummoves

32 EXAMPLE Method public void movemultiple(int numberofintersections) int counter = 0; while( counter < numberofintersections) if(this.frontisclear()) counter = counter + 1; numberofintersections 0 Main Let s imagine the user types in a 5 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); counter 5 nummoves

33 EXAMPLE Method public void movemultiple(int numberofintersections) int counter = 0; while( counter < numberofintersections) if(this.frontisclear()) counter = counter + 1; numberofintersections 0 Main counter 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); 5

34 EXAMPLE Method public void movemultiple(int numberofintersections) int counter = 0; while( counter < numberofintersections) if(this.frontisclear()) counter = counter + 1; 5 numberofintersections 0 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); counter 5 nummoves

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

36 Let s have a look-see 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.

37 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

38 Assignment 3: The Maze Read the Directions! You can work on Assignment 3 alone (one person) You can work on Assignment 3 with a partner (two people) -or- -or- 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 Assignment 3: The Maze HINT #1: 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.

Lecture 8. A series on variable types This episode starring Parameter Variables

Lecture 8. A series on variable types This episode starring Parameter Variables Lecture 8 A series on variable types This episode starring Parameter Variables Declaring an Integer Variable Variable Types Variable Type How it is defined Scope (how long it lasts) Uses Local Variable

More information

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 8  INPUT  Instructor: Craig Duckett Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment

More information

Lecture 11. Instructor: Craig Duckett. Instance Variables

Lecture 11. Instructor: Craig Duckett. Instance Variables Lecture 11 Instructor: Craig Duckett Instance Variables Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday,

More information

Lecture 6. Instructor: Craig Duckett

Lecture 6. Instructor: Craig Duckett Lecture 6 Instructor: Craig Duckett Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment 1 submissions. If you did not submit an Assignment

More information

Lecture 13. Instructor: Craig Duckett. Boolean Expressions, Logical Operators, and Return Values

Lecture 13. Instructor: Craig Duckett. Boolean Expressions, Logical Operators, and Return Values Lecture 13 Instructor: Craig Duckett Boolean Expressions, Logical Operators, and Return Values Assignment 3: The Maze DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of

More information

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section

More information

BIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett

BIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu Lecture 3 Announcements By now everyone should be up and running with Java, jgrasp, and the Becker Robots

More information

Lecture 7. Instructor: Craig Duckett OUTPUT

Lecture 7. Instructor: Craig Duckett OUTPUT Lecture 7 Instructor: Craig Duckett OUTPUT Lecture 7 Announcements ASSIGNMENT 2 is due LECTURE 8 NEXT LECTURE uploaded to StudentTracker by midnight Assignment 2!!! Assignment Dates (By Due Date) Assignment

More information

Lecture 12. Instructor: Craig Duckett

Lecture 12. Instructor: Craig Duckett Lecture 12 Instructor: Craig Duckett Assignment 2 Revision DUE TONIGHT! Uploaded to StudentTracker by midnight YOUR LAST CHANCE TO EARN POINTS! Assignment 3 Maze DUE Lecture 13 Uploaded to StudentTracker

More information

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

Combo Lecture Lecture 14/15. Instructor: Craig Duckett Combo Lecture Lecture 14/15 Instructor: Craig Duckett Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday,

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Instructor: Craig Duckett

Instructor: Craig Duckett Instructor: Craig Duckett cduckett@cascadia.edu Announcements Assignment 1 Due Lecture 5 by MIDNIGHT I will double dog dare try to have Assignment 1 graded and back to you by Wednesday, January 24 th hopefully

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Lecture 14. 'for' loops and Arrays

Lecture 14. 'for' loops and Arrays Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate

More information

Bjarne Stroustrup. creator of C++

Bjarne Stroustrup. creator of C++ We Continue GEEN163 I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone. Bjarne Stroustrup creator

More information

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose

More information

Claremont McKenna College Computer Science

Claremont McKenna College Computer Science Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions

More information

Robots. Byron Weber Becker. chapter 6

Robots. Byron Weber Becker. chapter 6 Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 5) Chapter 6 You don t have to spend much time on graphics in Ch6 Just grasp the concept Reminder: Reading

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University Overloading Methods Method overloading is to define two or more methods

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Gaddis_516907_Java 4/10/07 2:10 PM Page 51 Chapter 6 Lab Classes and Objects Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value

More information

Supplementary Test 1

Supplementary Test 1 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Supplementary Test 1 Question

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Lesson 10: Quiz #1 and Getting User Input (W03D2) Lesson 10: Quiz #1 and Getting User Input (W03D2) Balboa High School Michael Ferraro September 1, 2015 1 / 13 Do Now: Prep GitHub Repo for PS #1 You ll need to submit the 5.2 solution on the paper form

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

MIDTERM REVIEW. midterminformation.htm

MIDTERM REVIEW.   midterminformation.htm MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

More information

Asking For Help. BIT 115: Introduction To Programming 1

Asking For Help. BIT 115: Introduction To Programming 1 Asking For Help Start homeworks As Soon As Possible You may post homework questions to Canvas: Please do not cut and paste entire lines of code, but rather ask conceptual questions. You may also ask me

More information

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

Keyword this. Can be used by any object to refer to itself in any class method Typically used to Keyword this Can be used by any object to refer to itself in any class method Typically used to Avoid variable name collisions Pass the receiver as an argument Chain constructors Keyword this Keyword this

More information

Input. Scanner keyboard = new Scanner(System.in); String name;

Input. Scanner keyboard = new Scanner(System.in); String name; Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.

More information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

More information

CS 101 Fall 2005 Midterm 2 Name: ID:

CS 101 Fall 2005 Midterm 2 Name:  ID: This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts (in particular, the final two questions are worth substantially more than any

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

COMP String and Console I/O. Yi Hong May 18, 2015

COMP String and Console I/O. Yi Hong May 18, 2015 COMP 110-001 String and Console I/O Yi Hong May 18, 2015 Announcements Labs 0 & 1 are due today by 11:59pm Homework 0 grades are posted Office hours are fixed: MW 3-4pm, or by appointment Review of How

More information

Text User Interfaces. Keyboard IO plus

Text User Interfaces. Keyboard IO plus Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

Data Conversion & Scanner Class

Data Conversion & Scanner Class Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Methods As programmers, we often find ourselves writing the same or similar code over and over. We learned that we can place code

More information

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.

More information

Lesson 7 Part 2 Flags

Lesson 7 Part 2 Flags Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang Object Oriented Programming and Design in Java Session 2 Instructor: Bert Huang Announcements TA: Yipeng Huang, yh2315, Mon 4-6 OH on MICE clarification Next Monday's class canceled for Distinguished Lecture:

More information

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling CS61BL Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling About me Name: Edwin Liao Email: edliao@berkeley.edu Office hours: Thursday 3pm - 5pm Friday 11am - 1pm 611 Soda Or by

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Spring 2007 Midterm 2 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

Programming Exercise 7: Static Methods

Programming Exercise 7: Static Methods Programming Exercise 7: Static Methods Due date for section 001: Monday, February 29 by 10 am Due date for section 002: Wednesday, March 2 by 10 am Purpose: Introduction to writing methods and code re-use.

More information

What did we talk about last time? Examples switch statements

What did we talk about last time? Examples switch statements Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

More information

CS 211: Existing Classes in the Java Library

CS 211: Existing Classes in the Java Library CS 211: Existing Classes in the Java Library Chris Kauffman Week 3-2 Logisitics Logistics P1 Due tonight: Questions? Late policy? Lab 3 Exercises Thu/Fri Play with Scanner Introduce it today Goals Class

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want

More information

2.8. Decision Making: Equality and Relational Operators

2.8. Decision Making: Equality and Relational Operators Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

More information

Java I/O and Control Structures

Java I/O and Control Structures Java I/O and Control Structures CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying

More information

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS Day Class Midterm Exam Dr. R. Khedri DURATION : 50 minutes McMaster University Midterm Exam (CAS) October 29, 2012 Please CLEARLY print: NAME:

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

More information

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information

COMP 110 Introduction to Programming. What did we discuss?

COMP 110 Introduction to Programming. What did we discuss? COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1 Today Announcements

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

A+ Computer Science -

A+ Computer Science - import java.util.scanner; or just import java.util.*; reference variable Scanner keyboard = new Scanner(System.in); object instantiation Scanner frequently used methods Name nextint() nextdouble() nextfloat()

More information

A token is a sequence of characters not including any whitespace.

A token is a sequence of characters not including any whitespace. Scanner A Scanner object reads from an input source (keyboard, file, String, etc) next() returns the next token as a String nextint() returns the next token as an int nextdouble() returns the next token

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

Introduction to Software Development (ISD) David Weston and Igor Razgon

Introduction to Software Development (ISD) David Weston and Igor Razgon Introduction to Software Development (ISD) David Weston and Igor Razgon Autumn term 2013 Course book The primary book supporting the ISD module is: Java for Everyone, by Cay Horstmann, 2nd Edition, Wiley,

More information

Problem Solving With Loops

Problem Solving With Loops To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.! True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,

More information

c) And last but not least, there are javadoc comments. See Weiss.

c) And last but not least, there are javadoc comments. See Weiss. CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for

More information

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing. CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS These are sample pages from Kari Laitinen s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html CHAPTER 5

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

BASIC INPUT/OUTPUT. Fundamentals of Computer Science BASIC INPUT/OUTPUT Fundamentals of Computer Science Outline: Basic Input/Output Screen Output Keyboard Input Simple Screen Output System.out.println("The count is " + count); Outputs the sting literal

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

CSIS 10B Lab 2 Bags and Stacks

CSIS 10B Lab 2 Bags and Stacks CSIS 10B Lab 2 Bags and Stacks Part A Bags and Inheritance In this part of the lab we will be exploring the use of the Bag ADT to manage quantities of data of a certain generic type (listed as T in the

More information

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch

More information

Announcements. 1. Forms to return today after class:

Announcements. 1. Forms to return today after class: Announcements Handouts (3) to pick up 1. Forms to return today after class: Pretest (take during class later) Laptop information form (fill out during class later) Academic honesty form (must sign) 2.

More information

CSCI 355 Lab #2 Spring 2007

CSCI 355 Lab #2 Spring 2007 CSCI 355 Lab #2 Spring 2007 More Java Objectives: 1. To explore several Unix commands for displaying information about processes. 2. To explore some differences between Java and C++. 3. To write Java applications

More information

Chapter 4: Conditionals and Recursion

Chapter 4: Conditionals and Recursion Chapter 4: Conditionals and Recursion Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Agenda The modulus operator Random Number Generation Conditional Execution Alternative

More information

Mobile App:IT. Methods & Classes

Mobile App:IT. Methods & Classes Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -

More information

Java I/O and Control Structures Algorithms in everyday life

Java I/O and Control Structures Algorithms in everyday life Introduction Java I/O and Control Structures Algorithms in everyday life CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Source: http://xkcd.com/627/

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture #7 - Conditional Loops The Problem with Counting Loops Many jobs involving the computer require repetition, and that this can be implemented using loops. Counting

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1, 4.5 2 Interactive Programs with Scanner reading: 3.3-3.4 Interactive programs We have written programs that print console

More information