Download presentation
Presentation is loading. Please wait.
1
03 Data types1June 15 03 Data types CE00858-1: Fundamental Programming Techniques
2
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
3
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
4
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;
5
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
6
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 23 24 count 23 + 1 count
7
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...
8
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 = 3.14159;
9
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
10
03 Data types10June 15 String data groups characters together used for: titles headings prompts error messages instructions help facilities
11
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
12
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 ! 01234678910511 char letter = 'a';
13
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!"
14
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!"
15
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
16
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...
17
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);
18
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();
19
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)
20
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
21
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
22
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
23
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.