Other types of variables

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

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, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 2 Variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Welcome to AP Computer Science A We use the Java language, but this class is much more rigorous than Intro to Java More programming, but also more theory.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: / 2  this.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Arithmetic Expressions
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Chapter 2 More on Math More on Input
PowerPoint Presentation Authors of Exposure Java
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Variables, Expressions, and IO
Computer Science 3 Hobart College
Chapter 3 Assignment Statement
Introduction to C++ October 2, 2017.
Type Conversion, Constants, and the String Object
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Chapter 2 Basic Computation
Math in C The math blocks you've used in Scratch can all be recreated in C!
OPERATORS (1) CSC 111.
Examples of Primitive Values
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 - Introduction to Java Applications
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Fundamentals 2.
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Module 4 Loops.
Variables & Data Types Java.
Introduction to Java, and DrJava
Algorithms computer as the tool process – algorithm
Data Types and Expressions
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Java: Variables, Input and Arrays
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Arithmetic Expressions & Data Conversions
Python Creating a calculator.
Building Java Programs
Presentation transcript:

Other types of variables double (also known as “floating point number”) allows decimals much more useful than integers, but takes up more memory char A single character You must put the character in single quotes, if you are giving a char value in your code example: char myGrade = ‘ ’; String Words, phrases, sentences Capitalize String Use double quotes example: String firstName= “”; Demo

Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: int x = 0; x = (4 + 36 / 2); x now equals 22, not 20.

Input If you want a user of your program to input information, you must create an object of the Scanner class. You also must have an import statement so that you can access the Scanner class. Demo

char ltr = ‘ ‘; ltr = ‘b’; System. out char ltr = ‘ ‘; ltr = ‘b’; System.out.println(ltr + ltr); What would this display?

Type Casting Let’s say you have a double that you need to convert into an integer This can be accomplished by type casting: int x = 0; double y = 3.7; x = (int)y; // the (int) is where the type casting occurs What does x equal now? also valid: x = (int)(y); incorrect: x = int(y);

Integer vs. Double division Guess the result of the following: int x = 5; int y = 2; int z = x/y; Dividing with integers: the decimal part of the answer is truncated (erased). To be accurate, use at least one double, or one decimal in your division.

int x = 5; int y = 2; double z = x/y; what does z equal now? 2.0 ***It doesn’t matter that the “answer,” z, is a double. The sequence of how the code is executed is important here. Java does the division FIRST, then assigns the result to the variable z. ***Since 5/2 does equal 2, Java assigns 2 to the variable z.

int x = 5; double y = 2; double z = x/y; what does z equal now? 2.5

int x = 5; int y = 2; double z = (double)(x/y); what does z equal now? 2.0 why? because x/y = 2, then 2 is type-casted (converted) into a double.

int x = 5; int y = 2; double z = (double)x/y; what does z equal now? 2.5 why? Notice the slight change in parentheses from the previous slide. First, x is type-casted from 5 to 5.0. Next, 5.0/2 = 2.5. Remember, as long as one of the two numbers being divided is a double, then the result is a double.

Variables do not have to be used in order to work with doubles and integers. For example: System.out.println(9/4); (this would display 2) System.out.println(9.0/4); (this would display 2.25) System.out.println(9/4.0); (this would display 2.25) System.out.println(9.0/4.0); (this would display 2.25) Notice that as long as one of the two numbers being divided is a double, then the answer will be a double.

Modulus Division Using the modulus symbol, %, determines the remainder 10 % 6 = 4 13 % 5 = ? = 3 283 % 100 = ? = 83 26 % 2 = ? = 0 27 % 2 = ? = 1 (any even #) % 2 = ? (any odd #) % 2 = ? 3 % 8 = 3

Assignment #1 (InputNumbers) Ask the user to enter 2 numbers and 1 letter. Display the letter 3 times on the same line, like this: “QQQ” Display the sum, difference, product, and quotient of the two numbers. Display the first number, squared. Display the second number, cubed. All of the results should look like the following, for example: “The sum of 6 and 8 is 14.”

Assignments (files should be named using this format: P117num4, P117num5, etc) P. 117-118: #4 (Ignore “Create… application to”; look up formula online) #5 (remember: “floating point value” is another name for a double) #6 #7 (hints: use both modulus division and integer division; how many seconds are in an hour?) #11 (start by asking user for # of gallons used, starting odometer #, and ending odometer #) #12 (ask the user: how many of each coin?)