CC 102 - Fundamental Programming Lecture 4
CC 102 - Fundamental Programming Lecture 4
Lecture 4 Input
/ Output ,operators
and operator
precedence
Input and Output
flowchart symbol
Example:
Display “ hello”
sum = a+b
Input num
print sum
Java Basic Input and Output
Java Output
In Java, you can simply use
:System.out.print();
System.out.println();
Difference
between System.out.printf(); It prints
string inside
the quotes
Java Basic Input and Output
Java Output
In Java, you can simply use
:System.out.print();
It prints string
System.out.println(); inside the quotes
Difference
between similar like print()
System.out.printf();
method the
difference is the
cursor moves to
the beginning of
the next line.
Java Basic Input and Output
Java Output
In Java, you can simply use
:System.out.print();
System.out.println();
Difference It provides
between System.out.printf(); string formatting
(similar to printf
in C/C++
programming).
Java Basic Input and Output
Example:
Java Output
It is because to
display integers,
Printing
variables and so on,
Variables and
we don't use
Literals
quotation marks.
Java Basic Input and Output
Example:
Java Output
Syntax
Scanner identifier = new Scanner(System.in);
Example
create an object
of the class and
use any of the
available methods
found in the
Scanner class
documentation
create an object In order to run the
of the class and Scanner class you
use any of the
available methods have to import the
found in the Scanner class which
Scanner class found in java.util
documentation
This variable / identifier
called input that collects
the next value the user
inputs into the console
variables / identifiers that
collects the value the user
submits to the console
method, which is used to
read Strings
method, which is used to
read integer
method, which is used to
read float / approximate
Input Types
Java Operators
Operators are used to perform
operations on variables and
values.
Java divides the operators into the
following groups:
Java
Operators ▪ Arithmetic operators
▪ Assignment operators
▪ Comparison operators
▪ Logical operators
▪ Bitwise operators
Java
Operators
Arithmetic
Example: Draw a flowchart and write a program that will input two numbers ,
compute and display each result for the sum , difference , quotient and
product of to numbers (num1,num2 respectively)
flowchart Java program import java.util.*;
class example {
start
public static void main(String[] args)
{
Input num1 , float num1,num2,S,D,M,Q;
num2 Scanner opiang = new Scanner(System.in);
System.out.print("Enter num1 :");
num1 = opiang.nextFloat();
S = num1+num2 System.out.print("Enter num2 :");
D = num1 – num2
Q = num1/num2
num2 = opiang.nextFloat();
M = num1 * num2 S = num1+num2;
D = num1-num2;
M = num1*num2;
Display result Q = num1/num2;
of S,D,Q,M System.out.println("sum :" + S);
System.out.println("Difference :" + D);
System.out.println("Quotient :" + Q);
Stop System.out.println("Product :" + M);
} }
Example: Draw a flowchart and write a program that will input two numbers ,
compute and display each result for the sum , difference , quotient and
product of to numbers (num1,num2 respectively)
flowchart Java program import java.util.*;
class example {
start
public static void main(String[] args)
{
Input num1 , float num1,num2,S,D,M,Q;
num2 Scanner opiang = new Scanner(System.in);
System.out.print("Enter num1 :");
num1 = opiang.nextFloat();
S = num1+num2 System.out.print("Enter num2 :");
D = num1 – num2
Q = num1/num2
num2 = opiang.nextFloat();
M = num1 * num2 S = num1+num2;
D = num1-num2;
M = num1*num2;
Display result Q = num1/num2;
of S,D,Q,M System.out.println("sum :" + S);
System.out.println("Difference :" + D);
System.out.println("Quotient :" + Q);
Stop System.out.println("Product :" + M);
} }
Example: Draw a flowchart and write a program that will input two numbers ,
compute and display each result for the sum , difference , quotient and
product of to numbers (num1,num2 respectively)
flowchart Java program import java.util.*;
class example {
start
public static void main(String[] args)
{
Input num1 , float num1,num2,S,D,M,Q;
num2 Scanner opiang = new Scanner(System.in);
System.out.print("Enter num1 :");
num1 = opiang.nextFloat();
S = num1+num2 System.out.print("Enter num2 :");
D = num1 – num2
Q = num1/num2
num2 = opiang.nextFloat();
M = num1 * num2 S = num1+num2;
D = num1-num2;
M = num1*num2;
Display result Q = num1/num2;
of S,D,Q,M System.out.println("sum :" + S);
System.out.println("Difference :" + D);
System.out.println("Quotient :" + Q);
Stop System.out.println("Product :" + M);
} }
Example: Draw a flowchart and write a program that will input two numbers ,
compute and display each result for the sum , difference , quotient and
product of to numbers (num1,num2 respectively)
flowchart Java program import java.util.*;
class example {
start
public static void main(String[] args)
{
Input num1 , float num1,num2,S,D,M,Q;
num2 Scanner opiang = new Scanner(System.in);
System.out.print("Enter num1 :");
num1 = opiang.nextFloat();
S = num1+num2 System.out.print("Enter num2 :");
D = num1 – num2
Q = num1/num2
num2 = opiang.nextFloat();
M = num1 * num2 S = num1+num2;
D = num1-num2;
M = num1*num2;
Display result Q = num1/num2;
of S,D,Q,M System.out.println("sum :" + S);
System.out.println("Difference :" + D);
System.out.println("Quotient :" + Q);
Stop System.out.println("Product :" + M);
} }
Example: Draw a flowchart and write a program that will input two numbers ,
compute and display each result for the sum , difference , quotient and
product of to numbers (num1,num2 respectively)
flowchart Java program import java.util.*;
class example {
start
public static void main(String[] args)
{
Input num1 , float num1,num2,S,D,M,Q;
num2 Scanner opiang = new Scanner(System.in);
System.out.print("Enter num1 :");
num1 = opiang.nextFloat();
S = num1+num2 System.out.print("Enter num2 :");
D = num1 – num2
Q = num1/num2
num2 = opiang.nextFloat();
M = num1 * num2 S = num1+num2;
D = num1-num2;
M = num1*num2;
Display result Q = num1/num2;
of S,D,Q,M System.out.println("sum :" + S);
System.out.println("Difference :" + D);
System.out.println("Quotient :" + Q);
Stop System.out.println("Product :" + M);
} }
Laboratory Write a Pseudocode , Draw a
exercise 2
flowchart and write Java
program that will accept the
length and width of the
rectangle , compute and
display the Area and the
perimeter of the rectangle.