© The McGraw-Hill Companies, 2006 Chapter 1 The first step.

Slides:



Advertisements
Similar presentations
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
JavaScript, Third Edition
Introduction to C Programming
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to scripting
Introducing Java.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Introduction to Computer Systems and the Java Programming Language.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
By Mr. Muhammad Pervez Akhtar
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
Chapter 6 JavaScript: Introduction to Scripting
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 3 GC 101 Java Fundamentals.
Console Output, Variables, Literals, and Introduction to Type
Yanal Alahmad Java Workshop Yanal Alahmad
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 1 The first step

© The McGraw-Hill Companies, 2006 Software the set of instructions that tells a computer what to do is called a program; software is the name given to a single program or a set of programs. application software is the name given to useful programs that a user might need. system software is the name given to special programs that help the computer to do its job. programming is the task of writing instructions for the computer; these instructions have to be written in a special programming language.

© The McGraw-Hill Companies, 2006 Compiling programs modern programming languages like Java consist of instructions that look a bit like English; the set of instructions written in a programming language is called the program code or source code. these instructions have to be translated into binary instructions (i.e. 0's and 1's); the language of the computer is often referred to as machine code; a special piece of system software called a compiler translates source code to machine code.

© The McGraw-Hill Companies, 2006 Programming in Java Java is platform-independent; Java can work within the World Wide Web of computers via browsers, such as Netscape and Internet Explorer; Java programs that run on the Web are called applets; inside a modern browser is a special program, called a Java Virtual Machine (JVM); the JVM is able to run a Java program for the particular computer on which it is running.

© The McGraw-Hill Companies, 2006 How can it do this? Java compilers do not translate the program into machine code, but into special instructions called Java byte code; Java Byte Code (which still consists of 0's and 1's), contains instructions that are exactly the same irrespective of any computer; Java Byte Code is universal, whereas machine code is specific to a particular type of computer; the job of the JVM is to translate each instruction for the computer it is running on, before the instruction is performed.

© The McGraw-Hill Companies, 2006 Your first program when your program runs you will see the words "Hello world" displayed.

© The McGraw-Hill Companies, 2006 Adding comments to a program

© The McGraw-Hill Companies, 2006

Declaring variables in Java the above data types are used in programs to create named locations in the computer's memory; these will contain values while a program is running; this process is known as declaring; these named locations are called variables. we can choose any name for variables as long as: - the name is not already a word in the Java language (such as class, void); - the name has no spaces in it; - the name does not include operators or mathematical symbols such as + and -; - the name starts either with a letter, an underscore (_), or a dollar sign ($). the convention in Java programs is to begin the name of a variable with a lower case letter.

© The McGraw-Hill Companies, 2006 A variable is declared as follows: dataType variableName ; several variables can be declared on a single line if they are all of the same type:

© The McGraw-Hill Companies, 2006 Assignments in Java assignments allow values to be put into variables; they are written in Java with the use of the equality symbol (=); this symbol is known as the assignment operator; simple assignments take the following form: variableName = value; For example

© The McGraw-Hill Companies, 2006

Creating constants there are occasions where data items in a program have values that do not change. values that remain constant throughout a program (as opposed to variable) should be named and declared as constants; constants are declared much like variables in Java except that they are preceded by the keyword final, and are always initialized to their fixed value. For example

© The McGraw-Hill Companies, 2006 Arithmetic operators

© The McGraw-Hill Companies, 2006 For example terms on the right-hand side of assignment operators (like ) are referred to as expressions.

© The McGraw-Hill Companies, 2006 Order of evaluation brackets division multiplication addition subtraction

© The McGraw-Hill Companies, 2006 Expressions in Java the expression on the right-hand side of an assignment statement can itself contain variable names; if this is the case then the name does not refer to the location, but to the contents of the location. For example

© The McGraw-Hill Companies, 2006 You can use the name of the variable you are assigning to in the expression itself this means that the old value of the variable is being used to calculate its new value. For example

© The McGraw-Hill Companies, 2006 A special shorthand if a variable x has been declared as an int, then the instruction for incrementing x would be: x = x + 1; this is so common that there is a special shorthand for this instruction: x++; the '++' is therefore known as the increment operator; similarly there exists a decrement operator, '--': x--; is shorthand for: x = x - 1;

© The McGraw-Hill Companies, 2006 Output in Java To display a message in Java: System.out.println(message to be printed on screen); For example println is short for print line and the effect of this statement is to start a new line after displaying whatever is in the brackets. there is an alternative form of the System.out.print statement, which uses System.out.print.

© The McGraw-Hill Companies, 2006 When we run this program, the output looks like this: Hello world Hello world again!

© The McGraw-Hill Companies, 2006 The effect of changing the first println to print: now the output looks like this: Hello worldHello world again! If you want a blank line in the program, use println with empty brackets:

© The McGraw-Hill Companies, 2006 Introducing strings collections of characters such as "Hello world" are called strings; in Java, literal strings like this are always enclosed in speech marks; two strings can be joined together with the plus symbol (+); when using this symbol for this purpose it is known as the concatenation operator. For example

© The McGraw-Hill Companies, 2006

this program produces the following output: *** Product Price Check *** Cost after tax = 587.5

© The McGraw-Hill Companies, 2006 Input in Java: the Scanner class in order to use the Scanner class we have to place the following line at the beginning of our program: you can now use all the input methods that have been defined in this class. having imported the util package, you will need to write the following instruction in your program. this declares a Scanner object (more about this in chapters 6 and 7)

© The McGraw-Hill Companies, 2006 Input methods of the Scanner class if we want a user to type in an integer at the keyboard: in the case of a double, y: in the case of a char, c:

© The McGraw-Hill Companies, 2006 More about strings a String is not a simple data type like an int or a char; a String is a class - you learn about this in week 5; however, you can declare a String in a similar way to the way you do it with variables such as int or char; notice that String "type" has to start with a capital "S". For example

© The McGraw-Hill Companies, 2006 Java allows you to use the normal assignment operator ( = ) with strings: to obtain a string from the keyboard you can use the next method of Scanner. Note your string should not contain spaces; you will see a way around this in chapter 6.

© The McGraw-Hill Companies, 2006 Program design designing a program is the task of considering exactly how to build the software; writing the actual code is referred to as implementation. Java programs consist of one or more classes, each with one or more methods; it is the instructions within a method that determine the behaviour of that method; if the behaviour of a method is complex, then it will also be worthwhile spending time on designing the instructions that make up the method; when you sketch out the code for your methods a general purpose "coding language" can be used; code expressed in this way is referred to as pseudocode.

© The McGraw-Hill Companies, 2006 Example