Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter 10 Classes Continued
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
UML Basics & Access Modifier
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
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.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java exercise review Lesson 25: Exercise 1, 2 and 3.
Object Oriented Programming Lecture 5: BallWorld.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Swing One of the most important features of Java is its ability to draw graphics.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
OOP Basics Classes & Methods (c) IDMS/SQL News
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Block 1 Unit 2 Basic Constructs in Java. Objectives create a simple object using a constructor; create and display a window frame on your computer screen;
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
A structured walkthrough
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Advanced Java Topics Chapter 9
Chapter 9 Objects and Classes
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 7 Objects and Classes
Presentation transcript:

Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics

Constant data fields public static final int FWidth = 600; Easy to edit/change source (single place) Documentation Must be initialized! Usually public Behaves like a constant, as it cannot be modified while the program runs.

Constructing new objects BallWorld w = new BallWorld(Color.red) Operator “new” creates a new object, I.e instance of some class, takes a class name and some arguments. Ties together creation and initialization Prevents against common errors: Object used before initialization Object initialized several times

Constructors Like method, but no return type! Must have same name as class! Never called directly, only by “new” Does all the necessary initialization If we define none, a default constructor of no arguments is generated Can have several constructor with different parameter lists (“overloading”)

Constructing applications Some prefer separation: public class BallWorldProgram { public static void main(String[] args) { BallWorld world = new BallWorld(Color.red); world.show(); } 2 separate files, cumbersome, so we will have a “main” inside the application class

Inheritance public class BallWorld extends Frame { means class BallWorld is a type of Frame, but will add additional behavior We don’t have to reimplement general behavior defined by class Frame, like resizing, iconification, setTitle(…), setSize(…), show(), …

Graphics model: AWT Abstract windowing toolkit Example of a software “framework” paint( Graphics g) method defines how to draw Frame & Ball (abused for simple animation here, too) repaint(1) repaint after 1 millisecond repaint will call paint, but also clear stuff first

System.exit(0) Finishes execution, including possibly some cleanup Passes a return code to the operating system: 0 is used to indicate normal completion other integers are taken to be error codes

The class Ball Protected data members: allows subclasses to access these directly; (good idea?) Socalled “accessor” methods, for reading and writing data members (setLocation, radius, …) Use Rectangle class, and methods of that class as well as of class Graphics (setLocation, translate, setColor, fillOval, …)

Multiple objects of same class e.g. have 2 windows, or an array of balls: ballArray = new Ball [ BallArraySize ]; Must be initialized appropriately: for (int I = 0; I < BallArraySize; I++) { ballArray[I] = new Ball(10,15,5); ballArray[I].setColor(ballColor); ballArray[I].setMotion(3.0+I,6.0-I); } All have their own separate data members!

Summary 1 final fields cannot be modified; static final denote symbolic constants new generates new objects new invokes a constructor Constructor must have same name as its class, must initialize properly! new must supply all arguments

Summary 2 Classes can use “inheritance” Use class Frame for simple windows AWT provides a windowing framework Use paint method to create images Class Rectangle: useful library class Multiple instances of same class have their own separate data fields.