Unit 3: Variables in Java

Slides:



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

Introduction to C Programming
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
Introduction to C Programming
String Escape Sequences
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2: Java Fundamentals
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
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.
Primitive Data Types. Identifiers What word does it sound like?
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Chapter 2 Introduction to C++ Programming
Identifiers - symbolic names
BASIC ELEMENTS OF A COMPUTER PROGRAM
Introduction to Computer Science / Procedural – 67130
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Computing Fundamentals
Multiple variables can be created in one declaration
Variables, Expressions, and IO
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Chapter 2.
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Building Java Programs
MSIS 655 Advanced Business Applications Programming
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
elementary programming
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Variables.
Just Enough Java 17-May-19.
Chapter 2 Primitive Data Types and Operations
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Unit 3: Variables in Java Using numbers and Math in Java and Alice

What is a Variable? Data is constant when it can’t be changed while the program is running. It is variable if it might change. Variables are locations in memory where values are stored. Example: If we wanted a program to make a paycheck for a bunch of employees, it must be flexible enough to do it for each employee’s pay. Instead of using a specific number, we would use a variable such as grossPay. Variables must have a type, name, and a value.

Naming Variables Variable names may consist of letters, digits, underscores (_) or $. No spaces allowed! The first character cannot be a digit. Begin with a lowercase letter. If there are multiple words, the other words should each be capitalized. (This is a style standard. Java will accept variables with any capitalization.) Names should be meaningful. Case sensitive, so that number, Number, and NUMBER are all different variables to the computer.

Variable Types Type Size Range Use byte (integer) 8 bits -128 to 127 Infrequent short (integer) 16 bits -32768 to 32767 int (integer) 32 bits -2,147,483,648 to 2,147,483,647 Frequent long (integer) 64 bits -9223372036854775808 to 9223372036854775807 float (floating point) 32 bits single precision -3.4028235E+38 to +3.4028235E+38 (7 digits precision) Double (floating point) 64 bits double precision -1.7976931348623157E+308 to +1.7976931348623157E+308 (16 digits precision) char 16 bits unsigned One character using Unicode system Y/N, etc. Boolean True or false logic

Declaring and initializing variables Variables must be declared with a type, usually at the beginning of a section of code. Ex: int number; sets up integer called “number” but doesn’t give it a value to start. You can declare a variable and give it a starting value in one step. Ex: int number = 15; declares “number” and sets it to 15 to start. You can declare multiple variables by separating them with commas. Ex: int x, y, z; sets up 3 integer variables called x, y, and z.

Declaring and initializing variables You can declare and initialize multiple variables in one statement. Ex: int age = 30, hours = 45, sum = 0; But, it’s best to declare them on separate lines with comments so that you can add comments. Ex: int age = 30; //age is the employee’s age int hours = 45; //hours is the employee’s weekly hours worked int sum = 0; //sum is the total of all hours worked

Declaring and initializing variables If you try to assign a number with a decimal point to an integer, you will get an error! Ex: int z = 18.5; //ERROR! A Boolean variable should be initialized as true or false Ex: Boolean x = true; //assigns the value true to the variable x. A char variable can be assigned a value expressed by a single character surrounded by single quotes Ex: char myLetter = ‘A’; //assigns the character A to variable myLetter.

String variables If you need to represent a series of characters instead of just one, you can use a string. Useful for displaying messages, inputting text, etc. May include letters, digits, and special characters. Strings are considered objects in Java (not really variables) Declared as a sequence of characters within double quotes. Ex: String message = “Go Trojans!!!”; declares and assigns the string.

String Concatenation The + operator, when used with strings and other objects, creates a single string that contains the concatenation (combination). Does NOT mean to do arithmetic! Ex: System.out.println(“The total is “ + 25 + 10); would print The total is 2510 because it concatenates the 25 and 10. If you want it to do arithmetic, put parentheses around the (25+10). To include a variable as part of a string, use the + operator. Ex: double total = 50.95; System.out.println (“The total is “ + total); These lines would tell the computer to print: The total is 50.95

Java Basics A statement is the simplest thing you can do in Java. Each statement generally ends with a semicolon (;) A block of statements are surrounded by curly braces { }. Ex: int i; //declares a variable i to be an integer i = a + 1; //this is an arithmetic statement System.out.println(“Hello World”); //this is a print statement

Print Statements Printing in Java is done with two statements: System.out.println goes to a new line before printing System.out.print remains on the same line You can make the computer tab over spaces and displays backslashes etc. \n Newline \t Tab \b Backspace \\ Displays a Backslash \” Displays a Double Quote Ex: System.out.println(“I am going to earn an \”A\” in this course.”); Will print: I am going to earn an “A” in this course.

Arithmetic Statements Generally follow PEMDAS order of operations. New operation – modulus (%) – is the remainder from dividing. In terms of PEMDAS it is done with multiplication and division. Used for programs such as counting out change (when you figure out how many quarters you need to know how many more cents will have to be given in dimes, nickels, etc.) Ex: x = 5%3 would store 2 in the x variable.

Exercise #1 – Converting Fahrenheit to Celsius with Java Get into JCreator. Open a New File with the name Celsius and save it in a Unit 3 folder. Add some comments about the program at the beginning. Use /* at the beginning and another / at the end. The program will convert from Fahrenheit to Celsius. Make sure to include your name and date. We will add statements inside of the main method to tell the computer what to do. We want to add two variables that we will declare as doubles so that they can have decimal points. double fahrenheit=52.0; double celsius;

Exercise #1 – Converting Fahrenheit to Celsius with Java 4. Next we want the computer to calculate the value of celsius based on the Fahrenheit temperature. The mathematical formula is 𝐶= 5 9 (𝐹−32). celsius = 5/9 * (fahrenheit–32); 5. Finally we need to computer to print the calculation. System.out.println(“Fahrenheit: “+ fahrenheit); System.out.println(“Celsius: “+ celsius);

Exercise #1 – Converting Fahrenheit to Celsius with Java Compile the program (in the build menu) and correct any errors. Execute the program. Does it work? NO! The problem is with the 5/9 part of the arithmetic. Since 5 and 9 are both integers, the computer is dividing them and using just the integer part, which is 0. In order to fix the problem, change the 5 to 5.0 or the 9 to 9.0. Compile and execute the new version. It should work now! Try changing the value in the Fahrenheit variable to a number of your choice. Run the program. Do you get the right output?

Exercise #2 – Choose Your Own Computation Next Steps: Write your own program that will use a formula to compute something. You could think about the quadratic formula, distance formula, slope formula, etc. For now, you will need to set the inputs in the program. Later we will learn how to make the program ask for the inputs from the user.