Building Java Programs. Introduction to Programming and Simple Java Programs

Similar documents
Lecture 1: Basic Java Syntax

Building Java Programs

Building Java Programs. Chapter 1: Introduction to Java Programming

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Building Java Programs

Introduction to Computer Programming

Entering the world of Javatar

Building Java Programs Chapter 1

Static Methods & Decomposition

Building Java Programs. Introduction to Java Programming

Building Java Programs Chapter 1

3. Java - Language Constructs I

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

An overview of Java, Data types and variables

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSC 1214: Object-Oriented Programming

Computer Components. Software{ User Programs. Operating System. Hardware

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

COMP 202 Java in one week

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Getting Started With Java

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Building Java Programs

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

CompSci 125 Lecture 02

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

Computer Components. Software{ User Programs. Operating System. Hardware

2 rd class Department of Programming. OOP with Java Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Lecture 1: Overview of Java

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

ECE 122 Engineering Problem Solving with Java

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

13 th Windsor Regional Secondary School Computer Programming Competition

Java Programming Language Mr.Rungrote Phonkam

Building Java Programs

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CSE 142, Summer 2014

A web-based IDE for Java

3. Java - Language Constructs I

Java Programming. Atul Prakash

1.Which four options describe the correct default values for array elements of the types indicated?

Software and Programming 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CSE 142, Summer 2015

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Games Course, summer Introduction to Java. Frédéric Haziza

Accelerating Information Technology Innovation

Programming in C++ 4. The lexical basis of C++

CS Week 2. Jim Williams, PhD

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Program Fundamentals

Building Java Programs

Pace University. Fundamental Concepts of CS121 1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Fundamentals of Programming. By Budditha Hettige

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Introduction to C# Applications

CS 231 Data Structures and Algorithms, Fall 2016

Getting started with Java

Software Installation for CS121

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

First Java Program - Output to the Screen

Course Outline. Introduction to java

Software and Programming 1

Introduction to Java

Getting started with Java

Language Fundamentals Summary

Chapter 2. Elementary Programming

Lec 3. Compilers, Debugging, Hello World, and Variables

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

CHAPTER 2 Java Fundamentals

CS 112 Introduction to Programming

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

A variable is a name that represents a value. For

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

B.V. Patel Institute of BMC & IT, UTU 2014

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Javac and Eclipse tutorial

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Section 2.2 Your First Program in Java: Printing a Line of Text

BASIC ELEMENTS OF A COMPUTER PROGRAM

Transcription:

Building Java Programs Introduction to Programming and Simple Java Programs 1

A simple Java program public class Hello { public static void main(string[] args) { System.out.println("Hello, world!"); code or source code: The sequence of instructions in a particular program. The code in this program instructs the computer to print a message of Hello, world! on the screen. output: The messages printed to the user by a program. console: The text box onto which output is printed. 2

Compiling, running a program compiler: A program that translates a computer program written in one language into an equivalent program in another language (often, but not always, translating into machine language). interpreter: A program that dynamically executes programs written in a particular programming language. Java Virtual Machine: A theoretical computer whose machine language is the set of Java byte codes. source code (Hello.java) compile byte code (Hello.class) output execute 3

The command line To run a Java program using your Command Prompt: change to the directory of your program cd \np\ compile the program javac Hello.java execute the program java Hello 4

Another Java program public class Hello2 { public static void main(string[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); The code in this program instructs the computer to print four messages on the screen. 5

Structure of Java programs public class <name> { public static void main(string[] args) { <statement(s)>; Every executable Java program consists of a class... that contains a method named main... that contains the statements to be executed The previous program is a class named Hello, whose main method executes one statement named System.out.println 6

Java terminology class: (a) A module that can contain executable code. (b) A description of a type of objects. (seen later) statement: An executable piece of code that represents a complete command to the computer. every basic Java statement ends with a semicolon ; method: A named sequence of statements that can be executed together to perform a particular action or computation. 7

Syntax and syntax errors syntax: The set of legal structures and commands that can be used in a particular programming language. syntax error or compiler error: A problem in the structure of a program that causes the compiler to fail. If you type your Java program incorrectly, you may violate Java's syntax and see a syntax error. public class Hello { pooblic static void main(string[] args) { System.owt.println("Hello, world!")_ compiler output: H:\summer\Hello.java:2: <identifier> expected pooblic static void main(string[] args) { ^ H:\summer\Hello.java:5: ';' expected ^ 2 errors Tool completed with exit code 1 8

Details about Strings A string may not span across multiple lines. "This is not a legal String." A string may not contain a " character. ' is OK "This is not a "legal" String either." "This is 'okay' though." A string can represent certain special characters by preceding them with a backslash \ (this is called an escape sequence). \t tab character \n new line character \" quotation mark character \\ backslash character 9

Setting up your Java environment, and using various Java editors 10

Downloading Java JDK To run Java programs, you need to download Sun's Java Development Kit ("JDK"). There are several different versions and packages of Java; We want Java 2 Standard Edition (J2SE). Download it from http://java.sun.com/j2se/downloads/ 11

Java editor: TextPad Download it from http://www.textpad.com/ To run a Java program in TextPad: Open a.java file (or type in your own) Click Tools, Compile Java, or press Ctrl-1 (the window should go white and then return after a pause, while the program is compiling) Click Tools, Run Java, or press Ctrl-2 (a black console window should appear with the program's output) 12

Static methods static method: A group of statements that is given a name so that it can be executed in our program. Using a static method requires two steps: declare it When we declare a static method, we write a group of statements and give it a name. call it When we call a static method, we tell our main method to execute the statements in that static method. Static methods are useful for: denoting the structure of a larger program in smaller, more understandable pieces eliminating redundancy through reuse 13

Static method syntax The structure of a static method: public class <Class Name> { Example: public static void <Method name> () { <statements>; public static void printwarning() { System.out.println("This product is known to cause"); System.out.println("cancer in lab rats and humans."); 14

Static methods example public class TwoMessages { public static void main(string[] args) { displaymessage(); System.out.println(); displaymessage(); public static void displaymessage() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); Program's output: Now this is the story all about how My life got flipped turned upside-down Now this is the story all about how My life got flipped turned upside-down 15

Methods calling each other One static method may call another: public class TwelveDays { public static void main(string[] args) { day1(); day2(); public static void day1() { System.out.println("A partridge in a pear tree."); public static void day2() { System.out.println("Two turtle doves, and"); day1(); Program's output: A partridge in a pear tree. Two turtle doves, and A partridge in a pear tree. 16

Control flow of methods When a method is called, Java 'jumps' into that method, executes all of its statements, and then 'jumps' back to where it started. public class TwelveDays { public static void main(string[] args) { day1(); day2(); public static void day1() { System.out.println("A partridge in a pear tree."); public static void day2() { System.out.println("Two turtle doves, and"); day1(); public static void day1() { System.out.println("A partridge in a pear tree."); 17

Static method problems Write a program that prints the following output to the console. Use static methods as appropriate. I do not like my email spam, I do not like them, Sam I am! I do not like them on my screen, I do not like them to be seen. I do not like my email spam, I do not like them, Sam I am! 18

When to use static methods You should place a group of statements into a static method if any of the following conditions is met: The statements are related to each other and form a combined part of the program's structure. The statements are repeated in the program. You need not create static methods for the following: Individual statements. (One single println in its own static method does not improve the program, and makes it harder to read.) Unrelated or weakly related statements. (If the statements are not closely related, consider splitting the method into two or more smaller methods.) Only blank lines. (It's fine to have blank System.out.println(); statements in the main method.) 19

Identifiers identifier: A name that we give to a piece of data or part of a program. Identifiers are useful because they allow us to refer to that data or code later in the program. Identifiers give names to: classes methods variables (named pieces of data; seen later) The name you give to a static method is an example of an identifier. 20

Details about identifiers Java identifier names: first character must a letter or _ or $ following characters can be any of those characters or a number identifiers are case-sensitive; name is different from Name Example Java identifiers: legal: np second_place _myname TheCure ANSWER_IS_42 $variable illegal: me+u 49er question? side-swipe hi there ph.d jim's 2%milk np@yahoo.com can you explain why each of the above identifiers is not legal? 21

Keywords keyword: An identifier that you cannot use, because it already has a reserved meaning in the Java language. Complete list of Java keywords: abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized Java is case sensitive. 22

Comments comment: A note written in the source code by the programmer to make the code easier to understand. Comments are not executed when your program runs. Most Java editors turn your comments into a special color to make it easier to identify them. Comment, general syntax: /* <comment text; may span multiple lines> */ or, // <comment text, on one line> Examples: /* A comment goes here. */ /* It can even span multiple lines. */ // This is a one-line comment. 23