Robots. Byron Weber Becker. chapter 6

Similar documents
Robots. Byron Weber Becker. chapter 6

A+ Computer Science -

A+ Computer Science -

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

More on variables and methods

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

TaxiBot New attributes Variables Math! TaxiBot

Chapter 4: Writing Classes

Basic Computation. Chapter 2

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

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

Object Oriented Programming. Java-Lecture 1

Text User Interfaces. Keyboard IO plus

CS1083 Week 2: Arrays, ArrayList

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming with Java

Basic Computation. Chapter 2

Final Exam Practice. Partial credit will be awarded.

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

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

1 Short Answer (10 Points Each)

Basic Computation. Chapter 2

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Over and Over Again GEEN163

Introduction to Java (All the Basic Stuff)

ing execution. That way, new results can be computed each time the Class The Scanner

Lecture 11. Instructor: Craig Duckett. Instance Variables

Anatomy of a Class Encapsulation Anatomy of a Method

Lecture 10. Instructor: Craig Duckett

CS1004: Intro to CS in Java, Spring 2005

CS 101 Fall 2006 Midterm 3 Name: ID:

Java Review. Fundamentals of Computer Science

CS 112 Introduction to Programming

Programming Exercise 7: Static Methods

Fundamentals of Programming Data Types & Methods

CSCI 1103: File I/O, Scanner, PrintWriter

Section 2: Introduction to Java. Historical note

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CSCE 145 Exam 2 Review No Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

ACS-1903 Basic program structure Feb 16, 2017

Computational Expression

Software and Programming 1

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Full file at

Lecture 8 " INPUT " Instructor: Craig Duckett

Reading Input from Text File

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

CSCI 1103: File I/O, Scanner, PrintWriter

H212 Introduction to Software Systems Honors

Chapter 4: Loops and Files

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Using Java Classes Fall 2018 Margaret Reid-Miller

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

public static String[] manyme() {

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

COMP 202 Java in one week

Repetition CSC 121 Fall 2014 Howard Rosenthal

Intro to Programming in Java Practice Midterm

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Using Classes. GEEN163 Introduction to Computer Programming

Chapter 4: Loops and Files

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

Simple Java Input/Output

Class 9: Static Methods and Data Members

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Chapter 4: Conditionals and Recursion

Methods CSC 121 Fall 2016 Howard Rosenthal

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

UNDERSTANDING CLASS DEFINITIONS CITS1001

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

I. Variables and Data Type week 3

CS112 Lecture: Defining Instantiable Classes

COMP 110 Programming Exercise: Simulation of the Game of Craps

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Lecture 7. Instructor: Craig Duckett OUTPUT

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Methods CSC 121 Spring 2017 Howard Rosenthal

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

Console Input and Output

ECE 122. Engineering Problem Solving with Java

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Chapter 6 Lab Classes and Objects

Java Coding 3. Over & over again!

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Dining philosophers (cont)

4. Java Project Design, Input Methods

Ch. 11: References & the Copy-Constructor. - continued -

Transcription:

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 for Monday (Oct 10) Ch 7.1-7.2 Reminder: program#4 is due next Monday Exam 1: results and discussions Scanner class: corrections in the handout Always use new Scanner(.) DO NOT use Scanner.cretae( ) Check ch3 exercise solutions for a sample of fpseudo code

Review: Writing to the Console Text interface. System.out is an object that is provided to enable us to show data to the user. Two main methods: System.out.print( stuff t t( t to print ); System.out.println( stuff to print ); Can pass in any kind of thing to print

Using System.out System is a special class used for standard input and output (I/O) The System class contains several useful class fields and methods It cannot be instantiated out is a PrintStream class field within the System class All PrintStream methods are available using System.out.methodName();

Common PrintStream Methods println is used to print a string of data (passed as the parameter) with a line return at the end print is used to print a string of data without a line return at the end Multiple calls to print can be used to print information one after the other on the same line println() can be used to get a simple line return without printing data

The Scanner Class Used for data input Declaration for keyboard Scanner Scanner keyboard = new Scanner(System.in); Single token read methods keyboard.next(); // returns a String keyboard.nextint(); // returns an int keyboard.nextdouble(); bl // returns a double Multi-token read method keyboard.nextline(); // returns a String

Try it Create an application class to Request, get and store an int, a double, a single token word, a multi-token token phrase, and a single character As you read the data, echo it back to the screen using system.out.println(); Warning beware of line returns left on the input buffer

Dumping the Buffer In the Scanner class next, nextint, and nextdouble all leave trailing white space A line return (hitting enter) is white space These methods also skip leading white space When using only these 3 methods you never have a buffer problem

Dumping the Buffer (cont) However nextline reads all leading and trailing white space Leading white space is saved Trailing white space is also except line returns are never saved nextline LOOKS for the FIRST line return in the buffer, reads UP TO it, REMOVES it, and returns what was IN FRONT OF IT

Dumping the Buffer (cont) When next, nextint, or nextdouble is followed by nextline you must drop the remaining line return from the buffer using keyboard.nextline(); This buffer dump goes BEFORE a nextline read anytime it folows a next, nextint, or nextdouble read This is confusing but very important!!!

MorePractice Write a method to compute the sum of all integers between first and second (including first and second), where first and second are integers and first is less than or equal to second. The method should return the sum. You may not change the value of either first or second.

Still More Practice Write a method to find the smaller of two integers. The method will accept two integers and return the smaller of the two. If they are the same, then the method returns either one of them.

One More Practice Telephone company rules to calculate the cost of a long distance call are as follows. If the call is over 60 minutes, the cost is 7 cents per minute. If the call is over 20 minutes long, the cost is 10 cents per minute. If the call is 20 minutes or less, the cost is 13 cents per minute. Write a method that t takes the length of a call in minutes and returns the per minute rate for that call.

Practice Switch Statements Write a method to determine the purchaser s discount based on a code. If the code is 7, the discount is 10%. If the code is 3, the discount is 15%. If the code is 12, the discount is 4%. If the code is 1, there is no discount. If the code is 8, the discount is 30%. The method should return the discount.

Review: Instance Variables (Class Data) Attributes or data pertaining to a class object Different for each instance of the class Always private or protected never public Scope the life of the object Accessibility Visible within class methods Hidden outside of class

Review: Temporary/Local Variables Used to hold data for short time within a Used to hold data for short time within a method Calculation results Temporary copy of instance data Counters User input etc Scope the life of the method Accessibility Visible within this method only

Parameter Variables Data passed into a method Declaration done in the method header Names not associated with calling statement Scope the life of the method Accessibility Visible within this method only

Constants public static final int SIZE = 50; Names written in ALL_CAPS_WITH_UNDERSCORES Must be initialized when declared Typically declared as public Always visible within class or outside Declared as static stored once in memory and shared by all instances of class Accessed using class name not instance of class Declared as final Value cannot be changed by program

Instance Data in Robot Classes Consider the class data of the Robot class int street int avenue Direction direction ThingBag backpack No new data was added to RobotSE

Adding Instance Variables to Child Class Consider a child of RobotSE called Consider a child of RobotSE called HomingBot that can remember where it starts and return there when told to gohome Add instance variables to hold starting street, avenue, and direction Write the constructor so it stores this data How will gohome vary from the goto method in the GoBot from lab 6?

From GoBot in Lab 6 public void goto(int st, int ave) { this.facestreet(st); this.gotostreet(st); this.faceave(ave); this.gotoave(ave); } Assuming each method had been properly coded to work in GoBot how will they change in our HomingBot?

LimitedCarryBot Create as child of HomingBot Can only carry a limited number of Things Hold this as additional instance variable Set using a parameter passed to the constructor Override pickthing so if the robot tries to pick up too many things it will spin around twice, then drop everything on that spot A method called exercise will make the robot return home (presumably to exercise) and increase the number of Things the robot is able to carry by one

The Keyword final Values declared as final become constant their value cannot be changed They should always be declared using ALL_CAPS They must be given a value when declared Suppose we always want our HomingBot to face east after returning home what changes would we make in the class?

The Keyword static Since every instance of a class will have the same constant(s) and since those constants will always have the same value we only need to store that value once for all instances of the class Constants are typically declared as static and made public Only created once in memory Accessible from anywhere without out an instance of the class Accessed using the class name

Static used in a Class for Variables and Constants t Static class data (either variable or constant) is created once when the first instance is created and is accessible by all instances of the class Thus every instance of a class has the ability to change the value of static variables Used very sparingly If declared as public you can access the value using the class name double tax = Part.SALES_TAX;

Static used in a Class for methods Static methods cannot be dependent on any class data that is not also static Static methods are called using the class name NOT an instance of the class The Math class has all static methods and constants Never create a Math object Call all methods using Math.methodName() Access all constants using Math.CONSTANT

Accessor Methods Also called getters Used to access (get) values stored in instance data Always non-void return type Rarely has parameter values You won t always provide an accessor for every data element

Mutator Methods Also called setters Used to mutate (change or set) values stored in instance data Always void return type Typically has a single parameter value the value we will be storing You won t always provide a mutator for You won t always provide a mutator for every data element