CST252 Network Software Design - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

CST252 Network Software Design

Description:

CST252. Network Software Design. Lecture 3: Objects and Classes. First Assignment ... such as getYear() have been deprecated (see documentation) in favour of Calendar ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 42
Provided by: Davi825
Category:

less

Transcript and Presenter's Notes

Title: CST252 Network Software Design


1
CST252 Network Software Design
  • Lecture 3 Objects and Classes

2
First Assignment
  • Now available on the website
  • Simple Videostore program
  • Following a fairly explicit recipe
  • Due 11th March 2002
  • May need code from website as examples
  • EmployeeTest.java as basis
  • GregorianCalendar.add(..,..)
  • Searching for a String in an array of Strings

3
Resources
  • Java Tutorial (java.sun.com)
  • Learning the Java Language/OO prog concepts
  • Course Website
  • users.wmin.ac.uk/lancasd/CCTM91
  • Book
  • First chapters of any Java book
  • or C. Horstmann and G. Cornell. Core Java
  • Volume 1 chapter 4

4
Lecture 2 Review
  • Fundamental Programming Structures
  • Control Flow
  • Reading input - two choices
  • InputTest - using popup window
  • ConsoleInputTest - using console
  • Arrays
  • Strings (documentation)
  • Elementary Objects and Classes

5
If/Else conditional
  • if (YourSales gt Target)
  • performance OK
  • bonus 100
  • else
  • performance bad
  • bonus 0

NO
YES
Sales gt target
performanceOK
performancebad
bonus 100
bonus 0
6
Elementary Classes
  • Modified HelloWorld classes
  • Test class to create and invoke methods
  • Class methods and how to call them
  • Instance fields, private
  • Today in more detail

7
Quiz
  • What (if anything) is wrong with the following
    lines?
  • A) String inputstr ...//get input from user
  • int age integer.parseInt(inputstr)
  • B) long a new long256
  • boolean b new boolean10
  • for(int i0 ilta.length i)
  • ai ii
  • bi false

8
Lecture 3 Overview
  • About Objects and Classes
  • O-O style of programming
  • Using standard classes
  • Date and Calendar classes
  • Creating your own classes
  • Employee class

9
Object Oriented
  • For modern, large non-scientific programs, helps
    organisation and management
  • Classes are software Packages
  • Data and methods together
  • Clear and precise interface to use the class
  • Objects
  • Instances of a class
  • More than one instance possible

10
Classes - Examples
  • Represent a single concept
  • Point, Rectangle, Ellipse
  • Data location (x,y), size
  • Methods translate, rescale
  • BankAccount
  • Data account balance
  • Methods credit, debit, getbalance
  • Employee
  • Data name, salary, phonenumber, idnumber
  • Methods raisesalary, setphonenumber
  • classes - nouns
  • methods - verbs

11
Objects
  • An object is a particular example of a class
  • An ellipse of a given size at a particular
    position
  • A bankaccount with a given balance
  • A specific employee
  • There can be many objects from the same class
    differing in their internal data

12
Terminology
  • Encapsulation (data hiding)
  • combining data and behaviour (methods) in a
    package, and hiding the implementation
  • The data in an object are known as instance
    fields
  • The functions and procedures that operate on the
    data are called methods

13
Terminology cont.
  • A specific object that is an instance of a class
    has a particular set of instance fields that
    together define its state
  • Classes can be built up from other classes by
    extending them - this process is known as
    inheritance and is the subject of L4

14
Using Existing Classes
  • Java comes with large class libraries
  • Have already used
  • System.out
  • Math
  • These are special - you can use their methods
    without creating an object of the class (the
    methods are static)
  • HelloWorld and other Test classes that contain
    the static method main() are similarly special

15
Date class
  • Date is a typical class from the Java library
  • To create an instance of the class use a
    constructor
  • This is a special method in the class with the
    same name as the class
  • Use the new operator with the constructor
  • new Date()
  • This initialises the new object to the current
    date and time - so now can call a method

16
A Date program
  • import java.util.
  • public class DateTest
  • public static void main(String args)
  • Date now new Date()
  • String s now.toString()
  • System.out.println(s)

17
Syntax
  • Use new to create an object of the class
  • Call methods on that object using a dot
  • object.method(parameters)
  • The method toString() is a common method that is
    often found in Java library classes, and you
    should also implement it in your own classes

18
Object Variable (reference)
  • Date birthday new Date()

Date
birthday
  • The object variable refers to the newly created
    object
  • Different from variables for primitive types

19
Object Declaration
  • Date deadline //declaration
  • deadline new Date() //OK
  • deadline birthday //both refer to same object

Date
birthday
deadline
If deadline is now changed, birthday will also
change. This is not the same as happens for
primitive data types.
20
Date Class
  • The Date class is actually more like a time class
    - it contains the number of milliseconds since
    Jan 1st 1970.
  • This is not very useful for manipulating dates in
    terms of years, months, and days - but is good
    for comparing dates (before/after)
  • In order to allow internationalisation, methods
    of Date such as getYear() have been deprecated
    (see documentation) in favour of Calendar classes

21
GregorianCalendar Class
  • The GregorianCalendar class expresses dates in
    familiar calendar notation
  • new GregorianCalendar()
  • new GregorianCalendar(1999,11,31)
  • new GregorianCalendar (1999,Calendar.DECEMBER,31)
  • An add() method to change the date
  • Calendar deadline new GregorianCalendar()
  • deadline.add(Calendar.DAY_OF_MONTH, 3)

22
Relate Dates/Calendars
  • Conversion from Calendar to Date class
  • GregorianCalendar hirecal new
    GregorianCalendar( year, month, day)
  • Date hireday hirecal.getTime()
  • From Date to Calendar
  • Calendar cal new GregorianCalendar()
  • Date now new Date()
  • cal.setTime(now)

23
A Date/Calendar program
  • import java.util.
  • public class DCTest
  • public static void main(String args)
  • Calendar due new GregorianCalendar()
  • Date now due.getTime()
  • System.out.println(now.toString())
  • due.add(Calendar.DAY_OF_MONTH, 20)
  • Date duedate due.getTime()
  • System.out.println(duedate.toString())

24
Dates/Calendar Example Code
  • Examples used in this lecture
  • DateTest.java
  • DCTest.java
  • To demonstrate DateFormat (java.text)
  • DateFormatTest.java
  • Larger code prints out the calendar for this
    month
  • CalendarTest.java

25
Building Classes Yourself
  • So far HelloWorld type classes which just contain
    a single main() method and never need to be
    instantiated
  • Usually need several classes, one of which is
    like a class tester containing a main method
  • Last week modified HelloWorld class, today
    something more useful

26
Employee Class
  • Simple class representing employees, with
    instance fields for
  • name
  • salary
  • start date of employment
  • Methods for raising salary, and some get/set
    methods to access the private data
  • Another EmployeeTest class to test it

27
Files
  • Two files, one for each class
  • Can compile each separately, or just the test
    class (javac will compile the other classes
    referred to there)
  • Or one file called by the same name as the public
    class (EmployeeTest.java). The other class is not
    labelled public
  • In either case, two .class files are generated

28
Public/Private Keywords
  • One constructor and 4 methods, all public so any
    method in any class can call them
  • 3 instance fields, all private so the only
    methods that can access them are methods of the
    employee class itself
  • This is encapsulation

29
Constructor
  • Initialises the instance fields
  • Same name as that of class
  • No return value
  • Class can have more than one constructor
  • May take several parameters
  • Only ever use with the new operator
  • new Employee(James Bond,100000,1950,1,1)
  • Cannot use to reset the instance fields

30
Methods
  • Can access private instance fields of their class
    - eg raisesalary()
  • Get methods
  • getName()
  • getSalary()
  • getHireDay()
  • Sometimes make methods private to help break up
    the work of the public methods

31
Naming Convention
  • By convention (followed in all the code for the
    Java libraries)
  • Class names begin with a capital letter
  • Method and instance field names begin with a
    small letter (though they may have capitals later
    in the name)
  • Follow this in your own code
  • An example of quality of code for grading the
    assignment

32
Get/Set methods vs public Instance fields
  • If the instance fields are public (break
    encapsulation) then they can be accessed
    directly e.name (no brackets)
  • This is generally a bad thing, it is better to
    use get/set (mutator/accessor) methods for the
    fields you want to be able to access
  • This allows a change of internal rep and error
    checking without affecting other classes

33
Static Keyword
  • Static Fields
  • Are the same for each object of the class
  • Available without instantiating the class
    (Math.PI)
  • Static Methods
  • Do not operate on objects
  • No need to instantiate the class
    main(),Math.exp(x)
  • Cannot access other non-static methods or
    instance fields
  • If you find yourself using lots of static
    keywords - you are doing something wrong

34
Parameters to Methods
  • Call by reference
  • pass an address, so the method can modify whats
    there
  • Call by value
  • just pass a copy of the value, the original isnt
    changed

35
Java is always call by value
  • Straightforward for primitive types
  • double percent 10
  • harry.raiseSalary(percent)
  • For object references
  • The object reference is copied by value
  • So can modify the object (eg instance fields)
  • But cannot modify the original reference (eg swap)

36
More About Constructors
  • Often there are several constructors depending on
    what you want to initialise
  • new GregorianCalendar()
  • new GregorianCalendar(1999,11,31)
  • Default constructor without parameters does not
    need to be specified (but better if it is)

37
Packages
  • To organise files for large projects
  • Using packages
  • import java.util.
  • import java.sql.
  • import java.util.Date

38
Making Packages Yourself
  • Before all the code for classes
  • package uk.ac.wmin.lancasd.test
  • The files for this package go into a directory
    corresponding to this path
  • Classpath, and how the VM locates classes

39
Design of Classes
  • For large projects this is important
  • Also important to be able to rapidly explain the
    overall features of designs to other people
    without describing code details
  • Use pictures
  • UML is a widely accepted standard for such
    pictures showing the relationship between classes
    - not covered in this class

40
Class Design Hints
  • Keep data private
  • Initialise data
  • Not all fields need get/set methods
  • Break up large classes with too many
    responsibilities

41
Summary - OO vs structured
  • C programming
  • structured programming
  • common data and many functions
  • Java programing
  • Classes that encapsulate data and methods
  • Objects instantiating the classes
  • Reusability, safety, organisational/management
    advantages
Write a Comment
User Comments (0)
About PowerShow.com