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.

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
UML Basics & Access Modifier
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
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.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
Session 15 Chapter 8: Understanding Inheritance. Lab Exercise Solution Recall that we needed to know if a mousePress occurred on a target. We tried to.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
© A+ Computer Science - Chicken yeller = new Chicken();
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSC 142 Computer Science II Zhen Jiang West Chester University
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
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.
Session 13 Pinball Game Construction Kit (Version 3):
Session 22 Chapter 11: Implications of Inheritance.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Topics Instance variables, set and get methods Encapsulation
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
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;
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Introducing, the JFrame Gives us a work area beside System.out.println.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 More About Derived Classes and Inheritance Chapter 9.
A structured walkthrough
OOP: Encapsulation &Abstraction
A+ Computer Science METHODS.
slides created by Alyssa Harding
Simple Classes in Java CSCI 392 Classes – Part 1.
A+ Computer Science METHODS.
Defining Classes and Methods
Dr. R Z Khan Handout-3 Classes
Chapter 10: Mechanisms for Software Reuse
Presentation transcript:

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 to do 2.The core of the exercise is to move the bounds test into the BoundedBall class which should be an extended class of Ball (don’t copy the code from ‘Ball’). 3.Store the height and length of the window in the BoundedBall class. Through accessor functions call it in BallWorld when the window is created. 4.The end result should be three files called BallWorld.java (the new one), BoundedBall.java (the new one) and Ball.java (unchanged). You do not have to upload the unchanged file Ball.java. “Rather than testing whether or not the ball has hit the wall in our main program, we could have used inheritance to provide a specialized form of ball. Create a class called “BoundedBall” that inherits from class Ball. The constructor for this class should provide height and width of the window, which should subsequently be maintained in as data fields in the class. Rewrite the move function so that is the ball moves outside the bounds it automatically reverses direction. Finally rewrite the BallWorld class to use an instance of BoundedBall rather than an ordinary Ball, and eliminate the bunds test in the main program.”

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; 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); }} The new Ball.java file The old Ball.java file Removed section

import java.awt.*; public class BoundedBall extends Ball { private static final int FrameWidth = 700; private static final int FrameHeight = 500; public BoundedBall (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 int Width () { return FrameWidth; } public int Height () { return FrameHeight; } public void move () { location.translate ((int) dx, (int) dy); if ((x() FrameWidth)) setMotion (-xMotion(), yMotion()); if ((y() FrameHeight)) setMotion (xMotion(), -yMotion()); } The new BoundedBall.java file The old Ball.java file Removed section Since we are going to use Ball as the definition class and use special cases as the actual object definition (which allows for extensions), we must place the constructor in the class where the objects are created instead of leaving it in the “master” definition class.

import java.awt.*; public class BoundedBall extends Ball { private static final int FrameWidth = 700; private static final int FrameHeight = 500; public BoundedBall (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 int Width () { return FrameWidth; } public int Height () { return FrameHeight; } public void move () { location.translate ((int) dx, (int) dy); if ((x() FrameWidth)) setMotion (-xMotion(), yMotion()); if ((y() FrameHeight)) setMotion (xMotion(), -yMotion()); } The new BoundedBall.java file The old BallWorld.java file Moved section Since the framewidth are fixed in BoundedBall.class, there is no longer a need to test for location (the users cannot change the private, static final variables)

import java.awt.*; public class BoundedBall extends Ball { private static final int FrameWidth = 700; private static final int FrameHeight = 500; public BoundedBall (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 int Width () { return FrameWidth; } public int Height () { return FrameHeight; } public void move () { location.translate ((int) dx, (int) dy); if ((x() FrameWidth)) setMotion (-xMotion(), yMotion()); if ((y() FrameHeight)) setMotion (xMotion(), -yMotion()); } The new BoundedBall.java file The old BallWorld.java file Moved section The accessor functions allows the users in BallWorld to request the size of the frame (window) that is defined in BoundedBall

import java.awt.*; public class BallWorld extends Frame { public static void main (String [] args) { BallWorld world = new BallWorld (Color.red); world.show(); } private BoundedBall aBoundedBall; private int counter = 0; private BallWorld (Color ballColor) { aBoundedBall = new BoundedBall (10, 15, 5); setSize (aBoundedBall.Width(), aBoundedBall.Height()); setTitle ("BallWorld"); aBoundedBall.setColor (ballColor); aBoundedBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { aBoundedBall.paint(g); aBoundedBall.move(); counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } The new BallWorld.java file The old BallWorld.java file The accessor functions allows the users in BallWorld to request the size of the frame (window) that is defined in BoundedBall, and use the function called “setSize” inherited from the Ball.class to set the size of the frame (window)

Exercise 4 illustrates the importance of inheritance through extensions, the need for clear functional delineation of the program (as may be provided by a UseCase diagram), as well as the importance of deployment diagrams and a detailed class library that tells the programmer what is available. End result Extended by Used in

End result Java Files Compiling Class Files Output