Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Road Map Introduction to object oriented programming. Classes
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
CSCI 51 Introduction to Computer Science Dr. Joshua Stough January 20, 2009.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
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.
For Friday Finish reading chapter 2 Complete WebCT quiz.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
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”
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”
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.
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 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.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
BIT 115: Introduction To Programming
More About Objects and Methods
Values vs. References Lecture 13.
BIT 115: Introduction To Programming
BIT 115: Introduction To Programming
CE221 – Data Structures & Algorithms Lab Introduction
Chapter 7 User-Defined Methods.
Building Java Programs
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
BIT 115: Introduction To Programming
September 27 – Course introductions; Adts; Stacks and Queues
BIT 115: Introduction To Programming
Namespaces, Scopes, Access privileges
How to Be Successful in English 3000
BIT115: Introduction to Programming
CSC240 Computer Science III
BIT115: Introduction to Programming
CSC 143 Inheritance.
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Inheritance Basics Programming with Inheritance
MSIS 655 Advanced Business Applications Programming
CHAPTER 6 GENERAL-PURPOSE METHODS
BIT115: Introduction to Programming
Computer Programming with JAVA
More About Objects and Methods
BIT 115: Introduction To Programming
More on Creating Classes
Running a Java Program using Blue Jay.
LCC 6310 Computation as an Expressive Medium
Visibilities and Static-ness
Presentation transcript:

Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

Lecture 13 Attendance Announcements Lecture ICE13

Attendance

Announcements Mid-Course Survey Did you miss out on 5 points? It’s anonymous! Assignment 1 Revision due today Java Code Critic Feedback Read even if “Well done!” or “Done” Are you putting enough time into this course? Are you asking enough questions during ICE time? BIT 115: Introduction To Programming4

Group Work Common in industry (extreme/agile programming) Exchange contact info Cell Phone Share with your group what you are good at what are your challenges Plan a 2-hour meeting outside of class Don’t break up the work, instead collaborate & discuss together Take turns typing on 1 computer during the meeting A3 is due at 3:30pm, Thursday, (this week) Afterwards: We will discuss what worked well and what didn’t A4 will be group work too Complete the “Assignment 4 Partner Preferences” survey by midnight Thursday night Student groups will Peer Review other groups’ A3 work just after it is due So don’t be late with turning in A3! BIT 115: Introduction To Programming5

Assignment 3 (“The Maze”) Some Additional Comments… Please read the Directions carefully, especially the comments in red. Read the Guide & Rubrics for a sense of grading In the Maze.java file, pay special attention to the comments; they are there to help you! If you need a bit of help, are stuck, or have an error you are unable to debug, feel free to contact me but make sure to attach your Maze.java file so I can check your code and offer the pertinent comments. Remember, Assignment 3 is due Thursday, May 12 th by 3:30PM (so students can peer review it).

Today’s Topics Overriding Inherited Methods Using super. Debugging Using System.out Random Numbers Multiple Files

Overriding Inherited Methods

Inheritance: A Quick Refresher import becker.robots.*; public class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } public void turnAround() { this.turnLeft(); } public void move3() { this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); }

Overriding Inherited Methods Java allows us to override methods inherited from the superclass using the super. (‘super dot’) keyword in the method. Both the original method and the overriding method must: have the same name declare the same data type accept same number of parameters return the same data type (we’ll be going over return in the next lecture) See: ICE_13_Demo_1.java

Method Overriding vs. Method Overloading Don’t confuse method overriding (today’s lecture) same method name of a super class with same parameters with method overloading (last lecture) same method name of the current class or a super class with different parameters

Debugging

Debugging: A Brief Look Using System.out for simple debugging In a method: System.out.println(this); Printing an object (for example, a Robot object named rigby): System.out.println(rigby); [street=1, avenue=4, direction=EAST, isBroken=false, numThingsInBackpack=3] Printing an object (for example, a Thing object named t1): System.out.printlin(t1); [street=1, avenue=4] DebugExample.java

Class  Sub-Class  Sub-Sub-Class  Sub-Sub-Sub-Class  and so on… class MrRoboto extends Robot { MrRoboto( City c, int st, int ave, Direction dir, int num) { super(c, st, ave, dir, num); } > } class NeuvoRoboto extends MrRoboto { NeuvoRoboto( City c, int st, int ave, Direction dir, int num) { super(c, st, ave, dir, num); } > } See: ICE_13_Demo_2.java

ICE: Overriding Inherited Methods As always, we’ll wait until the end of the LECTURE to do all the ICEs together

Random Numbers

Depending on your program, sometimes it useful to be able to generate a random number (or numbers) that can be used to interact with your code to accomplish various tasks: Simple Gaming (spin the wheel, roulette, craps, etc.) Advanced Gaming (from what coordinate locations the zombies will attack, etc.) Statistical Sampling (political affiliation, voter turnout, employment / unemployment numbers, etc.) Computer Simulation (predicting earthquakes, paths of hurricanes, tide flows, etc.) Cryptography (codes and encryption) Completely Randomized Design (nuisance variables, response variables)

Random Numbers Java has a rich toolkit for generating random numbers, in a class named Random. Random can generate many kinds of random numbers, but we’ll won’t going into these in this introduction. For more information, see Random is defined in the "java.util" library package, so any Java source file that uses Random must begin with a line of the form: import java.util.Random; or import java.util.*; The easiest way to initialize a random number generator is to use the parameterless constructor, for example: Random generator = new Random(); Random as it stands is NOT truly random. An instance of the Random class is used to generate a stream of pseudo random numbers using a 48-bit seed, which is modified using a linear congruential formula: x i+1 = (0x5DEECE66DL * x i + 11) mod If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. Java implementations for Random have been developed this way for the sake of absolute portability of Java code. However, subclasses of Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. Example: you can develop a subclass algorithm for Random that interacts with its seed in some way (through multiplication, division, a bit of both) like using the current system time from your computer (e.g., total seconds from some predefined date or randomized date). Some applications will find the random method in class Math (Math.random) simpler to use, as it creates a pseudorandom double greater than or equal to 0.0 and less than 1.0.

Multiple Program Files

Separating main from new classes and new class methods to create more manageable code a main file one or more class/method files an optional.txt text file for configuration, additional input, or output (like creating a log file) Things to remember when dealing with multiple.java files: class name must match file name All files must be grouped together in the same folder/directory (desktop, drive, etc.) jGrasp automatically saves any changes made in the class files when compiling main Eclipse automatically saves any changes made in the class files when java files are saved (compiles everything in the background)

A Refresher: Using Single Files

import becker.robots.*; class MrRoboto extends Robot { public MrRoboto(City c, int s, int a, Direction d) { super(c, s, a, d); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } public class MrRobotoMain extends Object { public static void main(String[] args) { City lfp= new City(); MrRoboto lisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); lisa.turnAround(); } } MrRoboto: 1 File, 2 Styles import becker.robots.*; public class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City c, int s, int a, Direction d) { super(c, s, a, d); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } public static void main(String[] args) { City lfp = new City(); MrRoboto lisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); lisa.turnAround(); } } MrRoboto.java MrRobotoMain.java One Class Style Multiple Class Style

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 void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } SINGLE FILE | STYLE 1 In this style, since there is only one class name, then: the class name MrRoboto, the constructor name MrRoboto, and the file name MrRoboto must all be the same.

import becker.robots.*; class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } public class MrRobotoMain extends Object { public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } SINGLE FILE | STYLE 2 In this style, since there are two class names, then the file name must match the class name that contains the main method, MrRobotoMain. Also the class that holds the constructor and the new methods only starts with class, and the constructor starts with public. The class that holds main must start with public class

Splitting MrRobotoMain.java into Two Files METHODS and MAIN: 1 File with 2 Classes METHODS: 1 File with 1 Class MAIN: 1 File with 1 Class Example

import becker.robots.*; Needs its own ‘import’ statement class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } import becker.robots.*; Needs its own ‘import’ statement public class MrRobotoMain extends Object { public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } MrRoboto.java In this case, since there are two files, then the class names must match the files names, and both files must be in the same folder/directory. Each file needs to include the line import becker.robots.*; as well. MrRobotoMan.java Always compile the file that contains main when working with multiple files, since you cannot compile a file that does not contain main SPLIT IN TWO

import becker.robots.*; class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } import becker.robots.*; public class MrRobotoMain extends Object { public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } MrRoboto.java THE METHOD FILE MrRobotoMain.java THE MAIN FILE

Multiple Files Go in Same Folder/Directory MrRoboto.java THE METHOD FILE MrRobotoMain.java THE MAIN FILE The files need to be able “find” each other to work correctly, so storing them in the same directory or folder greatly simplifies this discovery process. NOW A DEMONSTRATION NOW A DEMONSTRATION Using MrRobotoMain.java

ICE13 Most ICE13 groups are a copy of A3 groups, so this is also the time to exchange contact info (phone & ) & plan your A3 meeting Don’t break up the work, do it together collaboratively I’ll accept today’s ICE13 as late as the beginning of Tuesday’s class, so if your group doesn’t finish tonight they can finish during the meeting(s) later this week

ICE: Overriding Inherited Methods ICE: Multiple File Programs Work together in ICE13 Groups on 1 computer Look at Both ICEs Before Beginning NOTE : If you want to learn how I constructed a City using a separate text file, please refer to the Becker Robot Library > becker.robots > City and scroll down to the Constructor Detail on the right-hand side (the section begins with “Construct a new city by reading information to construct it from a file…”) REMEMBER: ALWAYS download your multiple files from the website first and then work on them and run them from the same folder/directory, or else things will not work correctly.