SEEM 34601 Java – Basic Introduction, Classes and Objects.

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.
Chapter 4: Writing Classes
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
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 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
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.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
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 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.
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.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
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.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 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.
Creating Objects & String Class
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.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes
Writing Classes.
Chapter 4 Writing Classes.
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Packages & Random and Math Classes
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Outline Creating Objects The String Class The Random and Math Classes
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
What to expect this week
String Objects & its Methods
Presentation transcript:

SEEM Java – Basic Introduction, Classes and Objects

SEEM Writing Classes  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

SEEM Designing Classes and Objects  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

SEEM Classes  A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations

SEEM 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

SEEM Classes  In general, a class share some similarities with structures in C  Recall that a structure in C is a user-defined data type composed of some data fields and each field belongs to a certain data type or another structure type  A class is also composed of some data fields. In addition to it, a class is also associated with some methods

General Design of Objects and Classes SEEM In general, we should first declare an object reference Then, we allocate memory for the object SEEM AClass obj1; obj1 = new AClass();  The first line declares an object reference obj1 belonging to a class called AClass Data fields for obj1 obj1  The second line uses the new operator to allocate some memory for an object (under the class AClass) and lets obj1 points to it  The second line also invokes the AClass constructor, if exists, to initialize the data in the object

SEEM  Similar to structures in C, one can access the data fields of an object via the dot operator  For example, suppose in the class AClass, there are some data fields declared such as field1 and field2.  Then, we can access field1 in obj1 via: General Design of Objects and Classes obj1.field1 = 100;  We can process an object by a method specified in the corresponding class via dot operator  Suppose in the class AClass, there are some methods declared such as method1 and method2  Then we can process the object obj1 by method1 via: a_value = obj1.method1();

SEEM Creating Objects – Another Example  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference variable String title;  An object reference variable holds the address of an object  The object itself must be created separately  Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object

SEEM SEEM Creating Objects - Instantiation  Creating an object is called instantiation  An object is an instance of a particular class

SEEM SEEM Invoking Methods  We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length()  A method may return a value, which can be used in an assignment or expression  A method invocation can be thought of as asking an object to perform a service

SEEM Object References  Note that a primitive variable contains the value itself, but an object variable contains the address of the object  An object reference can be thought of as a pointer to the location of the object  Rather than dealing with arbitrary addresses, we often depict a reference graphically "Steve Jobs" title1 num1 38

SEEM SEEM  Return to the example of our Die class 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  We’ll want to design the Die class with other data and methods to make it a versatile and reusable resource A Complete Example of Classes and Objects

SEEM SEEM  Download the java source files with command  Open the terminal, and input the command below  “ wget torial/lab/RollingDice.java ” torial/lab/RollingDice.java  “wget ab/Die.java” ab/Die.java A Complete Example of Classes and Objects

SEEM SEEM //************************************************************* // RollingDice.java // // 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);

SEEM SEEM die1.roll(); die2.setFaceValue(4); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); sum = die1.getFaceValue() + die2.getFaceValue(); System.out.println ("Sum: " + sum); sum = die1.roll() + die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); System.out.println ("New sum: " + sum); }

SEEM SEEM //************************************************************* // Die.java // // 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; }

SEEM SEEM // // 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; }

SEEM SEEM // // Returns a string representation of this die. // public String toString() { String result = Integer.toString(faceValue); return result; }

SEEM SEEM 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

SEEM SEEM 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

SEEM SEEM 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

SEEM SEEM 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

SEEM SEEM 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

SEEM SEEM Constructors  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 and it has no return data type  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

SEEM SEEM RollingDice.java - Compilation  Assume that both RollingDice.java and Die.java are stored under the same directory in an Unix account  To compile under Unix platform, we can simply compile the RollingDice.java: cuse93> javac RollingDice.java  The compiler will first compile RollingDice.java. When it finds out that it needs to make use of the class Die.java. It will automatically compile Die.java  If the compilation is successful, you can find two bytecodes, namely, RollingDice.class and Die.class  If there is compilation error, the error message will be displayed on the terminal.  If you wish to make the error message be displayed screen by screen, you can compile in the following way: cuse93> javac RollingDice.java |& more

SEEM SEEM RollingDice.java - Sample Execution  The following is a sample execution of RollingDice.class cuse93> java RollingDice Die One: 6, Die Two: 1 Die One: 4, Die Two: 4 Sum: 8 Die One: 3, Die Two: 2 New sum: 5