230 likes | 365 Views
03 Data types. CE00858-1: Fundamental Programming Techniques. 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.
E N D
03 Data types CE00858-1: Fundamental Programming Techniques 03 Data types
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 types
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 + 5 adds two integers • String is not a primitive data type in Java 03 Data types
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 types
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 types
23 24 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 + 1 count count 03 Data types
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 types
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; 03 Data types
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 types
String data • groups characters together • used for: • titles • headings • prompts • error messages • instructions • help facilities 03 Data types
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 types
0 1 2 3 4 5 6 7 8 9 10 11 H e l l o w o r l d ! 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!"; char letter = 'a'; 03 Data types
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 types
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.replace('l', '*')); // "He**o wor*d!" System.out.println(greeting.substring(6)); // "world!" 03 Data types
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 types
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 types
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 types
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(); double x = myKeyboard.nextDouble(); String s = myKeyboard.next(); String l = myKeyboard.nextLine(); 03 Data types
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 helpfor example, cost of item in pounds (double) or pence (integer) 03 Data types
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 types
RectangleInput.java // 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 } } 03 Data types
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 types
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 03 Data types