Programming Basics: How To Create Java Programs - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Basics: How To Create Java Programs

Description:

Programming Basics: How To Create Java Programs Ankur M. Teredesai amt_at_cs.rit.edu www.cs.rit.edu/~amt – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 37
Provided by: adam2154
Learn more at: https://www.cs.rit.edu
Category:

less

Transcript and Presenter's Notes

Title: Programming Basics: How To Create Java Programs


1
Programming BasicsHow To Create Java Programs
Ankur M. Teredesai amt_at_cs.rit.edu www.cs.rit.edu/
amt
2
(No Transcript)
3
(No Transcript)
4
Identifiers
  • Identifiers in Java are composed of a series of
    letters and digits where the first character must
    be a letter.
  • Identifiers should help to document what a
    classes, variables, or methods are used for.
  • Upper and lower case letters are different in
    Java.
  • ThisOne is not the same as thisOne.
  • Identifiers should not include punctuation and
    can not include spaces.

5
Identifiers
  • A good programming standard for identifiers
    composed of multiple words is to capitalize the
    first character of the additional words.
  • The case of the first letter of the first word
    will depend upon the use of the identifier.
  • For Example,
  • myCar, or bobAndSusieAreFriends

6
Identifiers
  • Class names begin with a capital letter.
  • This standard makes it easier to understand which
    identifiers represent classes.
  • In the MyFirstApplication we have two class
    names
  • MyFirstApplication and MainWindow

7
Identifiers
  • Variable (object names) and method names begin
    with a lower case letter.
  • In MyFirstApplication we have one object
  • mainWindow
  • Note that mainWindow is different from
    MainWindow.
  • In MyFirstApplication we have one method
  • main()

8
Creating a Java Program
  • The first step to creating a Java program is to
    define a class containing the main method.
  • The MyFirstApplication program on page 39 of Wu
    is an example of a Java class containing a main
    method.
  • This Java class definition can be thought of as
    the driver class or the ring leader class.

9
MyFirstApplication
MyFirstApplication
main(String args)
10
Declaring Objects
  • In order to create objects to work within a Java
    program they must first be declared.
  • The object declaration includes the class name
    that the object belongs to and the variable name
    for the object.
  • The format for declaring an object is
  • ltclass namegt ltobject(variable) namegt
  • The class must be defined before you can declare
    the object.

11
Declaring Objects
  • Think about our cars.
  • Cars was the class name and we had three
    instances of the this class.
  • To declare each of the cars we had created we
    would type
  • Cars clintsPorsche
  • Cars jessicasBMW
  • Cars johnsLamborghini

12
Declaring Objects
  • The MyFirstApplication program in Wu defines one
    object called mainWindow of the class type
    MainWindow.
  • MainWindow mainWindow
  • Note the differences in the text above.

13
Declaring Objects
  • Declaring an object only creates a name to refer
    to the object, it does not create the instance of
    that object.
  • In order to use the named object instance, the
    object must be instantiated (or created).

14
Object Instantiation
  • Object instantiation (creation) actually creates
    a copy of the class and stores it in memory with
    the name defined in the object declaration.
  • The format for object instantiation is
  • ltobject namegt new ltclass namegt (ltargumentsgt)

15
Object Instantiation
  • ltobject namegt new ltclass namegt (ltargumentsgt)
  • The ltobject namegt is the name for the object as
    you declared it in your program.
  • The ltclass namegt is the name of the class that
    this object has been declared to belong to.
  • The ltargumentsgt represent possible data that may
    be required to create the object instance.

16
Object Instantiation
  • We declared three types of Cars
  • Cars clintsPorsche
  • Cars jessicasBMW
  • Cars johnsLamborghini
  • To instantiate the instances of the Cars
  • clintsPorsche new Car()
  • jessicasBMW new Car()
  • johnsLamborghini new Car()

17
Object Instantiation
  • The MyFirstApplication instantiates the object
    mainWindow on line two of the main method
  • mainWindow new MainWindow()

18
MyFirstApplication
MyFirstApplication
mainWindow
MainWindow
Main(String args)
19
Message Passing
  • Once an object exists, the program can begin to
    send messages to the object.
  • The message must include the object to which the
    message is addressed, the name of the task to be
    completed, and any data required to complete the
    task.
  • In Java speak
  • ltobject namegt.ltmethod namegt(ltargumentsgt)

20
Message Passing
  • Lets create messages for our cars.
  • We need to know how much gas Clint has.
  • clintsPorsche.getGasLevel()
  • We want to turn on Johns hazards.
  • johnsLamborghini.turnOnHazards()

21
Message Passing
  • Now lets look at the last line of the main method
    in MyFirstApplication.
  • mainWindow.setVisible(true)

22
Method Declarations
  • The method declaration provides the function
    implementation in the program.
  • The basic format of a method declaration is
  • ltmodifiersgt ltreturn typegt ltmethod namegt
    (ltparametersgt)
  • ltmethod bodygt

23
Method Declarations
  • ltmodifiersgt ltreturn typegt ltmethod namegt
    (ltparametersgt)
  • ltmethod bodygt
  • Modifiers represent terms that determine what
    kind of method is declared. (Chap 4)
  • The Return Type is the data type of the value
    returned by the method.
  • If the method does not return a value this value
    is void.

24
Method Declarations
  • ltmodifiersgt ltreturn typegt ltmethod namegt
    (ltparametersgt)
  • ltmethod bodygt
  • The method name should identify the task to be
    completed by the method
  • The parameters are the data that the method
    requires to complete the task.
  • The method body includes the code statements that
    implement the task.

25
Method Declarations
  • Lets look at the main method in
    MyFirstApplication.

26
Class Declarations
  • Java programs are composed of one or more
    classes.
  • Some classes are already defined in Java but we
    also need to be able to define our own classes.
  • class ltclass namegt
  • ltclass member declarationsgt

27
Class Declarations
  • class ltclass namegt
  • ltclass member declarationsgt
  • Every class must have a unique name.
  • The class member declarations include method
    definitions and variable (data value).

28
Class Declarations
  • One class in every program must include the main
    method.
  • This class is the driver class.
  • The main method is the first method that is
    called when a program is executed.
  • Lets look at the MyFirstApplication program.

29
Import Statement
  • The import statement is used to allow a class
    definition to access predefined Java classes.
  • Related predefined Java classes are stored in
    packages.
  • We will use the package called javabook provide
    by Wu.
  • In order to import predefined classes
  • import ltpackage namegt.ltclass namegt

30
Import Statement
  • import ltpackage namegt.ltclass namegt
  • The package name represents the the particular
    package to look into for the class name.
  • java. or javabook.
  • The class name can be a specific class or every
    class in the package.
  • import javabook.MainWindow
  • import javabook.

31
Comments
  • Providing a description of what the program does
    is called commenting the code.
  • Commenting is used to provide an overall
    description of the class and what it does, who
    the author is, when the class was created and
    modified, etc. Header comments
  • Commenting is also used throughout your code to
    provide a description of a method, as well as to
    provide descriptions of complicated code.
  • The use of meaningful variable and method names
    helps comment the code.

32
Comments
  • Java provides three ways to comment code.
  • MyFirstApplication includes two types.
  • The / . / is used for multiple line comments.
  • The // is used for a single line comment.
  • The final type is javadoc.

33
Comments
  • The javadoc comment appears before the class
    declaration.
  • The javadoc comment begins with / rather than
    /.
  • The javadoc comments end with /
  • The benefit of using a javadoc comment is that
    the programmer can use a tool that will
    automatically create a web page from the comment.
  • This is only true for javadoc comments.

34
Comments
  • Comments can not be put inside of another
    comment.
  • Comments are for the programmer only, they do not
    represent executable code.
  • The computer ignores comments.

35
Commenting Rules of Thumb
  • Do not state the obvious for self-evident
    information. For example if the code is
  • x x y // it is not necessary that you are
    adding x and y and storing the result in x.
  • Provide a brief statement about each variable and
    a brief description for each method.

36
Commenting Rules of Thumb
  • Do not comment code that is bad, if you are
    writing a long paragraph to describe what is
    going on, then re-write the code to make it
    simpler.
  • Make sure your comments agree with the code.
  • Use comments to Clarify not confuse the
    unsuspecting reader. Use them to help the reader.
Write a Comment
User Comments (0)
About PowerShow.com