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;

Slides:



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

 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.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
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
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Chapter 2 Basic Computation
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
Primitive Data, Variables, Loops (Maybe)
Chapter 2 Basic Computation
Escape Sequences What if we wanted to print the quote character?
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Fundamentals 2.
Building Java Programs Chapter 2
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

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; declare and assign values to variables; create constant values with the keyword final; join messages and values in output commands by using the concatenation (+) operator; use the input methods of the Scanner class to get data from the keyboard; design the functionality of a method using pseudocode.

2 public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello World Your first program public class Hello { } public static void main(String[] args) { } System.out.println("Hello world");

3 Adding comments to a program // this is a short comment, so we use the first method public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } /* this is the second method of including comments – it is more convenient to use this method here, because the comment is longer and goes over more than one line */ }

4 Simple data types in Java Interesting programs will have to store data in order to give interesting results; Types of value used within a program are referred to as data types; Price : for example £4.75 Total sold: for example 187 in Java the simple types are referred to as the scalar types or the primitive types A real number An integer

5 The scalar types of Java Java typeAllows forRange of values byte very small integers-128 to 127 short small integers to int big integers to long very big integers to float real numbers +/- 1.4 * to 3.4 * double very big real numbers +/- 4.9 * to 1.8 * char charactersUnicode character set boolean true or falsenot applicable

6 Variables A variable is a name given to a piece of data in your program You can choose almost any name for your variables as long as the name does not have spaces in it the name is not already used in Java (such as ‘class’ or ‘static’) ticket   cinema ticket cinemaTicket cinema_ticket void

7 Declaring variables in Java A variable is declared as follows dataType variableName ; For example to declare a variable suitable for holding the score in a computer game: int score;

8 The effect of declaring a variable on the computer's memory int score ; Computer MemoryJava Instruction score

9 Declaring many variables You may declare as many variables as you need int score; char level; Several variables can be declared on a single line if they are all of the same type: int score, hits; // two variables declared at once char level ; // this has to be declared separately

10 The effect of declaring many variables in Java int score, hits; char level ; Java InstructionsComputer Memory hits score level

11 Assignments in Java assignments allow values to be put into variables; variableName = value; 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: score 0

12 Assignments: some examples score = 0; Can combine the assignment statement with a variable declaration to put an initial value into a variable: int score = 0; this is equivalent to: int score; score = 0;

13 Spot the error! int score = 2.5 ; A real number cannot be placed into an integer variable!

14 Assigning a character to a variable char level = 'A'; when assigning a value to a character variable, you must enclose the value in single quotes:

15 Creating constants there are occasions where data items in a program have values that do not change EXAMPLES the maximum score in an exam (100); the number of hours in a day (24); the mathematical value of  ( ). constants are declared much like variables in Java except they are preceded by the keyword final, they are always initialized to their fixed value. final int HOURS = 24;

16 The arithmetic operators of Java OperationJava operator addition+ subtraction- multiplication* division/ remainder%

17 Carrying out calculations int x; x = ; At the end of these instructions ‘x’ holds the number 35 terms on the right-hand side of assignment operators are referred to as expressions.

18 Calculations: another example Cost = 500 Sales tax = 17.5% double cost; cost = 500 * ( /100);

19 Examples of the modulus operator in Java ExpressionValue % 9 6 % 8 40 % % 2

20 Modulus: An example 4 chairs per table30 people How many tables of four are required, and how many people will be left over? int tablesOfFour, peopleLeftOver; tablesOfFour = 30/4; peopleLeftOver = 30%4; 7 2 ?

21 Expressions in Java The expression on the right-hand side of an assignment statement can itself contain variable names; AN EXAMPLE: revisiting the cost of a product double price, tax, cost; price = 500; tax = 17.5; cost = price * (1 + tax/100);

22 Using variables on both sides of the assignment double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100); This will store original and final price The new value of ‘price’ The old value of ‘price’

23 A special shorthand x = x + 1;x++; increment (add one to an integer) decrement (take one off an integer) x = x - 1; x--;

24 A complete program RUN public class FindCost { } public static void main (String [] args) { } double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100);

25 Output in Java To display a message in Java: System.out.println(message to be printed on screen); For example System.out.println("Hello world"); Hello world _ System.out.print ("Hello world"); cursor on next line Hello world _ cursor on same line

26 Examples of ‘println’ and ‘print’ public class Hello { public static void main(String[] args) { System.out.println("Hello world"); System.out.println("Hello world again!"); } } Hello world Hello world again! RUN System.out.print("Hello world"); Hello worldHello world again!

27 More output examples System.out.print(10*10); 100 System.out.print("cost = " + (30*7.5) ); cost = expressions calculated and displayed Expressions and Strings joined by the concatenation operator ‘+’ A collection of characters are called Strings in Java, they are always enclosed in quotes

28 Modifying the FindCost program public class FindCost2 { public static void main(String[] args) { double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100); System.out.println("*** Product Price Check ***"); System.out.println("Cost after tax = " + price); } } RUN *** Product Price Check *** Cost after tax = 587.5

29 Keyboard input The Scanner class has recently been added into Java to simplify keyboard input score 35

30 Input in Java: the Scanner class In order to have access to the Scanner class you have to place the following line at the beginning of your program: import java.util.*; You will need to write the following instruction in your program. Scanner sc = new Scanner(System.in); This instruction creates a Scanner object, ‘sc’ that can be used to input values from the keyboard

31 Input methods of the Scanner class if we want a user to type in an integer at the keyboard, into the variable ‘x’: x = sc.nextInt(); in the case of a double, y: y = sc.nextDouble(); in the case of a char, c: c = sc.next().charAt(0);

32 Revisiting the FindCost program import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); } } RUN *** Product Price Check *** Enter initial price: _ 1000 Enter tax rate: _12.5 Cost after tax =

33 Re-running the FindCost program import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); } RUN *** Product Price Check *** Enter initial price: _ 50 Enter tax rate: _17.5 Cost after tax = 58.75

34 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 classes in week 6); You can declare a String in a similar way to variables of type int or char; String name; notice that String "type" has to start with a capital "S". Java allows you to use the normal assignment operator ( = ) with strings: name = “Dula"; To obtain a string from the keyboard you can use the next method of Scanner Your string should NOT contain spaces

35 Strings: A sample program import java.util.*; public class StringTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name; int age; System.out.print("What is your name? "); name = sc.next(); System.out.print("What is your age? "); age = sc.nextInt(); System.out.println(); System.out.println("Hello " + name); System.out.println ("When I was your age I was " +(age +1)); } } RUN What is your name? _Aaron What is your age? _15 Hello Aaron When I was your age I was 16

36 Program design Programmer thinking about how to build the software public class FindCost3 { public static void main(String[] args ) { Scanner …… } } Sketching out the solution as a program design Implementing the design by writing the code 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.

37 Pseudocode for the FindCost program BEGIN DISPLAY program title DISPLAY prompt for price ENTER price DISPLAY prompt for tax ENTER tax SET price TO price * (1 + tax/100) DISPLAY new price END