Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.

Similar presentations


Presentation on theme: "CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type."— Presentation transcript:

1 CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type

2 Review A General-Purpose Programming Language is… a programming language designed to be used for writing software in a wide variety of application domains. A programming language is platform-specific if… once compiled, it can only be guaranteed to be able to run on the machine it was compiled for. A High-Level Programming Language is… a language that is easily read and written by humans, and is needed to be translated before a machine can use it. It provided a high level of abstraction from the details of the workings of the computer’s hardware Compilation is… the process of translating high-level language into machine language for platform-specific languages or some other intermediate code representation for other languages. A program called a compiler does this.

3 Review The Java Virtual Machine (JVM) is… a program that reads instructions from Java Byte Code and executes them for the platform that the JVM is installed on. Steps to create and run Java Program: 1. Write the source code in the Java programming language. 2. Run the source code through the Java Compiler (Interpreter) 3. Run the resulting Java Byte Code through the JVM 4. The programs executes! Procedural Programming is… a programming methodology in which a program was made up of one of more procedures.

4 Review Object-Oriented Programming (OOP) is… a programming methodology in which a programs parts are abstracted into the interaction between Objects. An object is… an abstraction of a real-world (or sometimes not) entity that consists of data (attributes) and the procedures that act upon the objects data (methods). Encapsulation refers to… the combining of data and code into a single object. Data Hiding refers to… the object’s ability to hide its data from outside the object.

5 The Java Console The console or console screen is a screen where the input and output is simple text. This is a term from the days when computer terminals were only text input and output. Now when using the console for output, it is said that you are using the standard output device. Thus when you print to the console, you are using standard output. In Java, you can write to the console, using the methods included in the Java API.

6 Java API The Java Application Programmer Interface (API) is a standard library of prewritten classes for performing specific operations. These classes are available to all Java programs. There are TONS of classes in the Java API. That do MANY different tasks.API If you want to perform an operation, try either Googling it before implementing it from scratch. Often you will get a link to the Java API that will reference the class and method/data attribute you need. Example: “Java Round” Other times, you will get an example Example: “Java Ceiling”

7 Print and Println Like stated before, you can perform output to the Java console using the Java API. Two methods to do this are print, and println. print – print some text to the screen println – print some text to the screen and add a new line character at the end (go to the next line). These two methods are methods in the out class, which itself is contained in the System class. This creates a hierarchy of objects. System has member objects and methods for performing system level operations (such as sending output to the console) out is a member of the System class and provides methods for sending output to the screen. System, out, print, and println are all in the Java API. Because of this relationship of objects, something as simple as printing to the screen is a relatively long line of code. We’ve seen this before…

8 System.out.println(“Hello World!”); The dot operator here tells the compiler that we are going to use something inside of the class/object preceding it. So System.out says that we are using the out object in the System class. The text displayed is inside of the parentheses. This is called an argument We will talk more about arguments when we go over functions, but for now, just know that whatever is in the parentheses will be printed to the console for the print and println methods. The argument is inside of double quotes, this makes it a string literal. A String is a type in Java that refers to a series of characters We will go over literals, types, and strings later Dissecting println

9 Console Output Examples 1 & 2 New Topics: print println

10 Escape Sequences What would be the problem with this?: System.out.println("He said "Hello" to me"); The compiler does not know that the " s aren’t ending and starting a new string literal. But, we want double quotes in our string…How do we fix this? Answer: Escape Sequences Escape Sequences allow a programmer to embed control characters or reserved characters into a string. In Java they begin with a backslash and then are followed by a character. Control Characters are characters that either cannot be typed with a keyboard, but are used to control how the string is output on the screen. Reserved Characters are characters that have special meaning in a programming language and can only be used for that purpose.

11 Escape Sequences Common escape Sequences: \n – Newline – Advances the cursor to the next line \t – Horizontal Tab – Advances the cursor to the next tab stop \\ – Backslash – The backslash character \’ – Single Quote – The single quote Character \” – Double Quote – The double quote Character Even though they are written as two characters, the character represented by an escape sequence is only one character in memory.

12 Escape Sequence Examples New Topics: Escape Sequences

13 Variables A variable is a named storage location in the computer’s memory. If we want to hold some value in memory for later use, we need to use as variable. Variables have 3 characteristics: Name – the name we use in our code to refer to the memory location. Type – characteristic that defines what legal values the variable can hold. Value – what is actually held inside of the memory location.

14 Variable - Naming Variables and all other identifiers follow the same naming rules: 1. The first character must begin with an alphabetic character (A-Z or a-z), an underscore (_), or a dollar sign ($). 2. After the first character you must use an alphabetic character (A-Z or a-z), a numeric character (0-9), an underscore (_), or a dollar sign ($). 3. Identifiers are case-sensitive. 4. Identifiers cannot include spaces. 5. Identifiers cannot be reserved words.

15 Variables – Naming: Legal or Illegal? miles Legal 3dGraph Illegal – cannot start with a numeric character firstName3 Legal _miles Legal firstName#3 Illegal – cannot have a pound (#) symbol first name Illegal – cannot have a space in an identifier class Illegal – cannot be a reserved word

16 Variables - Naming The name of the variable should describe what it holds, this produces self-documenting programs. For example: If you have a variable that holds the number of miles a car traveled you could call it miles. But what if the variable holds something that is normally two words, like “first name”? I can’t use a space… Answer: Camel Casing Camel Casing is combining names that normally would be two words into one, and capitalizing the start of a new word. Example: Instead of first name, use firstName. Variables by convention start with a lowercase letter. Identifiers for class names by convention start with an uppercase letter.

17 Data Type Again, Data Type or just (Type) refers to the values that a variable can hold. We have type for multiple reasons, namely: So the compiler knows how much memory to free up for the variable So the compiler knows the legal operations that can be done to/with the variable. For example the int data type holds positive and negative integers. We will be introducing new types both shortly and probably throughout the semester

18 Variable Example New Topics: Variable Declaration Variable Assignment Variable Output

19 Variable Declaration Line 4 looks like: int number; This is called a variable declaration. A variable declaration is a statement that tells the compiler the variables name, and what data type it is. You MUST declare a variable before you can use it. General form of a variable declaration that is of a primitive data type dataType variableName; dataType – the data type of the variable. variableName – the identifier for the variable.

20 Assignment Line 9 looks like: number = 5; This is called an assignment statement. An assignment statement stores a value on the right side of the = to the variable on the left. For this reason the = is called the assignment operator in Java. This stores the value 5 into the memory location named number. 5 here is called an integer literal. A Literal is a value that is explicitly written in the code of the program. Literals have type just like variables, that is why 5 is an integer literal. Question… If the types on either side of an assignment statement are not the same, the result is a type-mismatch error. This happens because Java is a strongly-typed language. A programming language is strongly-typed if it only allows operations to be executed on operands of the same type.

21 Variable Usage Line 9 looks like: System.out.println(number); This prints out the value stored in the variable number (5 in this case) When a variable is used NOT on the left hand side of a assignment statement, the value stored in the variable is used. Question...

22 Primitive Data Types There are many types of data, you can even define your own type. int is known as a primitive data type. A primitive data type is a built-in data type that is a basic building block for all other types and DO NOT create objects. byte, short, int, long are all integer data types. float and double are floating-point (decimal) data types.

23 Primitive Numeric Data Types Data TypeSizeRange byte 1 byteIntegers in the range of -128 to +128 short 2 bytesIntegers in the range of -32,768 to +32,767 int 4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647 long 8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 float 4 bytesFloating-point numbers in the range of ±3.4x10 -38 to ±3.4x10 38 with 7 digits of accuracy double 8 bytesFloating-point numbers in the range of ±1.7x10 -308 to ±1.7x10 308 with 15 digits of accuracy

24 Notes on Type You want to choose a type based on what the variable should do Why would you not use double for a variable that counts the number of times the user clicks on something? Why would you not use int for a variable that calculates the exact weight a bridge can hold? One good thing about types in Java is that the sizes of these variables are the same on EVERY computer. Not the case in other languages.


Download ppt "CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type."

Similar presentations


Ads by Google