Introduction to Computer Science / Procedural – 67130

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

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.
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.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CSC Programming I Lecture 5 August 30, 2002.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Mathematical Calculations in Java Mrs. C. Furman.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106A, Lecture 4 Introduction to Java
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Basic Computation
Building Java Programs
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Lecture Set 4 Data Types and Variables
Chapter 2 Basic Computation
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
Type Conversion, Constants, and the String Object
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Building Java Programs
Fundamentals 2.
Building Java Programs Chapter 2
Expressions and Assignment
elementary programming
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
In this class, we will cover:
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Review of Previous Lesson
Variables in C Topics Naming Variables Declaring Variables
Building Java Programs
Building Java Programs
Presentation transcript:

Introduction to Computer Science / Procedural – 67130 Unit 2 Values and Variables I/O Procedures and Arguments Assignments and Expressions

Variables, Input, Output In Java (as in virtually all programming languages), variables are essential Variables are used for storing values Each variable has a type Input means getting information from the outside world Output means providing information to the outside world

Storage: The Issues When during program execution is storage set aside? How will it be named and referred to? What restrictions are there on the values that can be stored? Where in the program can stored values be used? Variable Declaration (setting aside storage) and Variable Assignment (actually storing values)

Using Values: The Issues What operations (addition, subtraction, etc.) should there be? In what order should operations be carried out? What restrictions should there be on operations? Numbers can be added; can letters be added?

Style: The Issues When should we use values, and when should we use names in their place? What names are going to be clearest? How should the program be laid out and commented? For example, the ability to use constants can have a large impact on programs’ readability.

Another Program (this one takes input) We want a program that will do the following: Please type the temperature (deg C): 20 20 deg C is 68.0 deg F

Input/Output in Java import intro2cs.utils.*; import java.io.*; public class Temperature { public static void main (String[ ] args) { int temperature; // The Celsius temperature System.out.print("Please type the temperature (deg C):"); temperature = SimpleInput.in.readInt(); System.out.print(temperature); System.out.print(" deg C is "); System.out.print(((9.0 * temperature)/5.0 ) + 32.0); System.out.println(" deg F"); } }

WARNING! When you write your programs, you are to follow the course guidelines regarding style These guidelines are important; they help groups of programmers communicate with one another Do not assume that what appears in the slides is written according to course guidelines. The slides have different constraints!

Variables temperature is a variable public class Temperature { public static void main (String[ ] args) { int temperature; // The Celsius temperature temperature is a variable The declaration of the variable int temperature; saves a space for a value to be stored later, but doesn’t store anything there The assignment to the variable temperature =SimpleInput.in.readInt(); actually puts something in the space that was created by the declaration

Variable Declaration temperature public class Temperature { public static void main (String[ ] args) { int temperature; // The Celsius temperature temperature You don’t know what’s in there; don’t use temperature yet

Simple Variable Assignment temperature = 36; 36 36 temperature Puts the integer 36 into temperature; now you can use temperature

You Can Combine the Two Steps class Temperature { public static void main (String[ ] args) { int temperature = 36; // The Celsius temperature 36 temperature

What the Program Looked Like – Getting an Integer from the User import intro2cs.utils.*; import java.io.*; public class Temperature { public static void main (String[ ] args) { int temperature; // The Celsius temperature System.out.print("Please type the temperature (deg C):"); temperature = SimpleInput.in.readInt(); System.out.print(temperature); System.out.print(" deg C is "); System.out.print(((9.0 * temperature)/5.0 ) + 32.0); System.out.println(" deg F"); } }

Variable Assignment – Getting an Integer from the User temperature = SimpleInput.in.readInt( ); 20 SimpleInput.in.readInt( ) 20 20 temperature Use SimpleInput.in.readInt( ) to get an integer from the user, which is then placed in temperature

Special “Variables” that Remain Constant We can define a variable (a “symbolic constant”) whose value will never change, by writing final int RETIREMENT_AGE = 70; This carries out assignment at the same time as declaration (as we can do with any variable) Now RETIREMENT_AGE cannot be legally changed in the program

Data Types and Expressions Different kinds of information Each kind is a “type” Java has many data types Each type has a name Each type has a set of literals Each type has a set of operations Data types in Java can be simple types or object types For now we are only concerned with simple types

Different Simple Types in Java int represents integers literals like 3 and 132 operators like + , - , * , / double represents real numbers literals like 3.0, 3.14159 and 2.997925e8 The integer 3 and the double 3.0 are represented differently inside the computer

More Simple Types in Java boolean Represents true and false char Represents individual characters that are typed at the keyboard

Expressions An Expression represents a value that can be used in Java statements Made out of literals, variables, symbolic constants, and operations Every expression also has a type 3 + 4 + temperature (has type int) (3.0 / 4.0)* RETIREMENT_AGE (has type double) Use parentheses to establish the order of operations (Can also be returned by a procedure, like SimpleInput.in.readInt( ) )

Type of an Expression No matter how simple or complicated an expression is, it always has a Java type, just like a variable.

Where’s the Expression Go? An expression produces a value, which is then often used in an assignment statement So typically, the expression is on the right hand side of the assignment statement: temperature = 36; temperature = (x / 7) + y; temperature = SimpleInput.in.readInt( );

The Order of Assignment In general, the computer evaluates the expression on the right side, and places that value in the variable on the left side, replacing the old value. So: temperature = temperature + 10; is completely legal; it adds 10 to the value of temperature Can also be written: temperature += 10;

Increment/Decrement Two special cases: incrementing and decrementing cent++; is the same as cent = cent + 1; cent--; is the same as cent = cent - 1;

One of Your Own Kind, Stick to Your Own Kind Java is very strict about data types and assignments Java only puts into a variable a value of the appropriate type Expressions having an integer value can go into an integer variable, etc. You can’t do this: int i; double x = 3.0; i = 10.3 * x;

Special Case You can assign an integer expression to a double variable The integer is converted to a double without loss of information, so it is done automatically You can do this: int i = 3; double x; x = 10 * i;

Converting Values from One Type to Another The cast operation lets you explicitly convert one type to another You can do this: int i; double x = 3.0; i = (int) (10.3 * x); The cast (int) takes the expression that follows it and converts it into an integer by removing any fractional part; i is assigned the integer value 30

The String Type This is an important special type that we are given as part of the Java system Can be used as follows: String t1 = “To be ”, t2 = “or not to be.”; System.out.print(t1 + t2); prints out: To be or not to be. String is actually different from int, boolean, etc., but we won’t find out why until later…

Fun with Strings and + System.out.print("4" + "5"); //prints: 45 System.out.print("4" + 5); //prints: 45 System.out.print(4 + 5); //prints: 9 System.out.print("4" + "005"); //prints: 4005 System.out.print("4" + 005); //prints: 45 System.out.print("4" * 5); //error! An integer concatenated to a string is automatically converted to a string

Perceiving Structure, Again import intro2cs.utils.*; import java.io.*; public class Temperature { public static void main (String[ ] args) { int temperature; // The Celsius temperature System.out.print("Please type the temperature (deg C):”); temperature = SimpleInput.in.readInt(); System.out.print(temperature); System.out.print(" deg C is "); System.out.print(((9.0 * temperature)/5.0 ) + 32.0); System.out.println(" deg F"); } }

Input and Output Procedures and Arguments Output procedures: System.out.print( ) and System.out.println( ) Input procedures, written as: temperature = SimpleInput.in.readInt(); There are a lot of details in using these procedures. We are not going to go over all of them in class.

Unit 2 Extra Slide Material

What we’ve covered: Types Literal values Expressions Operators Strings Initialization of variables Input and output procedures Constants