Data Type & Input/Output Session 3 Course: T0974-Algorithm & Object-Oriented Programming I Year:...

Post on 31-Mar-2015

217 views 2 download

Tags:

transcript

Data Type & Input/OutputSession 3

Course : T0974-Algorithm & Object-Oriented Programming IYear : 2010

Bina Nusantara

Learning Outcomes

After taking this course, student should be expected to be able to apply data types depend on the need of the programs and using syntax for Input – Output Operation

4

JAWABAN QUIZ

Bina Nusantara University

public class Gaji{public static void main(String [] args){

Hitung h= new Hitung();  //membuat obyek dari //kelas Hitung    h.basic=5000000;    h.tunjangan=1500000;    h.bolos=2;    h.hitung();}}

class Hitung{int bolos=0;int total=0;int basic=0;int tunjangan=0; String nama="RINA";void hitung(){if (bolos<=3)    {        total=basic+tunjangan;    }    else    {        total=basic;    }System.out.println ("Nama " + nama+ " Jumnlah bolos:" + bolos);    System.out.println ("Gaji :" + total);}}

Bina Nusantara

Outline Materi

• Data Types• Input/Output (I/O)• Output Format• Type Casting (Conversion)• ASCII• Import• Constant

Bina Nusantara

Data Type

• An attribut that has range value and type.• To be used as a value storage from execution

process.• Based on its data type :

– Boolean (boolean)– Numeric (byte, short, int, long, float, double)– Character (char)– String (String)

Bina Nusantara

Data Type

• Based on Complexity :– Atomic DT (boolean, byte, char, short, int, long, float,

double)– Composite DT (Array, Struct, List, Queue, Stack, String,

Tree)• Based on Source:

– Native / primitive / basic DT– Abstract DT

• Based on Customization:– Built-in DT– User-defined DT

Bina Nusantara

Data TypeName Range Size Class

boolean

true, false 1 bit Boolean

byte -27 (-128) to 27 – 1 (127)

8 bit Byte

char 0 to 65,535 16 bit Character

short -215 (-32,768) to 215 – 1 (32,767)

16 bit Short

int -231 (-2,147,483,648) to 231 (2,147,483,647)

32 bit Integer

long -263 to 263 – 1 64 bit Long

float -3.4E38 to 3.4E38 32 bit Float

double -1.798E308 to 1.798E308

64 bit Double

Bina Nusantara

Data Type

Filename : TipeData.java

Output:

Bina Nusantara

Input / Output (I/O)

• A Communication between computer to other entity (Human / Machine)

• Input: Signal/Data will be recevied by system.• Output: Signal/Data will be sent from system.• I/O perform or I/O operation.

Bina Nusantara

Input / Output (I/O)

• Input Devices e.g. : keyboard, mouse• Output Devices e.g. : monitor, printer• I/O Devices e.g. : disk, file

Bina Nusantara

Input / Output (I/O)

• Get input from console using Scanner.• Library java.util.Scanner.• It needs to import declaration :

import java.util.Scanner;• Afterward declare an object creation :

Scanner input = new Scanner(System.in);

Bina Nusantara

Input / Output (I/O)

Method Usenext(); Input String (word)

nextLine(); Input String (sentence)

nextByte(); Input number (byte)

nextShort(); Input number (short)

nextInt(); Input number (int)

nextLong(); Input number (long)

nextFloat(); Input number (float)

nextDouble(); Input number (double)

Bina Nusantara

Input / Output (I/O)

Bina Nusantara

Input / Output (I/O)

Bina Nusantara

Input / Output (I/O)

Specifier

Description Example

Output

%b boolean %6b ˽false˽˽true

%c character %5c ˽˽˽˽a

%d integer %5d ˽˽˽691234567

%f Floating-point

%5.2f ˽˽3.14˽20.60

%e scientific %10.2e ˽˽3.14e+02

%s string %10s ˽˽˽˽˽hello

• Note :˽ spasi

Bina Nusantara

Input / Output (I/O)

Specifier

Description Example

Output

%b boolean %-6b false˽true˽˽

%c character %-5c a˽˽˽˽

%d integer %-5d 69˽˽˽1234567

%f Floating-point

%-5.2f 3.14˽˽20.60˽

%e scientific %-10.2e

3.14e+02˽˽

%s string %-10s hello˽˽˽˽˽• Note :

˽ spasi

Bina Nusantara

Input / Output (I/O)

Bina Nusantara

Input / Output (I/O)

• System.out.print print into console without linefeed (newline).

• System.out.println print into console with linefeed (newline)

• System.out.printf same as System.out.print, supports format output

Bina Nusantara

Type Casting (Conversion)

• Convert a value to different type.• Type casting conversion :

– Widening a type: convert a value from smaller data to bigger data. (Automatically done by Java).

– Narrowing a type: convert a value from bigger data to smaller data.

• Size of data type (smallest to biggest)

byte, short, int, long, float, double

Bina Nusantara

Type Casting (Conversion)

• Parentheses is use as a syntax for narrowing.• Example 1:

– float f = (float) 10.1;– int i = (int) f;

• Example 2 :– double d = 4.5;– int i = (int) d;

• As depicted from example 1-2 above, f and d values haven’t been changed.

Bina Nusantara

Type Casting (Conversion)

Bina Nusantara

Type Casting (Conversion)

• Conversion from String to Atomic data type can be done by class helper.

Class Konversi ke tipe data

Pemakaian

Boolean boolean Boolean.parseBoolean(…);

Byte byte Byte.parseByte(…);

Character

char String.charAt(<index>);

Short short Short.parseShort(…);

Integer int Integer.parseInt(…);

Long long Long.parseLong(…);

Float float Float.parseFloat(…);

Double double Double.parseDouble(…);

Bina Nusantara

Type Casting (Conversion)

Bina Nusantara

Type Casting (Conversion)

Bina Nusantara

Type Casting (Conversion)

Bina Nusantara

ASCII

• American Standard Code for Information Interchange

• 7-bit, 128 characters (000 s/d 127)• uppercase/lowercase letters, digits, punctuation

marks, dan control characters• Next development of ASCII Unicode has

1,112,064 characters.

Bina Nusantara

ASCII

Bina Nusantara

ASCII

Bina Nusantara

ASCII

Bina Nusantara

Type Casting (Conversion) cont.

• ASCII characters from ‘0’ s/d ‘9’– ‘0’ 48– ‘1’ 49– ‘9’ 57

• Convert character to number can be done by substracting 48.

• Convert number to character can be done by adding 48.

Bina Nusantara

Type Casting (Conversion) cont.

Bina Nusantara

Constant

• a constant variable is a variable that is always constant

• Example :– π (PHI) = 3.14159 26535 89793 23846 26433 83279

50288 41971 69399 37510 – g (gravitation) = 9.8

• Constant should have been declared and initialized.

• Final is a keyword in Java to declare constant.• Declaration :

– final datatype CONSTANTNAME = VALUE;– Example: final double PHI = 3.1415;

Bina Nusantara

Constant

Bina Nusantara

Did You Know?

• For every Scanner use, it should need “import java.util.Scanner;”.

• Because Scanner is a class that should be included.

• Contrary to System library, it doesn’t use “import java.lang.System;” declaration which Java automatically import all of “java.lang” content to its new code.

Bina Nusantara

Did You Know?

• Content of java.lang which is usually used :– Boolean– Byte– Character– Double– Float– Integer– Long– Math– Short– String– System– Thread

Bina Nusantara

Advanced Learning

• Instead of scanner, BufferedReader can be used for handling input from console.

• It needs import from java.io.BufferedReader & java.io.InputStreamReader;

• And do object creation :– BufferedReader input = new BufferedReader (new

InputStreamReader(System.in));

• Either use read() method for reading single character or readLine() method for reading word.

Bina Nusantara

Advanced Learning

Bina Nusantara

Advanced Learning

• Import declaration like :import java.io.BufferedReader;import java.io.InputStreamReader;can be shortened to:import java.io.*;

• try and catch are going to be disscussed next lecture (Exception Handling)

40

LatihanKembangkan jawaban quiz untuk terima input dari

keyboard menggunakan Kelas Scanner

Bina Nusantara University

Bina Nusantara

References

• Introduction to Java. 7ed. 2009. Liang. p60, p67-71, p79-81, p117-118, p1300-1301

• Java Software Solutions. 5ed. 2007. Lewis. p99-103, p111-114

• The Complete Reference: Java. 5ed. 2005. Herbert. p33-48

• Dasar Pemrograman Java2. 2004. Abdul Kadir. p66-73

• Composite Data Types. http://remote.science.uva.nl/~heck/JAVAcourse/ch4

• Data Type. http://en.wikipedia.org/wiki/Data_type• Primitive Data types.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

• Primitive Data types. http://en.wikipedia.org/wiki/Primitive_type