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.

Slides:



Advertisements
Similar presentations
IT151: Introduction to Programming
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Chapter 1: Introduction
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
 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.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
Introduction to Methods
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
“Introduction to Programming With Java”
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java Applications & Program Design
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Comments are for people Header comments supply basic information about the artifact.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Welcome to the Lecture Series on “Introduction to Programming With Java”
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 Computers and Java Chapter 1.3. A Sip of Java: Outline History of the Java Language Applets A First Java Program Compiling a Java Program.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
POS 406 Java Technology And Beginning Java Code
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 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.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
CSI 3125, Preliminaries, page 1 Compiling the Program.
By Mr. Muhammad Pervez Akhtar
Creating a Java Application and Applet
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to programming in java
CSC201: Computer Programming
Lecture Note Set 1 Thursday 12-May-05
CompSci 230 Software Construction
Java Intro.
Anatomy of a Java Program
Java Looking at our first console application in Eclipse
Presentation transcript:

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 class ClassName { classMembers }

JAVA PROGRAMS Every program (NB! Not every class ) has one method called main. Sometimes a program might only have a main method. public static main (String[] args) { statement1;. statement2; }

/** * The FirstProgram class implements an application that * simply displays “Hello World!” to the standard output. */ public class Welcome { public static void main(String[] args) { System.out.println(“Hello World!”); //Display the string }

Class name –Starts with a capital letter. –E.g. FirstProgram Method name –Starts with a lower case letter. –E.g. main, getAge Methods consist of: –Comment at beginning (good idea) –Heading (method name) –Body consists of {sequence of statements}

Comments in Java Code /* text */ Compiler ignores everything from /* to */. /** documentation */ Indicates documentation comment (doc comment). Compiler ignores everything. JDK javadoc tool uses doc comments when preparing automatically generated documentation. // text Compiler ignores everything from // until end of line.

Defining a Class The skeleton of every program is a class definition. Keyword class begins the class definition. Variables & methods of the class are enclosed by {….} (begin & end the class definition block). This app. has no variables & has a single method called main.

The main Method Entry point of every Java application. To run a program you specify the name of it’s class (i.e. FirstProgram ). The interpreter invokes the main method defined within that class. Controls the flow of the program, allocates whatever resources are needed & runs any other methods that provide the functionality of the program.

The method System.out.println Displays a string of characters in a DOS window. After printing, moves the cursor to new line. Sometimes prefer to continue printing on the same line. Use: print instead of println To start printing on a new line –Insert the characters \n in a string of characters. System.out.print(“Hello World!\nSee you later\n”);

Explanation of Code... Code to begin the program:  Start by writing a class.  public - indicates that the method main should be accessible from outside.  File name must be exactly the same as the PUBLIC class (Welcome).  static - means that the method main is a class method. Relates to the class itself.  void - the method main will not leave a result value.  main - reserved word. Name of the method. Followed by its parameters.  (String [ ] args) - parameters. Gets a handle to an array of strings.

Explanation of Code... Code to display a text string: System.out.println("Hello world."); - System - standard class found in the Java package java.lang in the Java API (Application Programming Interfaces. Contains resources of a general nature (e.g. output stream). –Note the “dot” operator. –.out - a static object of the class System. - is an output stream with the name System.out –System.out normally called standard output. - will automatically be written in a text window on the screen.

Explanation of Code... Code to display a text string: System.out.println("Hello world."); –println - is a method of the object out - if it is a method, must pass it some parameters - double-quoted text inside the parentheses is an argument to the method –general syntax: Object_Name.Method_Name(Arguments)

Your First Cup Of Java Create a Java source file –Text editor (Notepad) –.java extension Compile the source file into a byte code file –javac filename.java from command line Run the program (byte code) - interpret –java filename (no extension specified)

Environments available? TextPad jGrasp

Types of Errors Syntax Run-Time Logic

Syntax Errors a “grammatical” error caught by compiler (“compiler-time error”) automatically found, usually the easiest to fix cannot run code until all syntax errors are fixed error message may be misleading Example: Misspelling a command, for example “rturn” instead of “return”

Run-Time Errors An execution error (during run-time) Not always so easy to fix Error message may or may not be helpful Example: Division by zero - if your program attempts to divide by zero it automatically terminates and prints an error message.

Logic Errors Just because it compiles and runs without getting an error message does not mean the code is correct! An error in the design (the algorithm) or its implementation –code compiles without errors –no run-time error messages –but incorrect action or data occurs during execution Generally the most difficult to find and fix Need to be alert and test thoroughly –think about test cases and predict results before executing the code

Logic Error Examples Algorithm Error: –averageOfFiveScores = SumOfScores/2 (should divide by 5) Implementation Error: –typed in wrong symbol in source code - sum = a - b; (should be “sum = a + b;)