Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Introduction to Object-Oriented Programming
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Objects as a programming concept
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes and Objects
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
4 Introduction to Classes and Objects.
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Chapter 3 Introduction to Classes, Objects Methods and Strings
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2

Note Set 2 Overview ATM Case Study Introduction Use Case Diagrams What is an object? Declaring class Learn/refresh basic object terminology Instance Variables and Instance Methods – Different from 1340? Implementing the Class vs. Using the class

Car Analogy To Drive a Car, do we need to know: the details of power steering? intricacies of a internal combustion engine? how anti-lock breaks work?

What are we building? We are building a software system In a software system, and under OOP, objects interact Must determine which objects interact in the system and then create the Java classes that represent those objects Today’s First Example: Class GradeBook – shall display message to user welcoming them to grade-book app Class GradeBookTest – Will be used to test the Grade-book class

GradeBook Class Evolution Example 1 – simple method to display welcome message Example 2 – method to receive course name and use it in displaying message Example 3 – Add an instance variable (data member) to the class to store the name of the course name Example 4 – Simple Class constructor to initialize instance variables.

GradeBook - 1 Iteration 1 – contains a method displayMessage that prints a welcome message to the screen public class GradeBook { public void displayMessage () { System.out.println(“Welcome to the GradeBook App.”); } public – can be used by other classes class – what we’re declaring/creating identifier – name of the class that we are defining Must be placed in a file named ________________________.

GradeBookTest A client object that will contain a main method used to test the GradeBook class public class GradeBookTest { public static void main (String [] args) { //create a GradeBook object (with new) and assign it //reference variable gb1 GradeBook gb1 = new GradeBook(); //call the method displayMessage gb1.displayMessage (); } Must be placed in a file named ________________________.

Aside – Compiling from the Command Line What is going on “behind the scenes” when you compile and run from inside NetBeans? Assuming that you’ve created the 2 source files GradeBook.java GradeBookTest.java Compile the files with javac GradeBook.java GradeBookTest.java Run Program with java GradeBookTest starts the JVM and looks inside class GradeBookTest for a static main method That’s where execution begins in a program Can’t run GradeBook because it doesn’t contain a main method

The Class Diagram Use a class diagram to graphically represent the class/object plus sign (+) means public – can be accessed outside the class ex: that method is being accessed from GradeBookTest

GradeBook – 2 – Welcome with parameter public class GradeBook { //display welcome message including course name in msg public void displayMessage (String courseName) { System.out.printf(“Welcome to the GradeBook for %s!”, courseName); } This method accepts a parameter – a value that is sent to the method from the call. Similar to variable declaration. A method can specify that it requires multiple parameters by including each in the parameter list separated by a comma.

GradeBookTest – 2 import java.util.*; //What is this for??? public class GradeBookTest { public static void main (String [] args) { //create scanner object and store in kb so we can //read from keyboard Scanner kb = new Scanner(System.in); String nameOfCourse; GradeBook gb = new GradeBook (); //get the name of the course from the user System.out.println(“Please enter course name: “); nameOfCourse = kb.nextLine();//read a line of text System.out.println();//print blank line //pass the name of the course to dispalyMessage method gb.displayMessage(nameOfCourse); }

GradeBookTest – 2 public class GradeBookTest { public static void main (String [] args) { // --- other stuff --- //pass the name of the course to dispalyMessage method gb.displayMessage(nameOfCourse); } public class GradeBook { //display welcome message including course name in msg public void displayMessage (String courseName) { System.out.printf(“Welcome to the GradeBook for %s!”, courseName); } string stored in nameOfCourse is copied to the parameter courseName when method call is executed

New Class Diagram Indicate a parameter in this fashion Note: We’re doing this in reverse right now. Class Diagrams should be done first.

Quick Data Type Review Primitive Data Types – boolean, byte, char, short, int, long, float, double when declared, memory reserved for a piece of data of that type e.g. int mySpecialIntVariable; Reference Any other data type when declared, as in: GradeBook gb; only a memory location is reserved that can reference an actual object using new – creates an object then we store the reference somewhere GradeBook gb = new GradeBook();

Reference Variables GradeBook gb = new GradeBook(); Executed first – reserves place in memory for a GradeBook object. GradeBook Object GradeBook Object then, reference is stored in reference variable gb

GradeBook – 3 – Instance Variables Instance Variable attribute of an object that is being modeled e.g. Car object – color might be an attribute e.g. Bank Account Object – account number would be attribute declared inside the class but outside the body of any methods Book calls them a field GradeBook – courseName would be a valid attribute store it as instance variable public class GradeBook { private String courseName; //Other stuff to come } Instance variables are usually declared private. Can only be accessed from members of class GradeBook (and not GradeBookTest).

GradeBook – 3 – Instance Variables Instance Variables Always given a default value Object references are initialized to null meaning they don’t reference an object yet byte, char, short, int, long, float, double  Initialized to zero (0) boolean  initialized to false public class GradeBook { private String courseName; //Other stuff to come } Given an initial value of null because it doesn’t reference to any particular String object in memory yet.

GradeBook – 3 – Instance Variables Class provide interface to instance variables through methods 2 main categories for access to data members accessor methods – return the value stored in a field usually start with get e.g. getCourseName mutator methods – set the value of a field usually start with set e.g. setCourseName public class GradeBook { private String courseName; public void setCourseName(String newName) { //set parameter value to instance variable courseName = newName; } public String getCourseName () { return courseName; } Mutator Accessor

GradeBook – 3 – Full Version public class GradeBook { private String courseName; public void setCourseName(String newName) { //set parameter value to instance variable courseName = newName; } public String getCourseName () { return courseName; } public void displayMessage () { System.out.printf(“Welcome to %s!”, getCourseName()); } String is return type - indicates the data type of what the method is returning

GradeBook – 3 – Full Version public class GradeBook { private String courseName; public void setCourseName(String newName) { //set parameter value to instance variable courseName = newName; } public String getCourseName () { return courseName; } public void displayMessage () { System.out.printf(“Welcome to %s!”, getCourseName()); } import java.util.*; public class GradeBookTest{ public static void main (String [] args) { Scanner kb = new Scanner(System.in); String name; GradeBook gb = new GradeBook (); System.out.printf(“Name before Init: %s\n”, gb.getCourseName()); System.out.println(“Enter course name: “); name = kb.nextLine(); gb.setCourseName( name ); System.out.println(); gb.displayMessage(); }

GradeBook – 3 – Class Diagram Put Attributes/Instance variables in the middle section of the class diagram indicates method is returning a String value

checkpoint Define public? private? accessor? mutator? instantiate?

GradeBook – 4 – The Constructor When we use new to create an object, that objects constructor is called. GradeBook gb = new GradeBook (); constructor – special method of a class that is called automatically when an object of that type is created default constructor – supplied by the compiler if the class does not explicitly supply one. Constructor is usually used to set up the initial state of the object. provide initial values to data members other than null, 0 and false. automatically calls constructor

public class GradeBook { private String courseName; //a constructor for class GradeBook public GradeBook (String name) { courseName = name; } public void setCourseName(String newName) { //set parameter value to instance variable courseName = newName; } public String getCourseName () { return courseName; } public void displayMessage () { System.out.printf(“Welcome to %s!”, getCourseName()); } GradeBook – 4 Constructor has: -> Same name as class -> No return type May or may not accept a parameter

GradeBookTest – 4 public class GradeBookTest { public static void main (String [] args) { GradeBook gb1 = new GradeBook(“CSE 1341”); GradeBook gb2 = new GradeBook(“CSE 3353”); } courseName = CSE 1341 gb1 courseName = CSE 2341 gb2 Each object instance of GradeBook that is created has its own copy of courseName stored inside it. This is true even though it looks like that variable is declared only once.

GradeBookTest – 4 public class GradeBookTest { public static void main (String [] args) { GradeBook gb1 = new GradeBook(“CSE 1341”); GradeBook gb2 = new GradeBook(“CSE 3353”); System.out.printf (“Course 1 Name: %s\n”, gb1.getCourseName()); System.out.printf (“Course 2 Name: %s\n”, gb2.getCourseName()); } Course 1 Name: CSE 1341 Course 2 Name: CSE 3353 Course 1 Name: CSE 1341 Course 2 Name: CSE 3353

GradeBook – 4 – Class Diagram constructor added to class diagram notice that it is preceded by > no return type indicated

Checkpoint What is a constructor? When are constructors called? Can constructors have return types? parameters?

From Scratch Create an class that represents a bank account. Each bank account object should maintain the account balance. It should also be able to return the current balance and credit an amount to the account. Class Name: ______________________ Attributes: _______________________ Methods: _______________________ Constructor: ____________________

Class Diagram for Bank Account Object In Class Exercise: Code the Account Class