Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with.

Slides:



Advertisements
Similar presentations
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Copyright © Texas Education Agency, Computer Programming Class Basics.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lec 7 Using Static Class Methods. Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them.
Road Map Introduction to object oriented programming. Classes
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Utilities (Part 2) Implementing static features 1.
Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Lecture 8 Using casts, Strings and WordUtil. Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
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.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Objects as a programming concept
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Java Primer 1: Types, Classes and Operators
CompSci 230 Software Construction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Defining Classes and Methods
An Introduction to Java – Part II
Classes & Objects: Examples
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Programs and Classes A program is made up from classes
Lec 13 Writing an Instantiable Class II
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Classes, Objects and Methods
Lecture 9 Using Objects.
Corresponds with Chapter 5
Chapter 7 Objects and Classes
Presentation transcript:

Lec 12 Writing an Instantiable Class

Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with it's own Javadoc! Constructor method Parameter passing into methods Returning values from methods

We have been learning how to represent data in Java Primitives – int, char, double, boolean – only hold simple pieces of data a temperature, a choice, how many layers of lettuce Objects (of existing Instantiable Classes) – Island, Particle, GolfAnimator, String, Scanner more complicated "things" data and methods (actions) combined Class as a blueprint that lets us make many "instances"

Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String [] args) – all of our programs to this point 2.Classes with only static utility methods – like Math class, NumericalStuff, StringUtil, Coins – to invoke a method: nameOfClass.method(parameters); double flip = Math.random(); // picks a random number // and stores in flip Coins.dispenseQuarter(); // prints a quarter on console

Instantiable Class 3. Instantiable Class – used to make objects from – String, Scanner, Point, Island, GolfAnimator, Particle – to invoke a method nameOfObject.method(parameters); – Island desert = new Island(5); // desert is name of new Island – desert.moveNorth(); // move N on Island desert – Island hawaii = new Island(1000); // hawaii is new Island – hawaii.moveEast(); // move E on Island hawaii

Review: Classes and Objects

Object (reference) variables

How to represent a Balloon in Java? Data to define it: size color Methods to modify it: can make a new Balloon can inflate it can get the size, color can change the color can pop it! size Color.YELLOW

Structure of an Instantiable Class class Balloon size color Balloon inflate getSize getColor setColor pop Class Name Instance Varbls Methods Constructor method same name as class

Balloon Demonstration We will now create our own Balloon class If you want you can just download these into new class files and run – Balloon Class Balloon – BalloonApp Class BalloonApp We will also learn the following – parameter passing into a method – returning a value – creating our own JavaDoc for our balloon class Also, here is the Particle class from our previous labsParticle

Using a class -- objects A class is a blueprint for making objects When you declare a variable of your new class it’s called an object reference variable: Balloon hotAir; hotAir = new Balloon(5,Color.RED); Balloon bal=new Balloon(2,Color.BLUE); Balloon weather= new Balloon(200,Color.WHITE); hotAir bal weather

You can use the public methods defined for the class Balloon bal= new Balloon(5,Color.BLUE); bal.inflate(); bal.pop();

Parameter Passing When we "call" a method: (in BalloonApp class) We jump into the method with the arguments (Balloon class)

When a method is invoked or "called": We jump into the method (with any params) When a value is "returned" from a method – we exit method body – return value replaces the "caller" Return statements

Why Classes and Objects ? It may seem overwhelming or unnecessary at first As the complexity/size of your code increases, classes will help you modularize your code Helps you visualize and solve problems better Brings more order into your code

Dairy.java make Lab12DairyApp folder Create new class, Dairy.java fill in definition of – Constructor – getNumCows – addCow Create a new class DairyApp.java – test your new Dairy class by constructing a dairy farm, adding cows and printing the number of cows