Designing Classes - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Designing Classes

Description:

Design a class or a blueprint for each of the object in a system ... String is a special class that we do not need to use the key word, 'new', to create an object ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 15
Provided by: IIT89
Category:

less

Transcript and Presenter's Notes

Title: Designing Classes


1
Designing Classes
  • Chapter 2, Part II

2
Java Classes
  • Design a class or a blueprint for each of the
    object in a system
  • Example 1 Customer in the DirectClothing Inc.
  • Example 2 Student, Faculty, and Course in
    a university scheduling system
  • Class is described by a noun.
  • Class name begins with a capital letter, and
    capitalize each subsequent word if the name is
    made up of more than one word.
  • A class has attributes that characterize the
    object
  • Example customer has first name, last name,
    customer ID, status, and total purchase.
  • An attribute name starts with a lower case, and
    capitalize each subsequent word if the name is
    made up of more than one word.
  • A class can have operations called methods
  • Example customer can have methods such as add a
    new customer, update the customer status, and
    calculate the total purchases
  • The name of a method begins with a lower case,
    and capitalize each subsequent word if the name
    is made up of more than one word.

3
Modeling a Class
  • In HTM 484D, we will use the Unified Modeling
    Language (UML) to model an object
  • The first box shows the name of the object, which
    always starts with a capital letter
  • The second box shows the attributes of the class.
  • The third box shows the method(s) of the class
  • The sign means an attribute is public
    (accessible to any objects)

4
Modeling a Class
  • After the colon (), indicates the data type
  • customerID is an integer
  • status is a character
  • totalPurchases is a double floating point
  • The key word , void, means the displayCustomerInfo
    () method does not return any information to the
    calling object

5
Modeling classes in Java
  • Customer.java
  • // Designing the first Java class named Customer
  • public class Customer
  • public int customerID
  • public char status
  • public double totalPurchases
  • // This method displays the values for an
    customer
  • public void displayCustomerInfo()
  • System.out.println("Customer ID "
    customerID)
  • System.out.println("Status "
    status)
  • System.out.println("Purchases "
    totalPurchases)
  • // end of display method
  • // end of class

6
Attribute Variables
  • Java uses variables for attributes
  • Variables defined outside a method are called
    member, class member, or instance variables
  • The syntax is
  • modifier type identifier value
  • where means optional
  • For example
  • public int customerID
  • public char status
  • public int customerID 0 // This initializes
    the CustomerID to a value of 0
  • public int I, j, k // declare several variables
    at once
  • Modifier can be public, private, and protected
    (we will use only public for the time being)

7
Basic Attribute Types (I)
  • Integer
  • byte (8 bits), short (16 bits) int (32 bits), and
    long (64 bits)
  • In HTM 484D, we will mostly use the int integer
    type
  • public int customerID
  • public int customerID 1 // initializing during
    decalaration
  • customerID 1 // assign 1 to customerID inside
    a method
  • Floating point
  • float (32 bits) and double (64 bits)
  • For example
  • public float short_non_integer 34.67f
  • public double long_non_integer 34.67d
  • public double long_non_integer 34.67 // you
    can ignore the d when initializing a dlouble
    floating point variable
  • public double totalPurchases
  • totalPurchases 100.0 // assign 100.0 inside a
    method

8
Basic Attribute Types (II)
  • Character type
  • char (16 bits)
  • Example public char status U
  • status N // Assign N to
    status inside a method
  • public char ch a
  • Java use unicode to represent characters
  • To assign a value to a char variable, the
    character must be enclosed in single quotes
  • Boolean (true or false)
  • boolean (I bit)
  • Example public boolean end true
  • public boolean redLight false
  • redLight true // set redLight
    to true
  • A boolean variable can have a value of either
    true or false (true and false are key words)

9
Basic Attribute Types (II)
  • Sequence of characters
  • String
  • Example public String firstName Jack
  • public String lastName Leu
  • Note String must starts with an upper case
    because it is a class
  • String is a special class that we do not need to
    use the key word, new, to create an object
  • String can be combined by using the operation
  • Example System.out.println(Customer ID
    customerID)
  • This will print Customer ID followed by a number
    on your screen

10
Testing the Customer Class
  • CustomerTest.java
  • // Test Customer.class
  • public class CustomerTest
  • // Java execution starts here
  • public static void main (String args)
  • Customer myCustomer new Customer()
  • myCustomer.customerID 1 //assign 1 to
    customerID
  • myCustomer.status N // for a
    new customer
  • myCustomer.totalPurchases 100.0
  • myCustomer.displayCustomerInfo() //
    call the method

11
Lab 1
  • Create and compile Customer.java
  • Create and compile CustomerTest.java
  • Note if you compile CustomerTest.java first, the
    Java compiler will compile Customer.java and
    CustomerTest.Java at the same time
  • Run CustomerTest.class

12
Initializations with a Constructor
  • Constructors are method-like structures that are
    invoked automatically when you instantiate an
    object.
  • Constructors are usually used to initialize
    values in an object.
  • Syntax of a constructor
  • modifiers class ClassName
  • modifier ConstructorName(arguments)
  • code_block

13
Classes with Constructors
  • Customer.java
  • // Designing the first Java class named Customer
  • public class Customer
  • public int customerID
  • public char status
  • public double totalPurchases
  • // The following is a constructor
  • public Customer ()
  • status U
  • totalPurchases 0.0
  • // This method displays the values for an
    customer
  • public void displayCustomerInfo()
  • // the statements for the method are
    the same as before

14
Lab 2
  • Modify Customer.java to include a constructor
  • Compile modified Customer.java
  • Modify CustomerTest.java to include the
    following
  • Customer firstCustomer new Customer()
  • Customer secondCustomer new Customer()
  • firstCustomer.displayCustomerInfo()
  • secondCustomer.customerID 1 //assign 1 to
    customerID
  • secondCustomer.status N // for a new
    customer
  • secondCustomer.totalPurchases 100.0
  • secondCustomer.displayCustomerInfo()
  • Compile revised CustomerTest.java
  • Run CustomerTest
Write a Comment
User Comments (0)
About PowerShow.com