cse 1341 principles of computer science i
DESCRIPTION
CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. 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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/2.jpg)
Note Set 2 OverviewATM Case Study IntroductionUse Case DiagramsWhat is an object?Declaring classLearn/refresh basic object terminologyInstance Variables and Instance Methods – Different from
1340?Implementing the Class vs. Using the class
![Page 3: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/3.jpg)
Car AnalogyTo Drive a Car, do we need to know:
the details of power steering?intricacies of a internal combustion engine?how anti-lock breaks work?
![Page 4: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/4.jpg)
What are we building?We are building a software systemIn a software system, and under OOP, objects interactMust 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 appClass GradeBookTest – Will be used to test the Grade-book
class
![Page 5: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/5.jpg)
GradeBook Class EvolutionExample 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.
![Page 6: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/6.jpg)
GradeBook - 1Iteration 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 byother classes
class – what we’redeclaring/creating
identifier – name of the classthat we are defining
Must be placed in a file named ________________________.
![Page 7: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/7.jpg)
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 ________________________.
![Page 8: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/8.jpg)
Aside – Compiling from the Command LineWhat is going on “behind the scenes” when you compile and run
from inside NetBeans?Assuming that you’ve created the 2 source files
GradeBook.javaGradeBookTest.java
Compile the files with javac GradeBook.java GradeBookTest.java
Run Program with java GradeBookTeststarts 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
![Page 9: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/9.jpg)
The Class DiagramUse a class diagram to graphically represent the
class/object
plus sign (+) means public – can be accessed outside the classex: that method is being accessed from GradeBookTest
![Page 10: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/10.jpg)
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 multipleparameters by including each in the parameter listseparated by a comma.
![Page 11: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/11.jpg)
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); }}
![Page 12: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/12.jpg)
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
![Page 13: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/13.jpg)
New Class Diagram
Indicate a parameter in this fashion
Note: We’re doing this in reverse right now. Class Diagrams should be done first.
![Page 14: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/14.jpg)
Quick Data Type ReviewPrimitive Data Types –
boolean, byte, char, short, int, long, float, doublewhen declared, memory reserved for a piece of data of that
typee.g. int mySpecialIntVariable;
ReferenceAny other data typewhen 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 somewhereGradeBook gb = new GradeBook();
![Page 15: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/15.jpg)
Reference Variables
GradeBook gb = new GradeBook();
Executed first – reserves place in memory for a GradeBook object.
GradeBookObject
then, reference is stored in reference variable
gb
![Page 16: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/16.jpg)
GradeBook – 3 – Instance VariablesInstance Variable
attribute of an object that is being modelede.g. Car object – color might be an attributee.g. Bank Account Object – account number would be attribute
declared inside the class but outside the body of any methodsBook 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).
![Page 17: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/17.jpg)
GradeBook – 3 – Instance VariablesInstance Variables
Always given a default valueObject references are initialized to null meaning they don’t
reference an object yetbyte, 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 becauseit doesn’t reference to any particular String object in memory yet.
![Page 18: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/18.jpg)
GradeBook – 3 – Instance VariablesClass provide interface to instance variables through
methods2 main categories for access to data members
accessor methods – return the value stored in a fieldusually start with get e.g. getCourseName
mutator methods – set the value of a fieldusually 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
![Page 19: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/19.jpg)
GradeBook – 3 – Full Versionpublic 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 typeof what the methodis returning
![Page 20: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/20.jpg)
GradeBook – 3 – Full Versionpublic 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(); }
}
![Page 21: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/21.jpg)
GradeBook – 3 – Class Diagram
Put Attributes/Instance variables in the middle section of the class diagram
indicates method is returning a String value
![Page 22: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/22.jpg)
checkpointDefine
public?
private?
accessor?
mutator?
instantiate?
![Page 23: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/23.jpg)
GradeBook – 4 – The ConstructorWhen 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
![Page 24: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/24.jpg)
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
![Page 25: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/25.jpg)
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 1341gb1
courseName = CSE 2341gb2
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.
![Page 26: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/26.jpg)
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
![Page 27: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/27.jpg)
GradeBook – 4 – Class Diagram
constructor added to class diagram
notice that it is preceded by <<constructor>>
no return type indicated
![Page 28: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/28.jpg)
CheckpointWhat is a constructor?
When are constructors called?
Can constructors have return types? parameters?
![Page 29: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/29.jpg)
From ScratchCreate 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: ____________________
![Page 30: CSE 1341 Principles of Computer Science I](https://reader035.vdocuments.net/reader035/viewer/2022062501/56815ff3550346895dcef183/html5/thumbnails/30.jpg)
Class Diagram for Bank Account Object
In Class Exercise: Code the Account Class