242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Introduction to Java Programming, 4E
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Object interaction Creating cooperating objects 3.0.
Programming with Objects Creating Cooperating Objects Week 6.
Object Interaction 1 Creating cooperating objects.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
Object Interaction 2 Creating cooperating objects.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Object interaction Creating cooperating objects. 28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Programming with Objects Object Interaction (Part 1) Week 9.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Introduction to Java Programming with Forte Y. Daniel Liang.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Introduction to programming in the Java programming language.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
COS 260 DAY 5 Tony Gauvin.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Objects First With Java A Practical Introduction Using BlueJ Using Java without BlueJ 1.0.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Introduction to Java Programming, 4E Y. Daniel Liang.
Objects First with Java Creating cooperating objects
using System; namespace Demo01 { class Program
Object INTERACTION CITS1001 week 3.
COS 260 DAY 6 Tony Gauvin.
Introduction to Java Programming
Abdulmotaleb El Saddik University of Ottawa
Creating cooperating objects
F II 3. Classes and Objects Objectives
Creating cooperating objects
Objects First with Java Creating cooperating objects
Week 4 Lecture-2 Chapter 6 (Methods).
Objects First with Java Creating cooperating objects
F II 4. Object Interaction Objectives
F II 8. More on Inheritance Objectives
COS 260 DAY 6 Tony Gauvin.
F II 2. Simple Java Programs Objectives
Presentation transcript:

Programming Fundamentals 2: Interaction/ F II Objectives – –introduce modularization and abstraction – –explain how an object uses other objects – –compare object and primitive types Semester 2, Object Interaction Original Slides by Dr. Andrew Davison

Programming Fundamentals 2: Interaction/4 2 Topics 1. Modularization and Abstraction 2. A Digital Clock 3. Using ClockDisplay 4. A More Graphical Clock

Programming Fundamentals 2: Interaction/ Modularization and Abstraction Modularization divides a problem into simpler sub-parts, which can be built separately, and which interact in simple ways. Abstraction is the ability to ignore low level details of a problem to focus on the higher levels.

Programming Fundamentals 2: Interaction/4 4 Use in Programming Use modularization to split a programming problem into sub-parts (modules). Use modularization to split a programming problem into sub-parts (modules). –implement the modules The implementation of the complete program will be easier, since abstraction can be used to write software in terms of the modules. The implementation of the complete program will be easier, since abstraction can be used to write software in terms of the modules.

Programming Fundamentals 2: Interaction/4 5 e.g. Robot Software Modules ("black boxes") Abstraction "links" the modules together using their visible interfaces.

Programming Fundamentals 2: Interaction/4 6 Use in OOP Use modularization to split a programming problem into objects. Use modularization to split a programming problem into objects. Implement the classes for the objects. Implement the classes for the objects. The implementation of the class for the complete program will be easier, since abstraction can be used to write the class in terms of other classes (yours and predefined ones). The implementation of the class for the complete program will be easier, since abstraction can be used to write the class in terms of other classes (yours and predefined ones).

Programming Fundamentals 2: Interaction/4 7 e.g. Airport Control System Classes for: Plane, Gate, Luggage, Passenger, etc. Use them to create objects such as plane1, plane2, gate2, myLuggage Abstraction simplifies the communication between the objects; only use their visible interface.

Programming Fundamentals 2: Interaction/ A Digital Clock Implement a digital clock display, which shows the hours (0-23) and minutes (0-59).

Programming Fundamentals 2: Interaction/4 9 Modularizing the Clock Display Divide the clock display problem into two parts Divide the clock display problem into two parts –how to display the hours –how to display the minutes We need two number display objects We need two number display objects We need a NumberDisplay class We need a NumberDisplay class two NumberDisplay objects

Programming Fundamentals 2: Interaction/4 10 Objects Diagram ClockDisplay object NumberDisplay object (for hours) NumberDisplay object (for minutes)

Programming Fundamentals 2: Interaction/4 11 NumberDisplay Interface What kind of interface is needed for a NumberDisplay class? What kind of interface is needed for a NumberDisplay class? –get and set the number –return the number as a string useful for printing useful for printing –increment the number the number will 'loop' the number will 'loop' e.g. 0, 1, 2,..., 59, 0, 1,... for the minutes display e.g. 0, 1, 2,..., 59, 0, 1,... for the minutes display

Programming Fundamentals 2: Interaction/4 12 The NumberDisplay Class public class NumberDisplay { private int currValue; private int currValue; private int maxValue; // number at which currValue goes back to 0 private int maxValue; // number at which currValue goes back to 0 public NumberDisplay(int max) public NumberDisplay(int max) { maxValue = max; { maxValue = max; currValue = 0; currValue = 0; } continued

Programming Fundamentals 2: Interaction/4 13 public void setValue(int newValue) public void setValue(int newValue) /* Set currValue to the new value. If the new value is less than zero or over maxValue, don't set it. /* Set currValue to the new value. If the new value is less than zero or over maxValue, don't set it. */ */ { if ((newValue >= 0) && (newValue = 0) && (newValue < maxValue)) currValue = newValue; currValue = newValue; } public int getValue() public int getValue() { return currValue; } { return currValue; } continued

Programming Fundamentals 2: Interaction/4 14 public String getDisplayValue() public String getDisplayValue() // return currValue as a string // return currValue as a string { if (currValue < 10) if (currValue < 10) return "0" + currValue; //pad string with leading 0 return "0" + currValue; //pad string with leading 0 else else return "" + currValue; return "" + currValue; } public void increment() public void increment() /* Increment currValue, rolling over to zero if the /* Increment currValue, rolling over to zero if the maxValue is reached. */ maxValue is reached. */ { currValue = (currValue + 1) % maxValue; } { currValue = (currValue + 1) % maxValue; } } // end of NumberDisplay class

Programming Fundamentals 2: Interaction/4 15 ClockDisplay Interface What kind of interface is needed for a ClockDisplay class? What kind of interface is needed for a ClockDisplay class? –initialize the clock and set the time –return the current time as a string useful for printing useful for printing –increment the time by one minute The time will be represented using two NumberDisplay fields. The time will be represented using two NumberDisplay fields.

Programming Fundamentals 2: Interaction/4 16 The ClockDisplay Class public class ClockDisplay { private NumberDisplay hours; private NumberDisplay hours; private NumberDisplay minutes; private NumberDisplay minutes; private String currTimeString; // the current time as a string private String currTimeString; // the current time as a string public ClockDisplay() public ClockDisplay() // intialize the clock to 00:00 // intialize the clock to 00:00 { hours = new NumberDisplay(24); hours = new NumberDisplay(24); minutes = new NumberDisplay(60); minutes = new NumberDisplay(60); setTimeString(); setTimeString(); } two private NumberDisplay fields create two NumberDisplay objects continued

Programming Fundamentals 2: Interaction/4 17 private void setTimeString() private void setTimeString() /* store the current time as a string of the form "hours:minutes" */ /* store the current time as a string of the form "hours:minutes" */ { currTimeString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } { currTimeString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } a private method is one that only other methods in the class can call continued method calling in NumberDisplay objects

Programming Fundamentals 2: Interaction/4 18 public void setTime(int hour, int minute) public void setTime(int hour, int minute) // set time to the specified hour and minute // set time to the specified hour and minute { hours.setValue(hour); hours.setValue(hour); minutes.setValue(minute); minutes.setValue(minute); setTimeString(); setTimeString(); } // end of setTime() } // end of setTime() public String getTime() public String getTime() // return the current time as a string // return the current time as a string { return currTimeString; } { return currTimeString; } continued method calling in NumberDisplay objects

Programming Fundamentals 2: Interaction/4 19 public void minIncrement() public void minIncrement() // increment the clock by one minute; // increment the clock by one minute; // hour increments when minutes roll over to 0 // hour increments when minutes roll over to 0 { minutes.increment(); minutes.increment(); if (minutes.getValue() == 0) // mins rolled if (minutes.getValue() == 0) // mins rolled hours.increment(); hours.increment(); setTimeString(); setTimeString(); } // end of minIncrement() } // end of minIncrement() } // end of ClockDisplay class

Programming Fundamentals 2: Interaction/4 20 Classes Diagram uses

Programming Fundamentals 2: Interaction/ Using ClockDisplay public class ClockDemo { public static void main(String[] args) public static void main(String[] args) { ClockDisplay clock = new ClockDisplay(); ClockDisplay clock = new ClockDisplay(); clock.setTime(14, 10); // set time to 14:10 clock.setTime(14, 10); // set time to 14:10 while(true) { while(true) { clock.minIncrement(); clock.minIncrement(); System.out.println(" tick..."); System.out.println(" tick..."); System.out.println("Current time: "+clock.getTime()); System.out.println("Current time: "+clock.getTime()); wait(100); // slow down the looping wait(100); // slow down the looping } } // end of main() } // end of main()

Programming Fundamentals 2: Interaction/4 22 private static void wait(int milliseconds) private static void wait(int milliseconds) /* stop execution for milliseconds amount of time */ /* stop execution for milliseconds amount of time */ { try { try { Thread.sleep(milliseconds); Thread.sleep(milliseconds); } catch (Exception e) { } catch (Exception e) { } } // end of wait() } // end of wait() } // end of ClockDemo class sleep() is a method in Java's Thread class wait() is a static method so it can be called by main() without main() having to create an object first.

Programming Fundamentals 2: Interaction/4 23 Compilation and Execution $ javac *.java Compile NumberDisplay.java, ClockDisplay.java, and ClockDemo.java I typed ctrl-c to stop the looping.

Programming Fundamentals 2: Interaction/4 24 Objects Diagram for ClocksDemo hours minutes currTimeString clock ClockDisplay object currValue maxValue NumberDisplay object currValue maxValue NumberDisplay object :19 String object

Programming Fundamentals 2: Interaction/ A More Graphical Clock Michael Kölling and Bruce Quig have developed a simple Canvas class for displaying text and basic shapes in a window. Michael Kölling and Bruce Quig have developed a simple Canvas class for displaying text and basic shapes in a window. We can use Canvas to display the changing clock display instead of using stdout. We can use Canvas to display the changing clock display instead of using stdout.

Programming Fundamentals 2: Interaction/4 26 Canvas Class Diagram Only showing the public methods (the interface). To use Canvas, we only need to understand its interface. I don't care how it is implemented.

Programming Fundamentals 2: Interaction/4 27 ClockCanvasDemo public class ClockCanvasDemo { public static void main(String[] args) public static void main(String[] args) { Canvas canvas = new Canvas("Clock Demo",300,150, Color.white); Canvas canvas = new Canvas("Clock Demo",300,150, Color.white); canvas.setVisible(true); canvas.setVisible(true); canvas.setFont( new Font("Dialog", Font.PLAIN, 96)); canvas.setFont( new Font("Dialog", Font.PLAIN, 96)); ClockDisplay clock = new ClockDisplay(); ClockDisplay clock = new ClockDisplay(); clock.setTime(14, 10); // set time to 14:10 clock.setTime(14, 10); // set time to 14:10 while(true) { while(true) { clock.minIncrement(); clock.minIncrement(); canvas.erase(); // clear the canvas canvas.erase(); // clear the canvas canvas.drawString( clock.getTime(), 30, 100); canvas.drawString( clock.getTime(), 30, 100); canvas.wait(100); // slow down the looping canvas.wait(100); // slow down the looping } } // end of main() } // end of main() } // end of ClockCanvasDemo class Using Canvas (compare to slide 21)

Programming Fundamentals 2: Interaction/4 28 Compilation and Execution $ javac *.java $ java ClockCanvasDemo Compilation includes ClockCanvasDemo.java and Canvas.java