0% found this document useful (0 votes)
25 views

Topic2-Basic Data Types and Operations

The document discusses basic data types and operations in programming. It covers: - Data types like numeric, character, and logical types that computers use to process data. - Variables and constants, and how they are declared and initialized in programs. Rules for naming variables are also described. - Common operators like arithmetic, assignment, comparison, and logical operators. How they are represented and used in programming. - The hierarchy of operations and how expressions are evaluated in programs.

Uploaded by

khai nisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Topic2-Basic Data Types and Operations

The document discusses basic data types and operations in programming. It covers: - Data types like numeric, character, and logical types that computers use to process data. - Variables and constants, and how they are declared and initialized in programs. Rules for naming variables are also described. - Common operators like arithmetic, assignment, comparison, and logical operators. How they are represented and used in programming. - The hierarchy of operations and how expressions are evaluated in programs.

Uploaded by

khai nisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

PROGRAMMING AND COMPUTER-BASED PROBLEM SOLVING (SSE5101)

TOPIC 2 –
Basic Data Types and Operations

 Data types, identifiers, variables and constants

 Operation and data conversion

 Input/output from console


1
At the end of this topic, student
should be able to:
Learning
Outcomes  Using the suitable variable
type to represent data (C3)
 Differentiate between
variables and constants (C2)
 Identify operators, operands
and resultants (C2)
 Use commands to read input
and display output from/to
console (C3)
Identifiers
• An identifier is a sequence of characters that consist of
letters, digits, underscores (_), and dollar signs ($).
• An identifier must begin with a letter, an underscore (_),
or a dollar sign ($).
• An identifier cannot:
– be a reserved word, e.g., true, false, or null
– start with a digit
– contain a (blank) space
– contain special characters, except _ and $
• An identifier can be of any length.

3
Identifiers
Examples
Valid identifiers: Invalid identifiers:
$2 2A
ComputeArea *salary
area gross-salary
radius class
showMessageDialog public
payRate String
salary income tax
J42Stop
gross_salary
4
Identifiers
Conventions for choosing identifier names:
• Name according to what it represents
• Consistent in the use of variable names
• Consistent when using upper and lowercase characters
– Variable name – lowercase, or mix-case
– Constant name – uppercase (all caps)

5
Data
• Data used in processing
• Constant is a value that never changes during the
processing of all instructions in a solution
– Constant is given a location in memory and a name.
• Variable – value may change during processing

6
Constants and
Variables on the Computer

7
Constants
• Represents permanent data that never changes.
• Use keyword (reserve word) final

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;


final int SIZE = 3;

8
Constants and
Variables on the Computer

9
Variable Names

10
Rules for Naming and Using
Variables
1. Name a variable according to what it
represents.
2. Do not use spaces.
3. Start a variable name with a letter.
4. Do not use a dash or any other symbol
that is used as a mathematical operator.

11
Rules for Naming and Using
Variables
5. Consistent usage of variable name.
6. Consistent use of upper, lowercase characters in
variable names
7. If the name consists of several words, concatenate all in
one, use lowercase for the first word, and capitalize the
first letter of each subsequent word in the name. For
example, the variables radius and area, and the
method computeArea.
8. Use naming convention specified by your company

12
Naming Conventions, cont.
• Choose meaningful and descriptive names

– Constants:
• Capitalize all letters in constants, and use underscores to
connect words. For example, the constant PI and
MAX_VALUE

– Class names:
• Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.

13
Processing Data—How a
Computer Balances a
Checkbook

14
Data Types
• Data are unorganized facts
• Goes as input and is processed to produce output,
information
• Computers must be told the data type of each variable
and constant
• Three common types: numeric, character, and logical
• Other data types: date data type and user-defined data
types

15
Data Types
and Their Data Sets

16
Examples of Data Types

0-17
Examples of Data Types

0-18
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;

19
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;

20
Declaring and Initializing
in One Step
From previous example:

int x;
double radius;
char a; int x = 1;
double radius = 1.0;
x = 1;
char a = ‘A’;
radius = 1.0;
a = ‘A’;

21
Declaring and Initializing
in One Step
• Example:
– Declare all variables involved in the following
problem:
• Find the sum and difference of two integer
numbers where the value of the first integer is 20
and the second integer is 35.

22
Numerical Data Types

23
Numerical Data Types
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
24
Numerical Data Types

25
Operators and Their Computer
Symbols

26
Mod/Remainder Operator
Remainder is very useful in programming. For example,
an even number % 2 is always 0 and an odd number % 2
is always 1. So you can use this property to determine
whether a number is even or odd. Suppose today is
Saturday and you and your friends are going to meet
in 10 days. What day is in 10 days? You can find that
day is Tuesday using the following expression:

27
Augmented Assignment Operators

28
Increment and
Decrement Operators

29
Increment and
Decrement Operators, cont.

30
Operators and Their Computer
Symbols

31
Definitions of the Logical
Operators

32
Definitions of the Logical
Operators

33
Definitions of the Logical
Operators

34
Hierarchy of Operations

35
Hierarchy of Operations

36
Expressions and Equations

37
Evaluating a Mathematical
Expression

0-38
Evaluating a Relational
Expression

39
Evaluating a Logical Expression

40
Evaluating an Equation That
Uses Both Relational and
Logical Operators

41
Developing a Table of All Possible
Resultants of a Logical Expression
• One unknown—A.
• Two combinations: A can be either True or
False.

42
Developing a Table of All Possible
Resultants of a Logical Expression
• Two unknowns—A and B.
• Four combinations: B can be either True or
False for each value of A..

43
Developing a Table of All Possible
Resultants of a Logical Expression
• Three unknowns—A, B, and C.
• Eight combinations.

44
Developing a Table of All Possible
Resultants of a Logical Expression

45
Introducing Java Programming
with an Example
Computing the Area of a Circle
This program computes the area of the
circle.
ComputeArea

46
animation

Trace a Program Execution


allocate memory
public class ComputeArea {
/** Main method */ for radius
public static void main(String[] args) {
double radius; radius no value
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}

47
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius no value
double area; area no value
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}

48
animation

Trace a Program Execution


public class ComputeArea { assign 20 to radius
/** Main method */
public static void main(String[] args) {
double radius; radius 20
double area;
area no value
// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}

49
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
// Assign a radius
radius = 20;

// Compute area compute area and assign it


area = radius * radius * 3.14159; to variable area

// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}

50
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}

51
Reading Input from the Console
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double
value. For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();

ComputeAreaWithConsoleInput Run

ComputeAverage Run
52
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();

53
Getting Input Using Scanner
// Example 2.1 Computing the Area of a Circle
import java.util.Scanner;
public class Circle {
/** Main method */
public static void main(String[] args) {
double radius; Create
scanner
double area; // Read radius from user object
// Assign a radius Scanner scanner = new Scanner(System.in);
Prompt
radius = 20; System.out.print("Enter radius: "); user
// Compute area radius = scanner.nextDouble();
Read
area = radius * radius * 3.14159; data
// Display results
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
}
54
Getting Input Using Scanner
import java.util.Scanner;
public class ReadInput {
public static void main(String[] args) { Create scanner object ONCE,
can be used many times
Scanner scanner = new Scanner(System.in);
// Read input from user
System.out.print("Enter radius: ");
double radius = scanner.nextDouble();
System.out.print("Enter an integer number: ");
int number = scanner.nextInt();
// …. More statements…
}
}

55
Common Error 1:
Undeclared/Uninitialized Variables
and Unused Variables
double interestRate = 0.05;
double interest = interestrate * 45;

56
Common Error 2: Integer Overflow

int value = 2147483647 + 1;


// value will actually be -2147483648

57
Common Error 3: Round-off Errors

System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);

System.out.println(1.0 - 0.9);

58
Common Error 4: Unintended
Integer Division

59
Common Pitfall 1: Redundant
Input Objects
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
 
Scanner input1 = new Scanner(System.in);
System.out.print("Enter a double value: ");
double v2 = input1.nextDouble();
60

You might also like