Primitive Types Operators Basic input and output

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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;
Expressions, Data Conversion, and Input
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Mathematical Calculations in Java Mrs. G. Chapman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Introduction to Java Primitive Types Operators Basic input and output.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 2 More on Math More on Input
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
User input We’ve seen how to use the standard output buffer
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Building Java Programs
MSIS 655 Advanced Business Applications Programming
Chapter 2 Variables.
Fundamentals 2.
Variables & Data Types Java.
elementary programming
Building Java Programs
CS2011 Introduction to Programming I Elementary Programming
Introduction to Java Applications
Building Java Programs
Java Programming First Program and Variables
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Unit 3: Variables in Java
Building Java Programs
Output Manipulation.
Chapter 2 Variables.
Just Enough Java 17-May-19.
Chapter 2 Primitive Data Types and Operations
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Primitive Types Operators Basic input and output Introduction to Java Primitive Types Operators Basic input and output

Basic Output For console output, there are 2 basic commands: System.out.print( <Your text goes here> ); Prints text and leaves cursor on same line System.out.println(<Your text goes here> ); Prints text and moves cursor to start of next line

Your first program The traditional first program in computer science is the hello world program We will complete this together to try your first basic output to the screen. Source Code: public static void main( String[] args ) { System.out.println( “Hello World” ); } Every statement in Java ends with a “;”

Escape Characters How do you print a “ character in Java? System.out.print( “ ); System.out.print( “”” ); Try them and see what Java has to say How would you tell Java to move to a new line or tab forward? To address this issue, Java uses the backslash as an “escape character” It indicates to Java that when it sees the “\” it has to do something special, and not just print what it sees.

There are others, but we’ll stick to these Escape Characters Character Sequence Description \” Quote \n New Line \t Tab \\ Backslash There are others, but we’ll stick to these

Working with escape characters What is printed: System.out.println(“\”\\\n\\\”/\\/n\t/t”); System.out.println( “\”\\n\n\\t\t\””); What code will print: “\n//\\\t \”” “\n\\/t\\” \n ””

String Concatenation You’ll notice a vertical line in your source code This represents a standard page width for printing You can write past the line, but when you print your work it will not fit on the paper One thing you can do is tell java to join two strings together using the “+” operator This is known as String concatenation

String Concatenation Example System.out.println( “Oh Canada, Our home and native land…” ); Error, string missing quotation Fix it as follows: System.out.println( “Oh Canada,” + “Our home and native land…” ); Prints: Oh Canada, Our home and native land… To make it print on multiple lines, we have a couple of choices: System.out.println( “Oh Canada” ); System.out.println( “Our home and native land…” ); OR System.out.println( “Oh Canada,\n” +

Basic Output Exercises Choose a poem or song to print on the console with Java Using only 1 System.out.println command (and the + and \n operators) make a “letter head” with your name and address: Ex. Johnston Heights 15350-99 Avenue Surrey, BC V3R 0R9 Telephone:  604-581-5500

Escape Character Exercises Write a program that displays the following: “\n\\n\”\t” \\tt\n\\” Write a program that displays student information for 4 of your class mates in a table using the \t and \n characters Ex Name Age Favorite Sport John 16 Soccer Jane 17 Hockey

Primitive Data Types Name Description Example int Integer int age = 17; double Floating point (decimal) number double radius = 1.23; boolean Stores the value True or False boolean stop = true;

Basic Math Operators Operator Description * Multiplication / Division + Addition - Subtraction % Remainder (Mod)

Precedence Math operators in Java have the same precedence as general Mathematics 6 + 7 * 3 = 27 Brackets can also be used to change the order of operations (6+7) * 3 = 39

Shortcut Operators Some tasks are common enough that there are shortcuts for them Operator Example Equivalent to ++ (increment) x++ x = x + 1 -- (decrement) x-- x = x - 1 += x += 5 x = x + 5 -= x -= 3 x = x – 3 /= x /= 2 x = x/2 *= x *= 6 x = x * 6 %= x %= 7 x = x %7

Integer Division Java treats division between integers different than your calculator For example, your calculator would tell you that 10 / 4 = 2.5 Java would tell you that 10/4 = 2 This is because integers do not have a decimal place, so it is thrown away Examples: 6 / 4 = 1 7 / 2 = 3 10 / 11 = 0 16 / 4 = 4

Modulus or Remainder So how do you get the missing decimal place back? The % operator gets the remainder that was thrown away when the integers were divided For example: 10 / 3 is 3 Remainder 1, so 10 % 3 = 1 15 / 5 is 3 Remainder 0, so 15 % 5 = 0 18 / 4 is 4 Remainder 2, so 18 % 4 = 2 There are surprisingly a lot of application of the % operator that we will use later on in the course

String Concatenation With Numbers Sometimes you want to print text with numbers and there is an important rule in Java to be aware of: When you use the + operator, if one of the operands is a string, then both will be treated as a string + only performs addition if BOTH operands are numbers This can make for some peculiar output

String Concatenation With Numbers Concatenation occurs from left to right What is printed? System.out.println( “Sum is: “ + 1 + 2 + 3 ); Prints: “Sum is 123” System.out.println( “Sum is: “ + (1+2+3) ); Prints “Sum is 6” System.out.println( 1 + “2” + 3 + (4 + 5) ); Prints 1239 As a general rule, put your arithmetic operations in brackets so they are done separately from the string concatenation

Math Operator Exercises Case Study: Multiplication table Together, we’ll program java to display a multiplication table for us. The first time we do this we won’t use a variable The second time we’ll compare how a variable makes our program more versatile

Constants in Java As a programmer you’ll run in to constant values like often In order to guarantee the value is not changed somewhere in your code, you can use the final keyword to indicate to java that the value is not to change Example: final double MILES_PER_KM = 1.6; Note the style to indicate a constant is to capitalize the variable name

Basic Input Interaction through the keyboard is done with the Scanner class. It is part of the standard Java library that comes with the compiler. In order to use it, we have to tell Java we want to add it to our program import java.util.Scanner;

Basic input First we have to tell Java where we want to scan, for our class it will always be System.in Create the Scanner as follows: Scanner keyboard = new Scanner( System.in ); It can have any name, it does not have to be called keyboard (it is a variable) The new keyword in Java tells java to create a new Object for us to use Objects perform tasks for us, to access these tasks we have to use the “.” operator Our compiler has this built in as a feature, when you type: “keyboard.” and wait, a list a methods pops up

Scanner Methods The most common methods we’ll use for Scanners are: next()  gets the next word (separated by space) nextLine()  gets the next line of text nextDouble()  gets data as a double nextInt()  gets data as an integer When you read data from the keyboard with these methods, the enter key invokes them to start

Our first program with input Together we’ll create a program that produces the following interactions: What is your name? Joe How many toonies are in your wallet? 5 How many loonies are in your wallet? 3 How many quarters are in your wallet? 6 How many dimes are in your wallet? 7 How many nickels are in your wallet? 2 How many pennies are in your wallet? 4 Joe, You have $15.34

Math Operator Exercises Create a program for each conversion: Celsius to Fahrenheit Pounds to kilograms inches to centimeters Create a program for each formula: Compute the area of a circle Compute the volume of a Sphere Computer the surface area of a Cylinder You can find the formulas on the internet or in your agendas Use constants and variables where appropriate. Create a program to determine the number of vehicles needed for a trip: How many people are going on the trip? 27 How many seats does each vehicle have? 5 You will need 6 vehicles. 5 will be full and the 6th will have only 2 people (3 empty seats). Complete the setTime method in the AnalogClock class

Interactive Programming Exercises Create a simple store front that displays a welcome screen and a menu. Prompt the person for how many of each item they want and create a bill for them: |**************************************************| | JOE’S JAVA JOINT | Coffee – $1.50 Espresso - $2.50 Latte - $3.25 Tea - $1.75 How many coffees do you want? 2 How many espresso do you want? 4 How many lattes do you want? 0 How many teas would you like? 1 Sub Total: $14.75 Taxes (12%): $1.77 Total: $16.52 Thank you, come again!

Data Conversions What gets printed? Rules for math operators: System.out.println(10 / 4.0 ); System.out.println(10.5 + 4 ); System.out.println( 10 - 4 ); System.out.println( 10.25 * 4.0 ); Rules for math operators: If BOTH operands are integers, then integer math is performed Otherwise, floating point arithmetic is used

Data Conversions What happens if you try this in main? int result = 10 / 4.0; double result = 10 / 4; double result = 10 % 3; int result = 10 % 4.0;

Casting Rules If the conversion will lose data, Java will give an error Going from 13.5 (double) to and int would lose information when 13.5 would become 13 There is no problem however if you want to store 13 (int) into a double value because nothing is lost by 13.0 The way you tell Java you’re aware of the error and that it’s OK to compile is by casting Casting changes the data type

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( i1 / (int)d2 );

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( d2 / (double)i2 );

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( d2 / (int)d4) );

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( (int)(d1 / d4) );

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( (double)(i1 / (int)d4) );

Casting Examples int i1 = 12; int i2 = 4; int i3 = 6; double d1 = 24.0; double d2 = 3.0; double d3 = 2.0; double d4 = 2.5; System.out.println( (double)i2 / i3 );

Casting exercises

Casting Exercises Cont’