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.

Slides:



Advertisements
Similar presentations
AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Basics M Taimoor Khan
Road Map Introduction to object oriented programming. Classes
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Introduction to Programming Writing Java Beginning Java Programs.
Object Oriented Design: Identifying Objects
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object Oriented Programming … and other things you need to program in java.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Introduction to Programming Writing Java Beginning Java Programs.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Inheritance A Review of Objects, Classes, and Subclasses.
Objects and Classes Mostafa Abdallah
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes and Objects Including inheritance. OOP — Object-Oriented Programming In OOP system, the class is a fundamental unit. An OOP program models a.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Java Classes, Objects, and Events: A Preview
Yanal Alahmad Java Workshop Yanal Alahmad
CS 302 Week 11 Jim Williams, PhD.
Java Methods Objects and Classes
Java Methods Objects and Classes Start on Slide 30 for day 2
Basics of OOP A class is the blueprint of an object.
Java Methods Objects and Classes Start on Slide 30 for day 2
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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 ;

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

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)

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

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.

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)

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.

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

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.

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

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?

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

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

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.

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’

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