Output Manipulation.

Slides:



Advertisements
Similar presentations
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Advertisements

Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
2.2 Information on Program Appearance and Printing.
Introduction to Python
2440: 211 Interactive Web Programming Expressions & Operators.
Input, Output, and Processing
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
CSC 110 – Intro to Computing - Programming
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Lecture 4 – Scanner & Style
Math operations 9/19/16.
CS 106A, Lecture 4 Introduction to Java
C++ First Steps.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Topic Pre-processor cout To output a message.
CSC201: Computer Programming
Building Java Programs
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 2, Part I Introduction to C Programming
Assignment statement:
Revision Lecture
Variables, Expressions, and IO
Computer Science 3 Hobart College
ITM 352 Expressions, Precedence, Working with Strings Class #5
Chapter 2 Basic Computation
Simplifying Expressions in Algebraic Expressions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Few More Math Operators
Introduction to C++ Programming
Building Java Programs
Objects and Primitive Data
Arithmetic Expressions & Data Conversions
Escape sequences escape sequence: A special sequence of characters used to represent certain special characters in a string. \t Inserts a tab in the.
Reading Input from the Keyboard
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Building Java Programs Chapter 2
Boolean Expressions to Make Comparisons
SSEA Computer Science: Track A
Topics Designing a Program Input, Processing, and Output
Java Programming First Program and Variables
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Beginning Python Programming
Review of Previous Lesson
Unit 3: Variables in Java
Building Java Programs Chapter 2
Chapter 2 Variables.
Chapter 1 c++ structure C++ Input / Output
© A+ Computer Science - Basic Java © A+ Computer Science -
Chapter 2 part #1 C++ Program Structure
Arithmetic Expressions & Data Conversions
Building Java Programs
Presentation transcript:

Output Manipulation

Manipulating Output There may come a time when outputting you may want to format the output in various ways to make it easier for readers For example, if you were creating a receipt display you may want items on one side and their values on the left. In Java we can manipulate our output in a number of ways. The first strategy is using the appropriate command, println vs. print The second strategy is using escape characters The third strategy is using text expressions

Escape Characters An escape character is a special character that we put INSIDE our quotes to produce a particular special output. All escape characters are preceded by a backslash, \. This is to tell the compiler that the next character following the \ has special meaning. We have 4 main escape characters in Java: \n  new line: This will force the cursor down to the next line immediately \t  tab: Inserts a tab (8 spaces) at the current cursor location \”  double quote: Our text is in quotes, this allows us to actually display a double quote in our output \\  backslash: Since \ is used to tell the compiler an escape character is coming, this allows us to include a \ in our output E.g. System.out.print(“Hello\n\tWorld!”); Outputs the following, exactly: Hello World!

Text Expressions An expression is something that results in a value. You see expressions in math class all the time, e.g. 5 + 4 is an expression that results in the value 9 A Text expression is when we combine values and end up with some resulting text. E.g. “Hello” + “ “ + “World!” results in “Hello World!”

Text Expressions What can we combine? (Also known as concatenate) Text and Text: “Hello” + “ “ + “World!” Text and escape characters: “Hello” + “\n” + “World!”  forces World! Down Text and numbers: “You are ” + 16 + “ years old.”  “You are 16 years old.” Whoa whoa whoa…how can we combine numbers with text? Can we do this: System.out.println(“You are ” + 16 + “ years old.”); Yes, we can. When Java sees that you are trying to combine text with a number it will automatically treat the number as text, e.g. 16 becomes “16” However, the number itself can be an expression that will be evaluated BEFORE the combining with the text occurs System.out.println(“You are “ + (10 + 6) + “ years old.”); will give us the same result as before Notice I wrapped the math in brackets, this is because Java will follow proper BEDMAS rules System.out.println(“You are “ + 10 + 6 + “ years old.”);  Gives “You are 106 years old.” This happens because it does “You are “ + 10 before the math can occur.

General Expressions As shown previously, other expressions can be combined with Text expressions to give us all kinds of results. When handling mathematical expressions there are a number of operators that Java will recognize: Now, typically we will NOT do math expressions inside output statements, they will be done elsewhere and simply their result will be output to the screen. Op. Name Description Example Result + Concatenate Adds text together, or a number and text “6” + “2” “62” Add Adds numbers together 6 + 2 8 - Subtract Subtracts 2nd number from 1st 6 – 2 4 * Multiply Multiplies numbers together 6 * 2 12 / Divide Divides 1st number by 2nd 6 / 2 3 % Modulus Returns the remainder after dividing the 1st number by the 2nd 6 % 2