Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012 Haidong Xue.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
August 6, 2009 Data Types, Variables, and Arrays.
Mathematical Calculations in Java Mrs. C. Furman.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Boolean Expressions and Conditionals (If Statements)
Formatted Output (printf)
Exceptions and User Input Validation
Building Java Programs
Lecture 2: Operations and Data Types
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
First Programs CSE 1310 – Introduction to Computers and Programming
IDENTIFIERS CSC 111.
Building Java Programs
Building Java Programs
Building Java Programs
Variables, Types, Operations on Numbers
Building Java Programs Chapter 2
Variables, Types, Operations on Numbers
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
First Programs CSE 1310 – Introduction to Computers and Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Declaring a Variable At any point, you can create a variable, by doing a variable declaration. There are two ways to declare a variable: you can simply declare the type and name, or you can also provide an initial value. type variable_name; type variable_name = initial_value; For example: int x = 123; int number_of_fingers = 5; double radius = ; 2

Declaration/Initialization before Use A variable must be declared before we try to use it. The code below illustrates a common mistake. – Variable var is not declared anywhere. Java will refuse to run this code, complaining that it "cannot find symbol var". 3 public class example1 // incorrect code { public static void main(String[] args) { var = 5; System.out.println(var); }

Declaration/Initialization before Use A variable must be declared and initialized before we try to use it. The code below illustrates a common mistake. – Variable var is declared but not initialized. Java will refuse to run this code, complaining that "variable var might not have been initialized". 4 public class example1 // incorrect code { public static void main(String[] args) { int var; System.out.println(var); }

Declaration/Initialization before Use One way to fix such problems is to provide an initial value for the variable at the same line where you declare the variable. The code below shows an example of doing that. – The line shown in red declares and initializes a variable called var. 5 public class example1 // correct code { public static void main(String[] args) { int var = 5; System.out.println(var); }

Declaration/Initialization before Use Another way is to first declare the variable in one line, and then set the value of the variable in another line. The code below shows an example of doing that. – The first line shown in red declares a variable called var. – The second line shown in red sets the value of var to 5. 6 public class example1 // correct code { public static void main(String[] args) { int var; var = 5; System.out.println(var); }

Using Variables 7 What is wrong with this code? public class hello1 // incorrect code { public static void main(String[] args) { radius = ; area = Math.PI * Math.pow(radius, 2); System.out.println(area); }

Using Variables 8 What is wrong with this code? – Variables "radius" and "area" are not declared. public class hello1 // incorrect code { public static void main(String[] args) { radius = ; area = Math.PI * Math.pow(radius, 2); System.out.println(area); }

Using Variables 9 Corrected version. public class hello1 // correct code { public static void main(String[] args) { double radius = ; double area = Math.PI * Math.pow(radius, 2); System.out.println(area); }

Using Variables 10 What is wrong with this code? public class hello1 // incorrect code { public static void main(String[] args) { double area = Math.PI * Math.pow(radius, 2); double radius = ; System.out.println(area); }

Using Variables 11 What is wrong with this code? – Variable "radius" is used before it has been declared. public class hello1 // incorrect code { public static void main(String[] args) { double area = Math.PI * Math.pow(radius, 2); double radius = ; System.out.println(area); }

Using Variables 12 Corrected version. public class hello1 // correct code { public static void main(String[] args) { double radius = ; double area = Math.PI * Math.pow(radius, 2); System.out.println(area); }

Using Variables 13 What is wrong with this code? public class hello1 // incorrect code { public static void main(String[] args) { double radius = ; double area = Math.PI * Math.pow(Radius, 2); System.out.println(area); }

Using Variables 14 What is wrong with this code? – Variable "radius" is misspelled in the line where the area is computed. public class hello1 // incorrect code { public static void main(String[] args) { double radius = ; double area = Math.PI * Math.pow(Radius, 2); System.out.println(area); }

Using Variables 15 Corrected version. public class hello1 // correct code { public static void main(String[] args) { double radius = ; double area = Math.PI * Math.pow(radius, 2); System.out.println(area); }

Using Variables 16 What is wrong with this code? public class example1 // incorrect code { public static void main(String[] args) { int x = 5; int x = 3 * 5; System.out.println(x); }

Using Variables 17 What is wrong with this code? – Variable x is being declared twice. public class example1 // incorrect code { public static void main(String[] args) { int x = 5; int x = 3 * 5; System.out.println(x); }

Using Variables 18 Corrected version. public class example1 // correct code { public static void main(String[] args) { int x = 5; x = 3 * 5; System.out.println(x); }

Types To declare a variable, you must specify the type of that variable. type variable_name; type variable_name = initial_value; The type of a variable defines what are legal values for that variable. – Java will never allow you to set a variable to a value incompatible with the type of the variable. 19

The Five Basic Types In this course, our main goal is to learn how to write programs that do basic processing of data. – The only data we care about in this course are numbers and text. To work with data and text, we will use five basic types. int double boolean String char 20

The Five Basic Types int – legal values? integers, like 0, 57, -1896… double – legal values? real numbers, like 3.0, 5.2, -0.23… boolean – legal values? only two: true and false. String – legal values? text, like "hello", "a cat jumped on the table", … – NOTE: text for strings must be enclosed in double quotes. char – legal values? singe characters, like 'c', '3', 'A', '#', … – NOTE: text for chars must be enclosed in single quotes. 21

Types Are NOT Interchangeable A common mistake for beginners in programming is to not pay attention to types. – Only beginners make this mistake. – You will not make it past beginner stage as long as you make this mistake. The following four values are NOT interchangeable: "2" '2' Why? 22

Types Are NOT Interchangeable A common mistake for beginners in programming is to not pay attention to types. – Only beginners make this mistake. – You will not make it past beginner stage as long as you make this mistake. The following four values are NOT interchangeable: 2 this is an int 2.0 this is a double "2" this is a string '2' this is a character Why? Because they are different types. 23

Types Are NOT Interchangeable For example: – If you write "2.5" when you should be writing 2.5, your code will not work. – If you write 5 when you should be writing '5', your code will not work. – If you write 2 when you should be writing 2.0, your code will not work. – If you write "true" when you should be writing true, your code will not work. 24

Types Are NOT Interchangeable For example: 25 IncorrectCorrect String a1 = 2.5;String a1 = "2.5"; double a2 = "2.5";double a2 = 2.5; int num = '5';int num = 5; char c1 = 5;char c1 = '5'; String str = '5';String str = "5"; int my_int = 2.0;int my_int = 2; boolean v = "true";boolean v = true; String v = true;String v = "true";

The ++ and -- Operators The ++ operator increments the value of a variable by 1. Syntax: variable_name++; The -- operator increments the value of a variable by 1. Syntax: variable_name--; 26 public class example1 { public static void main(String[] args) { double x = 5.5; x++; System.out.println(x); int y = 4; y--; System.out.println(y); } Output 6.5 3

The ++ and -- Operators The following two lines do the EXACT SAME THING: variable_name++; variable_name = variable_name + 1; The following two lines do the EXACT SAME THING: variable_name--; variable_name = variable_name - 1; 27 public class example1 { public static void main(String[] args) { double x = 5.5; x++; System.out.println(x); int y = 4; y--; System.out.println(y); } Output 6.5 3

The ++ and -- Operators An alternative version of the previous program, without using ++ and --. Whether you use ++ and -- or not is entirely up to you. However, you should understand what they do when you see them in code. 28 public class example1 { public static void main(String[] args) { double x = 5.5; x = x+1; System.out.println(x); int y = 4; y = y-1; System.out.println(y); } Output 6.5 3

The += and -= operators The += operator adds some value to a variable. Syntax: variable_name += value; The -= operator subtracts some value from a variable. Syntax: variable_name -= value; 29 public class example1 { public static void main(String[] args) { double x = 5.5; x += 3.2; int y = 20; y -= 5; System.out.println(x); System.out.println(y); } Output

The += and -= operators The following two lines do the EXACT SAME THING: variable_name += value; variable_name = variable_name + value; The following two lines do the EXACT SAME THING: variable_name -= value; variable_name = variable_name - value; 30 public class example1 { public static void main(String[] args) { double x = 5.5; x += 3.2; int y = 20; y -= 5; System.out.println(x); System.out.println(y); } Output

The += and -= operators An alternative version of the previous program, without using += and -=. Whether you use += and -= or not is entirely up to you. However, you should understand what they do when you see them in code. 31 public class example1 { public static void main(String[] args) { double x = 5.5; x = x + 3.2; int y = 20; y = y - 5; System.out.println(x); System.out.println(y); } Output

Multiple Ways to Add/Subtract 1 If we want to add 1 to x, in how many ways can we do it? If we want to subtract 1 from x, in how many ways can we do it? 32

Multiple Ways to Add/Subtract 1 If we want to add 1 to x, in how many ways can we do it? x++; x += 1; x = x+1; If we want to subtract 1 from x, in how many ways can we do it? x--; x -= 1; x = x-1; 33

Converting Doubles to Ints The above code gives an error: 34 public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = price; System.out.printf("Rounded price: %d dollars", dollars); }

Converting Doubles to Ints The above code gives an error: – Java does not allow assigning a double value to an int variable. There are several ways to get around that. 35 public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = price; System.out.printf("Rounded price: %d dollars", dollars); }

Converting Doubles to Ints First approach: casting. – Putting (int) in front of the double value asks Java to convert that value to an integer. – Casting simply removes the decimal part. – (int) evaluates to 18. – (int) evaluates to public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = (int) price; System.out.printf("Rounded price: %d dollars", dollars); }

Converting Doubles to Ints Second approach: rounding. – Math.round(number) rounds number to the closest integer. – We still need to put (int), to convert the result of Math.round into an integer. – (int) Math.round(18.53) evaluates to 19. – (int) Math.round(-18.53) evaluates to public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = (int) Math.round(price); System.out.printf("Rounded price: %d dollars", dollars); }

Converting Doubles to Ints Third approach: rounding down (taking the floor). – Math.floor(number) rounds number down to an integer. – We still need to put (int), to convert the result of Math.floor into an integer. – (int) Math.floor(18.53) evaluates to 18. – (int) Math.floor(-18.53) evaluates to public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = (int) Math.floor(price); System.out.printf("Rounded price: %d dollars", dollars); }

Converting Doubles to Ints Fourth approach: rounding up (taking the ceiling). – Math.ceil(number) rounds number up to an integer. – We still need to put (int), to convert the result of Math.ceil into an integer. – (int) Math.ceil(18.53) evaluates to 19. – (int) Math.ceil(-18.53) evaluates to public class example1 { public static void main(String[] args) { double price = 18.53; int dollars = (int) Math.ceil(price); System.out.printf("Rounded price: %d dollars", dollars); }

Constant Variables Some variables should never change value. Examples: – Number of days in a week. – Mathematical constants such as pi, e. – Physics constants like Newton's constant for gravity. 40 public class example1 { public static void main(String[] args) { int weeks = 12; final int days_per_week = 7; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Constant Variables If you want to tell Java that a variable is a constant, you use the final keyword when you declare the variable. Syntax: final type variable_name = value; 41 public class example1 { public static void main(String[] args) { int weeks = 12; final int days_per_week = 7; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Constant Variables What changes in the behavior of this program if we remove the final keyword? 42 public class example1 { public static void main(String[] args) { int weeks = 12; int days_per_week = 7; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Constant Variables What changes in the behavior of this program if we remove the final keyword? Nothing. Then, why should we ever use it? 43 public class example1 { public static void main(String[] args) { int weeks = 12; int days_per_week = 7; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Constant Variables What changes in the behavior of this program if we remove the final keyword? Nothing. Then, why should we ever use it? It is good programming practice: it makes code easier to read and understand, and prevents human errors. 44 public class example1 { public static void main(String[] args) { int weeks = 12; final int days_per_week = 7; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Example Where final Is Useful What do you think will happen with this code? 45 public class example1 { public static void main(String[] args) { int days_per_week = 7; int weeks = 12; days_per_week++; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Example Where final Is Useful What do you think will happen with this code? Java will run it, and it will give the wrong answer (12 weeks have 96 days). – The days_per_week++ should not have happened, probably the programmer put it there by accident. – However, Java cannot possibly know that it was a mistake. 46 public class example1 { public static void main(String[] args) { int days_per_week = 7; int weeks = 12; days_per_week++; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Example Where final Is Useful What do you think will happen with this code? 47 public class example1 { public static void main(String[] args) { final int days_per_week = 7; int weeks = 12; days_per_week++; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Example Where final Is Useful What do you think will happen with this code? Java will refuse to run it, will give an error: – A constant variable is not allowed to change. By declaring a variable as final, you tell Java that if you ever try to change it, you are probably making a mistake and it should not allow you. 48 public class example1 { public static void main(String[] args) { final int days_per_week = 7; int weeks = 12; days_per_week++; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }

Example Where final Is Useful You will see more examples of this as you learn programming: – Programming languages give us some tools, so that we do not allow ourselves to make mistakes. – The ability to declare a variable as a constant is such a tool. – This way, at least some mistakes are easily caught and fixed. 49 public class example1 { public static void main(String[] args) { final int days_per_week = 7; int weeks = 12; days_per_week++; int days = weeks * days_per_week; System.out.printf("%d weeks = %d days\n", weeks, days); }