Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.

Similar presentations


Presentation on theme: "Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of."— Presentation transcript:

1 Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of this Powerpoint is taken from the Litvins Java methods book. Thursday Quiz: Methods, Sorts, Intro to Objects

2 3-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 3-3 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.

4 3-4 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.

5 3-5 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.)

6 3-6 public class SomeClass Fields Constructors Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Methods for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import... import statements

7 3-7 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 public class Car {... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)

8 3-8 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

9 3-9 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: import javax.swing.JButton;... JButton go = new JButton("Go"); Fully-qualified name

10 3-10 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.*; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

11 3-11 public class SomeClass Fields Constructors Methods } 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) { Class header SomeClass.java import... import statements

12 3-12 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

13 3-13 private [static] [final] datatype name ; Fields (cont’d) Usually private Constant: May be present: means the field is a constant int, double, etc., or an object: String, Image, Foot You name it! 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. private int numberOfPassengers ;

14 3-14 Example: Fields/ Instance Variables public class Car { private String model; private int numberOfPassengers; private double amountOfGas; }

15 3-15 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)

16 3-16 Constructors (cont’d) public class Car { private String model; private int numberOfPassengers; private double amountOfGas; public Car (String name, int pass, double gas) { model = name; numberOfPassengers = pass; amountOfGas = gas; }... } The name of a constructor is always the same as the name of the class A constructor can take parameters Initializes fields

17 3-17 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, boolean s 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.

18 3-18 Constructors public class Car { private String model; private int numberOfPassengers; private double amountOfGas; public Car (String name, int pass, double gas) {... }... } // CarTest.java... Car firstCar = new Car (“Yugo”, 0, 5.0);... An object is created with the new operator The number, order, and types of parameters must match Constructor CarTest.java and Car.java need to be in the same Project (Folder)

19 3-19 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.

20 3-20 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; }... } // CarTest.java... Car secondCar = new Car(); Car firstCar = new Car (“Yugo”, 0, 5.0);... Calling different constructors

21 3-21 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.

22 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

23 3-23 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?

24 3-24 Review What do you recall about Constructors Methods new Calling a method Application Instance variables private public

25 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

26 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.

27 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

28 3-28 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.

29 3-29 Writing an Application to Make Objects public class Drawing { public static void main(String [] args) { Circle pumpkin = new Circle(); pumpkin.makeVisible(); Square smith = new Square(); smith.makeVisible(); Circle sun = new Circle(10); sun.makeVisible(); } Class name. This defines the type of the pointer (reference). Class Name. This defines the object that is created. After the object is created, you can call the public methods of object that were defined in the class. Create an Application to make a scene or a drawing using objects of the previous defined classes. Label it ‘yournameSceneApp’

30 3-30 Review What do you recall about Constructors Methods new Calling a method Application Instance variables this private public


Download ppt "Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of."

Similar presentations


Ads by Google