Objects First with Java

Slides:



Advertisements
Similar presentations
Understanding class definitions Looking inside classes 5.0.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Fields, Constructors, Methods
Lab 10: Bank Account II Transaction List, error checking & aggregation.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions Looking inside classes 3.0.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Object interaction Creating cooperating objects 3.0.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
Understanding class definitions Looking inside classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
COS 260 DAY 5 Tony Gauvin.
Lab 7: Tree & Forest. Drawing a Forest Created by Emily Hill & Jerry Alan Fails.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Lab 7: BoxCar Class. Drawing a BoxCar Step 1:Create classes for Circle & Rectangle (done!) Step 2:Create a BoxCarPart class that can draw this part at.
Looking inside classes Conditional Statements Week 4.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
Understanding class definitions Exploring source code 6.0.
Introduction to Object-oriented Programming
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Objects First with Java A Practical Introduction using BlueJ
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Agenda Warmup AP Exam Review: Litvin A2
Object INTERACTION CITS1001 week 3.
12 Data abstraction Packages and encapsulation
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Introduction to Object-oriented Program Design
Objects First with Java Transaction List, error checking & aggregation
Understanding class definitions
Objects First with Java A Practical Introduction using BlueJ
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
Chapter 4: Writing classes
Object-oriented Design in Processing
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
Implementing Classes Chapter 3.
Chapter 7 Part 2 Edited by JJ Shepherd
Creating cooperating objects
Object-oriented Design in Processing
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Creating cooperating objects
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Building Java Programs
COS 260 DAY 6 Tony Gauvin.
Object-oriented Design in Processing
Further abstraction techniques
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Object-oriented Design in Processing
Presentation transcript:

Objects First with Java Lab 4: Circle Outline Replace this with your course title and your name/contact details. © David J. Barnes and Michael Kölling

Objects First with Java Basic class structure public class ClassName { Fields Constructors Methods } Three major components of a class: Fields – store data for the object to use Constructors – allow the object to be set up properly when first created Methods – implement the behavior of the object © David J. Barnes and Michael Kölling

Objects First with Java Fields Fields store values for an object. They are also known as instance variables. Fields define the state of an object. public class Square { private int x; private int y; private int size; private Color fillColor;   // Further details omitted. } type visibility modifier variable name private int size; © David J. Barnes and Michael Kölling

Objects First with Java Constructors public Square(int x, int y) { this.x = x; this.y = y; size = 50; fillColor = Color.blue; } Constructors initialize an object. They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. Only here as a brief introduction and to reinforce what they saw last class © David J. Barnes and Michael Kölling

Objects First with Java Methods /** * Gets the size of the square. */ method header/signature return type visibility modifier method name parameter list (empty) public int getSize() { return size; } return statement Only here as a brief introduction start and end of method body (block) © David J. Barnes and Michael Kölling

Method & Field Practice Objects First with Java Method & Field Practice Modify the Circle class to display a colored outline: Add a field outlineColor, plus get & set methods Add another constructor that takes the outline color as a parameter What should the outline color be set to in the original constructors? Update paintComponent to draw the outline AND the filled oval (hint: use drawOval) Use your Picture applet to test and make sure the outline is showing up correctly Constructor + method practice THEN talk about what they’re going to do next time… with making the applet interactive © David J. Barnes and Michael Kölling

Extra: Change to Square & Triangle Make similar changes to Square and Triangle that you did to Circle Add a constructor that accepts as parameters the initial x and y positions, radius, and color Add a field outlineColor, plus get & set methods Add another constructor that takes the outline color as a parameter What should the outline color be set to in the original constructors? Update paintComponent to draw the outline AND the filled oval Use your Picture applet to test and make sure the outline is showing up correctly

What we did today … Concepts practiced Constructors Methods Classes Modified the Circle, Square, and Triangle classes: Added the ability to have a different colored outline

Homework Finish lab exercise Read Chapters 2 and 3