CMT Programming Software Applications

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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Chapter 2 - Introduction to Java Applications
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 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.
Chapter 2 - Introduction to Java Applications
Introduction to C Programming
Java Applications & Program Design
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Chapter 2 Variables.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Variables.
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Chapter 2 Introduction to Java Applications
Chapter 2 Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 3 – Introduction to C# Programming
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Computer Programming-1 CSC 111
Introduction to C Programming
Presentation transcript:

CMT4001 -- Programming Software Applications Java Data Types and Operators Week 2 Dr. Xiaohong Gao TP B107, ext. 2252 x.gao@mdx.ac.uk

Contents To become familiar with Java applications To become familiar with variables in Java To become familiar with Java primitive’s data types To become familiar with arithmetic operators

A Simple Program 1 // Fig. 2.1: Welcome1.java 2 // A first program in Java 3 4 public class Welcome1 { 5 public static void main( String args[]) 6 { 7 System.out.println( "Welcome to Java Programming!" ); 8 } } Welcome to Java Programming!

A Simple Program: Printing a Line of Text 1 // Fig. 2.1: Welcome1.java // indicates the remainder of the line is a comment Comments are ignored by the compiler Use comments to document and describe code Can also use multiple line comments: /* ... */ /* This is a multiple line comment. It can be split over many lines */ Another line of comments that describes the program Note: line numbers are not part of the program; they are added for our reference

A Simple Program: Printing a Line of Text 3 A blank line Blank lines and spaces make a program more readable Blank lines, spaces, and tabs are known as whitespace characters, and are ignored by the compiler public class Welcome1 { Begin a class definition for class Welcome1 Every Java program has at least one user-defined class class keyword immediately followed by class name Keyword: words reserved for use by Java Naming classes: capitalize every word SampleClassName

A Simple Program: Printing a Line of Text public class Welcome1 { Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit Contains no spaces Examples: Welcome1, $value, _value, button7 7button is invalid Case sensitive (capitalization matters) a1 and A1 are different For now, use public keyword Certain details are not important now - mimic programs, full discussions will come later

A Simple Program: Printing a Line of Text public class Welcome1 { Saving files File name is class name and .java extension Welcome1.java Left brace Begins body of every class Right brace ends definition (line 9) public static void main( String args[] ) Part of every Java application Applications begin executing at main Parenthesis indicate main is a method Java applications contain one or more methods

A Simple Program: Printing a Line of Text 5 public static void main( String args[] ) Exactly one method must be called main Methods can perform tasks and return information void means main returns no information For now, mimic main's first line 6 { Left brace begins body of method definition Ended by right brace

A Simple Program: Printing a Line of Text System.out.println( "Welcome to Java Programming!" ); Instructs computer to perform an action Prints string of characters between double quotes String - series characters inside double quotes White spaces in strings are not ignored by compiler System.out - standard output object Allows java to print to command window (i.e., MS-DOS prompt) Method System.out.println displays a line of text Argument inside parenthesis Entire line known as a statement All statements must end with a semicolon ;

A Simple Program: Printing a Line of Text 8 } Ends method definition } Ends class definition Some programmers add comments to keep track of ending braces Lines 8 and 9 could be rewritten as: 8 } // end of the method main () 9 } // end of class Welcome1 Remember that the compiler ignores comments

A Simple Program // Fig. 2.2: Welcome2.java 2 // Printing a line with multiple statements 3 public class Welcome2 { 5 public static void main( String args[]) 6 { System.out.print ( "Welcome to Java); System.out.println(“Java programming”); } System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

A Simple Program Line numbers 1-2: Comments 3: Blank 4: Begin class Welcome2 5: Method main 6: Begin main body 7: Method System.out.print 8: Method System.out.println 9: end main 10: end Welcome2

A Simple Program: Printing a Line of Text Escape characters Backslash ( \ ) Indicates that special characters are to be output Backslash combined with a character makes an escape sequence \n - newline \t - tab Usage Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" );

A Simple Program: Printing a Line of Text // Fig. 2.3: Welcome3.java 2 // Printing multiple lines with just 3 // a single statements 4 5 public class Welcome3 { 6 public static void main( String args[]) 7 { System.out.print (“Welcome\ \nto\nJava\nProgramming!”); 10 } 11 } Notice how a new line is output for each \n escape sequence. Welcome to Java Programming!

Variables A variable has three properties: a memory location to store this value the type of data stored in the memory location the named used to refer to the memory location

Variables The general syntax for declaring variables is: <data type> <variables> where variables is a sequence of identifiers separated by commas. Example: int x; int y; or int x,y;

Variables Variables Visual representation 45 Variable names correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable it replaces (and destroys) previous value Reading variables from memory does not change them Visual representation number1 45

Data type There are six numerical data types in Java: byte – integer short –integer int – integer long –integer float – real double - real Example: int j, k, l; float numberOne, numberTwo; long bigInteger; double bigNumber;

Java’s primitive data types Size Description Integers byte 8-bit Byte-length integer short 16-bit Short integer int 32-bit Integer 64-bit long Long integer Real Numbers float 32-bit Single-precision floating-point double 64-bit Double-precision floating-point Other Types char 16-bit Unicode character A single character boolean true or false A boolean value(true or false)

Variables Assignment statement <variable> = <expression>; Examples: firstNumber = 234; sum = firstNumber+ secondNumber; solution = x*x-2*x+1;

Variables The variables firstNumber and int firstNumber,secondNumber; after A is executed firstNumber = 234; secondNumber = 87; firstNumber secondNumber The variables firstNumber and secondNumber are declared and set in the memory

Variables Values are assigned to the variables firstNumber int firstNumber,secondNumber; after B is executed firstNumber = 234; secondNumber = 87; firstNumber 234 B secondNumber 87 Values are assigned to the variables firstNumber and secondNumber

Quick Check Why are the following declarations all invalid? int a, b, a; float x, int; float w, int x; bigNumber double;

Another Java Application: Adding Integers int number1; // first number to add int number2; // second number to add sum; // sum of number1 and number2 Declares variables number1, number2, and sum of type int int can hold integer values (whole numbers): i.e., 0, -4, 97 Data types float and double can hold decimal numbers

Another Java Application: Adding Integers // add the numbers sum = number1 + number2; Assignment statement First calculates sum of number1 and number2 (right hand side) Next, uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2

Another Java Application: Adding Integers System.out.printl(“The sum is”+ sum); "The sum is " + sum Uses the operator + to "add" the string literal "The sum is" and sum Allows concatenation of a String and another data type Results in a new string If sum contains 117, then "The sum is " + sum results in the new string "The sum is 117" Note the space in "The sum is "

Arithmetic Arithmetic calculations are used in most programs Use * for multiplication and / for division, +, - No operator for exponentiation Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator % returns the remainder 7 % 5 evaluates to 2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

Arithmetic

Decision Making: Equality and Relational Operators < _ > =

Summary Java applications Variables in Java Java primitive’s data types Arithmetic operators