console output, variables, literals, and introduction to type

Click here to load reader

Upload: beate

Post on 24-Mar-2016

69 views

Category:

Documents


0 download

DESCRIPTION

Console Output, Variables, Literals, and Introduction to Type. CS0007: Introduction to Computer Programming. Review. A General-Purpose Programming Language is… a programming language designed to be used for writing software in a wide variety of application domains. - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

CS0007: Introduction to Computer ProgrammingConsole Output, Variables, Literals, and Introduction to Type1ReviewA General-Purpose Programming Language isa programming language designed to be used for writing software in a wide variety of application domains.A programming language is platform-specific ifonce compiled, it can only be guaranteed to be able to run on the machine it was compiled for.A High-Level Programming Language isa 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 computers hardwareCompilation isthe 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.

2ReviewThe Java Virtual Machine (JVM) isa 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:Write the source code in the Java programming language.Run the source code through the Java Compiler (Interpreter)Run the resulting Java Byte Code through the JVMThe programs executes!Procedural Programming is a programming methodology in which a program was made up of one of more procedures.

3ReviewObject-Oriented Programming (OOP) isa programming methodology in which a programs parts are abstracted into the interaction between Objects.An object isan 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 tothe combining of data and code into a single object.Data Hiding refers to the objects ability to hide its data from outside the object.

4The Java ConsoleThe 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.

5Java APIThe 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.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 RoundOther times, you will get an exampleExample: Java Ceiling6Print and PrintlnLike 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 screenprintln 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.Weve seen this before

7System.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 argumentWe 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 charactersWe will go over literals, types, and strings laterDissecting println8Console Output Examples 1 & 2New Topics:printprintln9Escape SequencesWhat would be the problem with this?:System.out.println("He said "Hello" to me");The compiler does not know that the "s arent ending and starting a new string literal.But, we want double quotes in our stringHow do we fix this?Answer: Escape SequencesEscape 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.10Escape SequencesCommon 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 CharacterEven though they are written as two characters, the character represented by an escape sequence is only one character in memory.11Escape Sequence ExamplesNew Topics:Escape Sequences12VariablesA variable is a named storage location in the computers 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.Variable - NamingVariables and all other identifiers follow the same naming rules:The first character must begin with an alphabetic character (A-Z or a-z), an underscore (_), or a dollar sign ($).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 ($).Identifiers are case-sensitive.Identifiers cannot include spaces.Identifiers cannot be reserved words.

Variables Naming: Legal or Illegal?milesLegal3dGraphIllegal cannot start with a numeric characterfirstName3Legal_milesLegalfirstName#3Illegal cannot have a pound (#) symbolfirst nameIllegal cannot have a space in an identifier classIllegal cannot be a reserved wordVariables - NamingThe 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 cant use a spaceAnswer: Camel CasingCamel 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.Data TypeAgain, 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 variableSo 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

Variable ExampleNew Topics:Variable DeclarationVariable AssignmentVariable OutputVariable DeclarationLine 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 typedataType variableName;dataType the data type of the variable.variableName the identifier for the variable.

AssignmentLine 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.QuestionIf 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.Variable UsageLine 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...Primitive Data TypesThere 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.

Primitive Numeric Data TypesData TypeSizeRangebyte1 byteIntegers in the range of -128 to +128short2 bytesIntegers in the range of -32,768 to +32,767int4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647long8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807float4 bytesFloating-point numbers in the range of 3.4x10-38 to 3.4x1038 with 7 digits of accuracy double8 bytesFloating-point numbers in the range of 1.7x10-308 to 1.7x10308 with 15 digits of accuracy Notes on TypeYou want to choose a type based on what the variable should doWhy 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.