Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle
2 Sample Java Program: Time.java Let’s begin by examining the components of the second program from Lab 1 … Let’s begin by examining the components of the second program from Lab 1 …
3 Sample Java Program: Time.java 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class
4 Sample Java Program: Time.java Overall form of a Java program: Overall form of a Java program: class Time { public static void main ( String[] args ) { class Time { public static void main ( String[] args ) { // Code to do the work of the program // Code to do the work of the program // (i.e., instructions to the computer) } //end main } //end Time class // (i.e., instructions to the computer) } //end main } //end Time class Inner block Outer block Name of program Note: A "block" is a list of statements in curly braces. Indicates a comment, text used to annotate a program that the compiler will ignore
5 Sample Java Program: Time.java Line 3: float distance, speed, time; Line 3: float distance, speed, time; Line 3 says that distance, speed, time are variables that can store floating point numbers (decimal numbers). Line 3 says that distance, speed, time are variables that can store floating point numbers (decimal numbers). Line 3 is called a statement. All statements in Java must end with a semicolon. Line 3 is called a statement. All statements in Java must end with a semicolon.
6 Variables A variable is a location in the computer’s memory where a piece of data can be stored for use in a program. A variable is a location in the computer’s memory where a piece of data can be stored for use in a program. All variables in Java must be given a name and a type before they can be used. This is called “declaring” a variable. All variables in Java must be given a name and a type before they can be used. This is called “declaring” a variable. Line 3: float distance, speed, time; Line 3: float distance, speed, time; type names
7 Rules for Naming Java Variables Variable names can consist of: Variable names can consist of: Letters Letters Digits Digits Underscores ( _ ) Underscores ( _ ) Dollar signs ( $ ) Dollar signs ( $ ) Variable names cannot start with a digit or contain spaces. Variable names cannot start with a digit or contain spaces. Valid variable names: Valid variable names: num1, firstName$, exam_one num1, firstName$, exam_one Invalid variable names: Invalid variable names: 3integers, course average 3integers, course average
8 Rules for Naming Java Variables Variable names can consist of any number of valid characters (i.e., there is no official limit to the length of a variable name). Variable names can consist of any number of valid characters (i.e., there is no official limit to the length of a variable name). Variable names should be descriptive of their use. For example: Variable names should be descriptive of their use. For example: grade would be a good name to use for a variable that stores a student's test grade. grade would be a good name to use for a variable that stores a student's test grade. Variable names are case sensitive. For example: Variable names are case sensitive. For example: x1 is not the same as X1 x1 is not the same as X1 Keywords such as int and float cannot be used as variable names. Keywords such as int and float cannot be used as variable names. Keywords are words reserved for use by the Java language. Keywords are words reserved for use by the Java language. Additional keywords that appear in Time.java include: class, public, static, void, new Additional keywords that appear in Time.java include: class, public, static, void, new
9 More on Declaring Variables float distance, speed, time; could also have been declared separately as: float distance; float speed; float time; since the three variables are all the same type. float distance, speed, time; could also have been declared separately as: float distance; float speed; float time; since the three variables are all the same type.
10 Data Types There are two main kinds of types in Java: There are two main kinds of types in Java: Primitive type Primitive type Stores a single piece of data of a specific type Stores a single piece of data of a specific type Some primitive types and the kinds of data they store are: Some primitive types and the kinds of data they store are: int – a 32-bit integer (A bit is a 0 or 1.) int – a 32-bit integer (A bit is a 0 or 1.) float – a 32-bit floating point (decimal) number float – a 32-bit floating point (decimal) number double – a 64-bit floating point (decimal) number double – a 64-bit floating point (decimal) number char – a single character (e.g., ‘Y’) char – a single character (e.g., ‘Y’)
11 Data Types Class type Class type Stores multiple pieces of data and provides methods for manipulating that data Stores multiple pieces of data and provides methods for manipulating that data Some class types are: Some class types are: String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” input – stores and manipulates text entered at the keyboard input – stores and manipulates text entered at the keyboard Variables that are class types are called objects. Variables that are class types are called objects.
12 Declaring Variables/Objects of Different Data Types Examples: Examples: int firstNum, secondNum; // integer variables int firstNum, secondNum; // integer variables double root1, root2; // floating point (decimal) // variables double root1, root2; // floating point (decimal) // variables char response; // character variable char response; // character variable String first_name, last_name; // string objects String first_name, last_name; // string objects output out; // output object output out; // output object
13 Assignment in Java To give a variable/object a specific value, we assign that value to the variable/object using the assignment operator, ‘=’. To give a variable/object a specific value, we assign that value to the variable/object using the assignment operator, ‘=’. Assignment requires that the types on both sides of the assignment operator be compatible. Assignment requires that the types on both sides of the assignment operator be compatible.
14 Assignment in Java Examples using primitive types: Examples using primitive types: float distance; distance = 100.5;// compatible distance = 80;// compatible distance = “25 miles”;// NOT compatible float distance; distance = 100.5;// compatible distance = 80;// compatible distance = “25 miles”;// NOT compatible int num1; num1 = 10;// compatible num1 = 16.4;// NOT compatible int num1; num1 = 10;// compatible num1 = 16.4;// NOT compatible char response; response = ‘Y’;// compatible response = “Y”;// NOT compatible char response; response = ‘Y’;// compatible response = “Y”;// NOT compatible
15 Assignment in Java Examples using class types: Examples using class types: String fullName; fullName = “John Smith”; // compatible fullName = John Smith; // NOT compatible String fullName; fullName = “John Smith”; // compatible fullName = John Smith; // NOT compatible output out; out = new output();// compatible input in; in = new input();// compatible out = in;// NOT compatible output out; out = new output();// compatible input in; in = new input();// compatible out = in;// NOT compatible
16 Assignment in Java Examples mixing primitive and class types: Examples mixing primitive and class types: input in; in = new input(); float distance; distance = in.readfloat(); // compatible int newDistance; newDistance = in.readint();// compatible newDistance = distance;// NOT compatible (Why not?) input in; in = new input(); float distance; distance = in.readfloat(); // compatible int newDistance; newDistance = in.readint();// compatible newDistance = distance;// NOT compatible (Why not?) Notes: readfloat is a method that retrieves a decimal number. readint is a method that retrieves an integer. Notes: readfloat is a method that retrieves a decimal number. readint is a method that retrieves an integer.
17 Friday’s Topic Arithmetic expressions Arithmetic expressions