10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 2 Introduction to C Programming
 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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Just Enough Java 17-Apr-17.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
Condensed Java 19-Apr-17.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC Programming I Lecture 5 August 30, 2002.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
ICT Introduction to Programming Chapter 4 – Control Structures I.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
16-Jan-16 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 2. Program Construction in Java. 01 Java basics.
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.
Just Enough Java 29-Sep-17.
Chapter 2 Basic Computation
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Computer Programming Methodology Introduction to Java
Variables, Printing and if-statements
Lecture Sep-18.
Lecture Nov-18.
CIT 590 (basic Java syntax)
Lecture Nov-18.
Sudden Java 27-Nov-18.
Sudden Java 4-Dec-18.
Condensed Java 7-Dec-18.
Expressions and Assignment
Introduction to Primitives
Unit 3: Variables in Java
Just Enough Java 17-May-19.
Presentation transcript:

10-Jun-15 Just Enough Java

Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables start with a lowercase letter In multiword variables, each new word is capitalized Every variable has a type of value that it can hold For example, name might be a variable that holds a String (sequence of characters) age might be a variable that holds an integer value isMarried might be a variable that holds a boolean (true or false) value

Some Java data types In Java, the four most important primitive (simple) types are: int variables hold integer values double variables hold floating-point numbers, that is, numbers containing a decimal point boolean variables hold a true or false value char variables hold single characters Another important type is the String A String is an Object, not a primitive type A String is composed of zero or more char s

Declaring variables Every variable that you use in a program must be declared (in a declaration) The declaration specifies the type of the variable The declaration may give the variable an initial value Examples: int age; int count = 0; float distance = 37.95; boolean isReadOnly = true; String greeting = "Welcome to CIT 591"; String outputLine;

Assignment statements Values can be assigned to variables by assignment statements The syntax is: variable = expression ; The expression must be of the same type as the variable The expression may be a simple value or it may involve computation Examples: name = "Dave"; count = count + 1; area = (4.0 / 3.0) * * radius * radius; isReadOnly = false; When a variable is assigned a value, the old value is discarded and totally forgotten

Comments A comment is a note to any human reading the program; comments are ignored by Java A comment starts with // and goes to the end of the line A comment may be put after a statement (on the same line) to say something about that one statement A comment may be put on a line by itself, to say something about the following statements Example: // Swap the values of x and y temp = x; // save old value of x in temp x = y; // replace old value of x with y y = temp; // this many comments is just silly

Organization of a class A class may contain data declarations and methods (and constructors, which are like methods), but not statements A method may contain (temporary) data declarations and statements A common error: class Example { int variable ; // simple declaration is OK int anotherVariable= 5; // declaration with initialization is OK variable = 5; // statement! This is a syntax error void someMethod( ) { int yetAnotherVariable; //declaration is OK yetAnotherVariable = 5; // statement inside method is OK } }

Arithmetic expressions Arithmetic expressions may contain: + to indicate addition - to indicate subtraction * to indicate multiplication / to indicate division % to indicate remainder of a division (integers only) parentheses ( ) to indicate the order in which to do things An operation involving two int s results in an int When dividing one int by another, the fractional part of the result is thrown away: 14 / 5 gives 2 Any operation involving a float results in a float : gives 4.7

Boolean expressions Arithmetic comparisons result in a boolean value of true or false There are six comparison operators: < less than <= less than or equals > greater than >= greater than or equals == equals != not equals There are three boolean operators: && “and”--true only if both operands are true || “or”--true if either operand is true ! “not”--reverses the truth value of its one operand Example: (x > 0) && !(x > 99) “x is greater than zero and is not greater than 99”

Spaces You should put a single space around every binary operator, including comparisons and = Example: perimeter=2*(width+height); Do not put spaces just inside parentheses: perimeter = 2 * (width + height); //bad These are style rules, not Java rules If you break these rules, your program will still compile OK, but you will lose points if we notice

String concatenation You can concatenate (join together) Strings with the + operator Example: fullName = firstName + " " + lastName; In fact, you can concatenate any value with a String and that value will automatically be turned into a String Example: System.out.println("There are " + count + " apples."); Be careful, because + also still means addition int x = 3; int y = 5; System.out.println(x + y + " != " + x + y); The above prints 8 != 35 “Addition” is done left to right--use parentheses to change the order

if statements An if statement lets you choose whether or not to execute one statement, based on a boolean condition Syntax: if ( boolean_condition ) statement ; Example: if (x < 100) x = x + 1; // adds 1 to x, but only if x is less than 100 C programmers take note: The condition must be boolean An if statement may have an optional else part, to be executed if the boolean condition is false Syntax: if ( boolean_condition ) statement ; else statement ; Example: if (x >= 0 && x < limit) y = x / limit; else System.out.println("x is out of range: " + x);

Compound statements Multiple statements can be grouped into a single statement by surrounding them with braces, { } Example: if (score > 100) { score = 100; System.out.println("score has been adjusted"); } Unlike other statements, there is no semicolon after a compound statement Braces can also be used around a single statement, or no statements at all (to form an “empty” statement) It is good style to always use braces in the if part and else part of an if statement, even if the surround only a single statement Indentation and spacing should be as shown in the above example

while loops A while loop will execute the enclosed statement as long as a boolean condition remains true Syntax: while (boolean_condition) statement; Example: n = 1; while (n < 5) { System.out.println(n + " squared is " + (n * n)); n = n + 1; } Result: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 C programmers take note: The condition must be boolean Danger: If the condition never becomes false, the loop never exits, and the program never stops

Indentation and spacing if(x<10){ ___ x=x+1; }else{ ___ x=x-1; } OR: if(x<10){ ___ x=x+1; } else{ ___ x=x-1; } while(x>0){ ___ System.out.println(x); ___ x=x-1; } Recommended indentation is from 2 to 4 spaces, but must be consistent throughout the program Notice that there is not a space after println Most important style rule: If you are modifying or adding to an existing program, keep to the original style (whatever it may be)

Method calls A method call is a request to an object to do something, or to compute a value System.out.print( expression ) is a method call; you are asking the System.out object to evaluate and display the expression A method call may be used as a statement Example: System.out.print(2 * pi * radius); Some method calls return a value, and those may be used as part of an expression Example: h = Math.sqrt(a * a + b * b);

A complete program public class SquareRoots { // Prints the square roots of numbers 1 to 10 public static void main(String args[]) { int n = 1; while (n <= 10) { System.out.println(n + " " + Math.sqrt(n)); n = n + 1; } } } etc.

The End