Introduction To Java Program: 10/3/2019 (Sunday, 8.30 Am - 9.30 Am)
Introduction To Java Program: 10/3/2019 (Sunday, 8.30 Am - 9.30 Am)
Amran Ali
10/3/2019 (Sunday, 8.30 am – 9.30 am)
Problem Statement
• Find the total of two marks.
Problem Analysis
IPO Chart
Input : m1, m2
Process : calculate total
Output : total
Input : Concept
m1
m2
Input : Pseudocode / Flowchart
• input num1, num2
• read num1, num2
• get num1, num2
num1=97.5; num1=sc.nextDouble();
num2=86.5; num2=sc.nextDouble();
import java.util.Scanner;
Scanner sc=new Scanner(System.in);
Input for different data type
Intialized User input
int studentAge; int studentAge;
char studentGender; char studentGender;
boolean flag; boolean flag;
String name; String name;
name
Process : Concept
• Arithmetic and Assignment operators
– +, -, *, /, %, =
• Relational operators (true / false)
– ==, !=, <, <=, >, >=
• Logical operators (true / false)
– !, &&, ||
Process : Concept
<variable> = <expressions> 2 1
remainder div
Process : Pseudocode / Flowchart
m1 = 3
m2 = 5
total = m1 + m2
m1 = 3
m2 = 5
total = m1 + m2
Process : Concept
• Relational operators (true / false)
==, !=, <, <=, >, >=
• Logical operators (true / false)
!, &&, ||
Used in
selection (if)
and
repetition
(while/for)
control
structure
Process : Pseudocode / Flowchart
if num%2 = 0
print “Even” false print
num%2=0
“Odd”
else
true
print “Odd”
print
endif “Even”
Process : Java code
Pseudocode Java code
if num%2 = 0 if (num%2 == 0)
print “Even” System.out.print(“Even”);
else else
print “Odd” System.out.print(“Odd”);
endif
Output : Concept
Message Value
• To display a message. • To display a value.
• Example: • Example:
– “Hello world.” – total of two numbers
– “I Love Java” – average of three integers
– “Java is easy” – min of 20 numbers
– “Programming is the best” – area of a circle
– “num1 is odd” – perimeter of a rectangle
– “num2 is positive integer”
Output : Concept
97.5
m1
86.5
m2
184.0
total
Output : Pseudocode / Flowchart
• output total
• print total
• display total
Output/print/display total
Output : Java code
System.out.print(total);
System.out.print(“Java is easy.”);
Problem Analysis Pseudocode
Problem Analysis Pseudocode
IPO Start
Input : m1, m2 read m1, m2
Process : calculate total total = m1 + m2
Output : total print total
End
Pseudocode Java code
Pseudocode Java code
Start {
read m1, m2 m1=sc.nextDouble();
total = m1 + m2 m2=sc.nextDouble();
print total total = m1 + m2;
End System.out.print(total);
}
import java.util.Scanner;
class TotalTwoNumbers {
public static void main(String[] args) {
//declaration
Scanner sc=new Scanner(System.in);
double m1, m2, total;
//input
m1=sc.nextDouble();
m2=sc.nextDouble();
//process
total = m1 + m2;
//output
System.out.print(total);
}
}
import java.util.Scanner;
class TotalTwoNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double m1, m2, total;
m1=sc.nextDouble();
sc.nextDouble();
m2=sc.nextDouble();
sc.nextDouble();
total = m1 + m2;
System.out.print(total);
total
}
}
Slot 1 Summary
• Java Components
– Class declaration
– main method (Declaration / Input / Process / Output)
• Output
– Message “Message here.”
– Value store in a variable
• Sources of variables
– User input
– Calculations / conditions
• Declarations
Exercise
• Find the total and average of three numbers.
Problem Analysis
IPO Chart
Input : n1, n2, n3
Process : calculate total
calculate average
Output : total, average
Problem Analysis Pseudocode
Problem Analysis Pseudocode
IPO Start
Input : n1, n2, n3 read n1, n2, n3
Process : calculate total total = n1 + n2 + n3
calculate average print total, average
Output : total, average End
Pseudocode Java code
Pseudocode Java code
Start {
read n1, n2, n3 n1=sc.nextDouble();
total = n1 + n2 + n3 n2=sc.nextDouble();
print total, average n3=sc.nextDouble();
total = n1 + n2 + n3;
End
avg = total / 3;
System.out.print(total);
System.out.print(avg);
}
import java.util.Scanner;
class TotalTwoNumbers {
public static void main(String[] args) {
//declaration
Scanner sc=new Scanner(System.in);
double n1, n2, n3, total, avg;
//input
n1=sc.nextDouble();
n2=sc.nextDouble();
n3=sc.nextDouble();
//process
total = n1 + n2 + n3;
avg = total / 3;
//output
System.out.print(total);
System.out.print(avg);
}
}
import java.util.Scanner;
class TotalTwoNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double n1,
m1, n2,
m2, n3, total, avg;
total;
n1=sc.nextDouble();
sc.nextDouble();
n2=sc.nextDouble();
sc.nextDouble();
n3=sc.nextDouble();
sc.nextDouble();
total = m1
n1 + m2
n2 + n3;
avg = total
total // 3;
3;
System.out.print(total);
total
System.out.print(avg);
avg
}
}
Take 5!
Array : Concept
Amran Ali
10/3/2019 (Sunday, 10.30 am – 11.30 am)
Problem Statement
• Find the total of 20 integers entered by user.
Problem Analysis
IPO Chart
Input : 20 integers
Process : calculate total
Output : total
Input : Concept
n1
n2
.....
…..
…..
n20
Input : Concept
20 elements
Array
n …………
[0][1][2][3][4][5] [19]
Input : Pseudocode / Flowchart
c=0
c=0
while c<20
read n[c] false
c<20
c=c+1
true
endwhile
read n[c]
c=c+1
Input : Java code (User entry)
int[] arr=new int[20];
for(int i=0;i<arr.length;i=i+1){
arr[i] = sc.nextInt();
}
arr …………
[0] [1] [2] [3] [4] [5] [19]
Input : Java code (Initialized)
int[] arr={97, 86, …………, 76};
arr 97 86 … … … … ………… 76
Here,
arr[0] is the first element, contains value 97,
arr[1] is the second element , contains value 86,
…
…
arr[19] is the last element, of the integer array arr.
Input for array
User input
int[] arr=new int[20];
for(int i=0;i<arr.length;i=i+1){
arr[i] = sc.nextInt();
}
Intialized
int[] arr={97, 86, …………, 76};
Process : Concept
arr …………
[0][1][2][3][4][5] [19]
for(int i=0;i<arr.length;i=i+1){
total = total + arr[i];
}
Output : Java code
System.out.print(total);
System.out.println(total);
System.out.print(total);
import java.util.Scanner;
class Total20Numbers {
public static void main(String[] args) {
//declaration
Scanner sc=new Scanner(System.in);
int[] arr=new int[20];
double total=0;
//input
for(int i=0;i<arr.length;i=i+1){
arr[i]=sc.nextInt();
}
//process
for(int i=0;i<arr.length;i=i+1){
total = total + arr[i];
}
//output
System.out.print(total);
}
}
import java.util.Scanner;
class Total20Numbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] arr=new int[20];
double total=0;
for(int i=0;i<arr.length;i=i+1){
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i=i+1){
total = total + arr[i];
}
System.out.print(total);
}
}
Summary Slot 2
• Array is an ordered sequence of element of
same data type.
• Declare array:
double[] marks;
• Create array:
marks = new double[20];
• Declare and Create array:
double[] marks = new double[20];
int[] numbers = {97,86, 58, …, 89};
Exercises
• Write a Java program to accept 100 speeds
from user. Then find the slowest and the
fastest speed among 100 speeds.
Method : Concept
Amran Ali
10/3/2019 (Sunday, 12 pm – 12.30 pm)
Method : Concept
• 1 process represented by 1 method.
• 1 method will produce 1 value.
• Example:
total = m1 + m2;
if(x%2==0)
flag = true;
else
flag = false;
Problem Statement
• Write a method to calculate total of two
numbers.
Problem Analysis
IPO Chart
Input : n1, n2
Process : calculate total
Output : total
import java.util.Scanner;
class Total {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double m1, m2, total;
m1=sc.nextDouble();
m2=sc.nextDouble();
total = m1 + m2;
System.out.print(total);
}
}
To define a method
<Return type> <Method name> (Parameters){
<Method body>
}
Method Components
• Return type (look at the output type)
– Return a value (int, double, char, boolean)
– Return NO value / message only (void)
• Method name
– verb + Task (calcTotal, computeAvg, getMin, findLargest)
• Parameters (input for method)
– Variables (int a, int b, double c, char d, boolean e)
• Method body (process)
– Formula/condition (Arithmetic/Relational/Logical)
To define a method
Problem Analysis User-defined Method
IPO
int calcTotal(int a, int b){
Input: n1, n2
return a + b;
Process: calculate total
}
Output: total
import java.util.Scanner;
class Total {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double m1, m2, total;
Total obj=new Total();
m1=sc.nextDouble();
m2=sc.nextDouble();
total = obj.calcTotal(m1,m2);
System.out.print(total);
}
int calcTotal(int a, int b) {
return a + b;
}
}
Conclusions
• Introduction to Java
– Input statement
• Intialized (variableName = 97.5; )
• Entered by user (variableName = sc.nextInt(); )
– Process / Formula / Condition
• Arithmetic (digit value)
• Relational/Logical (true/false value)
– Output statement (System.out.print())
• Message (display/print)
• Value (assigned to a variable)
• Arrays
– Declare (int[] arrayName;)
– Create (arrayName = new int[20];)
• Methods (type, name, parameter, body)
– return a value
– return NO value
Exercise
• Write a complete Java program to find the
following:
– total, average, minimum, maximum, no. of even,
and no. of odd.
• Based on the data set given below:
76 45 24 88 54
98 87 34 65 12
21 53 25 1 32
4 23 69 3 68
import java.util.Scanner;
class Exercise {
public static void main(String[] args) {
//declaration
Scanner sc=new Scanner(System.in);
//input
//process
//output
}
}