0% found this document useful (0 votes)
15 views

CC 102 - Fundamental Programming Lecture 4

The document discusses Java input/output, operators, and provides an example program. It covers: 1) How to use System.out.print(), System.out.println(), and System.out.printf() for output in Java. 2) How to use the Scanner class to take user input in Java by creating a Scanner object and using methods like nextInt() and nextFloat(). 3) The different types of operators in Java including arithmetic, assignment, comparison, logical, and bitwise operators. 4) An example program that takes two numbers as input, calculates the sum, difference, product, and quotient, and displays the results. The program includes a flowchart and Java code

Uploaded by

cleofe calo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CC 102 - Fundamental Programming Lecture 4

The document discusses Java input/output, operators, and provides an example program. It covers: 1) How to use System.out.print(), System.out.println(), and System.out.printf() for output in Java. 2) How to use the Scanner class to take user input in Java by creating a Scanner object and using methods like nextInt() and nextFloat(). 3) The different types of operators in Java including arithmetic, assignment, comparison, logical, and bitwise operators. 4) An example program that takes two numbers as input, calculates the sum, difference, product, and quotient, and displays the results. The program includes a flowchart and Java code

Uploaded by

cleofe calo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

JAVA PROGRAMMING

Lecture 4 Input
/ Output ,operators
and operator
precedence
Input and Output
flowchart symbol
Example:

Input num a,b

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

we have used the +


operator to
Print concatenate (join)
Concatenated
the two strings:
Strings
Java Basic Input and Output

The Scanner class is used to get user


Java Input input, and it is found the java.util
package. ( import java.util.Scanner )

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.

You might also like