03 Data types1June 15 03 Data types CE00858-1: Fundamental Programming Techniques.

Slides:



Advertisements
Similar presentations
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
04 WeightLoss program1May WeightLoss program CE : Fundamental Programming Techniques.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
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;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
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 Elementary Programming
EXAM 1 REVIEW. days until the AP Computer Science test.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
02 Variables1November Variables CE : Fundamental Programming Techniques.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
CompSci 230 S Programming Techniques
Introduction to Computer Science / Procedural – 67130
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
Primitive Types Operators Basic input and output
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Introduction to Computer Science and Object-Oriented Programming
elementary programming
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Computer Science
In this class, we will cover:
Primitive Types and Expressions
Presentation transcript:

03 Data types1June Data types CE : Fundamental Programming Techniques

03 Data types2June 15 Objectives In this session, we will: look at data types and converting between types increment and decrement variables use constants introduce classes and objects use String objects use Scanner objects to input data

03 Data types3June 15 Data types data type defines: what data is stored int double boolean what operations can be performed on the data "Hello" + " Cathy" concatenates two Strings 4 + 5adds two integers String is not a primitive data type in Java

03 Data types4June 15 Converting between types conversion between types done automatically if no danger of losing information error reported if possible loss of precision int i = 10; double x = i; double x = 10.0; int i = x;

03 Data types5June 15 Casting can force conversion by using a cast required type in brackets placed in front of value to be converted int x = (int) 5.0;//5 int y = (int) 4.8;//4 int answer = (int) (5.0 / 3.0); //1 double answer2 = (double) 5 / 3; //1.6667

03 Data types6June 15 Incrementing incrementing is most common operation count = count + 1 current value of count is retrieved 1 is added to this value new value is then put back into count count count

03 Data types7June 15 Increment and decrement operators shortcut to increase or decrease integers by 1 prefix increment: postfix increment: avoid using in calculations j = ++i;// increase i by 1 THEN store in j j = i++;// store in j THEN increase i by 1 j = i i;// syntactically correct, but...

03 Data types8June 15 Constants used for data that will never change during execution benefits: can't inadvertently change it refer to meaningful name, rather than number only need to make single edit if different value needed by convention, constants are in UPPER_CASE final double PI = ;

03 Data types9June 15 Classes and objects large programs are constructed using smaller building blocks called objects objects are built from classes a class is a template that groups: data: how it appears methods: what it can do to use an object need to know: where the class can be found how to create an object what the object can do do not need to know how it works

03 Data types10June 15 String data groups characters together used for: titles headings prompts error messages instructions help facilities

03 Data types11June 15 String class String is a class not a primitive data type differs slightly from most classes in the way it is declared String objects are created from the String class to use a String object need to know: where String class is found String class is part of the Java language how to create a String object what a String object can do

03 Data types12June 15 How to create a String object declared in same way as primitive variables: variables of type String: contain zero or more characters in sequence are enclosed by double quotes the first character is at location 0 character is enclosed by a single quote String greeting = "Hello world!"; Helloworld ! char letter = 'a';

03 Data types13June 15 What a String object can do classes have methods that provide useful operations to get a single character from a string: get the position of the first occurrence of a character: convert a string to upper case: System.out.println(greeting.charAt(4));// o System.out.println(greeting.indexOf('l'));// 2 System.out.println(greeting.toUpperCase()); // "HELLO WORLD!"

03 Data types14June 15 String methods continued get the length of a string: replace every occurrence of one character with another: get the substring starting from a position: System.out.println(greeting.length());// 12 System.out.println(greeting.substring(6)); // "world!" System.out.println(greeting.replace('l', '*')); // "He**o wor*d!"

03 Data types15June 15 Scanner class Scanner objects are used to read input from the keyboard to use a Scanner object need to know: where Scanner class is found how to create a Scanner object what a Scanner object can do

03 Data types16June 15 Where Scanner class is found Scanner class is found in a package (library of classes) called java.util package must be imported into application so the compiler can find it import statement must appear before class header import java.util.*; public class ReadExample...

03 Data types17June 15 How to create a Scanner object uses new keyword requires parameter to indicate where to get input from System.in is the console input stream Scanner myKeyboard = new Scanner(System.in);

03 Data types18June 15 What Scanner object can do read an integer: read a double: read a word up to space or new line: read the rest of the current input line: int i = myKeyboard.nextInt(); String s = myKeyboard.next(); double x = myKeyboard.nextDouble(); String l = myKeyboard.nextLine();

03 Data types19June 15 Using data in programs for each data item need to consider: what is it called? where does its value come from? user input, calculated, hard coded if it is calculated, what is the formula? what type of data is it? integer, double, boolean, etc. considering the units of measurements can help for example, cost of item in pounds (double) or pence (integer)

03 Data types20June 15 Input example – Rectangle with input problem: input width and height of rectangle calculate and output area of rectangle using input values analysis: what data is used? width: positive integer input by user height: positive integer input by user area: integer calculated by program what operations are performed? create Scanner to read in data prompt user for width and height input width and height area = width * height output area

03 Data types21June 15 // input width and height, calculate and output area of rectangle import java.util.*; public class RectangleInput { public static void main (String[] args) { //create Scanner to read in data Scanner myKeyboard = new Scanner(System.in); //prompt user for input – use print to leave cursor on line System.out.print("Please enter width: "); int width = myKeyboard.nextInt(); System.out.print("Please enter height: "); int height = myKeyboard.nextInt(); //calculate and output area - unchanged } RectangleInput.java

03 Data types22June 15 Testing calculations must check results produced by programs can't assume that any results are correct calculating volume and surface area of rectangle width:2 height:3 area: width * height = 6 choose different values for each variable simple values => simple calculations

03 Data types23June 15 Summary In this session we have: looked at data types and converting between them introduced classes and objects used Scanner objects to input data In the next session we will: analyse and implement a program that uses the concepts introduced so far