Variables, Types, Operations on Numbers

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

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.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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;
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
August 6, 2009 Data Types, Variables, and Arrays.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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:
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Midterm preview.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
CompSci 230 S Programming Techniques
Chapter # 2 Part 2 Programs And data
Boolean Expressions and Conditionals (If Statements)
Formatted Output (printf)
Exceptions and User Input Validation
PowerPoint Presentation Authors of Exposure Java
Building Java Programs
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
First Programs CSE 1310 – Introduction to Computers and Programming
Static Variables ICS 111: Introduction to Computer Science I
IDENTIFIERS CSC 111.
Type Conversion, Constants, and the String Object
Variables ICS2O.
Chapter 2: Basic Elements of Java
Building Java Programs
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Building Java Programs
Building Java Programs Chapter 2
Variables, Types, Operations on Numbers
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Building Java Programs
Building Java Programs
elementary programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
Building Java Programs
In this class, we will cover:
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
Input and Formatted Output (printf)
Building Java Programs
Presentation transcript:

Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming University of Texas at Arlington Updated 8/28/18

Summary See the summary document as well. Variable declaration, initialization, and use. Possible syntax errors Syntax – rules for code that can compile. Types: Define what are legal values for a variable. Both variables and values have types. A variable can only have one type throughout the program. Common errors: Initialize a variable with a value of another type Use data of a ‘wrong’ type for an operator(e.g. "catapult" - "cat" ) Wrong type: operator – is not defined for strings Use data of a ‘wrong’ type as function argument Function takes arguments of a different type. ++/--, +=/-= (not recommended in exams) Type conversion: Casting: (int) Casting and: Math.round(), Math.floor() or Math.ceil() Constant variables: "final int DAYS_PER_WEEK = 7; "

Declaring a Variable A program uses variables to refer to data. You can think of a variable as a ‘box’ that holds data and has a label. You can only get to the box using the label. You create a variable, by doing a variable declaration. There are two ways to declare a variable: type variable_name; // declare only: name and type type variable_name = initial_value; // declare and initialize For example: int x; //declaration only int numberOfFingers = 5; //declare and initialize double radius = 20.231;

Declaration/Initialization before Use Java executes the code line-by-line from top to bottom. A variable must be declared before we try to use it. This code does not compile. Gives error: "cannot find symbol age". // incorrect code: Variable age is not declared before use. age = 5; System.out.println(age); A variable must be declared and initialized before we use it. This code does not compile. Gives error: " variable age might not have been initialized ". // incorrect code: Variable age is declared but not initialized. int age; System.out.println(age);

Declaration/Initialization before Use Solution 2: declare the variable in one line, and then set the value of the variable in another line. See code below: The first red line declares a variable called age2. The second red line sets the value of age2 to 5. Solution1: provide an initial value for the variable at the same line where you declare the variable. See code below The red line declares and initializes a variable called age1. // correct code version 1 int age = 5; System.out.println(age); // correct code version 2 int age; age = 5; System.out.println(age);

Using Variables What is wrong with each piece of code below? radius = 20.231; area = Math.PI * Math.pow(radius, 2); System.out.println(area); double area = Math.PI * Math.pow(radius, 2); double radius = 20.231; System.out.println(area); double radius = 20.231; double area = Math.PI * Math.pow(Radius, 2); System.out.println(area);

Using Variables Answers: radius = 20.231; area = Math.PI * Math.pow(radius, 2); System.out.println(area); double radius = 20.231; double area = Math.PI * Math.pow(radius, 2); System.out.println(area); double area = Math.PI * Math.pow(radius, 2); double radius = 20.231; System.out.println(area); double radius = 20.231; double area = Math.PI * Math.pow(radius, 2); System.out.println(area); double radius = 20.231; double area = Math.PI * Math.pow(Radius, 2); System.out.println(area); double radius = 20.231; double area = Math.PI * Math.pow(radius, 2); System.out.println(area);

Types The type defines: what are legal values for a variable of that type. What operations can be done with data of that type (either hardcoded or from a variable) Java will never allow you to set a variable to a value incompatible with the type of the variable. Both variables and values have a type.

The Five Basic Types In this course we will use numbers and text (and boolean values). They will be represented by five basic types. int double boolean String char

The Five Basic Types int double boolean String char legal values? integers, like 0, 57, -1896… double legal values? real numbers, like 3.0, 5.2, -0.23… illegal: 5,300 (comma). Use: 5300. 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 strings must be enclosed in single quotes.

Types Are NOT Interchangeable Not pay attention to types makes programming very hard and confusing. The following four values are NOT interchangeable: 2 2.0 "2" '2' Why?

Types Are NOT Interchangeable Not pay attention to types makes programming very hard and confusing. 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.

Types Are NOT Interchangeable For example: Incorrect Correct 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";

Converting double values to int values You cannot store a value of type double in a variable of type int (a ‘box’ for int). double price = 18.53; int dollars = price; // Java gives an error There are 4 ways to convert a value of type double to a value of type int. Description Examples Casting (removes the decimal part) int n = (int) 18.53; // n is 18 Round up int n = (int) Math.ceil (18.53); // n is 19 int n = (int) Math.ceil (18.22); // n is 19 Round down int n = (int) Math.floor (18.53); // n is 18 int n = (int) Math.floor (18.99); // n is 18 Round (to closest integer) int n = (int) Math.round (18.53); // n is 19 Student question: “n is already of type int. Why do I need to convert to an int?” You cannot store a value of type double in a ‘box’ for int. You are converting the value (18.53) to an int value and then it can be stored in a ‘box’ for int.

Constants Some variables should never change value. Examples: Number of days in a week. Constants from math and physics such as pi and e. Other data specific to your application (e.g. minimum number of staff at a desk during a work day) To tell Java that a variable is a constant, use the keyword final when you declare the variable. Syntax: final type variable_name = value; 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);

Using Constants – correct practice Using a constant (declared with final) has these advantages: The code is easier to read and understand It is clear that DAYS_PER_WEEK will not change throughout the program There is a meaningful name, as opposed to just the number 7. The compiler will check and report errors such as modifying DAYS_PER_WEEK. final int DAYS_PER_WEEK = 7; int weeks = 12; DAYS_PER_WEEK ++; // Java prevents it. It will give an error int days = weeks * DAYS_PER_WEEK; System.out.printf("%d weeks = %d days\n", weeks, days);

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

The += and -= operators public class example1 { public static void main(String[] args) { double x = 5.5; x += 3.2; // same as x = x+3.2; int y = 20; y -= 5; // same as y = y-5; System.out.println(x); System.out.println(y); } Output 8.7 15 The += operator adds some value to a variable. Syntax: variable_name += value; The -= operator subtracts some value from a variable. Syntax: variable_name -= value; Whether you use += and -= or not is entirely up to you. Do NOT use these in an exam (miss a symbol => wrong answer)

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;