Graphics – Class Demo

advertisement
L11: Overloading
Name__________________________________
LEQ: How do you overload methods and constructors?
1. Activating Strategy
A. Read the Prologue of Lesson 11.
B. Our new scenario will be a simulation of planets and stars in space using ____________________ law of
universal gravitation.
2. Define Vocab words (definition only for now on, new terms)
Term
abstract class
Definition
template superclass that cannot be
instantiated
Connection
constant
overload
this
3. Newton Scenario (part 1)
(Note: As you work through this scenario, use your notes to answer questions)
1. Lab: Open Newtons-Lab-1 in (Share/Greenfoot/Chap06) . Save it as Newton to your account.
a. What is the World subclass named? ____________________
b. What is the name of the 2 Actor subclasses?
____________________________ and _______________
c. Place some Body objects into space (use 2nd constructor with no parameters) and run the program. Of
course, it does nothing right now, it will be your job to make the bodies move.
d. Explore Space Class (public methods)
1. Right click in the Space World (any blank area)
should see >>>>>>>>
2. Invoke each of the 3 methods one at a time to see what they do.
3. Invoke sunPlanetMoon. Right click each object and get the mass:

Mass of Sun
_______________

Mass of Planet
_______________

Mass of Moon
_______________
e. Open the Space class to see how it has been coded. You will see the World method addObject being used
and a new World method to get rid of an object named
_______________Objects.
L11 cont’d…
Page 2
2. Read 11.1 - Section A (answer these questions)
a. When we simplify a set of real objects, like astronomical objects, to a basic set of shared characteristics, like a
Body Class, we are using what OOP concept? Inheritance Polyorphism Abstraction Encapsulation
b. T F The Body class has a constructor that allows you to specify 8 parameters.
c. T F You can right click on the class SmoothMover to create an object.
d. T F Abstract classes serve as a “general template” used as a superclass for other classes.
e. T F An advantage to Abstract Classes is code reuse.
3. Read 11.1 – Section B (answer questions below)
a. Objects that move have a _____________________________.
b. Velocity is both a ____________ such as 100 MPH and a _______________________ such as UP
c. A ______________ is a geometric object with both ___________________ and __________________
d. Specifying a vector using direction and length is known as the _______________________ representation.
e. Specifying a vector as a pair of distances (dx, dy) is known as the _____________________ representation.
f.
The change in X is denoted as _______ and the change in y is denoted as ________.
4. Read 11.1 - Sections C,D (answer questions below)
a. The difference between an integer and a floating point data type is the ability to hold a __________ point.
b. Declare a variable suitable for holding decimal numbers: ______________
_____________________
c. We will always use type ________________ for decimal numbers.
5. Read 11.1 - Sections E,F (answer questions below)
a. When 2 constructors/methods have the same name, this is known as? overloading overriding impossible
b. When you overload a method, each method you create must be different in one of 2 ways:
1. Different ___________________ of parameters (Ex: int, int and int, int, int, int) -- (2 vs 4)
2. Same number of parameters, but the ____________ are not identical (Ex: int, int and int,double).
3. Two of the following are considered the same and cannot both exist. Which two? _________
A. foo (int a) B. foo (int a, int b) C. foo (double a) D. foo (double a, double b) E. foo (int b)
6. Read 11.1 - Section G (answer questions below)
a. You can reference the current object using the keyword ______________
b. Suppose an instance variable was named height and the associated parameter was named height
Initialize the instance variable to the parameter. _________.________________ = ______________
7. Lab: Explore Some More!
a. Try to create a SmoothMover object. Can you? _________ Why not? ____________________________
b. Open SmoothMover class. Identify the names of all of the overloaded constructors and methods.
1. _________________________________
(hint: 1 constructor)
2. _________________________________
(hint: 1 method)
Lesson 11 cont’d…
Page 3
8. Read 11.2 (answer questions below)
a. What is the type of the 2 variables used for tracking the position of a “body” object? __________________
b. What is the name of the Vector variable that determines amount to move ? _________________________
c. In move( ), what method is used to update the position of the body object? __________________________
d. What direction is an object facing if it has a rotation value of zero degrees? _______________
e. Constants are declared using what keyword? _______________
f.
______________ is used with constants so that only ____ copy is created for use by all objects
g. Define a constant named SIZE to store integer 20.
private _______________
_________________ __________ _________________ = ___________;
9. Lab: Make a Body Object Move
a. Open the Body Class and modify the act( ) method by adding 1 line: move (_);
b. Create some Body objects and Run the scenario.
c. Pause the scenario and answer these questions: Right click on an object to ask it questions.
1. What is the default direction of movement? _____ degrees or to the ___________ (invoke getRotation)
2. Default Speed ? __________ (invoke SmoothMover method getSpeed( ))
d. Invoke Space method sunAndPlanet and run the scenario. (slow speed down some, and watch closely)
1. What direction does the Sun Move In ? _____________ Planet ________________
2. What is the speed of the sun? ________
Planet? ____________
3. Where are these values specified? (hint: In method ___________________________ of Space class.)
e. Change the default direction of a body object to be to the left (what degrees is this?)
1. Open the Body Class and modify Constructor line: this (20, 300, new Vector(0, 1.0), defaultColor);
Only 1 VALUE NEEDS CHANGED! --- HINT: The Vector controls the direction!
2. Compile. Create 1 Body Object and Test until you get this working properly.
f.
From all of this testing, we see that all bodies move in a ________________ line. To fix this, we need to
add code that applies gravitational force which is covered in the next Lesson!
4. Finish Vocabulary
5. Key Concepts Review
1. A vector is a geometric object with both__________________and ___________________________.
2. A vector is often used to represent the ______________________ of an object.
3. The 2 ways to represent a vector is to use ____________________ or _________________________ coordinates.
4. The d in dx and dy means_____________________.
5. What Java type will we be using to store decimal values? ________________
(ie: int is the type for Integer).
6. What java Keyword allows you to refer to the “current object”? _______________
7. What keyword makes a “variable” a constant ? ________________ Name constants using ____________ case.
8. void move(int amount) and void move(double amount) is an example of ______________________________.
6. Answer LEQ: How do you overload methods and constructors?
_________________________________________________________________________________________________
Download