03 data types1june 15 03 data types ce00858-1: fundamental programming techniques
Post on 20-Dec-2015
217 views
TRANSCRIPT
03 Data types 218 Apr 2023
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 318 Apr 2023
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 418 Apr 2023
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 518 Apr 2023
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; //5int y = (int) 4.8; //4int answer = (int) (5.0 / 3.0); //1double answer2 = (double) 5 / 3; //1.6667
03 Data types 618 Apr 2023
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
03 Data types 718 Apr 2023
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 818 Apr 2023
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 918 Apr 2023
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 1018 Apr 2023
String data
• groups characters together• used for:
• titles• headings• prompts• error messages• instructions• help facilities
03 Data types 1118 Apr 2023
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 1218 Apr 2023
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!";
H e l l o w o r l d !
0 1 2 3 4 6 7 8 9 105 11
char letter = 'a';
03 Data types 1318 Apr 2023
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 1418 Apr 2023
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 types 1518 Apr 2023
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 1618 Apr 2023
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 headerimport java.util.*;public class ReadExample...
03 Data types 1718 Apr 2023
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 1818 Apr 2023
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 types 1918 Apr 2023
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 types 2018 Apr 2023
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 2118 Apr 2023
// 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 dataScanner 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 types 2218 Apr 2023
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