Console Output, Variables, Literals, and Introduction to Type

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CMT Programming Software Applications
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
Hello, world! Dissect HelloWorld.java Compile it Run it.
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;
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Getting Started with Java
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Variables.
Working With Objects Tonga Institute of Higher Education.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Lecture 4 – Scanner & Style
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Lecture 2 Data Types Richard Gesick.
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2, Part I Introduction to C Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Revision Lecture
Variables and Primative Types
Data types and variables
IDENTIFIERS CSC 111.
Type Conversion, Constants, and the String Object
Introduction to C++ Programming
Chapter 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Chapter 2 Variables.
Introduction to Primitive Data types
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Introduction to Primitive Data types
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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: 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 JVM The programs executes! Procedural Programming is… a programming methodology in which a program was made up of one of more procedures.

Review Object-Oriented Programming (OOP) is… An object 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.

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.

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

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…

Dissecting println 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

Console Output Examples 1 //Output1 - shows the use of println public class Output1 { public static void main(String[] args) { System.out.println("Here I am printing on the first line..."); System.out.println("...Now, I'm on the second line"); }

Console Output Examples 1 & 2 //Output2 - shows the use of print public class Output2 { public static void main(String[] args) { System.out.print("Here I am printing on the first line..."); System.out.println("and I am still on the first line!"); }

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.

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.

Escape Sequence Example //Output3 - shows the use of escape sequences in string literals public class Output3 { public static void main(String[] args) { System.out.println("This is how I can put an new line: |\n|"); System.out.println("This is how I can put a tab: |\t|"); System.out.println("This is how I can put a backslash: |\\|"); System.out.println("This is how I can put a single quote: |\'|"); System.out.println("This is how I can put a double quote: |\"|"); }

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.

Variable - Naming Variables 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? miles Legal 3dGraph Illegal – cannot start with a numeric character firstName3 _miles 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

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.

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

Variable Example //Variable1 - shows the declaration and assignment of a variable public class Variable1 { public static void main(String[] args) { int number; number = 5; System.out.print("The value of the variable \"number\" is "); System.out.println(number); }

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.

Assignment Statement Line 5 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.

Variable Usage Line 7 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. Questions...

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.

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

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.