1 / 30

CSE 1341 Principles of Computer Science I

CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. 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

zihna
Download Presentation

CSE 1341 Principles of Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341Principles of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 2

  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

  3. 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?

  4. 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

  5. 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.

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

  7. GradeBookTest 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 ________________________. A client object that will contain a main method used to test the GradeBook class

  8. 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

  9. 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

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

  11. 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; GradeBookgb = 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); } }

  12. 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); } } string stored in nameOfCourseis copied to the parameter courseName when method call is executed 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); } }

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

  14. 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. intmySpecialIntVariable; • Reference • Any other data type • when declared, as in:GradeBookgb;only a memory location is reserved that can reference an actual object • using new – creates an object then we store the reference somewhere • GradeBookgb = new GradeBook();

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

  16. GradeBook – 3 – Instance Variables 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). • 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

  17. GradeBook – 3 – Instance Variables 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. • 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

  18. GradeBook – 3 – Instance Variables 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 • 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

  19. 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

  20. 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; GradeBookgb = 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(); } }

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

  22. checkpoint • Define • public? • private? • accessor? • mutator? • instantiate?

  23. GradeBook – 4 – The Constructor automatically calls constructor • When we use new to create an object, that objects constructor is called. GradeBookgb = 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.

  24. GradeBook – 4 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()); } } Constructor has: -> Same name as class -> No return type May or may not accept a parameter

  25. 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 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. gb1 courseName = CSE 2341 gb2

  26. 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

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

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

  29. 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: ____________________

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

More Related