BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)

Slides:



Advertisements
Similar presentations
Intro to CIT 594
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
CIT 590 Intro to Programming First lecture on Java.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Refactoring1 Improving the structure of existing code.
Intermediate 2 Software Development Process. Software You should already know that any computer system is made up of hardware and software. The term hardware.
BIT 115: Introduction To Programming Instructor: Jon Peck
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC Programming I Lecture 6 September 4, 2002.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Refactoring1 Improving the structure of existing code.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Getting ready. Why C? Design Features – Efficiency (C programs tend to be compact and to run quickly.) – Portability (C programs written on one system.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
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”
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
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”
Introduction to Object Oriented
Abstract classes and interfaces
Programming Language Concepts (CIS 635)
Abstract classes and interfaces
Improving the structure of existing code
Abstract classes and interfaces
Classes, Objects and Methods
Agenda Types and identifiers Practice Assignment Keywords in Java
LCC 6310 Computation as an Expressive Medium
Zorah Fung University of Washington, Winter 2016
How to Run a Java Program
Presentation transcript:

BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)

BIT 115: Introduction To Programming 2 Quiz 12 ngton.edu/webq/survey/b abaw/55235https://catalysttools.washi ngton.edu/webq/survey/b abaw/55235 Due to Java Code Critic by 11:15 AM! 2

BIT 115: Introduction To Programming Quiz More “write me some code” type quizzes This sort of thing will be on the final …and in real life, of course! –we have to be fast developers, so we can't always wait for the computer to catch every little mistake –Little mistakes can lead to big confusion! –we must be able to try out several designs in our head before typing any one of them up How to practice doing this: –Write parts of your homework on paper first –Then type it in, see what you did wrong!

BIT 115: Introduction To Programming 4 We’ve sometimes included this for instructional purposes public void turnAround() { this.turnLeft(); // You almost never need "this" for a method call. It's just clutter. You can just use: public void turnAround() { turnLeft(); // this will call the closest method in the inheritance hierarchy Avoid infinite recursion! public void turnAround() { some.parent.turnAround(); // this is fine turnAround(); // Avoid calling your own method! 4

BIT 115: Introduction To Programming 5 Convert MS Excel Files If you have turned in an.xlsx file, convert it to.xls and it to me. So far I can convert.docx files to.rtf 5

BIT 115: Introduction To Programming Today Chapter 3.6 –public, private, protected, package-private Programs in multiple files Non-Robot software Unit Testing –Using JUnit with Eclipse

BIT 115: Introduction To Programming Next Lecture Ch = Complex boolean expressions Ch = For loops, Do...while loops Ch = Documentation; JavaDoc

BIT 115: Introduction To Programming 8 Predicate Methods & Return OK public boolean isRobotOnAvenueFive() { if (this.getAvenue() == 5) { return true; } else { return false; } BEST (simpler)! public boolean isRobotOnAvenueFive() { return this.getAvenue() == 5; }

BIT 115: Introduction To Programming 9 Return Return can also be used in void methods to indicate that you simply want to stop the method & return to the caller – public void doNothing() { return; System.out.println(“Never say this.”); } 9

BIT 115: Introduction To Programming 10 Access Modifiers for Classes, Methods, & Objects public : anybody can call this method –Good for methods and constants that we want to make widely available private : nobody (except other methods in the same class) can access this –Good for helper methods (see pattern p ) –HOWEVER, private methods aren't made available to classes that inherit from this class, either (subclasses) protected : like private, except that subclasses can call the method (see Robot.breakRobot() JavaDoc)Robot.breakRobot() JavaDoc 10

BIT 115: Introduction To Programming 11 Access Modifiers for Classes, Methods, & Objects no modifier (default/package-private): nobody (except other methods in the same package) can access this –Packages are named groups of related classes –Not in textbook: see link on next slide 11

BIT 115: Introduction To Programming 12 Access Levels See: /javaOO/accesscontrol.html /javaOO/accesscontrol.html 12 ModifierClassPackageSubclassWorld public Yes protected Yes No no modifier (package- private) Yes No private YesNo

BIT 115: Introduction To Programming 13 Access Modifiers for Classes, Methods, & Objects In general declare everything private –UNLESS you want to make it available to anybody public –UNLESS you want to make it available to subclasses protected –UNLESS you want to make it available to its package no modifier (package-private) 13

BIT 115: Introduction To Programming 14 Why hide (encapsulate)? Why would you want something that's private? –Other parts of the program can't manipulate. –Fewer bugs, but isn't any more secure. The main advantage is that other code won't accidentally change your object's internal data. –This idea is called encapsulation: the internal, private, state of an object isn't accessible to other parts of the program, but the public interface is available. –Public code limits you to a particular implementation and limits your flexibility to change your code later.

BIT 115: Introduction To Programming 15 encapsulate |en ˈ kaps(y)ə ˌ lāt| verb [ trans. ] enclose (something) in or as if in a capsule. express the essential features of (someone or something) succinctly : the conclusion is encapsulated in one sentence. Computing enclose (a message or signal) in a set of codes that allow use by or transfer through different computer systems or networks. Computing provide an interface for (a piece of software or hardware) to allow or simplify access for the user. [as adj. ] ( encapsulated) enclosed by a protective coating or membrane. 15

BIT 115: Introduction To Programming 16 ICE 12 Part 1: public, private, protected About 5 minutes, must finish by 11:50 AM 16

BIT 115: Introduction To Programming 17 ICE12 Part 2: Putting Classes in Separate Files About 5 minutes, must finish by Noon 17

BIT 115: Introduction To Programming 18 ICE 12 Part 3: Non-robotic Software static classes/methods/objects: Always have the same value within a Java Virtual Machine (Java VM) Open the ICE_12.java. Note that the main method is always a static method: It can be called on a class without instantiating an object first –Can only call static methods or make objects and call their non-static methods –Useful for instantiating objects including those of it’s own class, often done to get things going in a program 18

BIT 115: Introduction To Programming 19 final and static keywords final keyword means the value never changes once it is set – final classes can’t have any subclasses final static objects thus never change and are called constants –constants’ names should be in all caps – public static final Direction EASTDirection 19

BIT 115: Introduction To Programming 20 ICE_12.java: main & println Does not import any robot classes Has a main method so it can be run as Java application –Uses default system output (using System.out.println(“…”) ). This is fine because the System.out object is static and its println method is public. –However the ICE_12.countdown() method is not static, so it is only callable from the static main method if it is being called on an existing object. So an ICE_12 object called runningObject is instantiated so that runningObject.countdown() can be called.

BIT 115: Introduction To Programming 21 ICE 12 Part 4: Using println for debugging, using JUnit for testing –Test the ICE_12.countdown() method using JUnit 4 –Fix the ICE_12.countdown() method and see how JUnit confirms that it is fixed –Writing test cases is crucial for applications people will use Allows software developers to change an application without fear of missing the reoccurrence of old bugs! 21

BIT 115: Introduction To Programming 22 A03 Groups Who doesn’t have a partner? 22