You are on page 1of 46

COMP6598

Introduction to Programming

Elementary Programming
(Data Type, Input and Output)
Week 2
Session 3
Acknowledgement
These slides have been adapted from
Introduction to Java Programming. 9ed. D.
Liang. 2013. Chapter 2
Learning Outcomes
• LO 2: Understanding the basic of Java
programming
Lecture Outline :
• Writing a simple program
• Reading input from the console
• Identifier
• Variables
• Constants
• Numerical Data Types
• Numerical Type Conversions
• Characters and String Type
• Exercise
Writing a simple program
Java Program Rules
There some rules should be applied while
creating JAVA Program, which are:
• Every Java program begins with a class definition in
which the keyword class is followed by the class name.
• Every Java Program should have 1 public class e.g.
WelcomeWorld
• The public class name should be the same as the file
name.
• To execute a class, it should have a main method
where program execution begins.
Simple Java Code
Example
Access modifier Class name

Function name

Class
Block
Statements

Main Method Block

Note: access modifier will be taught in details in the next subject. In this basic step, you
just need to apply the public access modifier.
Simple Java Code
Example
• Result :
Writing a simple
program
• Algorithm is needed to wrote a program
before translating into code
• Example : calculating the area of a circle
• The algorithm
read the circle's radius
compute the area using following formula :
area = radius x radius x 3.14
display the result
12/7/2019
Writing a simple
program

Result

12/7/2019
Reading input from console
Scanner
• Scanner class is used to create an object to read input
from System.in
Scanner input = new Scanner(System.in);
• The syntax new Scanner(System.in) creates an
object of the Scanner type.
• The syntax Scanner input declares that input is a
variable whose type is Scanner.

12/7/2019
Method for Scanner
Object

12/7/2019
Example

12/7/2019
• The Scanner class is in the java.util package. It is imported in first
line : import.java.util.Scanner
• The statement that displays a message to prompt the user for
input. : System.out. print("Enter a number for radius: ");
• .nextDouble is usedto read the input from console because the
radius data type is double.. The statement to read input from the
keyboard :
radius = input.nextDouble();
• The statement to calculate the area :
area = radius * radius * 3.14159;
• The statement to display the result :
System.out.println("The area for the circle of radius "
+radius + " is " + area);
12/7/2019
Identifier
Identifier rules
• Identifiers are :
– the names that identify the elements such as classes, methods, variables, and
constants, in a program.
– An identifier is a sequence of characters that consists of letters, digits,
underscores (_), and dollar signs ($). example : number1, _number1,
number_1
• Rules :
– An identifier must start with a letter, an underscore (_), or a dollar sign ($). It
cannot start with a digit.
– An identifier cannot be a reserved word.
– An identifier cannot be true, false, or null.
– An identifier can be of any length.
• Note
– Java is case sensitive
– Descriptive identifiers make programs easy to read. Avoid using abbreviations
for identifiers. Using complete words is more descriptive.
– Do not name identifiers with the $ character. By convention, the $ character
should be used only in mechanically generated source code.

12/7/2019
Comment and Reserved
Words
Comment: Helping a programmer to communicate and
understand a program. (internal program documentation)
Notation?
// line comment
/* … */ paragraph comment
Reserved Words: words which have a specific meaning to a
compiler that couldn’t be used for another use in program.
abstract default while package this class continue
assert do if private throw finally float
boolean double implements protected synchronized native new
break else import public transient super switch
byte enum instanceof return TRUE for null
case extends int short try char final
catch FALSE interface static void long volatile
Variables
Variables
• Variables are for representing data of a certain type.
• Before we can use the variable, we have to declare the variable and its data type.
• The syntax for declaring a variable is
datatype variableName;
• Example :
int count;
double radius;
double interestRate, balance;
String name;
boolean flag;
• data types such as byte, short, long, float, char, String and boolean.
• Initial value can be assigned to variable
• Example
int count = 1;
double radius = 20.0;

12/7/2019
Variabels
• Note :
– A variable must be declared before it can be assigned a value.
– Whenever possible, declare a variable and assign its initial value in
one step. This will make the program easy to read and avoid
programming errors.
– Every variable has a scope. The scope of a variable is the part of the
program where the variable can be referenced.

12/7/2019
Constant
Constant
• A named constant is an identifier that represents a
permanent value.
• The value of a variable may change during the
execution of a program, but a named constant, or
simply constant, represents permanent data that never
changes.
• The syntax for declaring a constant:
final datatype ConstantName = value;
• Example :
–final double PI = 3.14159;

12/7/2019
12/7/2019
Numerical Data types
Data Type Categories
• 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
Numeric Data Types
• Every data type has a range of values
according to its data types for numeric values,
characters and Boolean values.

12/7/2019
Numerical Data Types
and Range

12/7/2019
Numeric Data Types
• Java uses two types for floating-point numbers:
– Double :
• Is twice as big as float
• Known as double precision
• More accurate than float type
– Float :
• Known as single precision

12/7/2019
Numeric Type Conversion
Numeric Type
Conversion
• Java allows binary operations on values of
different types. Java automatically converts the
operand based on following rules :
– If one of the operands is double , the other is
converted to double
– Otherwise, if one of the operands is float, the other
is converted into float
– Otherwise, if one of the operands is long, the other
is converted into long
– Otherwise, both operands are converted to int
12/7/2019
Example

12/7/2019
Type Casting
• Type casting can be used to assign a value to
smaller range data type
• Casting is an operation that converts a value of
one data type into a value of another data type.
• Two type of casting :
– Widening a type : casting a variable with a small
range to a variable of a type with larger range
– Narrowing a type : casting a variable with a large
range to a variable of a type with smaller range

12/7/2019
Example

Result :

12/7/2019
Boolean, Character and String
Type
Boolean
• The boolean data type only have two
values : true or false
• Example :
boolean check = true;

12/7/2019
Character
• The character data type is used to
represent a single character.
• Example :
char letter = ‘A’;
char numChar = ‘4’;
• The first statement assign character A to
the char variable letter. The second
statement assigns the digit character 4 to
12/7/2019the variable
Escape sequence for
special character
• Escape sequence represents special
characters
Escape Description
Sequence
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a form feed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

12/7/2019
Example

12/7/2019
String Type
• The char type represents only one character, but to
represent string of characters , the data type is called
String.
• String is actually a predefined class in the Java Library.
• declaring a string and assigning value
String message = "Welcome to Java";
• to read from console .next() or .nextLine is used
• The next() method reads a string that ends with a
whitespace character. The characters ' ', \t, \f, \r, or \n
are known as whitespace characters.
• the nextLine() method to read an entire line of text.
12/7/2019
example

Result

12/7/2019
example

Result

12/7/2019
String Method
• Common Used of String Methods
Exercise
Exercise
• write the result of following code
– 25 % 4
– 4%5
– 20 * 7 + 56
– 3-2*5
• write the code that can convert meter to feet
• write the code that can read two strings and
convert into lower case and upper case
Thank You

You might also like