A structured walkthrough

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
C++ Classes & Data Abstraction
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Lecture 2: Object Oriented Programming I
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Java Applets A First Program. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
UML Basics & Access Modifier
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Object Oriented Programming Lecture 5: BallWorld.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
CSC 142 Computer Science II Zhen Jiang West Chester University
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Creating a Java Application and Applet
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.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Functions + Overloading + Scope
Topic: Classes and Objects
Building Java Programs
Inheritance ITI1121 Nour El Kadri.
3 Introduction to Classes and Objects.
Inheritance and Polymorphism
CSC240 Computer Science III
Interface.
Java Applets.
A+ Computer Science METHODS.
Building Java Programs
Tonga Institute of Higher Education
COP 3330 Object-oriented Programming in C++
Object based programming in Java
Object based programming in Java
Java Programming Language
Defining Classes and Methods
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

A structured walkthrough BallWorld.java A structured walkthrough -part 2 the ball class

The BallWorld program Recap Two classes are created (last time we looked at the frame and some behaviors, now we will look at the ball class we used in the program. It is located in a separate file called Ball.java) The BallWorld features (from the previous lecture) : Execution is done through the procedure called “main” which are declared as static, void and public All main programs must take arguments as a string values (ignored in this case). The class defines some private internal variables, some of which are constant, some are initialized and some that are not initialized. The main creates an instance of the class BallWorld. This object is initialized through the constructor that created it. The class is declared as an extension of an existing java class called “frame”. This is built through inheritance. The output is displayed through the use of primitives provided by the Java runtime library. Recap

Recap

The BallWorld class file and compilation Recap

The Ball class file and compilation Compiling the file

The bytecode file This class cannot be executed. It has no main section. However it is a public class that can be used in other programs (such as our BallWorld) Hint: Always think “what is the instigator of this program”, as well as “what type of program is this (applet vs. application)

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} “awt” stands for the Abstract Windowing Toolkit. Which gives us access to a large portion of code we do not have to write. Behaviors of ball should be (and are) written in generalities so it can be used in many ways) This is a stored in a separate file so that together with “BallWorld” it can be executed.

The ball has four data fields that are “protected” import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} The ball has four data fields that are “protected” “Rectangle” is representing the location of the ball. It is also a general purpose class provided by the Java run-time library “dx” and “dy” are horizontal and vertical direction of the movement of the ball. “Color” is a Java library class (not “run-time”). “protected” fields allows child classes to have access to the fields without allowing other objects to modify them. In general some developers prefer “protected” instead of “private”

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} Three arguments are needed to call the public class “Ball”. They all need to be integers. The first two “x” and “y” are the initial location of the ball, while “r” is the radius of the ball. The constructor is made by recording the location of the new ball (represented by the rectangle).

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} A simple calculation is used to determine the four corners of the rectangle, since this Java general run-time class requires four arguments (one for each corner in pixels). Default values are set for motion (dx and dy) and color. These values can be modified by an object through messages (as we saw in setColor and setMotion in the BallWorld class) The notation “Color.blue” is used since Color is a Java provided library class and “blue” is a member of this class.

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} This allows users of the class to pass a message to this object to change the color through the call “setColor”. We used this in BallWorld.java:

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} This allows users of the class to pass a message to this object to change the motion through the call “setMotion”. We used this in BallWorld.java:

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} Behaviors that return the radius, the x and y location (in pixels), the dx and dy directional movement of the ball and the region occupied by the ball to anyone accessing the ball class Functions such as these that allows access to the data in a class is called “accessor functions”

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} “accessor functions” are preferred instead of making these data fields public. This is due to the fact that accessor functions only gives read-only access to these values and thereby protect them from updates. However the use of the functions above can modify the ball (setMotion and setColor)

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) // functions that change attributes of ball { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} Functions of the run-time Java class “Rectangle”. It provides the width and the “anchor” of the upper corner in terms of x,y coordinates Function of the run-time Java class “Rectangle” that allows you to move the rectangle to another location in terms of pixels (as we represents as allowable arguments “x” and “y” that must be integers when we use this function).

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} Function of the run-time Java class “Rectangle” that allows you to “transliterate” the rectangle from one location to the next. The movement-to location is represented by the dx and the dy variables. The function “paint” use two operators provided by the Java library class “Graphics”: Setting color for the graphics to be drawn Painting an oval at a given location (need 4 parameters) We can print an oval anywhere by this simple code: Public void paint (Graphics xyz){ xyz.fillOval(1,1,10,10);} And call it through: paint(xzy);

How is BallWorld using the Ball class? import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class?

Some observations: The following classes are extensively used in Ball and BallWorld: The Frame class (the window) The run-time class Rectangle The Graphics class (the display) The only way to use these classes are through knowing what operators can be used with them. We have used some of the most common, but each time you develop a system, you will frequently need to consult the object library to see what is available to you and how you can use the classes provided by Java as well as those already created by vendors such as Microsoft or the company you work for.

Next we will look at MultiBall World To do: Study the presentations on Ball and BallWorld. They are integral parts of what you will be working on for the next couple of weeks.