Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments.

Similar presentations


Presentation on theme: "1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments."— Presentation transcript:

1 1 CSE1340 Class 4

2 2 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

3 3Introduction 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

4 4Introduction –Applet A program with a graphical user interface that runs within a web browser

5 5 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

6 6 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

7 7 Console application output

8 8

9 9 Windowed application output

10 10

11 11 Applet

12 12

13 13 Program Development 1.Analyze the requirementsAnalyze the Welcome to My Day problem. 2.Design the solutionDesign the user interface for both the application and the applet. 3.Implement the desginTranslate the design into code 4.Test the solutionTest the program; find and correct any errors

14 14 Program Development 5. Document the program Include internal comments explaining purpose of the code ; print copies of the code

15 15 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

16 16 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.

17 17 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.

18 18 _________________________________________________ _________________________________ Approvals Approval status:XApproved Rejected Approved by:David Reneau Date:September 24, 2007 Assigned to:J. Starks, Programmer

19 19

20 20 P. 51

21 21 Coding/Implementing the Solution Integrated development environments (IDE) such as NetBeans can be used to make coding simpler.

22 22 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

23 23 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 */

24 24 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)

25 25 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

26 26 Sample Class Header Use braces { } after the class header to enclose the class body public class Person { // body of the class }

27 27 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

28 28 Coding the Program - The Method Header 1.public static void main(String[] args) 2. public void paint(Graphics g) 3. public void init()

29 29 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

30 30 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

31 31 Coding Output for our Welcome Day application Call the System.out.println() method in the SDK to display output to the monitor –S–System is the class –o–out is the object representing the default display –p–println() is the method

32 32 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

33 33 Special Characters/ keywords CharacterUse // Double slash Marks the beginning of a comment import Tells the compiler where to search for the packages that contain predefined code

34 34 Special Characters CharacterUse { } 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 (….)

35 35 Special Characters CharacterUse “ ” 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.

36 36 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”); } }

37 37 Common Escape Sequences \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 EscapeSequence Description

38 38 Common Escape Sequences \\ Backslash \’ Single quote \” Double quote Causes a backslash to be printed Causes a single quotation mark to be printed Causes a double quotation mark to be printed EscapeSequence Description

39 39 A Simple Java Program Programming is _great fun!_ // A simple Java program public class Welcome { public static void main(String args[]) { System.out.print(“Programming is ”); System.out.print( “great fun!”); System.out.print( “great fun!”);} }

40 40 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(“sellers”); System.out.println(“during the month of June:” ); System.out.println(“Computer games”); System.out.println(“Coffee”); System.out.println(“Aspirin” ); } }

41 41 A Simple Java Program Following items were top sellers _ during the month of June: _ Computer games _ Coffee _ Aspirin _

42 42 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.print(“top sellers\n”); System.out.println(“during the month of June:”); System.out.print(“Computer games\nCoffee”); System.out.println(“\nAspirin”; System.out.println(“\nAspirin”; }

43 43 A Simple Java Program Following items were top sellers _ during the month of June: _ Computer games Coffee Aspirin _

44 44 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)

45 45 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

46 46 Debugging the Solution Logic and Run-Time Errors –Unexpected conditions during execution of a program

47 47 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”)

48 48 Running the Application After compilation is successful, run the program to test for logic and run-time errors

49 49 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


Download ppt "1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments."

Similar presentations


Ads by Google