![Page 1: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/1.jpg)
Fundamentals of Programming
Budditha HettigeDepartment of Computer Science
![Page 2: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/2.jpg)
Fundamentals of Programming
Course Structure• Lectures
– 30 Hours
• Practical /Tutorials
– 30 hours
• Assignment (30%)
– Practical Test (10 marks)
– Quizzes 4 (Get maximum 2 ) (10 x2 = 20 marks )
– Total 30%
• Final Examination (70 %)
– 4 Questions (3 hours) (70%)
• Reference:– https://budditha.wordpress.com/
7/11/2015 Budditha Hettige ([email protected]) 2
![Page 3: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/3.jpg)
Course Structure
• Introduction
• Steps to Computer Programming
• Introduction to Java
• Input and output
• Variables
• Selections (if, switch)
• Loops (for, while, do-while)
• Methods• To-Down Design• Arrays
• Console application
• Menu driven application development3Budditha Hettige ([email protected])
![Page 6: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/6.jpg)
Machines• Café Machine make a tea
• ATM machine money transaction
• Calculator solve equation
• Each Machine has …
– Input
– Output
– Process
6Budditha Hettige ([email protected])
input Process Output
![Page 7: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/7.jpg)
Café Machine
• Input– Sugar
– Water
– Coffee
– Milk
• Output– Tea
• Process– ????
Input
Output
Process
7Budditha Hettige ([email protected])
![Page 10: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/10.jpg)
Process of a Machine
• Work through the instructions given to it– Read
– Write
– Do some calculations
– Go to next instruction
• The sequence of instructions is call a
Program
• Program is a story to tell some task to others
10Budditha Hettige ([email protected])
![Page 11: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/11.jpg)
What is a Programming?
• Programming is the way to give instructions
• Each program has– Start
– Some work
– End
• Program is given/execute through the machine understood language
11Budditha Hettige ([email protected])
![Page 12: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/12.jpg)
A Computer
• Machine that can solve problems for
people by carrying out instructions given to it
• The sequence of instructions is called Program
• The language machine can understand is called machine language
12Budditha Hettige ([email protected])
![Page 13: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/13.jpg)
Machine Language
• Machine language is a set of instructions
executed directly by a computer's central processing unit (CPU)
13Budditha Hettige ([email protected])
![Page 14: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/14.jpg)
Machine Language contd..
• Advantages– Machine can directly access (Electronic circuit)
– High Speed
• Disadvantages– Human cannot identify
– Machine depended
(Hardware depended)
14Budditha Hettige ([email protected])
![Page 15: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/15.jpg)
Computer Language(s)
• Artificial Languages
• Human readable
• Ex: C/C++, Java
Programming Language(Ln)
Machine Language (L0)
Translator/ Interpreter
Machine
Programming
Language(Ln)
Machine Language
(L0)
15Budditha Hettige ([email protected])
![Page 16: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/16.jpg)
Interpretation
• Each instruction in L1 can execute through the relevant L0 instructions directly
• Program is call interpreter
• Two languages are needed to closely
related
• Example:
• Sinhala – English
• Sinhala - Pali
16Budditha Hettige ([email protected])
![Page 17: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/17.jpg)
Translator (L1 L0)
1. Replace each instruction written in L1 in to LO
2. Program now execute new program
3. Program is called compiler/ translator
17Budditha Hettige ([email protected])
![Page 18: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/18.jpg)
Compilers
• Translate high-level language to machine language
• Input (Source code)– The original program in a high level language
• Output (Object code/ Machine Code)– The translated version in machine language
• Example– C++ compiler, JAVA Compiler etc.
18Budditha Hettige ([email protected])
![Page 19: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/19.jpg)
Programming steps
7/11/2015 Budditha Hettige ([email protected]) 19
• Compilers. A compiler translates the source code to target code
• Preprocessor—adds to or modifies the contents of the source file before the compiler begins processing the code
• Linker—combines the compiler-generated machine code with precompiled library code orcompiled code from other sources to make a complete executable program
![Page 20: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/20.jpg)
Programming language
generations
This classification is used to indicate increasing power of programming styles
1. First-generation programming languages
2. Second-generation programming languages
3. Third-generation programming languages
4. Fourth-generation programming languages
5. Fifth-generation programming languages
20Budditha Hettige ([email protected])
![Page 21: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/21.jpg)
First-generation programming
language (1GL)• Is a machine-level programming language
• Translator isn’t used to compile
• The instructions in 1GL are made of binary numbers, represented by 1s and 0s
• Advantage
– The code can run very fast and very efficiently
because the instructions are executed directly by the CPU
• Disadvantage
– When an error occurs, the code is not as easy to fix
21Budditha Hettige ([email protected])
![Page 22: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/22.jpg)
Second-generation
programming language(2GL)
• Assembly language.
• Properties
– The code can be read and written by a programmer
– The language is specific to a particular processor family and environment
• Used in kernels and device drivers
22Budditha Hettige ([email protected])
![Page 23: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/23.jpg)
Third-generation programming
languages (3GL)• Languages are more programmer-friendly
• Example
– C, C++, C#, Java, BASIC and Pascal
• Support structured programming.
• Must be translated into machine language by a compiler or interpreter
• Advantages
– Easier to read, write, and maintain
23Budditha Hettige ([email protected])
![Page 24: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/24.jpg)
Fourth-generation programming
languages(4GL)• Designed to reduce programming effort
• Consist of – Set of libraries
– CRUD generators
– Report generators
– DBMS
– Visual design tool and integration API
• Different types of 4GLs– Table-driven (codeless) programming
• PowerBuilder
– Data management• SAS, SPSS
– Report-generator programming languages • Oracle Developer Suite
24Budditha Hettige ([email protected])
![Page 25: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/25.jpg)
Fifth-generation programming
language(5GL)
• Based on solving problems using constraints given to
the program, rather than using an algorithm written by a programmer
• Use mainly in Artificial Intelligence research
• Example– Prolog, OPS5, and Mercury
25Budditha Hettige ([email protected])
![Page 26: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/26.jpg)
Selecting a suitable Computer
Language
• Readability
• How easy to write the program in this particular language?
• Reliability
• How much would it cost to develop using a given language?
• How complicated the syntax going to be?
• Does the language have standards for greater readability?
26Budditha Hettige ([email protected])
![Page 27: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/27.jpg)
Example
• Assembly Language Program– Device drivers, Virus
• C / C++ Program– Device drivers , DLLs
• JAVA / Visual C++ Program– Desktop applications, Web Applications
• Prolog– AI based applications, Games, Translators
27Budditha Hettige ([email protected])
![Page 28: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/28.jpg)
Exercise
• To solve the following problems, identify the input, output and the process
Find the area of a room
Search a place of a city
Calculate grade for the given mark
Get some amount from ATM machine
28Budditha Hettige ([email protected])
input Process Output
![Page 29: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/29.jpg)
Stages of Computer
Programming1. Planning
2. Analysis
3. Design
4. Implementation
5. Testing
6. Maintenance and update
29Budditha Hettige ([email protected])
![Page 30: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/30.jpg)
Planning
• Identify scope of the project
• Estimate the work involved
• Create a project schedule
• Begins with requirements that define the software to be developed.
• The project plan can be used to describe the task
30Budditha Hettige ([email protected])
![Page 31: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/31.jpg)
Analysis
• Is a complete description of the behavior of a system
• Consist of – Functional requirements
– Non-functional requirements
• Methods– Interview
– Questionnaires
– Observation etc.
31Budditha Hettige ([email protected])
![Page 32: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/32.jpg)
Software Design
• Software design is a process of problem-solving and planning for a software solution
• Types– Top down
– Bottom up
– Module design
• Use to describe– Algorithm
– Flowchart
– Pseudo code
32Budditha Hettige ([email protected])
![Page 34: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/34.jpg)
Design cont.…
• Flowchart:– is a type of diagram that
represents an algorithm or process
– Gives diagrammatic
representation solution to a given problem
– Use in analyzing, designing,
documenting or managing a process or program
34Budditha Hettige ([email protected])
![Page 38: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/38.jpg)
Example
• Draw a flow chart to display total of the two numbers
38Budditha Hettige ([email protected])
![Page 39: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/39.jpg)
Example
• Draw a flow chart to identify correct login for the following interface
39Budditha Hettige ([email protected])
![Page 40: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/40.jpg)
Implementation
• Is a realization of a technical specification or
algorithm as a program, software component through computer programming
• May exist for a given specification or standard– Example World Wide Web Consortium-recommended
specifications
• After implement– Source code, together with documentation to make
the code more readable.
40Budditha Hettige ([email protected])
![Page 41: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/41.jpg)
Program Testing
• is an investigation conducted to provide
information about the quality of the product
• A primary purpose of testing is to detect software failures
• Test– White box test
– Black box test
41Budditha Hettige ([email protected])
![Page 42: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/42.jpg)
Program Maintenance and
Update
• Is the modification of a software product after delivery to correct faults
• Maintenance may span for 20 years, whereas development may be 1-2 year
• User guide, Maintenance manual need to provides
• Example– Windows XP (Development few years, Maintenance ….)
– Provides set of software updates patches etc.
42Budditha Hettige ([email protected])
![Page 43: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/43.jpg)
The Characteristics of a Good
Computer ProgramReliability:
The program should provide correct results at all times and should be free from errors.
Maintainability:
The existing program should be able to change or modify to meet new requirements.
Portability:
The program should be able to transfer to a different computer system.
Readability:
The program must be readable and understandable with the help of documentation.
Performance:
The program should handle the task more quickly and efficiently.
Storage saving:
The program should be written with the least number of instructions
43Budditha Hettige ([email protected])
![Page 44: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/44.jpg)
Steps to Computer programming
• Identify Input, Output and process
• Make a design
• Use suitable programming language and implement your system
• Test your program
44Budditha Hettige ([email protected])
![Page 45: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/45.jpg)
Activity
• You are required to design a software solution for a
small library explain each steps of the software
development
1. Identify requirements
2. Write project proposal
3. Make Complete Design
4. Implement a solution
5. Test solution
7/11/2015 Budditha Hettige ([email protected]) 45
![Page 50: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/50.jpg)
Tools & Tips for
Computer programming
• Source code Editor– TextPad, Notepad
• Compiler– JAVA Compiler, C++ compiler
• Language Knowledge
• Logical thinking ability
50Budditha Hettige ([email protected])
![Page 51: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/51.jpg)
Programming with IDE
• IDE : integrated design environment
• consists of– source code editor
– compiler and/or an interpreter
– build automation tools
– Debugger
– Construction of a GUI
– Class browser
– Object inspector
– Etc.
51Budditha Hettige ([email protected])
![Page 52: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/52.jpg)
Programming IDEs
• Eclipse
Eclipse is a multi-language software development environment
http://www.eclipse.org/
• Code:blockshttp://www.codeblocks.org/
• Netbeanshttp://netbeans.org/
• Microsoft Visual Studio
http://www.microsoft.com/visualstudio/en-us
52Budditha Hettige ([email protected])
![Page 53: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/53.jpg)
Exercise
• Download Netbeans IDE and install it
• Download:
http://netbeans.org/
Budditha Hettige ([email protected]) 53
![Page 55: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/55.jpg)
Introduction to Java
• Is a programming language created by James
Gosling from Sun Microsystems in 1991
• Is a general-purpose, class-based, object-oriented
Programming language
• is intended to let application developers
“write once, run anywhere.”
• URL : http://www.oracle.com/
557/11/2015 Budditha Hettige ([email protected])
![Page 56: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/56.jpg)
Java cont…
• Java programming language consists
– Java compiler:
Java compiler translates Java coding into
byte-code
– Java virtual machine(JVM)
Java virtual machine interprets this byte-code and runs the program
– Java class libraries
Java Class Library is a set of dynamically loadable libraries that Java applications can call at run time
567/11/2015 Budditha Hettige ([email protected])
![Page 57: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/57.jpg)
Java Virtual Machine (JVM)
Architecture
JAVA Source File(.java)
JAVA Bite code(.class)
Java Compiler
Java Virtual Machine
Memory Manager
Byte code Verifier
Interpreter
Java API
577/11/2015 Budditha Hettige ([email protected])
![Page 58: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/58.jpg)
JAVA Vs C++
58
JAVA Source File(.java)
JAVA Bite code(.class)
Java Compiler
Java VM
C++ Source File(.cpp)
Executable program(.exe)
C++ Compiler
Operating system can
directly execute
JAVA VM can directly execute
7/11/2015 Budditha Hettige ([email protected])
![Page 59: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/59.jpg)
Programming
• Task
– Design
– Implement
– Compile and RUN
7/11/2015 Budditha Hettige ([email protected]) 59
![Page 60: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/60.jpg)
Characteristics
Java has the following properties
• Platform independent
• Object-orientated programming language
• Strongly-typed programming language
• Interpreted and compiled language
• Automatic memory management
607/11/2015 Budditha Hettige ([email protected])
![Page 61: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/61.jpg)
Platform independent
• Can Run on any Platform
– Windows
– Linux
– MaC OS
61
JAVA Source File(.java)
JAVA Bite code(.class)
Java Compiler
Java VM
7/11/2015 Budditha Hettige ([email protected])
![Page 62: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/62.jpg)
Object-orientated programming
language
• Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (State) and methods (Behavior)
• What is an Objects?
– Object is a software bundle of related state and behavior
– Characteristics: state and behavior
– Example (Person)
• State (Name, NIC, height)
• Behavior (Speech, Sleep, eat)
– Object is an instance of a class (instance is a specific realization of any object)
627/11/2015 Budditha Hettige ([email protected])
![Page 63: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/63.jpg)
What is a class?
• A Java class is a group of Java methods and
variables
• Example (Person)
– State (Name, NIC, height)
– Behavior (Speech, Sleep, eat)
63
class Person
{
}
7/11/2015 Budditha Hettige ([email protected])
![Page 64: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/64.jpg)
Requirements for Class name
• Class name must begin with letter of the alphabet
• Contains only letters, digits, underscores or dollar
sign
• Cannot be a language reserved keywords (public,
class etc)
• Name cannot be following values (true, false or null)
647/11/2015 Budditha Hettige ([email protected])
![Page 65: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/65.jpg)
Java Class name standard
• Begin with uppercase letter
• No spaces
• Emphasizes new word with an initial uppercase letter
• Example
– EmployeRecords
– Student
– FirstExample
– SampleProgram
657/11/2015 Budditha Hettige ([email protected])
![Page 67: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/67.jpg)
Install JAVA
• Download Java Development Kit (JDK)
– http://www.oracle.com
• RUN Installation setup
• SET PATH for the JAVA
• Test JAVA is working
677/11/2015 Budditha Hettige ([email protected])
![Page 68: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/68.jpg)
Simple Java Program• A class with a main Method
public class FirstProgram{
public static void main(String[] args) {
System.out.println("Hello");}
}
68
Operating System
FirstProgram
Main MethodOutput
7/11/2015 Budditha Hettige ([email protected])
![Page 69: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/69.jpg)
A class without a main Methodpublic class FirstProgram{
}
Operating System Can not Execute the program
69
Operating System
FirstProgram
7/11/2015 Budditha Hettige ([email protected])
![Page 70: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/70.jpg)
Your first Java program
• Open a text editor (text pad, Notepad etc.)
• Type the following sample
• Save program as “FirstProgram.java”
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
707/11/2015 Budditha Hettige ([email protected])
![Page 71: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/71.jpg)
Compile and RUN
• Using command prompt go to the place where in
your java file
• To Compile: type javac <space><fileName>
Javac FirstProgram.java
• To Run: type java<space><fileName>
Java FirstProgram
717/11/2015 Budditha Hettige ([email protected])
![Page 72: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/72.jpg)
First Java program
72
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Flow chart
Start
Display“Hello World”
End
7/11/2015 Budditha Hettige ([email protected])
![Page 73: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/73.jpg)
Print Output on Command window
• Function
– System.out.println(“Some Text”);
– System.out.print(“Some Text”);
• Examplepublic class FirstProgram{
public static void main(String[] args) {
System.out.println("Hello World");}
}
737/11/2015 Budditha Hettige ([email protected])
![Page 74: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/74.jpg)
Escape Sequences• A character preceded by a backslash (\) is an escape
sequence
• has special meaning to the compiler
Escape Sequence Description
\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 formfeed 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.
747/11/2015 Budditha Hettige ([email protected])
![Page 75: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/75.jpg)
ASCII art with JAVA• ASCII art is a graphic design technique that uses computers for
presentation and consists of pictures pieced together from the
95 printable (from a total of 128) characters defined by the
ASCII Standard from
75
System.out.println(" @ @ @ ");
System.out.println(" @ @ @ @ ");
System.out.println(" @ @ @ ");
System.out.println(" \\|/ ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" ( ) ");
System.out.println(" ^^^^^ ");
7/11/2015 Budditha Hettige ([email protected])
![Page 76: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/76.jpg)
Exercise1. Why JAVA is Platform independent?
2. What is a class ?
3. What is JAVA Runtime Environment (JRE) ?
4. What is JAVA Class file?
5. Create a JAVA Program to display the following
output
76
EMPLOYEE DETAILS
---------------------------------------
1. Employee NO : HIT1001
2. Name : Mr. A. B. Gamage
3. Age : 30
4. Salary : 23507.50
5. Married : Y
6. Car (Y/N) : N
---------------------------------------7/11/2015 Budditha Hettige ([email protected])
![Page 78: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/78.jpg)
Exercise
• Write a Java program to calculate and display total
amount of the given unit price and quantity of an
item.
![Page 79: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/79.jpg)
Data Types
• Use various variables to store various information
• Variables are reserved memory locations to store
values
• When you create a variable you reserve some
space in memory
• Based on the data type of a variable, the operating
system allocates memory and decides what can be
stored in the reserved memory
![Page 81: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/81.jpg)
Java Data types
• The eight primitive data types supported by the Java
programming language
81Budditha Hettige ([email protected])7/11/2015
![Page 84: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/84.jpg)
Default values
• The default values for the above data type
84
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false
Budditha Hettige ([email protected])7/11/2015
![Page 88: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/88.jpg)
Java Variables
• Declaration
– DataType <space> VaribaleName
• Example:
– Int count;
– Int studentAge = 0;
– float GPA = 3.56
88Budditha Hettige ([email protected])7/11/2015
![Page 89: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/89.jpg)
Variable name( rules)
• Can use only letters, digits and underscore
• The first character must be a simple letter
• Case sensitive
• Cannot use keywords
• No limits on length
![Page 92: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/92.jpg)
Exercise
• Write a Java program to calculate and display total
amount of the given unit price and quantity of an
item.
• Solution
– What are the input(s)
– What are the output(s)
– How many variable are required
– What is the process
![Page 94: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/94.jpg)
Constant variables
• Constants are declared like variables with
the addition of the static final keywords
static final double PI = 3.14159;
Once declared and initialized, a constant
can be used like a variable
• A constant may not be reassigned
7/11/2015 Budditha Hettige ([email protected]) 94
![Page 95: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/95.jpg)
Type Casting
• A way to convert a variable from one data type to
another data type
• Use cast operator
– (type_name) expression
– (int) float_value
![Page 96: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/96.jpg)
Java Memory concept
• Variable names correspond to location in the computer’s
memory
• Every variable has a name, a type, a size and a value
• A memory cell is never empty. But its initial contents may
be meaningless to your program.
• The current contents of a memory cell are destroyed
whenever new information is placed in that cell.
• Java has Total control about variables
• Programmer’s No need to think much
![Page 97: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/97.jpg)
Input / Output
![Page 98: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/98.jpg)
Input from Keyboard
• Use Scanner
– import java.util.Scanner;
• Create a Scanner object to Scan input from command window
– Scanner input = new Scanner(System.in);
• Read value from keyboard
– nextInt() interger
– nextLine () String
– nextFloat() float
– nextDouble ()double
• Example
– value = input.nextInt();
98Budditha Hettige ([email protected])7/11/2015
![Page 99: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/99.jpg)
Example
• Create a JAVA Program that read data from key
bard and display the result as follows
99
EMPLOYEE DETAILS
---------------------------------------
1. Employee NO : HIT1001
2. Name : Mr. A. B. Gamage
2. Age : 30
3. Salary : 23507.50
4. Married : Y
5. Car (Y/N) : N
---------------------------------------
Budditha Hettige ([email protected])7/11/2015
![Page 100: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/100.jpg)
Exercise
• Write a Java program to read two numbers from
keyboard and display the Average.
![Page 101: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/101.jpg)
Solutionimport java.util.Scanner;
public class FundamantalApp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int value1, value2;
float avg;
System.out.println("Enter value1 :");
value1 = input.nextInt();
System.out.println("Enter value1 :");
value2 = input.nextInt();
avg = (value1 + value2)/2;
System.out.println("Average is "+ avg);
} }
101Budditha Hettige ([email protected])7/11/2015
![Page 102: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/102.jpg)
Examples:
• Create a java program to calculate area of a room.
102Budditha Hettige ([email protected])7/11/2015
![Page 104: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/104.jpg)
Example
• Create a java program to calculate area of the
following figure
104
a
b
c
Budditha Hettige ([email protected])7/11/2015
![Page 106: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/106.jpg)
Exercise
Create a Java program to calculate and display total
amount of given unit price and quantity of the some
item.
![Page 108: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/108.jpg)
Java Formatting
• System.out.format("%f, %1$+020.10f %n", Math.PI);
7/11/2015 Budditha Hettige ([email protected]) 108
![Page 109: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/109.jpg)
Example
Write a C++ program which will convert a weight in KG
to pounds and ounces.
( 1 Kg = 2.2046 pounds)
![Page 110: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/110.jpg)
Example
• Write a C++ program which will convert a Celsius
temperature into Fahrenheit
![Page 111: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/111.jpg)
Operators
![Page 113: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/113.jpg)
Java Operators• Assignment operator
– (=)
• Arithmetic operators
– ( +, -, *, /, % )
• Compound assignment
– (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
• Increment and decrement
– (++, --)
• Relational and comparison operators
– ( ==, !=, >, <, >=, <= )
• Logical operators
– ( !, &&, || )
• Conditional ternary operator
– ( ? )
• Comma operator
– ( , )
Budditha Hettige ([email protected])
![Page 115: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/115.jpg)
Binary Operators
• The binary operators take two arguments as
operands
Budditha Hettige ([email protected])
Left Operand
RightOperand
Operator
![Page 116: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/116.jpg)
Unary Operators
The unary operators take one arguments as operand
Budditha Hettige ([email protected])
Operand Operator
![Page 117: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/117.jpg)
Assignment operator (=)
• The assignment operator assigns a value to a
variable.
Budditha Hettige ([email protected])
![Page 118: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/118.jpg)
Arithmetic operators( +, -, *, /, % )
• The five arithmetical operations supported by C++
are
Budditha Hettige ([email protected])
![Page 120: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/120.jpg)
Combined assignment operators
• Each arithmetic operator has a corresponding
assignment operator.
Budditha Hettige ([email protected])
Operator EffectL = Left Operator
R = Right Operator
+= Assign (L + R) to L
-= Assign (L - R) to L
*= Assign (L * R) to L
/= Assign (L / R) to L
%= Assign (L % R) to L
![Page 122: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/122.jpg)
Unary Operators
The unary operators take one arguments
- unary minus (negation)
+ unary plus
-- decrement
++ increment
Budditha Hettige ([email protected])
![Page 123: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/123.jpg)
Unary Operators
• The unary minus (-) makes a positive number into a
negative number and a negative number into a
positive number.
• The unary plus (+) does not change the number.
• The decrement operator (--) decrements the value of
its operand by 1.
• The increment operator (++) increments the value of
its operand by 1.
Budditha Hettige ([email protected])
![Page 124: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/124.jpg)
The prefix version (++x or --x)
• Comes before the operand, as in ++x
• First increments or decrements the variable by 1
and then uses the value of the variable.
Budditha Hettige ([email protected])
meansChange xThen assign to yy = 6, x = 6.
![Page 125: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/125.jpg)
The postfix version (x++ or x--)
• Comes after the operand, as in x++
• Uses the current value of the variable and then
increment or decrements the variable by 1.
Budditha Hettige ([email protected])
meansAssign z to y.Then change z.y is 5, z is 6.
![Page 126: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/126.jpg)
Relational and comparison operators
• The result of such an operation is either true or false
(i.e., a Boolean value)
Budditha Hettige ([email protected])
![Page 128: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/128.jpg)
Logical Operators
• To combine or modify existing expressions.
! NOT
&& AND
| | OR
• Example
a > 5 && b > 5
ch == ‘y’ | | ch == ‘Y’
!valid
!(x > 5)
Budditha Hettige ([email protected])
![Page 129: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/129.jpg)
Conditional ternary operator ( ? )
• The conditional operator evaluates an expression,
returning one value if that expression evaluates to
true, and a different one if the expression evaluates
as false.
• Syntax is:
condition ? result1 : result2
Budditha Hettige ([email protected])
![Page 130: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/130.jpg)
Bitwise operators
( &, |, ^, ~, <<, >> )
Bitwise operators modify variables considering the bit
patterns that represent the values they store.
Budditha Hettige ([email protected])
![Page 132: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/132.jpg)
Selections
![Page 133: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/133.jpg)
Exercise
• Write a Java program to read an examination mark
from keyboard and print “pass” if mark >= 40.
Budditha Hettige ([email protected])
![Page 134: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/134.jpg)
Java Selection (if Statement)
• One-Way (if) Selection
Syntaxif (expression)
{statement(s)
}
• Statement executed if value of expression true
Budditha Hettige ([email protected])
If m>=40
PASS
![Page 135: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/135.jpg)
Java Selection (if Statement)• Two-Way (if...else)
if (expression)
{
statement1;
}
else
{
statement2;
}
• If expression true, statement1 executed, otherwise statement2 executed
Budditha Hettige ([email protected])
![Page 136: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/136.jpg)
if-then-else Statement
• Syntaxif (Condition) {
statement(s)
}
else {
statement(s)
}
• Example
Budditha Hettige ([email protected])
![Page 137: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/137.jpg)
Exercise
Write a C++ program
to print the biggest
number of the two
given numbers
Budditha Hettige ([email protected])
![Page 138: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/138.jpg)
Exercise
Write a C++ program
to print the biggest
number of the three
given numbers
Budditha Hettige ([email protected])
![Page 139: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/139.jpg)
Exercise
• Write a C++ program to print the grade for a given
mark.
Budditha Hettige ([email protected])
1000 40 60 75
![Page 141: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/141.jpg)
Nested if and if...else Statements
Budditha Hettige ([email protected])
• Nesting: one control statement in another
• Syntax of nested if...else statements:
if (condition1)
statement1;
else if (condition2)
statement2;
. . .
else if (condition-n)
statement-n;
else statement-z;
![Page 143: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/143.jpg)
Exercise
Write a Java program
to print the grade for a
given marks
Budditha Hettige ([email protected])
![Page 144: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/144.jpg)
DebugDebugging is a methodical process of finding and
reducing the number of bugs, or defects
![Page 145: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/145.jpg)
Debug
• Programming errors are called bugs
• Programming errors may be
– Compiler errors
– Runtime errors
– Logical errors
• Going through the code, examining it and looking for
something wrong in the implementation (bugs) and
correcting them is called debugging
Budditha Hettige ([email protected])
![Page 146: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/146.jpg)
Debugging Options
• Make break points
– Right click on statement and add break point
• Add watches to see the values of the variables
– Click Debug – Debugging window - Watches
Budditha Hettige ([email protected])
![Page 147: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/147.jpg)
Debugging cont.…
• Start debugging (Press F8)
• Debug Commands
– F8 – Start / Continue
– F7 – Next Line
– Shift+F8 Stop
Budditha Hettige ([email protected])
![Page 148: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/148.jpg)
Exercise
• Write a C++ program that reads month as an integer
and print the name of the month
• Draw a flowchart to describe your solution
• Debug your code and find execution path for the
following
– Month = 1;
– Month = 12;
– Month = 6;
Budditha Hettige ([email protected])
![Page 149: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/149.jpg)
SwitchRun with number of possible execution paths
A switch works with the byte, short, char, and intprimitive data types
![Page 150: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/150.jpg)
Switch
• switch structure: alternate to if...else
• Example 1:
switch(x) {
case x1: statements1;
break;
case x2: statements2;
break;
…
default: statements4;
break;
}
Budditha Hettige ([email protected])
![Page 151: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/151.jpg)
Exercise 5.6
• Write a C++ program that reads month as an integer
and print the name of a month.
(Use Switch)
Budditha Hettige ([email protected])
![Page 153: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/153.jpg)
Exercise
• Write a Java program that reads day as an integer (1-7)and print the name
– Use only if-else statement and implement your solution
– Use switch statement and implement your solution
• Draw flowcharts to above two programs
• Compare performance of the two methods (if and Switch)
– Use debug and find the execution path
Budditha Hettige ([email protected])
![Page 154: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/154.jpg)
Exercise
• Write a JAVA program that reads day as an integer
(1-7)and print the name
154Budditha Hettige ([email protected])7/11/2015
![Page 155: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/155.jpg)
Exercise
• Write a Java program to display a given date as the
following formats.
Your program should read date as the three inputs
(day, month and year) and generates the output
forms.
– Option 1: 21.05.2001
– Option 2: 21.05.01
– Option 3: 21 st May 2001
Budditha Hettige ([email protected])
![Page 157: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/157.jpg)
Answer
1. Print a day
2. Print (st/nd/rd/th)
3. Print month
4. Print year
Budditha Hettige ([email protected])
![Page 158: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/158.jpg)
Exercise
• Write a Java program to print the bill for an item bought by a customer from a shop.
– The program should ask unit price and quantity of an item and calculate the total cost
– If item quantity greater than 10 give one item free
– Add 3.5 % discount for the total if total cost grater than 2500.
– The bill should contain all the above information and amount of money tendered and the correct amount of change.
Budditha Hettige ([email protected])
![Page 159: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/159.jpg)
Exercise
• Write a JAVA program to find the roots of the quadratic equation 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎 where xrepresents a variable or an unknown, and a, b, and c are constants.
• Hint: The following equation shows the solutions of the quadratic equation
• Read more on quadratic equation: http://en.wikipedia.org/wiki/Quadratic_equation
159Budditha Hettige ([email protected])7/11/2015
![Page 160: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/160.jpg)
LoopsLoops have as purpose to repeat a
statement a certain number of times or while a condition is fulfilled.
![Page 161: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/161.jpg)
Repetitions: Loops
• A loop is a sequence of instruction s that is
continually repeated until a certain condition is
reached.
• The statement may be repeated
– For a specific number of items
– For an indeterminate number of times, depending
on the truth or falsity of some condition.
Budditha Hettige ([email protected])
![Page 162: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/162.jpg)
Loops
• C++ provides three types of loops
– for loops (1- n times)
• Repeat a section of code known number of times
– while loops (0 – more times)
• Loop is used to repeat a specific block of code an unknown number of times
– do while loops (1 – more times)
• A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block
Budditha Hettige ([email protected])
![Page 163: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/163.jpg)
Repeat some work
• Do some repeated work
– Initialization (Start number)
– Condition (do repeat action until satisfy some condition)
– Update (Next Value)
• Example
– Initialization Start with 1
– Condition Count up to 50
– Update Count 1 by 1
Budditha Hettige ([email protected])
![Page 164: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/164.jpg)
For Loop• Syntax
for (initialization; condition; update)
{
statement(s)
}
• ExampleInitialization
Update
Condition
Budditha Hettige ([email protected])
![Page 165: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/165.jpg)
Exercise
Write a Java program to find the
factorial of a given number
Example:
Budditha Hettige ([email protected])
![Page 166: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/166.jpg)
Nested for Loops
• A loop can be nested inside of another loop
• Syntaxfor ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
Budditha Hettige ([email protected])
![Page 167: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/167.jpg)
Exercise
• Write a Java program to display the following
Multiplication table
Budditha Hettige ([email protected])
![Page 168: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/168.jpg)
Exercise
1. Write a Java program to print the
following figure
a) * b) 1
** 12
*** 123
**** 1234
***** 12345
Budditha Hettige ([email protected])
![Page 169: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/169.jpg)
While Loop
• Allows the repetition of a statement based on the
truth value of a condition
• Can run 0 to infinite times
Budditha Hettige ([email protected])
![Page 170: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/170.jpg)
While Loop
• Syntax
while (Condition)
{
statement(s)
}
• ExampleInitialization
Budditha Hettige ([email protected])
Condition
Update
![Page 171: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/171.jpg)
Exercise
Budditha Hettige ([email protected])
• Write a Java
program to find
the factorial of a
given number
(Use While loop)
![Page 172: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/172.jpg)
Exercise
• Write a Java program
to accept numbers
until the user enters a
999 and output the
sum of the given
numbers
Budditha Hettige ([email protected])
![Page 173: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/173.jpg)
Exercise
Create a java program to identify the given number is Palindrome number or not.
– Read number as an integer
– Find the number is Palindrome or not
– Print the results
Hint:A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461
Budditha Hettige ([email protected])
![Page 175: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/175.jpg)
Do-while Loop
• do…while loop also depends on a condition, but
unlike while loop, its condition is evaluated at the
bottom of the loop, after the body has already
executed.
Budditha Hettige ([email protected])
![Page 176: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/176.jpg)
Do-while Loop
• Syntaxdo
{
statement(s)
}
while (condition);
Example
Budditha Hettige ([email protected])
Update
Condition
Initialization
![Page 178: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/178.jpg)
Exercise
Accept numbers until the
user enters a 999 and
output the average of the
given numbers
Budditha Hettige ([email protected])
![Page 179: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/179.jpg)
Exercise
• Using do-while loop, create a java program to
display the selected option in the following menu;
[1] Enter data
[2] Print data
[3] Exit the program
do-while
Switch
Budditha Hettige ([email protected])
![Page 181: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/181.jpg)
Exercise
Write a Java program to read N number of integers
and find the total and average.
• N is an input 1, 2, 3….. N
• Use for, while and do-while loops
• Draw 3 flow chart for the above 3 programs
Budditha Hettige ([email protected])
![Page 182: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/182.jpg)
Exercise
Write a Java program to compute the gross pay for an
employee. An employee is paid at hourly rate for the
first 40 hours worked in a week. Any hours worked in
excess of 40 hours are paid at the overtime rate of one
and half times that. Your program should print the pay
sheets of all the employees.
Budditha Hettige ([email protected])
![Page 183: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/183.jpg)
Functions
and
Methods
![Page 184: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/184.jpg)
Exercise
• Create a Java program to display the following
wordArt.(MAM)
Budditha Hettige ([email protected]) 184
![Page 185: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/185.jpg)
Functions
• Functions are a basic building block for writing Java programs.
• Breaking a program up into separate functions, each of which performs a particular task, makes it easier to develop and debug a program
• Solution for code reuse
• Functions allow for breaking down the program into discrete units
• Use to modular Programing
Budditha Hettige ([email protected]) 185
![Page 186: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/186.jpg)
Methods
• Is a group of statements that is executed when it
is called from some point of the program.
• Types
– Built-in: Build-in methods are part of the compiler
package, such as
• System.out.println( )
– User-defined: User-defined methods are created by you
• Format
Return type MethodName (Argument..)
{ }
186Budditha Hettige ([email protected])7/11/2015
![Page 191: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/191.jpg)
Example
• Create a Java Programs to do to the following
1. Read Celsius temperature and calculate the
Fahrenheit temperature
Celsius to Fahrenheit: (°C × 9/5) + 32 = °F
Hint:
– A Method to read data from keyboard
– A Method to Calculate data
– A Method to Print Data
191Budditha Hettige ([email protected])7/11/2015
![Page 194: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/194.jpg)
Complete Program
194
getValue
Claculate
printData
Main
getValue
calculate
printData
Budditha Hettige ([email protected])7/11/2015
![Page 196: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/196.jpg)
Bottom-up Design
196
Main
getValue claculate printData
Budditha Hettige ([email protected])7/11/2015
![Page 198: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/198.jpg)
Example
• Create a JAVA Program that reads 3 integer values
from keyboard and print total and average values.
• Hint:
– Draw a top down design
– Implement your solution
198Budditha Hettige ([email protected])7/11/2015
![Page 199: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/199.jpg)
Arrays
![Page 200: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/200.jpg)
What is Array
• An array, is a data structure consisting of a
collection of elements
• Each identified by at least one array index or key
• simplest type of data structure is a linear array, also
called one-dimensional array
7/11/2015 Budditha Hettige ([email protected]) 200
![Page 201: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/201.jpg)
Arrays
• An array is a container object that holds a fixed
number of values of a single type
7/11/2015 Budditha Hettige ([email protected]) 201
![Page 202: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/202.jpg)
Arrays contd.
• Declares an array of integers
– int[] anArray;
• Allocates memory for 10 integers
– anArray = new int[10];
• Initialize first element
– anArray[0] = 100;
• Display Values
– System.out.println(“E0”+ anArray[0]);
7/11/2015 Budditha Hettige ([email protected]) 202
![Page 203: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/203.jpg)
Example
• Write a Java program to store marks of 10 students
and print average mark ? Output of your program is
as follows
Student Marks
1 67
2 89
...
---------------------
Average 45.56
=====================
Budditha Hettige ([email protected]) 2037/11/2015
![Page 204: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/204.jpg)
Exercise
• Write a Java program that read 10 integer marks
(Range 0 to 100) form keyboard and print the result
sheet as shown below.
7/11/2015 Budditha Hettige ([email protected]) 204
Write a Java program that read 10 integer marks (Range 0 to 100) form keyboard and print the result sheet as shown below.
ICT 126 2.0 Introduction to Computer Programming
------------------------------------------------
####### RESULT SHEET ######
------------------------------------------------
Index NO Marks
================================================
1 56
2 79
3 45
1. 23
...
![Page 205: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/205.jpg)
Find elements
• Find the largest and small mark of the above marks
array
7/11/2015 Budditha Hettige ([email protected]) 205
![Page 206: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/206.jpg)
Example (several arrays)
• Create 2 Arrays to store Student name and GPA
values of the 25 students in a class
• Enter all values and print results in the following
format
Student summary
---------------------
Saman Kumara 2.56
Ruwan Silva 1.94
7/11/2015 Budditha Hettige ([email protected]) 206
![Page 207: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/207.jpg)
Multiple Arrays
• Write a C++ program to store 10 student information
in class with considering name, age, height. Your
program should read 10 students information from
keyboard and display results as given below
name age height
--------------------------
saman 14 5.7
kamal 12 5.1
7/11/2015 Budditha Hettige ([email protected]) 207
![Page 208: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/208.jpg)
Console
Applications
![Page 209: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/209.jpg)
Console Application
• Runs only console screen(Command window)
• NO GUI
• Use Low resources
• Use only key board action
7/11/2015 Budditha Hettige ([email protected]) 209
![Page 215: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/215.jpg)
Example 4
• Create Java Application to calculate total price for
the sold items.
– Your program should run as a menu driven
application
– User can add items (any ) and print total, number
of items and discount
– User can total sales price for a day
– This program should run until user exit
7/11/2015 Budditha Hettige ([email protected]) 215
![Page 218: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/218.jpg)
Model Questions
![Page 219: Fundamentals of Programming - · PDF fileFundamentals of Programming Course Structure ... • Linker—combines the compiler- ... runs the program –Java class libraries](https://reader031.vdocuments.net/reader031/viewer/2022021818/5aad25dc7f8b9aa06a8df2e5/html5/thumbnails/219.jpg)
7/11/2015 Budditha Hettige ([email protected]) 219