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.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Chapter 1: Introduction
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Introduction to Java Programming, 4E
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
Chapter 2 How to Compile and Execute a Simple Program.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Chapter 1: Creating Java Programs
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Introduction to Java Programming with Forte Y. Daniel Liang.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Creating a Java Application and Applet
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
CSC 110 – Intro to Computing - Programming
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 6 JavaScript: Introduction to Scripting
CSC201: Computer Programming
Chapter 1 Introduction to Computers, Programs, and Java
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Chapter 2, Part I Introduction to C Programming
Introduction to Scripting
Intro to Java.
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Instructor: Alexander Stoytchev
Presentation transcript:

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 Use proper naming conventions for classes and files Identify the parts of a class header and method header Understand the common types of errors

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

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

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 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 Console application output

8

9 Windowed application output

10

11 Applet

12

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 Program Development 5. Document the program Include internal comments explaining purpose of the code ; print copies of the code

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 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 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 _________________________________________________ _________________________________ Approvals Approval status:XApproved Rejected Approved by:David Reneau Date:September 24, 2007 Assigned to:J. Starks, Programmer

19

20 P. 51

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

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 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 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 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 Sample Class Header Use braces { } after the class header to enclose the class body public class Person { // body of the class }

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 Coding the Program - The Method Header 1.public static void main(String[] args) 2. public void paint(Graphics g) 3. public void init()

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 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 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 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 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 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 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 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 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 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 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 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 A Simple Java Program Following items were top sellers _ during the month of June: _ Computer games _ Coffee _ Aspirin _

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 A Simple Java Program Following items were top sellers _ during the month of June: _ Computer games Coffee Aspirin _

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 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 Debugging the Solution Logic and Run-Time Errors –Unexpected conditions during execution of a program

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 Running the Application After compilation is successful, run the program to test for logic and run-time errors

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