Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 4: Writing Classes
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes The programs we’ve written in previous examples have.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4 Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved2/48 Writing Classes We've been using predefined classes. Now we will learn.
Writing Classes (Chapter 4)
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
CSE 1201 Object Oriented Programming Writing Classes.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
SEEM Java – Basic Introduction, Classes and Objects.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
ELC 312 Day 4. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Agenda Questions? Problem set one due Problem set two Posted in WebCT  Due Sept.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Anatomy of a Class & Method
Chapter 4: Writing Classes
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Chapter 5 – Writing Classes
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Chapter 4 Writing Classes.
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes
Writing Classes.
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
What to expect this week
Presentation transcript:

Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005

8-2 Quiz 3 – Problem 1 String s1 = new String(“\tWorld!\n”); String s2 = “Hello”; s3 = s1; System.out.println(“s3: ” + s3); s1 = s2 + s1; System.out.println(“s3: ” + s3); System.out.println(“Length of s1: ” + s1.length()); System.out.println(“Substring: ” + s1.substring(3, 6)); String s3 s3:World! S3:World! Length of s1: 13 Substring: lo

8-3 Quiz 3 – Problem 2 & Bonus 1 import java.util.Random; public class RandomGenerator { public static void main(String[] args) { Random generator = new Random(); int num1 = generator.nextInt(5) + 1; //1, 2, 3, 4, 5 int num2 = 2 * generator.nextInt(9) – 4; // -4, -2, …, 10, 12 int num3 = 3 * generator.nextInt(13) – 5; // -5, -2, …, 31 }

8-4 Quiz 3 – Bonus 2 String s = 5+3*2+"Quiz 3"+5+3/5; System.out.println(s.substring(1,4) + s.substring(8, 10)); //1Qu50

8-5 Quiz 4 – Problem 1 import java.text.DecimalFormat; import java.text.NumberFormat; public class FormatDemo { public static void main(String []args) { //create DecimalFormat object fmt = new DecimalFormat ("0.###"); // DecimalFormat fmt System.out.println(fmt.format(5.9986)); // System.out.println(fmt.format(3.8)); // 3.8 percentFmt = NumberFormat.getPercentInstance(); // NumberFormat percentFmt System.out.print(percentFmt.format(0.06)); }

8-6 Quiz 4 – Problem 2 public class EnumDemo{ enum LetterGrade {A, B, C, D, F} public static void main(String[] args) { LetterGrade stdOne; LetterGrade stdTwo; stdOne = LetterGrade.A; stdTwo = LetterGrade.B; //System.out.println(stdOne); //System.out.println(stdOne.ordinal()); //System.out.println(stdOne.name()); }

8-7 Quiz 4 – Bonus String s = “111” + 5 * 2; int val = Integer.parseInt(s); System.out.println(val); //

8-8 Chapter 4 We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: –class definitions –instance data –encapsulation and Java modifiers –method declaration and parameter passing –constructors –graphical objects –events and listeners –buttons and text fields

8-9 Outline Anatomy of a Class Encapsulation Anatomy of a Method

8-10 Writing Classes The programs we’ve written in previous examples have used classes defined in the Java standard class library Now we will begin to design programs that rely on classes that we write ourselves The class that contains the main method is just the starting point of a program True object-oriented programming is based on defining classes that represent objects with well- defined characteristics and functionality

8-11 Classes and Objects Recall from our overview of objects in Chapter 1 that an object has state and behavior Consider a six-sided die (singular of dice) –It’s state can be defined as which face is showing –It’s primary behavior is that it can be rolled We can represent a die in software by designing a class called Die that models this state and behavior –The class serves as the blueprint for a die object We can then instantiate as many die objects as we need for any particular program

8-12 Classes A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations

8-13 Classes The values of the data define the state of an object created from the class The functionality of the methods define the behaviors of the object For our Die class, we might declare an integer that represents the current value showing on the face One of the methods would “roll” the die by setting that value to a random number between one and six

8-14 Classes We’ll want to design the Die class with other data and methods to make it a versatile and reusable resource Any given program will not necessarily use all aspects of a given class See RollingDice.java (page 157)RollingDice.java See Die.java (page 158)Die.java

8-15 The Die Class The Die class contains two data values –a constant MAX that represents the maximum face value –an integer faceValue that represents the current face value The roll method uses the random method of the Math class to determine a new face value There are also methods to explicitly set and retrieve the current face value at any time

8-16 The toString Method All classes that represent objects should define a toString method The toString method returns a character string that represents the object in some way It is called automatically when an object is concatenated to a string or when it is passed to the println method

8-17 Constructors As mentioned previously, a constructor is a special method that is used to set up an object when it is initially created A constructor has the same name as the class The Die constructor is used to set the initial face value of each new die object to one We examine constructors in more detail later in this chapter

8-18 Data Scope The scope of data is the area in a program in which that data can be referenced (used) Data declared at the class level can be referenced by all methods in that class Data declared within a method can be used only in that method Data declared within a method is called local data In the Die class, the variable result is declared inside the toString method -- it is local to that method and cannot be referenced anywhere else

8-19 Instance Data The faceValue variable in the Die class is called instance data because each instance (object) that is created has its own version of it A class declares the type of the data, but it does not reserve any memory space for it Every time a Die object is created, a new faceValue variable is created as well The objects of a class share the method definitions, but each object has its own data space That's the only way two objects can have different states

8-20 Instance Data We can depict the two Die objects from the RollingDice program as follows: die1 5 faceValue die2 2 faceValue Each object maintains its own faceValue variable, and thus its own state

8-21 UML Diagrams UML stands for the Unified Modeling Language UML diagrams show relationships among classes and objects A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods) Lines between classes represent associations A dotted arrow shows that one class uses the other (calls its methods)

8-22 UML Class Diagrams A UML class diagram for the RollingDice program: RollingDice main (args : String[]) : void Die faceValue : int roll() : int setFaceValue (int value) : void getFaceValue() : int toString() : String