Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Similar documents
Where do objects come from? Good question!

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Designing Classes Part 2

Designing Classes part 2

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

Chapter 4 Writing Classes

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 4: Writing Classes

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

ECE 122. Engineering Problem Solving with Java

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

writing classes objectives chapter

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS1004: Intro to CS in Java, Spring 2005

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Assignment 1 due Monday at 11:59pm

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Using Classes and Objects

ECE 122. Engineering Problem Solving with Java

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Data Representation and Applets

Selection Statements and operators

Applets and the Graphics class

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Packages & Random and Math Classes

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

CSC 1051 Data Structures and Algorithms I

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Data Representation and Applets

Selection Statements and operators

Data Representation and Applets

Chapter 9 Inheritance

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

Writing Classes Chapter 5. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Using Classes and Objects

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Chapter 5: Writing Classes and Enums

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Adding Buttons to StyleOptions.java

Introduction to Arrays

Encapsulation. Mason Vail Boise State University Computer Science

Algorithms and Conditionals

Objects and Classes -- Introduction

Using Classes and Objects

Graphical User Interfaces

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Classes and Methods: Classes

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) COMP 202 Objects 2 1

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

AP CS Unit 12: Drawing and Mouse Events

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Recommended Group Brainstorm (NO computers during this time)

CH. 2 OBJECT-ORIENTED PROGRAMMING

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Selection and Repetition

Homework 6 part 2: Turtle Etch-A-Sketch (40pts)

CSC 1051 Data Structures and Algorithms I

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) Instance Data. COMP Objects 2 1

CS 302 Week 9. Jim Williams

Chapter 3 Using Classes and Objects

Practice Midterm 1. Problem Points Score TOTAL 50

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

CS-202 Introduction to Object Oriented Programming

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Chapter 4 Defining Classes I

Data Representation and Applets

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Building Your Own Classes

Lab 3: Work with data (IV)

Practice Midterm 1 Answer Key

Java I/O and Control Structures Algorithms in everyday life

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

Transcription:

Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects come from? Good question! Example: Account datatype - represents a generic bank account We will learn how to create a class that defines a new datatype, i.e., a new type of objects We need to learn: 1. What is the structure of a class definition? 2. How to specify what happens when an object is instantiated (i.e., when the new operator is used)? 3. How do we define the methods that can be invoked through objects of this class? balance 102.56 acct2 69713 balance 40.00 "Jane Smith" Dr. Papalaskari 1

Data Methods 1. Structure of class definition Account class int ; String ; Constructor Account object balance 72354 102.56 The object: is like the house built from the tostring() blueprint is an instance of the class The class is the blueprint has its own data space & shares Classes define DATA and METHODS methods defined for this datatype i.e., a datatype 2. Object instantiation Creating Objects old example: We have already seen something like this: Scanner scan = new Scanner (System.in); This invokes the Scanner constructor, which is a special method that sets up the object 2. Object instantiation 2. Object instantiation Creating Objects our newly defined Account class: Creating Objects our newly defined Account class: Account acct1 = new Account (, 72354, 102.56); Account acct1 = new Account (, 72354, 102.56); int ; String ; Constructor Invokes the Account constructor, which is a special method that sets up the object A new Account object is created! balance 102.56 tostring() Dr. Papalaskari 2

3. Method invocation As we have seen, once an object has been created, we can use the dot operator to invoke its methods: ans = scan.nextline(); numchars = title.length(); amount = acct1.; acct1.deposit (25.85); Chapter 4: Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. class definitions constructors instance data method declaration and parameter passing encapsulation and Java modifiers more about creating graphical objects (next week) balance 128.41 Datatype / Client (also referred to as slave / driver classes) Transactions.java Author: MA Papalaskari (based on Lewis/Loftus example) int ; Demonstrates the creation and use of multiple Account objects. String ; public class Transactions ******************************************************************** Constructor Account.java Author: Lewis/Loftus Creates some bank accounts and requests various services. Simplified code by MA Papalaskari Represents a bank account with methods deposit public and withdraw. static void main (String[] args) ******************************************************************** Account acct1 = new Account (, 72354, 102.56); import java.text.numberformat; Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); public class Account System.out.println (acct1); int ; tostring() System.out.println (acct2); System.out.println (acct3); String ; acct1.deposit (25.85); Client --------------------------------------------------------------- acct1.withdraw (60, 2.50); -- Sets up the account Account by defining its owner's, System.out.println account (); number, and initial balance. System.out.println (acct1); --------------------------------------------------------------- System.out.println (acct2); -- System.out.println (acct3); public Account (String x, Datatype int y, double z) = x; = y; balance = z; Transactions Account.java Author: Lewis/Loftus Simplified code by MA Papalaskari Represents a bank account with methods deposit and withdraw. import java.text.numberformat; public class Account int ; String ; Sets up the account by defining its owner's, account int ; number, and initial balance. public Account (String x, int y, double z) String ; Constructor = x; = y; balance = z; Deposits the specified amount x into the account. tostring() public void deposit (double x) balance = balance + x; Dr. Papalaskari 3

Withdraws the specified amount from the account and applies the fee. public void withdraw (double x, double fee) balance = balance - x - fee; Returns the current balance of the account. int ; public double getbalance () return balance; String ; Constructor Returns a one-line description of the account as a string. public String tostring () NumberFormat fmt = NumberFormat.getCurrencyInstance(); tostring() return ( + "\t" + + "\t" + fmt.format(balance)); Transactions.java Author: MA Papalaskari (based on Lewis/Loftus example) Demonstrates the creation and use of multiple Account objects. public class Transactions Creates some bank accounts and requests various services. public static void main (String[] args) Account acct1 = new Account (, 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); Transactions.java Author: MA Papalaskari (based on Lewis/Loftus example) Demonstrates the creation and use of multiple Account objects. public class Transactions Creates some bank accounts and requests various services. public static void main (String[] args) Account acct1 = new Account (, 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); Sample Run 72354 Ted Murphy $102.56 69713 Jane Smith $40.00 93757 Edward Demsey $759.32 72354 Ted Murphy $65.91 69713 Jane Smith $40.00 93757 Edward Demsey $759.32 Transactions class: Creating Account objects int ; String ; Constructor tostring() Transactions class Account acct1 = new Account (, 72354, 102.56); Account class public Account (String x, int y, double z) = x; Constructor method = y; balance = z; Dr. Papalaskari 4

Transactions class: Creating Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); Transactions class: Creating more Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); int ; String ; Constructor Account class public Account (String x, int y, double z) = x; Constructor method = y; balance = z; Transactions class balance 102.56 Account acct2 = new Account ("Jane Smith", 69713, 40.00); tostring() balance 102.56 acct2 69713 balance 40.00 "Jane Smith" Account class: Using methods acct1.deposit (25.85); Account class: Using methods acct1.deposit (25.85); --------------------------------------------------- Deposits the specified amount into the account. --------------------------------------------------- public void deposit (double x) balance = balance + x; --------------------------------------------------- Deposits the specified amount into the account. --------------------------------------------------- public void deposit (double x) balance = balance + x; balance 102.56 balance 128.41 Dr. Papalaskari 5

Account class: Another Example acct1.withdraw (60, 2.50); Account class: Another Example acct1.withdraw (60, 2.50); ------------------------------------------------ Withdraws the specified amount from the account and applies the fee. ------------------------------------------------ public void withdraw (double x, double fee) balance = balance - x - fee; ------------------------------------------------- Withdraws the specified amount from the account and applies the fee. ----------------------------------------------- public void withdraw (double x, double fee) balance = balance - x - fee; balance 128.41 balance 65.91 tostring() method can be omitted! getbalance () method System.out.println(acct1.toString()); double amount = acct1.; Note: this code is not used in Transactions.java public String tostring () NumberFormat fmt = NumberFormat.getCurrencyInstance(); return ( +"\t + +"\t + fmt.format(balance)) public double getbalance () return balance; acct1 72354 acct1 72354 balance 102.56 balance 102.56 Dr. Papalaskari 6

Class definitions A class can contain data declarations and method declarations int ; String ; Constructor tostring() Data declarations (also called fields) Method declarations (note: the constructor is also a method) Bank Account Example There are some improvements that can be made to the Account class The design of some methods could also be more robust, such as verifying that the amount parameter to the method is positive Some of these improvements are in the book examples Account.java, Transactions.java (simplified versions) Account.java, Transactions.java (book versions) Examples of datatypes (Classes) Another Example: RollingDice.java RollingDice.java Author: Lewis/Loftus Demonstrates the creation and use of a user-defined class. public class RollingDice Creates two Die objects and rolls them several times. public static void main (String[] args) Die die1, die2; int sum; die1 = new Die(); die2 = new Die(); die1.roll(); die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); die1.roll(); die2.setfacevalue(4); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); sum = die1.getfacevalue() + die2.getfacevalue(); System.out.println ("Sum: " + sum); Sample Run Die One: 5, Die Two: 2 Die One: 1, Die Two: 4 Sum: 5 Die One: 4, Die Two: 2 New sum: 6 sum = die1.roll() + die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); System.out.println ("New sum: " + sum); Dr. Papalaskari 7

Die.java Author: Lewis/Loftus Represents one die (singular of dice) with faces showing values between 1 and 6. public class Die private final int MAX = 6; maximum face value private int facevalue; current value showing on the die Constructor: Sets the initial face value. public Die() facevalue = 1; Rolls the die and returns the result. public int roll() facevalue = (int)(math.random() * MAX) + 1; return facevalue; Face value mutator. public void setfacevalue (int value) facevalue = value; Face value accessor. public int getfacevalue() return facevalue; Returns a string representation of this die. public String tostring() String result = Integer.toString(faceValue); return result; UML Class Diagrams UML class diagram for Transactions program? UML = Unified Modelling Language Example: A UML class diagram for the RollingDice program: RollingDice main (args : String[]) : void facevalue : int Die roll() : int setfacevalue (value : int) : void getfacevalue() : int tostring() : String Dr. Papalaskari 8

NEXT: Focus on Methods Method control flow Method definition Parameters return statement Different ways of thinking about methods Method Control Flow If the called method is in the same class, only the method is needed compute mymethod(); mymethod Method Control Flow The called method is often part of another class or object main obj.doit(); doit helpme(); helpme Thus the dot operator is an addressing mechanism. Note that it can also be used to access an object s or class s data directly, for example acct1. Color.black more on this later (encapsulation) Invoking methods within the same class An object s method may access any of the object s other methods directly. Eg: public void addinterest(double rate) deposit (rate*balance); Client code, eg: compound the interest for acct2 over 10 years int year = 1; while (year <= 10) acct2.addinterest(0.03); year ++; effect: acct2.deposit(0.03*balance) Dr. Papalaskari 9

Method Control Flow: example main addinterest deposit Method definition: Example parameters return type return statement acct2.addinterest(0.03); ; char ch = obj.calc (start, 2, "ABCDE"); char calc (int num1, int num2, String message) int sum = num1 + num2; char result = message.charat (sum); return result; More Method Examples: Write a method with two double parameters a and b that computes and returns the sum of squares of its two parameters (i.e., a 2 + b 2 ). More Method Examples: Write a method with one int parameter num, that returns a String composed of Happy Birthday num times How do we invoke the method to compute & print: (14.8) 2 + (37.65) 2? How do we invoke the method to print happy birthday 4 times? Dr. Papalaskari 10

Getting to know classes so far Using predefined classes from the Java API. Defining classes for our own datatypes. datatypes: Clients (Driver classes): Account Transactions, OnePercent Die RollingDice Shoe YouVeGotShoes (Project) Person PeopleBeingPeople (Lab) Next: visibility modifiers, graphical objects, Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods which in turn manage the instance data Client Violating Encapsulation It is possible for a class to access the instance data of another class directly Methods Data Violating Encapsulation - WRONG It is possible for a class to access the instance data of another class directly but it s not a good idea! Account.java Use Accessors & Mutators - RIGHT Indirect access through methods accessors and mutators ( getters and setters ) Usually d getx() and setx() Account.java Bad Client acct1. = Joe ; Transactions.java int x1 = acct1.; balance balance Private Data Dr. Papalaskari 11

Visibility Modifiers In Java, we enforce encapsulation through the appropriate use of visibility modifiers: public can be referenced from other classes private can be referenced only within that class: protected involves inheritance (discussed later) Data declared without a visibility modifier have default visibility and can be referenced by any class in the same package An overview of all Java modifiers is presented in Appendix E Violating Encapsulation experiment Revisit the Account example (use our simplified versions from the course website or Lab 8) Add some code to the client (Transactions or Onepercent) to modify the value of an instance variable, eg: acct1. = Bernie ; Run the program to verify that the on that account has changed. Now modify Account.java insert the modifier private in front of the instance variable declaration: private String ; Without changing anything in the client, re-compile the Account class and run your program again. Note the error you get: Error: Public Constants OK Example: The Account class can have a constant for the interest rate: public final double RATE = 0.015; A client (eg, OnePercent.java) can access this constant directly: System.out.print ("Interest rate = " + acct1.rate); static Public Constants BETTER It is better to declare constants as static public final static double RATE = 0.015; This way, a client can access the constants without creating an object, using the class : System.out.print ("Interest rate = " + Account.RATE); Dr. Papalaskari 12

Visibility Modifiers the RULES Graphical Objects Variables Methods public NO (but OK for public constants) Yes private Yes Yes, for support methods only Some objects contain information that determines how the object should be represented visually Graphical objects data about position, size, and other attributes methods to draw the object Let's look at some other examples of graphical objects: Example 1: SmilingFacePanel SmilingFace.java SmilingFacePanel.java Example 2: SplatPanel Splat.java SplatPanel.java Circle.java See also ImInUrClassMessingUrInstanceData.java SmilingFace.java Author: Lewis/Loftus Demonstrates the use of a separate panel class. import javax.swing.jframe; public class SmilingFace Creates the main frame of the program. public static void main (String[] args) JFrame frame = new JFrame ("Smiling Face"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); SmilingFacePanel panel = new SmilingFacePanel(); frame.getcontentpane().add(panel); SmilingFace.java Author: Lewis/Loftus Demonstrates the use of a separate panel class. import javax.swing.jframe; public class SmilingFace Creates the main frame of the program. public static void main (String[] args) JFrame frame = new JFrame ("Smiling Face"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); SmilingFacePanel panel = new SmilingFacePanel(); frame.getcontentpane().add(panel); frame.pack(); frame.setvisible(true); frame.pack(); frame.setvisible(true); Dr. Papalaskari 13

The SmilingFace program draws a face by defining the paintcomponent method of a panel A UML class diagram: SmilingFace main (args : String[]) : void BASEX: int BASEY: int panel BASEX 120 BASEY 60 SmilingFacePanel paintcomponent(p: Graphics): void SmilingFacePanel.java Author: Lewis/Loftus Demonstrates the use of a separate panel class. import javax.swing.jpanel; import java.awt.*; public class SmilingFacePanel extends JPanel private final int BASEX = 120, BASEY = 60; base point for head Constructor: Sets up the main characteristics of this panel. public SmilingFacePanel () setbackground (Color.blue); setpreferredsize (new Dimension(320, 200)); setfont (new Font("Arial", Font.BOLD, 16)); Draws a face. public void paintcomponent (Graphics page) super.paintcomponent (page); page.setcolor (Color.yellow); page.filloval (BASEX, BASEY, 80, 80); head page.filloval (BASEX-5, BASEY+20, 90, 40); ears page.setcolor (Color.black); page.drawoval (BASEX+20, BASEY+30, 15, 7); eyes page.drawoval (BASEX+45, BASEY+30, 15, 7); page.filloval (BASEX+25, BASEY+31, 5, 5); page.filloval (BASEX+50, BASEY+31, 5, 5); pupils Jpanel Class let s look at the Java API The SmilingFacePanel class is derived from the JPanel class using inheritance We are defining a subclass of JPanel page.drawarc (BASEX+20, BASEY+25, 15, 7, 0, 180); eyebrows page.drawarc (BASEX+45, BASEY+25, 15, 7, 0, 180); page.drawarc (BASEX+35, BASEY+40, 15, 10, 180, 180); nose page.drawarc (BASEX+20, BASEY+50, 40, 15, 180, 180); mouth page.setcolor (Color.white); page.drawstring ("Always remember that you are unique!", BASEX-105, BASEY-15); page.drawstring ("Just like everyone else.", BASEX-45, BASEY+105); SmilingFacePanel Dr. Papalaskari 14

Smiling Face Example Every Swing component has a paintcomponent method The paintcomponent method accepts a Graphics object that represents the graphics context for the panel We define the paintcomponent method to draw the face with appropriate calls to the Graphics methods Note the difference between drawing on a panel and adding other GUI components to a panel Objects with a draw() method The next example - Splat - is structured differently It draws a set of colored circles on a panel, but each circle is represented as a separate object that maintains its own graphical information The paintcomponent method of the panel "asks" each circle to draw itself See Splat.java See SplatPanel.java See Circle.java Splat.java Author: Lewis/Loftus Demonstrates the use of graphical objects. import javax.swing.*; import java.awt.*; public class Splat Presents a collection of circles. public static void main (String[] args) JFrame frame = new JFrame ("Splat"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); frame.getcontentpane().add(new SplatPanel()); Splat.java Author: Lewis/Loftus Demonstrates the use of graphical objects. import javax.swing.*; import java.awt.*; public class Splat Presents a collection of circles. public static void main (String[] args) JFrame frame = new JFrame ("Splat"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); frame.getcontentpane().add(new SplatPanel()); frame.pack(); frame.setvisible(true); frame.pack(); frame.setvisible(true); Dr. Papalaskari 15

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintcomponent(p: Graphics): void diameter: int color: Color x: int y: int Circle draw(p: Graphics): void getdiameter(): int getcolor(): Color getx(): int gety(): int setdiameter(size: int): void setcolor(shade: Color): void setx(upperx: int): void sety(uppery: int):void Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintcomponent(p: Graphics): void diameter: int color: Color x: int y: int Circle draw(p: Graphics): void getdiameter(): int getcolor(): Color getx(): int gety(): int setdiameter(size: int): void setcolor(shade: Color): void setx(upperx: int): void sety(uppery: int):void circle1 circle2 diameter 60 color x 70 y 35 circle3 circle4 CSC 1051 M.A. Papalaskari, Villanova circle5 CSC 1051 M.A. Papalaskari, Villanova diameter 50 color x 30 y 20 SplatPanel.java Author: Lewis/Loftus Demonstrates the use of graphical objects. import javax.swing.*; import java.awt.*; public class SplatPanel extends JPanel private Circle circle1, circle2, circle3, circle4, circle5; Constructor: Creates five Circle objects. public SplatPanel() circle1 = new Circle (30, Color.red, 70, 35); circle2 = new Circle (50, Color.green, 30, 20); circle3 = new Circle (100, Color.cyan, 60, 85); circle4 = new Circle (45, Color.yellow, 170, 30); circle5 = new Circle (60, Color.blue, 200, 60); Draws this panel by requesting that each circle draw itself. public void paintcomponent (Graphics page) super.paintcomponent(page); circle1.draw(page); circle2.draw(page); circle3.draw(page); circle4.draw(page); circle5.draw(page); setpreferredsize (new Dimension(300, 200)); setbackground (Color.black); Dr. Papalaskari 16

Circle.java Author: Lewis/Loftus Represents a circle with a particular position, size, and color. import java.awt.*; public class Circle private int diameter, x, y; private Color color; Constructor: Sets up this circle with the specified values. public Circle (int size, Color shade, int upperx, int uppery) diameter = size; color = shade; x = upperx; y = uppery; Draws this circle in the specified graphics context. public void draw (Graphics page) page.setcolor (color); page.filloval (x, y, diameter, diameter); Diameter mutator. public void setdiameter (int size) diameter = size; Color mutator. public void setcolor (Color shade) color = shade; X mutator. public void setx (int upperx) x = upperx; Y mutator. public void sety (int uppery) y = uppery; Diameter accessor. public int getdiameter () return diameter; Color accessor. public Color getcolor () return color; X accessor. public int getx () return x; Y accessor. public int gety () return y; Dr. Papalaskari 17