File - Mr. Smith`s Classes

Download Report

Transcript File - Mr. Smith`s Classes

Much of this
Powerpoint is
taken from the
Litvins Java
methods book.
Java Methods
A & AB
Object-Oriented Programming
and Data Structures
Maria Litvin ● Gary Litvin
Thursday
Quiz:
Methods,
Sorts, Intro
to Objects
h
C
t
p
a
e
r
3
Objects and Classes
Start on Slide 30 for day 2
Objectives:
• See an example of a small program written in
OOP style and discuss the types of objects
used in it
• Learn about the general structure of a class,
its fields, constructors, and methods
• Get a feel for how objects are created and
how to call their methods
• Learn a little about inheritance in OOP
3-2
Object Oriented Programming:
OOP
• An OO program models the application as a
world of interacting objects.
• An object can create other objects.
• An object can call another object’s (and its
own) methods (that is, “send messages”).
• An object has data fields, which hold values
that can change while the program is running.
3-3
Objects
• A software bundle of related variables and
methods.
• Can model real-world objects
• Can represent software entities (events,
files, images, etc.)
• It is kind of like a turbo-charged Pascal
Record. It not only contains the different
fields of data, it also contains code.
3-4
Classes and Objects
• A class is a blueprint or prototype that
defines the variables and the methods
common to all objects of a certain type.
• Similar to a type declaration in Pascal
• An object is called an instance of a class.
A program can create and use more than
one object (instance) of the same class.
• (This process is also called instantiation.)
3-5
SomeClass.java
import ...
import statements
public class SomeClass
{
• Fields
Attributes / variables that define the
object’s state; can hold numbers,
characters, strings, other objects
• Constructors
• Methods
}
Class header
Methods for constructing a
new object of this class and
initializing its fields
Actions that an object
of this class can take
(behaviors)
3-6
Classes and Source Files
• Each class is stored in a separate file
• The name of the file must be the same as the
name of the class, with the extension .java
Car.java
public class Car
{
...
}
By convention, the
name of a class
(and its source file)
always starts with
a capital letter.
(In Java, all names are case-sensitive.)
3-7
Libraries
• Java programs are usually not written from
scratch.
• There are hundreds of library classes for all
occasions.
• Library classes are organized into packages
(folders.) For example:
java.util — miscellaneous utility classes
java.awt — windowing and graphics toolkit
javax.swing — GUI development package
3-8
import
• Full library class names include the package
name. For example:
java.awt.Color
javax.swing.JButton
• import statements at the top of the source file
let you refer to library classes by their short
names:
Fully-qualified
import javax.swing.JButton;
name
...
JButton go = new JButton("Go");
3-9
import (cont’d)
• You can import names for all the classes in a
package by using a wildcard .*:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Imports all classes
from awt, awt.event,
and swing packages
• java.lang is imported automatically into all
classes; defines System, Math, Object,
String, and other commonly used classes.
3-10
SomeClass.java
import ...
import statements
public class SomeClass
{
• Fields
• Constructors
• Methods
}
Class header
Attributes / variables that define the
object’s state; can hold numbers,
characters, strings, other objects
Procedures for constructing a
new object of this class and
initializing its fields
Actions that an object
of this class can take
(behaviors)
3-11
Fields
• A.k.a. instance variables
• Constitute “private memory” of an object
• Each field has a data type (int, double, String,
Image, Foot, etc.)
• Each field has a name given by the
programmer
3-12
You name it!
Fields (cont’d)
private [static] [final] datatype name;
Usually
private
May be present:
means the field is
shared by all
objects in the class.
Does not need to
be made into an
object to use it.
int, double, etc., or an
object: String, Image,
Foot
Constant:
May be present:
means the field
is a constant
private int numberOfPassengers;
3-13
Example: Fields/
Instance Variables
public class Car
{
private String model;
private int numberOfPassengers;
private double amountOfGas;
}
3-14
Constructors
•
•
•
•
•
Short methods for creating objects of a class
Always have the same name as the class
Initialize the object’s fields
May take parameters
A class may have several constructors that
differ in the number and/or types of their
parameters. (Polymorphism)
3-15
Constructors (cont’d)
public class Car
{
private String model;
private int numberOfPassengers;
private double amountOfGas;
The name of a constructor
is always the same as the
name of the class
public Car (String name, int pass, double gas)
{
model = name;
A constructor can take parameters
numberOfPassengers = pass;
amountOfGas = gas;
}
...
}
Initializes fields
3-16
Constructor Rules
• It is usually a good idea to provide a default or “no-
•
args” constructor that takes no parameters
(arguments).
If no constructors are supplied at all, then Java
provides one default no-args constructor that only
allocates memory for the object and initializes its fields
to default values (numbers to zero, booleans to
false, objects to null).
• A class may have only one no-args constructor. In
general, two constructors for the same class with
exactly the same number and types of parameters
cannot coexist.
3-17
Constructors
CarTest.java and Car.java need to
be in the same Project (Folder)
An object is created with
// CarTest.java
the new operator
...
...
Car firstCar = new Car (“Yugo”, 0, 5.0);
...
public class Car
{
private String model;
private int numberOfPassengers;
private double amountOfGas;
The number, order, and
types of parameters must
match
public Car (String name, int pass, double gas)
{
...
}
...
}
Constructor
3-18
Default Constructor: No parameters
public class Car
{
private String model;
private int numberOfPassengers;
private double amountOfGas;
public Car ()
{
model = “”;
numberOfPassengers = 0;
amountOfGas = 0.0;
}
public Car (String name, int pass, double gas)
{
model = name;
numberOfPassengers = pass;
amountOfGas = gas;
}
Default constructor
If you have
multiple
constructors, they
must have
different numbers
and/or types of
parameters.
...
}
3-19
Calling different constructors
public class Car
{
private String model;
private int numberOfPassengers;
private double amountOfGas;
public Car ()
{
model = “”;
numberOfPassengers = 0;
amountOfGas = 0.0;
}
// CarTest.java
...
...
Car secondCar = new Car();
Car firstCar = new Car (“Yugo”, 0, 5.0);
...
public Car (String name, int pass, double gas)
{
model = name;
numberOfPassengers = pass;
amountOfGas = gas;
}
...
}
3-20
Review: Match the following
1. Class
2. Constructor
3. Object
4. private
5. static
6. final
7. Instance variables
8. import
a) Constant
b) Can be used by methods inside
the object only
c) Can be used without creating an
object
d) Used to tie to libraries of classes.
e) Used to create (instantiate) an
object.
f) A blueprint for making objects.
g) Fields, the data part of an object.
h) An instance of a class.
3-21
Copy and Open the Shapes
Project
• Copy the Shapes folder from the
Assignments Folder into your AP Java Folder.
• From BlueJ, Open the Shapes Project.



File
Open Project
Double Click on ‘Shapes’
• Open the ‘Circle’ class
3-22
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Circle()
{
diameter = 30; xPosition = 20;
yPosition = 60; color = "blue";
isVisible = false;
}
public Circle(int sentDiameter)
{
diameter = sentDiameter;
xPosition = 20; yPosition = 60;
color = "blue"; isVisible = false;
}
public void makeVisible()
{
isVisible = true;
draw();
}
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter));
canvas.wait(10);
}
}…
• What is the class name?
• Where is the constructor?
• What, if any, parameters are
there?
• Are there any instance
variables? If so, what are they?
• How can you find the
constructor?
• What else is in the class?
3-23
Review
•
•
•
•
•
•
•
•
•
What do you recall about
Constructors
Methods
new
Calling a method
Application
Instance variables
private
public
3-24
Experimenting with Previously
Written Methods:
Circles and the Object Bench
• Calling the constructor.
• Create circles
– Right click on the class
Circle
– Select ‘new Circle()’
– Give the circle a name
Looking at Methods from the
Object Bench
• Right click on the object you have just
created.
• Try makeVisible(), makeInVisible()
• Experiment with the other methods to
manipulate the object.
• Make other circles.
• Make other objects.
Make a drawing
• Make a house with a sun, roof, and
window
• Make a… turkey, hedgehog, tennis racket,
robot, baseball field, beaver, thanksgiving
scene,…
• Demo to Smith
Under construction
• Open the Shapes class and add constructors for the
Circle, Square and Triangle classes.








Circle: For entering the radius
Circle: For entering the x and y coordinates of the point
Circle: Add Radius, x, and y coordinates
Square: Size
Square: X and Y coordinates
Square: Size, X and Y coordinates.
Triangle: X and Y Coordinates
Traingle: Height, Width, X and Y coordinates.
• Test your constructors using the object bench.
3-28
Class
Name.
Writing an Application to Make
Objects
This defines the
object that is created.
Class
name. This
public class
Drawing
defines the type of the
{
(reference).
publicpointer
static void
main(String [] args)
{
After the object is
Circle pumpkin = new Circle();
created, you can
pumpkin.makeVisible();
call the public
Square smith = new Square();
methods of object
smith.makeVisible();
that were defined
Circle sun = new Circle(10);
in the class.
sun.makeVisible();
} Create an Application to make a scene or a drawing
}
using objects of the previous defined classes.
Label it ‘yournameSceneApp’
3-29
Review
•
•
•
•
•
•
•
•
•
•
What do you recall about
Constructors
Methods
new
Calling a method
Application
Instance variables
this
private
public
3-30