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

CC1 - Computing Fundamentals: Chapter 1 Data Types, Variables, and Algorithm

The document provides an explanation of a simple Java "Hello World" program that prints "Hello World!" to the screen, including comments, the main method, and how output is accomplished using the println method. It also covers basic Java data types like integers, floating-point numbers, characters, booleans, and variables, and defines an algorithm as a process or set of rules to be followed to solve a problem.

Uploaded by

WAlEED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

CC1 - Computing Fundamentals: Chapter 1 Data Types, Variables, and Algorithm

The document provides an explanation of a simple Java "Hello World" program that prints "Hello World!" to the screen, including comments, the main method, and how output is accomplished using the println method. It also covers basic Java data types like integers, floating-point numbers, characters, booleans, and variables, and defines an algorithm as a process or set of rules to be followed to solve a problem.

Uploaded by

WAlEED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CC1 – Computing Fundamentals

Chapter 1 Data Types, Variables, and


Algorithm
BASIC JAVA PROGRAM
/* This is a simple Java program.
Call this file "HelloWorld.java". */
public class HelloWorld {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
Explanation
/* This is a simple Java program.
Call this file "HelloWorld.java". */
This is a comment. Like most other programming
languages, Java lets you enter a remark into a
program’s source file.
Explanation
public class HelloWorld {
 
This line uses the keyword class to declare that a
new class is being defined. HelloWorld is an
identifier that is the name of the class.

{ - start of a block
} – end of a block
Explanation
// Your program begins with a call to main().

This is the second type of comment supported


by Java. A single-line comment begins with a //
and ends at the end of the line.
Explanation
public static void main(String [ ] args) {

This line begins the main( ) method. As the


comment preceding it suggests, this is the line at
which the program will begin executing. All Java
applications begin execution by calling main( ).
Explanation
System.out.println("Hello World!");

This line outputs the string "Hello World!"


followed by a new line on the screen. Output is
actually accomplished by the built-in println( )
method.
Output
Hello World!
DATA TYPES
Integer
• Integers - This group includes byte, short, int,
and long, which are for whole-valued signed
numbers.
Name Width Range

–9,223,372,036,854,775,808
long 64
to 9,223,372,036,854,775,807

–2,147,483,648
int 32 to
2,147,483,647

short 16 –32,768 to 32,767


byte 8 –128 to 127
byte
• The smallest integer type is byte. This is a
signed 8-bit type that has a range from –128
to 127.

• byte z, x;
short
• short is a signed 16-bit type. It has a range
from –32,768 to 32,767. It is probably the
least-used Java type.

• short s;
• short t;
int
• The most commonly used integer type is int. It
is a signed 32-bit type that has a range from –
2,147,483,648 to 2,147,483,647.

• int a;
• int b;
long
• long is a signed 64-bit type and is useful for
those occasions where an int type is not large
enough to hold the desired value. The range
of a long is quite large.

• long c;
• long d;
Floating-Point Types
• Floating-point numbers, also known as real
numbers, are used when evaluating
expressions that require fractional precision.
Approximate
Name Width in Bits
Range

4.9e–324 to
double 64
1.8e+308

1.4e–045 to
float 32
3.4e+038
float
• float can be useful when representing dollars
and cents. Here are some example float
variable declarations:

• float highTemp, lowTemp;


double
• Double precision, as denoted by the double
keyword, uses 64 bits to store a value.

• double pi = 3.1416; //pi, approximately


• double r = 10.8; //radius
• double a = pi * r * r; //compute for the area
Characters
• In Java, the data type used to store characters
is char. char is a 16-bit type. The range of a
char is 0 to 65,536.

• ch1 = 88; // code for X


• ch2 = 'Y';
Booleans
• Java has a primitive type, called boolean, for
logical values. It can have only one of two
possible values, true or false.

• boolean b = false;
• boolean a = true;
Variables
• In Java, all variables must be declared before
they can be used. The basic form of a variable
declaration is shown here:
 
• type identifier [ = value ], [ identifier [= value ]
…];
ALGORITHM
Algorithm
• a process or set of rules to be followed in
calculations or other problem-solving
operations, especially by a computer.
Examples
• Write an algorithm to add two numbers entered
by user.
• Step 1: Start
• Step 2: Declare variables num1, num2 and sum.
• Step 3: Read values num1 and num2.
• Step 4: Add num1 and num2 and assign the result
to sum. sum←num1+num2
• Step 5: Display sum
• Step 6: Stop
Examples
Pseudo Code:
Write "Please enter a number"
Read colornum
If (colornum >0 and colornum <= 10)
Write blue
else If (colornum >0 and colornum <= 20)
Write red
else If (colornum >0 and colornum <= 30)
Write green
else
Write "not a correct color option"

You might also like