Java exercise review Lesson 25: Exercise 1, 2 and 3.

Slides:



Advertisements
Similar presentations
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
OOP Week 3 1 Object Oriented Programming in Java Monday, Week 3 Interface PinBallTarget OOP Concepts Last Week’s Assignment Arrays Collection Class --
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
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
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
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!
Session 21 Chapter 10: Mechanisms for Software Reuse.
Object Oriented Programming Lecture 5: BallWorld.
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.
© 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.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.
Even-Driven Programming and basic Graphical User Interface.
Agenda Object Oriented Programming Reading: Chapter 14.
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.
© 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.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
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 22 Chapter 11: Implications of Inheritance.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
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.
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.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
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;
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Introducing, the JFrame Gives us a work area beside System.out.println.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A structured walkthrough
Building Java Programs
Stack Memory 2 (also called Call Stack)
A+ Computer Science METHODS.
Simple Classes in Java CSCI 392 Classes – Part 1.
Building Java Programs
A+ Computer Science METHODS.
Building Java Programs
An Example of Inheritance
Debugging Exercise 00 Try compiling the code samples.
Chapter 10: Mechanisms for Software Reuse
Presentation transcript:

Java exercise review Lesson 25: Exercise 1, 2 and 3

import java.awt.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show(); } public static final int FrameWidth = 600; public static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (Math.random()*15.0, Math.random()*15.0); } public void paint (Graphics g) { aBall.paint(g); aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #1 Using Math.random modify the program so that the ball initially moves in a random direction Since Ball.Class accepts only ‘double’ as an argument, we must force the result into a double format by using a decimal point: From the Ball.class: public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; }

The code for exercise#1

The Result for exercise#1

import java.awt.*; public class MultiBallWorld extends Frame { public static void main (String [] args) { MultiBallWorld world = new MultiBallWorld (); world.show(); } public static final int FrameWidth = 600; public static final int FrameHeight = 400; private Ball[] ballArray; private double pickColor; private int counter = 0; private static final int BallArraySize = 8; private MultiBallWorld () { setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); ballArray = new Ball [BallArraySize]; for (int i = 0; i < BallArraySize; i++) { ballArray[i] = new Ball(10, 15, 5); pickColor = Math.random()*10.0; if (pickColor < 3.3) ballArray[i].setColor (Color.green); else if (pickColor >= 3.3 && pickColor < 6.6) ballArray[i].setColor (Color.red); else if (pickColor >= 6.6 && pickColor < 10.0) ballArray[i].setColor (Color.blue); ballArray[i].setMotion (3.0+i, 6.0-i); }} public void paint (Graphics g) { for (int i = 0; i < BallArraySize; i++) { ballArray[i].paint(g); // then move it slightly ballArray[i].move(); if ((ballArray[i].x() FrameWidth)) ballArray[i].setMotion (-ballArray[i].xMotion(), ballArray[i].yMotion()); if ((ballArray[i].y() FrameHeight)) ballArray[i].setMotion (ballArray[i].xMotion(), -ballArray[i].yMotion()); } counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #2 Using Math.random modify the program so that the balls have different colors A variable is used (pickColor) to store the randomized value. The value is increased by 10 to get a double and conditioned through if…else if statements to set the color using the setColor function from Ball.class: public void setColor (Color newColor) { color = newColor; }

The code for exercise#2

The Result for exercise#2

import java.awt.*; public class MultiBallWorld extends Frame { public static void main (String [] args) { MultiBallWorld world = new MultiBallWorld (); world.show(); } public static final int FrameWidth = 600; public static final int FrameHeight = 400; private Ball[] ballArray; private double pickColor; private int counter = 0; private static final int BallArraySize = 8; private MultiBallWorld () { setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); ballArray = new Ball [BallArraySize]; for (int i = 0; i < BallArraySize; i++) { ballArray[i] = new Ball(10, 15, (i+5)); pickColor = Math.random()*10.0; if (pickColor < 3.3) ballArray[i].setColor (Color.green); else if (pickColor >= 3.3 && pickColor < 6.6) ballArray[i].setColor (Color.red); else if (pickColor >= 6.6 && pickColor < 10.0) ballArray[i].setColor (Color.blue); ballArray[i].setMotion (3.0+i, 6.0-i); }} public void paint (Graphics g) { for (int i = 0; i < BallArraySize; i++) { ballArray[i].paint(g); // then move it slightly ballArray[i].move(); if ((ballArray[i].x() FrameWidth)) ballArray[i].setMotion (-ballArray[i].xMotion(), ballArray[i].yMotion()); if ((ballArray[i].y() FrameHeight)) ballArray[i].setMotion (ballArray[i].xMotion(), -ballArray[i].yMotion()); } counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #3 Using Math.random modify the program so that the balls have different radiuses and colors Notice that the constructor for ball needs to be in format of X,Y coordinate and then the radius. All messages must be integers since this is the way “ball.java” was defined: public Ball (int x, int y, int r) { ……… }

The code for exercise#3

The result for exercise#3

In-class assignment 1.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’). 2.Write the psudocode and the deployment diagram for what you are planning to do 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.