1 - Introduction, Flowchart, and Basics of Java Programming
1 - Introduction, Flowchart, and Basics of Java Programming
What is a
Computer?
A computer consists of a CPU,
memory, hard disk, floppy disk,
monitor, printer, and communication
devices.
Bus
Programs
Computer programs,
known as software,
are instructions to the
computer.
You tell a computer what to do through programs. Without
programs, a computer is an empty machine. Computers do
not understand human languages, so you need to use
computer languages to communicate with them.
1101101010011010
Programming
Languages
Machine Language Assembly Language
Assembly languages were developed to make programming
High-Level Language
easy. Since the computer cannot understand assembly
language, however, a program called assembler is used to
convert assembly language programs into machine code.
For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3
5
Assembly Source File
Machine Code File
…
Assemble …
ADDF3 R1, R2, R3 1101101010011010
r
… …
Programming
Languages
Machine Language Assembly Language 6
The high-level languages are English-like
High-Level and easy to
Language
learn and program. For example, the following is a high-
level language statement that computes the area of a circle
with radius 5:
area = 5 * 5 * 3.1415;
7
Ad Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
a language was developed for the Department of Defense and is used mainly in defense
BASI projects.
C Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used
C easily
by beginners.
C+ Developed at Bell Laboratories. C combines the power of an assembly language with the
ease of
+ use and portability of a high-level language.
C# C++ is an object-oriented language, based on C.
COBOL Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
FORTRA COmmon Business Oriented Language. Used for business applications.
N FORmula TRANslation. Popular for scientific and mathematical applications.
Pasca
Java
l Developed by Sun Microsystems, now part of Oracle. It is widely used for developing
Python platform-
independent Internet applications.
Visua Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is
l a
Basic simple, structured, general-purpose language primarily for teaching programming.
8
Interpreting/Compiling Source
• A program written Code
in a high-level language is
called a source program or source code.
• Because a computer cannot understand a source
program, a source program must be translated
into machine code for execution.
• The translation can be done using another
programming tool called an interpreter or a
compiler.
9
Compiling Source
A compiler translatesCode
the entire source code into a
machine-code file, and the machine-code file is
then executed, as shown in the following figure.
… … Output
Compile Executor
area = 5 * 5 * 3.1415; 0101100011011100
r 1111100011000100
...
…
1
0
Why C#?
The answer is that C# enables users to develop and
deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of
computing is being profoundly influenced by the
Internet, and C# promises to remain a big part of that
future.
C#’s History
• An Object Oriented Programming (OOP)
language created by Microsoft
• Runs on the .Net Framework
• Similar to C/C++ and Java
• Developed by Anders Hejlsberg
• First version was released in 2002. Current
version is V9 released in 2020
• https://docs.microsoft.com/en-
us/dotnet/csharp/whats-new/csharp-version-
history
1
6
Characteristics of C#
• C# Is Simple
• C# Is Object-Oriented
• C# Is Distributed
• C# Is Interpreted
• C# Is Robust
• C# Is Secure
• C# is Performance driven
• C# Is Multithreaded
• C# Is Dynamic
1
6
A Simple C# Program
Public class Welcome
{
public static void Main(string[] args){
Console.WriteLine(“Welcome to C#!”);
}
}
Creating, Compiling,
and Running Programs
Create/Modify Source Code
Trace a Program
Execution Enter main method
Trace a Program
Execution Execute statement
}
}
2
4
Anatomy of a C# Program
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks
2
5
Class
Name
Every C# program must have at least one class.
Each class has a name. By convention, class names
start with an uppercase letter. In this example, the
class name is Welcome.
Main
Method
Line 2 defines the main method. In order to run a
class, the class must contain a method named
Main. The program is executed from the Main
method.
Stateme
ntrepresents an action or a sequence of
A statement
actions. The statement Console.WriteLine("Welcome to
C#!") in the program in Listing 1.1 is a statement to
display the greeting "Welcome to C#!“.
}
}
2
8
Statement
Terminator
Every statement in C# ends with a semicolon (;).
Reserved
words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program.
Block
s
A pair of braces in a program forms a block that groups
components of a program.
Special
Character Name Description
Symbols
{} Opening and closing Denotes a block to enclose statements.
braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
3
2
{ …}
( … )
// …
"…"
Appropriate
Comments
Include a summary at the beginning of the
program to explain what the program does, its key
features, its supporting data structures, and any
unique techniques it uses.
Naming
• Conventions
Choose meaningful and descriptive names.
• Class names:
– Capitalize the first letter of each word in the
name. For example, the class name
ComputeExpression.
4
4
• Indentation
– Indent two spaces.
• Spacing
– Use blank line to separate segments of the
code.
4
5
Block
Styles
Next-line
Use end-of-line
public class Test
style {
style for braces.
public static void Main(string[] args)
{
Console.WriteLine("Block Styles");
}
}
End-of-line
style
public class Test {
public static void Main(string[] args) {
Console.WriteLine("Block Styles");
}
}
4
6
Programming
• Syntax Errors Errors
– Detected by the compiler
• Runtime Errors
– Causes the program to abort
• Logic Errors
– Produces incorrect result
4
7
Syntax
public class Errors
ShowSyntaxErrors {
public static void Main(string[] args)
{ Console.WriteLine("Welcome to C#);
}
}
4
8
Runtime
public class Errors
ShowRuntimeErrors {
public static void Main(string[] args) {
Console.WriteLine(1 / 0);
}
}
4
9
Logic
public class ShowLogicErrors {
public static void Main(string[] Errors
args)
{ Console.WriteLine("Celsius 35 is Fahrenheit degree
"); Console.WriteLine((9 / 5) * 35 + 32);
}
}
5
0
Escape Sequences for Special Characters
• PSEUDOCODE
• ALGORITHM
• FLOWCHART
DESIGNING TECHNIQUES
• A typical programming task can be divided in two
phases:
1) Problem solving phase
produce an ordered sequence of steps that
describe
solution of problem
this sequence of steps is called an algorithm
2) Implementation phase
implement the program in some programming
language
STEPS IN PROBLEM SOLVING
Ova D e n o t e s t h e b e g i n n i n g o r e n d of t h e p r o g r a m
l
R e c ta n g l D e n o t e s a p r o c e s s to b e c a r r i e d o u t
e e . g. a d d i t i o n , s u b t r a c t i o n , d i v i s i o n
e tc .
D e n o t e s a d e c i s i o n ( o r b r a n c h ) to b e m a d e .
Diamon
T h e p r o g r a m s h o u l d c o n t i n u e a l o n g o n e of
d
t w o r o u te s . ( e . g . IF / T H E N / E L S E )
Flowlin D e n o t e s t h e d i r e c t i o n of l o g i c f l o w in t h e p r o g r a
e m
EXAMPLE 2
Algorithm FLOWCHART
Step 1: Input
L_ft
Step 2:
STAR
Lcm L_ft x 30 T
Lcm L_ft x 30
L_cm
STOP
EXAMPLE 3
Algorithm
STAR
Step 1: Input W,L T
A LxW
Print
A
STOP
EXAMPLE 4
Pseudocode:
• Input the coefficients (a, b, c) of the quadratic
equation
• Calculate d
• Calculate x1
• Calculate x2
• Print x1 and x2
EXAMPLE 4
STAR
T
Algorithm:
Input
Step 1: Input a, b, c a, b, c
Step 2: d sqrt ( b x b – 4 x a x c
) d sqrt(b x b – 4 x a x c)
Step 3:
Step 4: x1 (–b + d) / (2 x a)
x2 (–b – d) / (2 x a) x1 (–b + d) / (2 x a)
Step 5:
Print x1, x2
X2 (–b – d) / (2 x a)
Print
x1 ,x2
STOP
DESIGN STRUCTURES
Y N
is
A>B
Print Print B
A
IF – THEN – ELSE STRUCTURE
If A>B then
print
Y N
A is
A>B
else B
print
Print B
endifPrint A
RELATIONAL OPERATORS
Input
VALUE1,VALUE2
Y is N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
NESTED IFS
N2 [N3>N2>N1]
else
MAX N3
endif
Step 3: Print “The largest number is”, MAX
endif
EXAMPLE 6
Input
N, Current
Max Current
Counter 1
Counter < N N
Y Print
Counter Counter +1
Max
Input STOP
Next
Next
>Max
Y
STD511: C# Programming
Basics of C# Programming
9
4
Identifier
• s
An identifier is a sequence of characters that consist of
letters, digits, underscores (_), and dollar signs ($).
• 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.
9
5
Declaring
int x;
Variables
// 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;
9
6
Assignment
x = 1; Statements
// Assign 1 to x;
• double d = 1.4;
9
8
Named
const datatype Constants=
CONSTANTNAME VALUE;
Naming
• Conventions
Choose meaningful and descriptive names.
• Variables and method names:
– Use lowercase. 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.
Naming Conventions,
• Class names: cont.
– Capitalize the first letter of each word in
the name. For example, the class name
ComputeArea.
• Constants:
– Capitalize all letters in constants, and use
underscores to connect words.
For example, the constant PI and
MAX_VALUE
100
Arithmetic
• operators
Arithmetic calculations used in most programs
– Usage
• * for multiplication
• / for division
• % for remainder
• +, -
– Integer division truncates remainder
7 / 5 evaluates to 1
– Remainder operator % returns the remainder
7 % 5 evaluates to 2
102
Operator precedence -
• Operator precedence I
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Use parenthesis when needed
– Example: Find the average of three variables b and
• Do a,
not use: a + b + c / 3 c
• Use: a + b + c ) /
( 3
103
Operator precedence -
II
104
How to Evaluate an Expression
3 + 4 * 4 + 5 * (4 + 3)
- 1 (1) inside parentheses
3 + 4 * 4 + 5 * 7 first
– 1 (2) multiplication
3 + 16 + 5 * 7
– 1 (3) multiplication
3 + 16 + 35 –
1 (4) addition
19 + 35 –
1 (5) addition
54 -
1 (6) subtraction
5
3
105
Arithmetic
Expressions
is translated to:
106
1
0
6
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
Console.WriteLine("The area for the circle of radius " +
radius + " is " + area);
}
}
1 animation
0
8
Trace a Program Execution
public class ComputeArea {
/** Main method */ memory
public static void Main(string[] args) {
double radius; radius no value
double area; no value
area
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;
// Display results
Console.WriteLine("The area for the circle of radius " +
radius + " is " + area);
}
}
1 animation
0
9
Trace a Program Execution
public class ComputeArea { assign 20 to radius
/** Main method */
public static void Main(string[] args) {
double radius; 20
radius
double area; no value
area
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
Console.WriteLine("The area for the circle of radius " +
radius + " is " + area);
}
}
1 animation
1
0
// Display results
Console.WriteLine("The area for the circle of radius " +
radius + " is " + area);
}
}
1 animation
1
1
Trace a Program Execution
public class ComputeArea {
/** Main method */ memory
public static void Main(string[] args) {
double radius; 20
radius
double area; 1256.636
area
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
Console.WriteLine("The area for the circle of radius " +
radius + " is " + area);
}
}
The area for the circle of radius 5 is 78.53975
1
1
2
Exercise
• Draw a flowchart and write a C# program to
read the radius from the console and compute
the area of the circle.
Problem: Displaying
Time
Write a program that obtains hours and
minutes from seconds.
1
1
5
Exponent
Operations
Console.WriteLine(Math.pow(2, 3));
// Displays 8.0
Console.WriteLine(Math.pow(4, 0.5));
// Displays 2.0
Console.WriteLine(Math.pow(2.5, 2));
// Displays 6.25
Console.WriteLine(Math.pow(2.5, -2));
// Displays 0.16
1
1
7
Number Literals
A literal is a constant value that appears directly
in the program. For example, 34, 1,000,000, and
5.0 are literals in the following statements:
int i = 34;
long x = 1000000;
double d = 5.0;
1
1
8
Increment and
Decrement Operators
Operator Name Description
++var preincrement The expression (++var) increments var by 1 and
evaluates to the new value in var after the increment.
Increment and
Decrement Operators, cont.
1
2
1
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)
range increases
String
Concatenation
// Three strings are concatenated
string message = "Welcome " + "to " + "X#";
Converting Strings to
Integers
To convert a string into an int value, you can use the
static ToInt32 method in the Convert class as
follows:
Converting Strings to
Doubles
To convert a string into a double value, you can use the
static ToDouble method in the Convert class as
follows:
double doubleValue
=Convert.ToDouble(doubleString);
Debugging
• Logic errors are called bugs. The process of finding and
correcting errors is called debugging.
Debugger
Debugger is a program that facilitates debugging. You
can use a debugger to
•Execute a single statement at a time.
•Trace into or stepping over a method.
•Set breakpoints.
•Display variables.
•Display call stack.
•Modify variables.
References
• C# Programming for Absolute Beginners;
Radek Vystavel.
• https://www.geeksforgeeks.org/csharp-progr
amming-language/
• https://docs.microsoft.com/en-us/dotnet/csh
arp/
• https://www.w3schools.com/cs