Special Topics: Java - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

Special Topics: Java

Description:

The artist in this case is represented with their first ... We will talk about the details as we pick up more * stuff in the class. import java.util. ... – PowerPoint PPT presentation

Number of Views:1345
Avg rating:3.0/5.0
Slides: 64
Provided by: geraldh6
Category:
Tags: java | special | topics

less

Transcript and Presenter's Notes

Title: Special Topics: Java


1
Special TopicsJava
  • Introduction

2
Goal
  • My goal is to familiarize you with the Java
    platform and some of its capabilities.
  • The successful student will be able use the
    standard features and at least a portion of the
    Enterprise Edition features
  • We will also use the course as a vehicle to talk
    about Object Oriented Programming as well as
    Extreme Programming as well.

3
The Course
  • Assumes familiarity with C/C or a similar high
    level programming language
  • Will move quickly and not dwell too much on
    syntax
  • Will have different requirements for Graduates
    and Undergraduates
  • Will involve both individual and paired projects.

4
The Texts
  • Head First Java
  • Not your usual text
  • Does a good job of presenting concepts
  • Somewhat interesting to read
  • Main shortcoming is that it isnt a reference
    book (Will use the net as a reference)
  • Java 2 Enterprise Edition in a Nutshell
  • A quick reference on Enterprise Edition topics
  • Will get into that as we go through the course

5
  • Grading
  • Midterm (20)
  • Projects (50)
  • Paper (5)
  • Final (25)

6
Mode of This Class
  • This is a Distance Learning Class. That means
    that it is being presented through a computer
    connection to a different site.
  • Use the mikes when asking questions so the
    other site can hear you!
  • The overheads will be on the web.
  • Will not always originate from Toledo I expect
    to hold some of the classes at the other site.
    Not quite sure how and when this will occur yet.

7
Graduate Students
  • Graduate Students will have separate, more
    challenging projects (except for the first
    project).

8
Web Sites
  • We have 3 places that we can go for web sites
  • http//mars.eecs.utoledo.edu/java
  • http//chee1.ni.utoledo.edu/sip
  • http//www.dl.utoledo.edu/login.htm

9
Other Resources
  • http//java.sun.com/ -- Sun developers connection
  • http//www.safari.com/ -- online bookshelf
  • http//www.eclipse.org/ -- the eclipse
    development environment

10
Online Contact
  • There is a Chat Room set up under DL
  • I will have some set times Im available online
    especially for distance learning students.
  • There will be a course bulletin board available
    as well.

11
The Compilation Process
Type in Source(.java)
Compile javac filename.java
Compiler
Byte Codes(.class)
Run java filename
JVM
CPU
12
Java Versions
13
A First Program
  • public class FirstProgram public static void
    main (String arguments) System.out.println(Th
    is is a test) System.out.println(It is only
    a test)
  • System.exit(0)
  • Points
  • Public means something is accessible by
    everybody. If the class itself was NOT public it
    wouldnt be accessible outside of the file.
  • When you run a java application the class that
    the program starts in must have a main method.
  • System.out is about the same as cout or stdout.

14
Statements
  • Assignments
  • Follow normal rules for precedence
  • () Parenthesis (grouping)
  • , /, Multiplication, division, remainder
  • , - Addition and Subtraction

15
More Statements
  • Loops
  • while ( condition )
  • do . while ( condition)

16
More Loops
  • for (init condition incr) .

17
Conditions
  • Conditions are Identical to C/C
  • lt Less Than
  • gt Greater Than
  • lt Less Than or Equal to
  • gt Greater Than or Equal to! Not equal
  • Equal
  • And
  • Or
  • ! Not

18
Conditional Statements
  • If-Else
  • if (condition)
  • true part
  • else false part
  • Switch
  • switch ( expression) case value1
  • breakcase value2
  • break
  • default
  • break

19
Declarations and Types
  • Declarations
  • type variable1, variable2,
  • Variable Naming Rules
  • Must begin with a letter, , or underscore
  • Must consist only of Letters, , underscores,
    and digits
  • Cannot be a reserved word.

20
Primitive Types
  • All Primitive Types start with a lower case
    letter
  • boolean true or false
  • char can hold a unicode character
  • byte -128..127
  • short -32768..32767
  • int -2147483468..2147483467
  • long 64 bits (you do the math!)
  • float 32 bits
  • double 64 bits
  • Remember Be Careful! Bears Shouldnt Ingest
    Large Fluffy Dogs

21
Objects
  • What I dont want you to do is write programs
    exactly like you have done in C and (sometimes)
    in C.
  • Java was designed to be based on objects
  • This should encourage you to look at your designs
    in a different way

22
Simplistic View
  • Define the important pieces
  • Decide on the objects in your system (tend to be
    nouns)
  • What does each object know? (instance variables
    or attributes)
  • What does each object do? (methods)

23
Class ! Object
  • Classes arent objects!
  • Class is a blueprint or description of an object.
  • The object is the actual instance. It is
    constructed using the Class as the guide.
  • Head First Java builds a dog class and we will
    build one too. Ours will differ slightly.

24
Define The Class Using UML
  • UML Unified Modeling Language
  • Basics Class Diagrams
  • Relationships are indicated with connectors
  • Unidirectional association
  • Bidirectional association
  • Dependency
  • Generalization (inheritance)

Class Name
Instance Variables
Methods
25
Building Our Class From Our UML Diagram
  • public class Dog
  • string breed, nameint weight, height
  • void bark () System.out.println( Woof!
    Woof!)
  • void scratch()
  • void beg()
  • void rollover()
  • void sit()

26
Still Need a Main Program
  • Main program can be in a separate class in this
    case we will do it that way.
  • Remember file name should match class name!

27
The testing class
  • public class TestDog
  • public static void main(String arguments)
    Dog fido new Dog()
  • fido.name Fido
  • fido.breed Mixed
  • fido.height 45 fido.weight 170
  • fido.bark()
  • fido.rollover()
  • System.exit(0)

28
Objects are Manipulated via References
  • Dog fido new Dog()
  • Dog bingo
  • Fido is a reference to the object -- not the
    actual object!
  • bingo fido // assigns the reference
    does // NOT create a new object!

29
Arrays are Objects Too!
  • We havent mentioned arrays but they tend to be
    objects as well.
  • Declaration
  • int values// Creates a reference to an array
  • values new int 20 // Creates the array and
    // connects it with values.
  • values0 123 // Using the array...
  • values1 -25

30
Garbage Collection
  • Rather than making the programmer delete objects
    once they are no longer in use Java does it for
    you.
  • The process is known as garbage collection.

31
How does it know what to collect?
  • Objects have a reference count.
  • The reference count determines how many
    references to the object exist.
  • If there are no references to an object it is
    marked for garbage collection since there is no
    way anybody can ever use it again.

32
Programming Environments
  • The JDK Java Developers Kit from Sun.
  • Free
  • Command Line based
  • Need your own editor
  • Eclipse
  • Open Software IDE
  • Not Just Java (but originally so it still does a
    good job)
  • Extendable Framework
  • Sun Workshop
  • IDE
  • NetBeans Based

33
Projects for This Class
  • Will be tested on Multiple Platforms
  • Will be implemented with documentation
  • UML diagrams
  • JavaDoc Comments
  • Will encourage you to use a code control system.

34
Sample Program Hit List
35
The Artist Class
  • /
  • _at_author Jerry Heuring
  • Created Dec 31, 2003
  • _at_version 0.0
  • Bugs
  • Revisions
  • The artist in this case is represented with
    their first
  • and last names along with a birth and death
    date.
  • Date is a class inside of Java and the import
  • java.util. imports all the types in a group.
    We could
  • alternately do a import java.util.Date to just
    import
  • the one class.

36
  • Comments starting with the / are javadoc
    comments and can be converted into
    documentation using a tool. We will talk about
    the details as we pick up more stuff in the
    class. /
  • import java.util.
  • public class Artist
  • private String firstName, lastName
  • private Date born, died

37
Default Constructor
  • / Artist() -- Default constructor. No
    name, no dates/
  • public Artist() born null died
    null firstName "" lastName ""

38
The Complete Constructor
  • / Artist -- The complete constructor. User
    provides dates and names. If the artist
    is still living pass null for the date
    they died. _at_param first The artist's first
    name _at_param last The artist's last name
    _at_param b The birth date of the artist _at_param
    d The death date of the artist (may be null) /
  • public Artist(String first, String last, Date b,
    Date d) born b died d firstName
    first lastName last

39
getName and getFirstName
  • / getName() - returns the full name of the
    artist _at_return the full name of the
    artist /public String getName() return
    firstName " " lastName/
    getFirstName() -- get the artist's first name
    _at_return the first name of this artist
    /public String getFirstName() return
    firstName

40
getLastName()
  • / getLastName() -- get the artist's last
    name _at_return get this artist's last name
    /public String getLastName() return
    lastName

41
getLastName isDead
  • / getLastName() -- get the artist's last
    name _at_return get this artist's last name
    /public String getLastName() return
    lastName/ isDead -- returns a true if
    the artist is deceased _at_return true if the
    artist died prior to the current
    date. false otherwise. /public boolean
    isDead() if (died ! null) if ((new
    Date()).after(died)) return
    true return false

42
Died and Main
  • / died -- set the date the artist died.
    _at_param dod the date of death for the artist.
    /public void died(Date dod) died
    dod/ I don't have anything in the main
    program right now. Normally my main routines
    contain at least some minimal test code. We
    will add tests as we go on... _at_param
    args The command line arguments
    nothing recognized right now. /public static
    void main(String args)

43
Album
  • / _at_author Jerry Heuring Created Dec 31,
    2003 Description This class
    represents an album. Each album has an
    artist associated with it and it has a number of
    tracks (currently artificially limited to
    100 tracks). The current version has a
    pair of constructors but probably needs yet
    another constructor that will accept tracks
    as well. _at_version 0.0 Bugs
    Revisions /

44
Default constructor
  • public class Album private int inUseprivate
    Track tracks new Track100private String
    titleprivate Artist artist/ A pair of
    constructors -- one with no arguments and one
    with a title and an artist. Tracks must be
    added later using the addTracks method.
    /public Album() inUse 0 title
    "" artist null

45
Album(), getTitle(), getArtist()
  • public Album(String t, Artist a) title
    t artist a inUse 0/ getTitle --
    return the album's title _at_return the album's
    title /public String getTitle() return
    title/ getArtist -- return the artist
    who recorded the album Note There should be
    a way to have more than one artist on an
    album but I'm not going to address that
    here. _at_return the artist's name /public
    Artist getArtist() return artist

46
getRunningTime()
  • / getRunningTime sums the running times of
    the separate tracksand returns a time, in
    minutes, for the total album. _at_return the
    total running time of the album in
    minutes /public float getRunningTime()
    float totalTime 0.0f int
    currentTrack for ( currentTrack0
    currentTrackltinUse currentTrack)
  • totalTime trackscurrentTrack.getLength()
    return totalTime

47
getNumberOfTracks(), addTrack()
  • / getNumberOfTracks _at_return the
    number of tracks currently on the album
    /public int getNumberOfTracks() return
    inUse/ addTrack -- Add a track to an
    album _at_param newTrack the new track to be
    added to the album /public
    void addTrack(Track newTrack) if (inUse lt 100)
    tracksinUsenewTrack inUse

48
Play
  • / play -- plays all the tracks on the album.
    The racks are played in the order they were
    added to the album. It might be interesting
    to allow other ways to play the tracks.
    /public void play() int trackNbr for
    (trackNbr 0 trackNbr lt inUse
    trackNbr) trackstrackNbr.play() publ
    ic static void main(String args)

49
Track Class
  • / _at_author Jerry Heuring Created Dec 31,
    2003 Description This class
    represents a track on a particular album. It
    currently has no way of distinguishing the
    type of data but an array of bytes is currently
    being used to represent the audio data.
    _at_version 0.0 Bugs Revisions
    /

50
  • public class Track String titlefloat
    runningTimebyte audioAlbum album

51
Track() constructors
  • / The default constructor -- no title, no
    audio, no album. /public Track() title
    "" runningTime 0.0f album null/
    A complete constructor for a track _at_param
    t the title of the track _at_param a the album
    the track is associated with _at_param time the
    running time of the track _at_param song the byte
    array of data for playing the
    album /public Track (String t, Album a, float
    time, byte song) title t album
    a runningTime time audio
    song

52
getTitle(), getAlbum()
  • / getTitle -- get the title for this track
    _at_return title for track /public String
    getTitle() return title/ getAlbum --
    get the album that the track appears on
    _at_return the album the track appears on.
    /public Album getAlbum() return album

53
getArtist(), hasAudio()
  • / getArtist -- get the artist associated
    with the track. _at_return
    the Artist object associated with the track
    /public Artist getArtist() return
    album.getArtist()/ hasAudio -- returns
    true if the byte array exists for
    this track. _at_return -- true if byte array
    exists, false otherwise /public boolean
    hasAudio() return audio ! null

54
Play()
  • / play -- plays the audio track if it
    exists. Lots of stuff still to be done
    here. Haven't settled on a format or a method
    to actually play it. Will we need to hold the
    entire track in memory or allow a file
    reference? /
  • public void play () if (audio ! null)
  • // Need to do something to play the
    music....

55
getLength()
  • / getLength -- return the running time
    in minutes for this track
    _at_return the running time of the track /public
    float getLength() return runningTimepublic
    static void main(String args)

56
Hints and Gotchas
  • The classes must be in files named the same as
    the class (Case Matters)
  • Artist.java
  • Track.java
  • Album.java
  • When you compile (if you use javac) the compile
    command has the full name
  • javac Artist.java Track.java Album.java

57
Hints and Gotchas Continued
  • The output will be .class files if it is
    successful.
  • To run using a command line
  • java classToStartIn
  • There is no .class after the class name in the
    command to run. It is simply the class name.
  • java Album

58
Where Can I Get The Software?
  • Eclipse http//www.eclipse.org/
  • JDK http//www.javasoft.com/
  • Java http//www.sun.com/
  • Sun One Studio
  • Ill also make a few CDs available to keep the
    net a bit less loaded.

59
Project 1
  • Description
  • You are to implement a login system for a
    company. Users log in with a username and a
    password. The system needs to keep track of the
    last login time for a particular user as well as
    group membership for the user (the user may be a
    manager, clerical, consultant, etc). Groups may
    be extended further in the future to include
    detailed permissions for objects controlled by
    the system.

60
Project 1 -- Input
  • Your program should allow the user to input a set
    of groups for the system followed by a set of
    users. The user information must include the
    users first and last names as well as the
    username and password. It should also allow for
    input of a set of groups for the user.
  • The application should also implement an object
    that will be responsible for encrypting the
    passwords. The passwords for the users should be
    stored in encrypted form. When a user attempts
    to log in the password they type in should be
    similarly encrypted and then compared to the
    stored password. The company has not settled on
    an encryption method so that you should provide a
    class that has the routines for encrypting the
    password but does not actually encrypt the
    password.

61
Project 1 -- Output
  • Your program should allow the testing team to
    login to user accounts and, if the login is
    successful, it should print out the information
    associated with the user account (except for the
    password!). It should also allow the testing
    team to dump all information (including the
    passwords) to the screen.

62
Deliverables
  • Turn In
  • A listing of your code.
  • E-Mail to jheuring_at_eecs.utoledo.edu
  • Your source files (.java).

63
Grading
  • Correctness
  • Does the program do what it is supposed to?
  • Does it handle extreme cases correctly?
  • Coding Style (Programming Practices)
  • Implies indentation, readability, good choice of
    algorithms, good naming practices, reasonable
    design.
  • Deliverables
  • Did you give me what I asked for?
  • Can I identify it as yours?
  • Does it compile?
Write a Comment
User Comments (0)
About PowerShow.com