Lec 13 Writing an Instantiable Class II

Slides:



Advertisements
Similar presentations
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Advertisements

Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with.
CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.
OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Object-Oriented Programming Concepts
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
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!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC 142 Computer Science II Zhen Jiang West Chester University
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Objects as a programming concept
Building Java Programs
Objects as a programming concept
Examples of Classes & Objects
Chapter 7- Inheritance.
Haidong Xue Summer 2011, at GSU
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Road Map Introduction to object oriented programming. Classes
Classes A class is a blueprint of an object
suggested reading: Java Ch. 6
CS Week 13 Jim Williams, PhD.
CS 106A, Lecture 22 More Classes
CSC 113: Computer programming II
CS 200 More Classes Jim Williams, PhD.
Classes & Objects: Examples
ITunes Lab Copyright © 2012 Pearson Education, Inc.
CS 302 Week 9 Jim Williams.
Tonga Institute of Higher Education
Which best describes the relationship between classes and objects?
PreAP Computer Science Quiz Key
JAVA CLASSES.
Classes / Objects / Methods
Dr. R Z Khan Handout-3 Classes
Agenda About Homework for BPJ lesson 15 Overloading constructor
Classes and Objects CGS3416 Spring 2019.
Chapter 4 Test Review First day
Presentation transcript:

Lec 13 Writing an Instantiable Class II

Agenda More class examples Adding to our Balloon Class demo: default Constructor method inflate method that takes a parameter (amount) pop method volume method

We have been working with objects to represent complex things (Strings, Islands, Scanners)

Now we are taking a look at what's inside an object

data code (methods)

Structure of an Instantiable Class – blueprint for objects Class Name class Dog size breed Dog bark rollOver beg Instance Varbls Methods Constructor method same name as class

Suppose we wanted to represent a piggy bank? Class Name class PiggyBank Instance Varbls Methods Constructor method same name as class

Suppose we wanted to represent a piggy bank? (solution) Class Name class PiggyBank numQuarters numDimes PiggyBank addQuarters addDimes removeQuarters removeDimes currentValue Instance Varbls Methods Constructor method same name as class

Suppose we wanted to represent a hamburger? (from lab 4) Class Name class Hamburger Instance Varbls Methods Constructor method same name as class

Suppose we wanted to represent a hamburger? (solution) Class Name class Hamburger numPatties numLettuce Hamburger addPatty addLettuce print Instance Varbls Methods Constructor method same name as class

Back to Balloon Class Name Instance Varbls Methods class Balloon size color Balloon inflate getSize getColor setColor pop Instance Varbls Methods size Color.YELLOW Constructor method same name as class

Method Overloading We can add methods to Balloon with same names as long as have different parameters the Constructor we made in our Balloon class A second "Default" constructor with no parameters

Which one is used? Balloon myBal = new Balloon(10,Color.RED); Balloon yrBal = new Balloon(); default constructor automatically sets size to 0 sets color to Color.WHITE

Now we'll begin BalloonDemo2 start with BalloonDemo from last time add default constructor add inflate method that takes an amount add pop method modify both inflate methods add volume method should return 0 if balloon has been popped

More Dairy.java make Lab13DairyFarm folder Copy, Dairy.java file from Lab12 folder, then add default Constructor addCow takes a number calcMllkProfit takes a price/gallon calcButterProfit takes a price/pound Create a new class Farm.java that tests your new Dairy class by constructing a dairy farm, adding cows and printing the number of cows And printing profit from selling milk or butter