How to Create a Java program CS115 Fall 2014- George Koutsogiannakis.

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.
How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute.
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 do we make our HelloTester.java program do something? The java in our HelloTester.java file won’t do anything by itself. We need to tell the computer.
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.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to Java.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using.
public static void main (String[] args)
Introducing Java.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Chapter 1: Creating Java Programs
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
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.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
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.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.
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.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
Introduction to programming in the Java programming language.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Mixing integer and floating point numbers in an arithmetic operation.
© 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.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Java FilesOops - Mistake Java lingoSyntax
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 110 – Intro to Computing - Programming
CS 177 Recitation Week 1 – Intro to Java. Questions?
ITP 109 Week 2 Trina Gregory Introduction to Java.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
CS 201 Lecture 1 (b) Using an IDE Tarik Booker CS 201: Introduction to Programming California State University, Los Angeles.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Introduction to Java Import Scanner class to use in our program
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Introduction to.
Java programming lecture one
Intro to Java.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
PROGRAM STRUCTURE CSC 111.
String Output ICS 111: Introduction to Computer Science I
Introduction to Algorithm Design
Java Intro.
Running a Java Program using Blue Jay.
Presentation transcript:

How to Create a Java program CS115 Fall George Koutsogiannakis

INFORMATION ON JAVA TOOLS JDK – Java Development Kit- (Used when you write a program) – Provides: The compiler Other tools (not needed in this class). JRE – Java Runtime Environment- Used when you are ready to execute (interpret) a program. – Provides: The runtime environment JVM (Java Virtual Machine) The interpreter

INFORMATION ON JAVA TOOLS Notepad++ – The text editor tool used by this class to write (edit) java programs. (note that there are other editors besides Notepad++ but this one is the approved for the class).

COMPILING AND RUNNING A JAVA PROGRAM 1.Use Notepad++ to write a program. – Use Java API as help. – Use text and text examples for help. – Use class lectures and exercises done in class as help. – This file is sometimes referred as the “source code file” 2.Save the source code file with a name and extension.java i.e. MyFirstProgram.java Note: Later on we will explain that the name of the file has to match something called the ‘’class” name that you used in your program.

COMPLILING AND RUNNING A JAVA PROGRAM THE COMPILING AN DTEH RUNNING OF THE PROGRAM CAN BE DONE 1.BY USING THE TOOLBAR MENU UNDER MACRO-> COMPILER JAVA OR RUN JAVA 2.BY USING A DOS WINDOW AND TYPING DOS COMMANDS.

COMPILING AND RUNNING A JAVA PROGRAM USING A DOS WINDOW 3.Call the java compiler from the directory you have your source code file saved. – Open a DOS pane (we open a DOS window from accessories and clicking on “command prompt”) – On the DOS pane go to where your program is saved i.e. C:\CS115\MyPrograms\> where MyPrograms is the folder where you saved your program (we change the directory on the DOS pane going down by typing cd.. or going up one directory step by typing cd followed by the name of the folder one level up). – Call the java compiler, javac, to compile your program i.e. C:\CS115\MyPrograms\> javac MyFirstProgram.java

COMPILING AND RUNNING A JAVA PROGRAM 4.The compiler will complain if errors were made in writing the source code. These errors are called : “ COMPILER ERRORS”. – Correct source code errors as needed and re compile. – Keep this process going until no errors are reported by the compiler. – Compiler error messages identify the line # where the error was made and give some hint (usually not very well phrased-some guessing required gained with experience of using the compiler).

COMPILING AND RUNNING A JAVA PROGRAM 5.When all errors are corrected the compiler creates a file called the “bytecodes file” under the name you gave to the program but with the extension:.class i.e. MyFirstProgram.class – This file is created in the same folder as your source code file. – Opening this file does not give you any recognizable information by the human eye (No English words).

COMPILING AND RUNNING A JAVA PROGRAM 6.Now we are ready to interpret (or “execute” as the term used in other programming languages). – On a DOS pane call the interpreter (called java) i.e. C:\CS115\MyPrograms\> java MyFirstProgram Notes: java is the command for the interpreter. Your bytecodes file name is used without any extension. The output of the program will appear on the DOS window(pane). We could, however, get errors during execution, called: “RUNTIME ERRORS”.

COMPILING AND RUNNING A JAVA PROGRAM 7.Runtime Errors – These are errors that take place during the time the program runs (these errors can not be caught by the compiler). – Quite often they are referred to as “exceptions” – Usually the result of programmer’s mistakes. – They are reported by Java’ s runtime environment called JVM (Java Virtual Machine). – Need to go back to the source code file and correct them, then re compile and try to interpret again. – The interpreter will give you messages (usually cryptic) regarding the runtime errors. Sometimes we can make sense out of the message, but other times we have to kind of guess.

COMPILING AND RUNNING A JAVA PROGRAM 8.If there are no compiler or runtime errors, it is possible to get another category or errors called “LOGICAL ERRORS” (in spite of the fact that your program was inetrpreted) – It is possible that you are not getting any errors but your program does not give you the correct result. – This is due to your logical errors when you wrote the code: i.e. Suppose that you meant to add but instead you multiplied two numbers.

STRUCTURE OF A JAVA PROGRAM Your java source code should have the following sections: – Import statements (not always needed) – The class name – “Global” variables declared (not always needed). Also called instance variables. – Methods inside the class One of those methods must be the main method – Inside the method we have “local” variables declared and the source code for the method. – Note: Don’t worry about the terms: import statements, class, global variables and local variables and methods at this time.

STRUCTURE OF A JAVA PROGRAM Note: A java program can consist of more than one class sometimes. Not all Java programs (classes) need a main method. But, in order for your program to be interpreted and produce a result then at least on of your classes needs to have a main method. The program starts always with the interpretation (execution) of the main method in whatever class is located. The classes without a method are there to provide additional support for the class that has the main method.

STRUCTURE OF A JAVA PROGRAM Example of a simple program. This program has no import statements (not needed for this program). public class MyFirstProgram { //double slashes are used for comments //anything after the double slashes is disregarded by the compiler //the name of the class is: MyFirstProgram public static void main (String[] args) { System.out.println(“This is a Java program”); //the above statement tells the computer to print out on the DOS window the String (text) //enclosed by the double quotation marks. } Save the file as MyFirstProgram.java Notice that the name of the source code file has to match the name of the class The output of this program will appear on the DOS pane. What is the output?

STRUCTURE OF A JAVA PROGRAM Observations – A class starts with a curly bracket and ends with a curly bracket. – A method starts with a curly bracket and ends with a curly bracket – Comments that get disregarded by the compiler start with // – A line of code ends with semicolon ; – The output in English words in this case is surrounded by double quotations (this type of output is called : String). – System.out.println is a command that tells the computer’ s operating system that we want to output on the standard output DOS window as a String. – The code that ends with the semicolon is sometimes referred to as ”one line of code” or statment.

STRUCTURE OF A JAVA PROGRAM Class – Notice that the program starts with something called “ class” after the word public. A java program always needs a class with a name (chosen by you). – A class represents a category of items. We will explain the meaning of the class further as we go deeper into the course. – It is the basis of what is called Object Oriented Programming (OOP).

STRUCTURE OF A JAVA PROGRAM Method – The part of the java program where you place the code that performs the desired action(s) that bring the desired result(s). The method name in the previous example was “main”. – There can be more than one method in the program. – There must be a method called main besides any others (the other methods will have different names). – The main method is always written the way shown in the example. i.e public static void main(String[] args).

STRUCTURE OF A JAVA PROGRAM Notice that a line of code always ends with a semicolon. – System.out.println(“Hello World”); Also notice that Java is case sensitive for some words called “keywords”. – Keywords are reserved words by the language and are case sensitive. – i.e the word “class” is a keyword. Typing “Class” with a capital C will create an error by the compiler (because of the capital C). – public is also a keyword etc. – Names that you give to classes and methods and variables are not case sensitive but nevertheless have some restrictions (i.e you can not name a class with numbers).

STRUCTURE OF A JAVA PROGRAM COMBINING TWO OUTPUT STATEMENTS by using the concatenation operator : + – Output statements in the form of English are called Strings. – We can combine two Strings by using the plus operator + – When the plus sign is used in that context it is called the “concatenation operator”. We will see that quite often in Java a symbol can be used in more than one context. (as with the + symbol where sometimes is used as an arithmetic plus and sometimes as a means to combine Strings).

STRUCTURE OF A JAVA PROGRAM Example public class CombineTwoStrings { public static void main(String[] args) { System.out.println(“This example combines”+”two Strings”); } Later we will discover that anything that comes after the concatenation operator gets converted into a String automatically (If you don’t understand this statement, don’t worry.It would become clearer later on in the course).

STRUCTURE OF A JAVA PROGRAM FORCING A PROGRAM TO TERMINATE – A Java program terminates automatically when the last instruction in the main method has been interpreted (executed) – We could force a program to terminate before the last instruction (if we desired so)by using the command: System.exit(0); – Notice that our programs terminate automatically anyway but later on we might, under certain conditions, want to terminate a program before all lines of code have been executed.