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

Topic2-Basic Data Types and Operations

The document discusses basic data types and operations in programming. It covers key topics like data types, variables, constants, operators, and input/output. The intended learning outcomes are for students to understand how to represent data using appropriate types, differentiate variables from constants, identify operators and operands, and use input/output commands.

Uploaded by

Abdul Majid
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Topic2-Basic Data Types and Operations

The document discusses basic data types and operations in programming. It covers key topics like data types, variables, constants, operators, and input/output. The intended learning outcomes are for students to understand how to represent data using appropriate types, differentiate variables from constants, identify operators and operands, and use input/output commands.

Uploaded by

Abdul Majid
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

COURSE - Problem Solving and

Programming Concepts (CSC3100)

TOPIC 2 – Basic Data Types and


Operations
 Data types, identifiers, variables and constants

 Operation and data conversion

 Input/output from console

1
Learning Outcomes
• At the end of this topic, student should be able to:

o Using the suitable type to represent data (C3)


o Differentiate between variables and constants (C2)
o Identify operators, operands and resultants (C2)
o Use commands to read input and display output
from/to console (C3)

ASSESSMENT 1, FINAL EXAM

2
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
Name Range Storage Size

byte –27 to 27 – 1 (-128 to 127) 8-bit signed

short –215 to 215 – 1 (-32768 to 32767) 16-bit signed

int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed

long –263 to 263 – 1 64-bit signed


(i.e., -9223372036854775808 to 9223372036854775807)

float Negative range: 32-bit IEEE 754


-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324

Positive range:
4.9E-324 to 1.7976931348623157E+308

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
Name Range Storage Size

byte –27 to 27 – 1 (-128 to 127) 8-bit signed

short –215 to 215 – 1 (-32768 to 32767) 16-bit signed

int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed

long –263 to 263 – 1 64-bit signed


(i.e., -9223372036854775808 to 9223372036854775807)

float Negative range: 32-bit IEEE 754


-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324

Positive range:
4.9E-324 to 1.7976931348623157E+308

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:

Saturday is the 6th day in a week


A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days

27
Augmented Assignment Operators

28
Increment and
Decrement Operators

29
Increment and
Decrement Operators, cont.

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;

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 { for radius
/** Main method */
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
for area
// Compute 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 and assign
// Compute area it to variable area
area = radius * radius * 3.14159;

// 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();

Method Description

nextByte() reads an integer of the byte type.


nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
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; object
// Read radius from user
// 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
int number1 = 1; int number1 = 1;
int number2 = 2; int number2 = 2;
double average = (number1 + number2) / 2; double average = (number1 + number2) / 2.0;
System.out.println(average); System.out.println(average);

(a) (b)

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