Java Introduction

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 88

Introduction to Problem

Solving

Sibi Chakkaravarthy Sethuraman


Associate Professor
School of Computer Science and Engineering
VIT-AP University
Module No. 1 Introduction to Problem Solving 5 hours

Introduction to computers, algorithms, Flow Charts, Problem solving concepts:


exchanging the values of two variables, Counting, Summation of a set of numbers,
Factorial computation, Sine function computation, Generation of the Fibonacci
sequence, Reversing the digits of an integer, Base conversion and character to
number conversion.
Text Books
1. Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill
Education,Tenth edition, 2017.

References
1.Paul Dietel, Harvey Dietel “Java How to Program”, Pearson Education Ltd, 10th
Edition, 2015.
2. Cay S. Horstmann and Gary Cornell, “Core Java: Volume I – Fundamentals”,
Sun Microsystems Press, Ninth Edition, 2013.
Objectives
• To provide essential skills for developing solutions in a structured,
algorithmic format using pseudo code
• To introduce implementation of the algorithms with computer
programming and presentation of results in a clear manner
• To develop problem solving skills and core programming techniques
Outcome
• On completion of the course, students will have the ability to
• Use basic data structures, and produce programs employing
them
• Express algorithms using sequencing, conditional and iterative
constructs
• Implement and solve mathematical and scientific problems in
Java language.
Course Components
Component Duration Weightage

CAT -I 90 Min 20%

CAT - II 90 Min 20%

CAT – III 90 Min 20%

Lab Evaluation 20%

Assignment ------ 20%


Points to ponder
• Attendance - based on biometric
• Lab evaluation - Every lab
• Modification in attendance – Not possible
• Note* - Timely submission of assignments.
What is a computer
• A computer is an electronic device capable of performing
computations, and we all know that it is composed of a monitor,
keyboard, mouse, and memory to store information. But the most
important component of the computer is a PROCESSOR.
Introduction to computer
• What is the motto of a computer? and Why it is built for?
• What is the name of the first computer?
What is the name of the first computer.

• 1840 – Analytical Engine Code (Mechanical general purpose computer)


• 1943 – ENIAC Coding System (Electronic general purpose computer)
Why you need programming languages?
Which language computer knows?
Introduction to Programming language

• 1949 – Brief Code (First assembly language)


• 1954 – Fortran (First High level language)
• 1972 – Smalltalk (First High level object oriented language)
32 bit vs 64 bit
8 –bit computing
How a human sees How a computer sees

Current day processor is capable of decoding 64-bit time


Problem statement
• Suppose if you want to tell the computer to add two numbers
(1+2) which is represented by some binary numbers (10000011),
how are you going to tell the computer?
Yes, we are going to use assembly language
to inform computer
• "Assembly Language is the most elementary form of software
development languages."
OMG! Computer knows only 0s and 1s
• Assembly code is Ok!
• Now, you informed computer about the assembly code
• Assembly code is also a low level programming language (But
machine knows only 0s and 1s)
Who is Assembler?
Assembler converts the assembly code into corresponding machine code
(110001..) and feeds to your PC (processor)
Constraint in execution
• PLATFORM – OS + Processor

• Most common platform – Windows + Intel (Wintel)

• Likewise, there are plenty of platforms (different OS, different


processors) in the world.

• Programming in one platform differs from other.


Platform independent
Procedural vs Object Oriented language
Procedural vs Object oriented language
Class and objects
Object vs (Object)Instance

Object
• Collection of objects
• Defines object properties,
behavior and structure.

Instance
• Concrete object
• Created from a particular
class

• Concrete objects has the physical reference to the base class,


Example: empire state building has reference to the building class
Top programming languages
• Java ranks number 1 in Rapid Application Development
• Depends upon the application
Where it is used?
• According to SUN, 3 billion devices
run java
• Some of them are as follows
• Desktop Applications:
• Acrobat reader, media player, antivirus
etc.
• Web Applications:
• irctc.co.in (old), javapoint.com etc.
• Enterprise Applications:
• Banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games etc
Introduction to JAVA
• JAVA was developed by Sun Microsystems Inc in 1995, later acquired
by Oracle Corporation.
• James Gosling and Patrick Naughton
Some common terminologies in Java
• Java Virtual Machine (JVM)
• Java Development Kit (JDK)
• Java Runtime Environment(JRE)
• Bytecode
Advantages of JAVA
• Java is a platform independent language
• Java is an object oriented language
How JVM works
Installation and Configuration
• Download the Java setup from the below link

https://www.oracle.com/technetwork/java/javase/downloads/ind
ex.html

• Add java to the system path, so that it can be accessed from


anywhere in the system
Flowchart and Algorithm
Role of a Software Engineer

• To develop Softwares

• Software Engineer uses various programming languages to


create programs.

• No restrictions in using programming languages


How to solve the given problem

• Problem statement will be given

• How you are going to solve it?


How to solve the given problem - cont’d…
• How you are going to solve it?

• Before writing a program, first we need to find a procedure


for solving the problem.

• The program written without proper pre-planning has higher


chances of errors.
What is the basic use of Flowchart and
Algorithm ?

Powerful and Pre-planning tools


Flowchart and Algorithm – Cont’d..

• An algorithm is a step-by-step analysis (procedure) of the


process.

• A flowchart explains the steps of a program in a graphical way.

• Algorithm and flowcharts helps to clarify all the steps for


solving the problem.
Flowchart and Algorithm – Cont’d..
• For beginners, it is always recommended to first write algorithm and
draw flowchart for solving a problem and then only write the
program

• Beginners find it difficult to write algorithm and draw flowchart.

• The algorithm can vary from person to person to solve a particular


problem.
Technical Aspect
Control Structures in algorithm and flowchart

• Sequence

• Branching (Selection)

• Loop (Repetition)
How to draw flowchart
How to draw flowcharts
Symbols
• Assignment symbols
• Mathematical Operators
• Relational Operators
• Logical Operators
• Control Statements (also called as selection)
• Loop control Statements (also called as branching)
Assignment Symbols (or =)

• The symbol ‘=’ is used in most of the programming language as an


assignment symbol.
• The same symbol ‘=’ has been used in all the algorithms and
flowcharts in the manual.
Mathematical Operator
Relational Operators
Logical Operators
Control Statements (Selection Controls)
Control Statements ( Looping controls)
Algorithms
• An algorithm is a sequence of steps to solve a particular problem

• Algorithm has the following characteristics


• Input: An algorithm may or may not require input
• Output: Each algorithm is expected to produce at least one result
• Definiteness: Each instruction must be clear and unambiguous.
• Finiteness: If the instructions of an algorithm are executed, the
algorithm should terminate after finite number of steps.
How to write algorithms
• Step1: Define your algorithms input (if required)

• Step2: Define the variables (if required)

• Step3: Outline the algorithm's operations

• Step4: Output the results of your algorithm's operations


Printing numbers from 0 to 19
Example problems – Numbers
• Design an algorithm and the flow chart for the sum of the digits in the
given number.
• Rem = N % 10;
• Sum = Sum + Rem;
• N = N / 10;
• Design an algorithm and the flow chart to add the numbers ranging
from 0 to 20.
• Read the maximum limit
• Sum = sum + i, i = i + 1
Temperature conversion
• Design an algorithm and a flowchart to convert temperature from
Celsius to Fahrenheit
• Formula = (9.0/5.0 x C) + 32
• Input: temp in Celsius
• Output: temp in Fahrenheit
• Design an algorithm and a flowchart to convert temperature from
Fahrenheit to Celsius
• Formula = C = 5.0/9.0 (F - 32 )
• Input: temp in Celsius
• Output: temp in Fahrenheit
• Github.com
• Gitlab (optional)
• Linkedin
• Microsoft virtual academy
• Coursera
• Edx
Reversing the number
Reversing the number
• Input the number n;
• Initialize rev = 0;
• While num>0
• Multiple rev with 10 and then add the reminder value
• rev = (rev *10) + (n%10)
• Divide the number n by 10
• n/10
• Print the reversed number
Swapping of two numbers
Swapping two numbers
Swapping of two numbers without another variable
Swapping of two numbers without another variable
Classwork problem
• Design an algorithm and a flowchart to find the given number is odd
or even
• Design an algorithm and a flowchart to find the Even number
between 1 to 50
• Design an algorithm and a flowchart to find the given year is leap or
not
• Design an algorithm and a flowchart to find the area of a circle
(Formulae - 3.14*r*r)
Algorithm and flowchart to find leap year or not
Algorithm and flowchart to find area of the circle
Classwork problem
• Design an algorithm and a flowchart to find the sum of the even
number between 1 to 100
• Design an algorithm and a flowchart to find the sum of the odd
number between 1 to 100
Algorithm and flowchart
to find the sum of the odd number
between 1 to 100
Algorithm and flowchart
to find the sum of the even number
between 1 to 100
Armstrong number
Sum of N numbers
• Loop
• Sum = sum + i
• Print the sum
Sum of N numbers
Algorithm & Flowchart to find sum of series
2 3
1 – X + X –X ….XN

• Initialize  i, TERM, SUM = 1;


• Input N, X,
• Check for the condition
• TERM = - TERM * X
• SUM = SUM + TERM
• i=i+1
• Print SUM
Algorithm & Flowchart to find sum of series
2 3
1 – X + X –X ….XN
Factorial of the given number
• Use for loop
• Fact = fact * i
• Print fact
Algorithm and flowchart for factorial
step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Fibonacci Series
Fibonacci series
Initialize  f1=-1, f2=1, f= 0
Input  n
While (f<n)
f=f1+f2
f1=f2
f2=f
Print f
Fibonacci series
Sine series
• sin (x) = (x^1/1!) - (x^3/3!) + (x^5/5!) - (X^7/7!) +
Sine series - Logic
• Initialize  i = 1; TERM, SUM = X;
• Input N, X,
• Check for the condition using while (i<n)
• TERM = - TERM * X2 / (2*i*(2*i+1))
• SUM = SUM + TERM
• i=i+1
• Print SUM
Cos series
Cos series – Logic
• Initialize  i ,TERM, SUM = 1;
• Input N, X,
• Check for the condition using while (i<n)
• TERM = - TERM * X2 / (2*i*(2*i-1))
• SUM = SUM + TERM
• i=i+1
• Print SUM
Structure of Java
My First program in JAVA

• Every line of code that can actually run needs to be inside a class.
• Class name can start with any name but the first letter of the class name
should starts with a capital letter.
• File name should be as same as the class name.
My First program in JAVA

• public again means that anyone can access it.


• static means that you can run this method without creating an instance
of Main.
• void means that this method doesn't return any value.
• main is the name of the method.
• String[] or String… is an array of Strings.
String args []
• The string args [] is compulsory in main method because when the
JVM starts executing it searches for the argument signature which
is string arg[].
Write a java program to

You might also like