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