Class 1 ~ Chapter 1

Download Report

Transcript Class 1 ~ Chapter 1

CSE1340
Class 4
1
Objectives

Write a simple computer program in Java


Use simple Output statements
Understand the different types and uses of
comments
 Use proper naming conventions for
classes and files
 Identify the parts of a class header and
method header
 Understand the common types of errors
2
Introduction

Users enter data and instructions into a
computer and receive feedback from the
computer through a user interface
 Programmers can create many types of user
interfaces in Java
 We will create programs with one of three
different types of user interfaces
– Console application
• Stand alone program using a command
line interface
– Windowed application
• Stand alone program using a graphical
user interface
3
Introduction
– Applet
• A program with a graphical user
interface that runs within a web
browser
4
The Welcome to My Day Program
This program will display a splash
screen
– A splash screen is a screen that is
displayed before the main program
starts
 The screen will contain a welcome
message, user’s name, and system
date

5
The Welcome to My Day Program
– The console application will
display text only
– The windowed application will
contain a message box
– The applet will contain text, color,
and a graphic
6
Console application output
7
8
Windowed application output
9
10
Applet
11
12
Program Development
1. Analyze the requirements
Analyze the
Welcome to My Day
problem.
2. Design the solution
Design the user
interface for both the
application and the
applet.
3. Implement the desgin
Translate the design
into code
4. Test the solution
Test the program; find
and correct any errors
13
Program Development
5. Document the program
Include internal
comments explaining
purpose of the code ;
print copies of the code
14
Analysis and Design
Verify that the requirements are
specific enough
 Design the user interface using a
storyboard
 Design the program logic using a
flowchart and event diagram

15
Date submitted:
August 28, 2007
Submitted by:
Linda Numez
Purpose:
Our firm has begun the process of implementing
an electronic calendar application for each
employee. The calendar application needs to
run as a stand-alone application on a desktop
and handheld computers and also be accessible
via the Web via the Web. The company wants
to create a prototype welcome splash screen
that displays a welcome message, the user’s
name, and the system date. This prototype later
will be modified to interface with the database
of an electronic calendar application purchased
from a major software company.
Application title:
Welcome to My Day
Algorithms:
Text and graphics will display on the screen
when the program executes.
16
Notes:
1)As some of our employees have
handheld computing devices with small
monochrome screens, the stand-alone
console application should display text
only.
2)If employees choose to view their
calendar over the Web, the welcome
splash screen should display as an applet.
3)The application and applet should use the
system date, so that it is always current.
The system date does not have to be
formatted in any special way. Our
employees are used to reading the system
date on printouts and electronic transfer
reports.
17
_________________________________________________
_________________________________
Approvals
Approval status:
X Approved
Rejected
Approved by:
David Reneau
Date:
September 24, 2007
Assigned to:
J. Starks, Programmer
18
19
P. 51
20
Coding/Implementing the Solution
Integrated development environments (IDE) such as
NetBeans
can be used to make coding simpler.
21
Coding the Program Comments as Documentation

Purpose of comments
– Provides clear description when reviewing
code
– Helps programmer think clearly when coding
 Placement of comments
– Use a comment header to identify a file and
its purpose
– Place a comment at the beginning of code for
each event and method
– Place comments near portions of code that
need clarification
22
Coding the Program Comments as Documentation
General form:
/* block comments */
// line comments
Example: /* Programmer:
Judy
Date:
Sept. 3, 2007
Filename:
MyProgram.java
Purpose: This program displays the name
and webaddress of a company */
23
Coding the Program The Class Header

Identify how the code can be accessed with an
access modifier
– public indicates that the code can be accessed
by any and all entities
 Specify a unique name for the class
– The class name at the beginning of the
program must match the file name exactly
– Java is case-sensitive
– Must begin with an underscore, dollar sign or
letter and can then contain underscores, $,
letters, or digits (no special characters)
24
Coding the Program The Class Header
– Cannot be reserved words
– By convention, uppercase letters are used
for class names and to distinguish words
in class names
25
Sample Class Header

Use braces { } after the class header to
enclose the class body
public class Person {
// body of the class
}
26
Coding the Program The Method Header


The method header (line 12) contains modifiers,
return value, method name, and parameters along
with their data type
Every stand-alone Java application must contain
a main() method, which is the starting point
during execution
27
Coding the Program The Method Header
1. public static void main(String[] args)
2. public void paint(Graphics g)
3. public void init()
28
Coding the Program The Method Header

Modifiers set properties for a method
– public allows other programs to invoke
this method
– static means this method is unique and
can be invoked without creating a
subclass or instance
 Return value is the data type of the data
returned by the method
– If no data is returned, the keyword void
is used
29
Coding the Program The Method Header

Parameters are pieces of data received by
the method to help the method perform its
operation
– Identifiers are used to name the variable
sent to the method
30
Coding Output for our Welcome Day
application

Call the System.out.println() method in the
SDK to display output to the monitor
– System is the class
– out is the object representing the default
display
– println() is the method
31
Coding Output

When calling a method, arguments are
placed in parentheses
– String literals are placed in quotation
marks
– Numeric literals and variables do not
need quotation marks
 Period delimiters separate the class,
object, and method
 Semicolons must be placed after every
statement except headers and braces
 Braces { } enclose the body of a method
32
Special Characters/
keywords
Character
Use
// Double slash
Marks the beginning
of a comment
import
Tells the compiler
where to search for
the packages that
contain predefined
code
33
Special Characters
Character
Use
{ } Open / close
braces
Encloses a group of
statements, such as
the contents of a
method
( ) Open / close
parentheses
Used in naming a
method such as in
public void paint (….)
34
Special Characters
Character
Use
“ ” Open / close
quotation
marks
Encloses a string of
characters, such as a
message that is to
printed on the screen.
; Semicolon
Marks the end of a
complete
programming
statement.
35
java application
// Printing a line with multiple statements
public class Welcome
{
public static void main (String args[ ] )
{
System.out.println( “Welcome to my day!” );
System.out.println( “Daily Planner for LindaNunez”);
System.out.println(“October 6, 2007”);
}
}
36
Common Escape Sequences
Escape
Sequence
Description
\r Return
Causes the cursor to
go to the beginning of the
current line, not the next
line.
\b Backspace
Causes the cursor to
backup, or move left one
position
37
Common Escape Sequences
Escape
Sequence
Description
\\ Backslash
Causes a backslash to
be printed
\’ Single quote
Causes a single quotation
mark to be printed
\” Double quote Causes a double quotation
mark to be printed
38
A Simple Java Program
// A simple Java program
public class Welcome {
public static void main(String args[])
{ System.out.print(“Programming is ”);
System.out.print( “great fun!”);
}
}
Programming is _
great fun!_
39
A Simple Java Program
public class Items {
public static void main (String args[ ] )
{ System.out.print(“Following items were top ” );
System.out.println(“sellers”);
System.out.println(“during the month of June:” );
System.out.println(“Computer games”);
System.out.println(“Coffee”);
System.out.println(“Aspirin” );
}
}
40
A Simple Java Program
Following items were top sellers
_uring the month of June:
d
_ omputer games
C
Coffee
_
_
Aspirin
_
41
A Simple Java Program
public class Items {
public static void main (String args[ ])
{System.out.print(“Following items were “);
System.out.print(“top sellers\n”);
System.out.println(“during the month of June:”);
System.out.print(“Computer games\nCoffee”);
System.out.println(“\nAspirin”;
}
42
A Simple Java Program
Following items were top sellers
_
during the month of June:
_
Computer games
Coffee
Aspirin
_
43
Testing the Solution

Compile the source code
 If the compiler detects errors, fix the
errors and compile again
 If the compilation was successful, a new
bytecode file for each class is created with
a .class extension
 run the program (test it logically)
44
Debugging the Solution

System Errors
– System command is not set properly
– Software is installed incorrectly
– Location of stored files is not accessible
 Syntax Errors
– One or more violations of the syntax rules of
Java
 Semantic Errors
– The code meaning is unrecognizable to the
compiler
45
Debugging the Solution

Logic and Run-Time Errors
– Unexpected conditions during execution of a
program
46
Debugging Exercises
1. System.out.Println(“hello”);
2. Public class My Sales
3. public static void main(string args[])
4. System.out.println(“welcome to java”)
47
Running the Application

After compilation is successful, run the
program to test for logic and run-time
errors
48
Editing the Source Code - cont.
Recompile and run the application
– The bytecode should be updated
after any changes to the source
code
 Print a hard copy of the source code
– The final step of the program
development cycle is to document
the solution

49