Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science / Procedural – 67130

Similar presentations


Presentation on theme: "Introduction to Computer Science / Procedural – 67130"— Presentation transcript:

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

2 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

3 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)

4 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?

5 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.

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

7 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 ) ); System.out.println(" deg F"); } }

8 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!

9 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

10 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

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

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

13 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 ) ); System.out.println(" deg F"); } }

14 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

15 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

16 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

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

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

19 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 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( ) )

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

21 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( );

22 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;

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

24 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;

25 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;

26 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

27 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…

28 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

29 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 ) ); System.out.println(" deg F"); } }

30 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.

31 Unit 2 Extra Slide Material

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


Download ppt "Introduction to Computer Science / Procedural – 67130"

Similar presentations


Ads by Google