Object Oriented Paradigm
Topic: JAVA
1
scanf(“%d”,&age);
Java User Input scanf(“%s”,name);
• The Scanner class is used to get user input, and it is found in the java.util package.
• To use the Scanner class,
• Create an object of the class, and
• Use any of the available methods found in the Scanner class documentation.
import java.util.Scanner; OR import java.util.*;
String s = "Hello, This is Java Class.";
Inside main()
//Create scanner Object and pass string in it
Scanner myObj = new Scanner(s);
Scanner myObj = new Scanner(System.in);
System.out.println("String:" +myObj.nextLine());
int age = myObj.nextInt();
String name = myObj.nextLine();
String: Hello, This is Java
Class. 2
Content
• JAVA User Input
• Scanner Class
• Control Statements
• Selection Statements
• Jump Statements
• Loop Statements
• Example Programs (4)
3
Program - 1 In your window, try getting
import java.util.Scanner; user inputs: integer, Boolean,
float, character.
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
Enter name, age and salary:
System.out.println("Enter name, age and Mary
salary:"); 21
30000
// String input
String name = myObj.nextLine(); Name: Mary
Age: 21
// Numerical input Salary: 30000
int age = myObj.nextInt();
double salary = myObj.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
4
Control Statements
Control
Statements
Selection Iteration Jump
Statements Statements Statements
If…Else Switch…Case Break Continue
While Do…While For
Selection Statements
IF IF-ELSE
if(condition) if (condition)
{ {
// Statements to execute if // Executes this block if
// condition is true // condition is true
} }
else
{
// Executes this block if
// condition is false
}
6
Selection Statements
NESTED-IF IF-ELSE-IF LADDER
if (condition1) if (condition)
{ statement;
// Executes when condition1 is else if (condition)
true
statement;
if (condition2)
.
{
.
// Executes when condition2 is
true else
} statement;
}
7
Selection Statements
SWITCH-CASE
switch (int|char)
{
case value1:
statement1;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
8
Program 2
Additional Discount of 5% if your final amount is
greater than 1500. 9
retail_shop.java
public class retail_shop {
public static void main(String[] args) {
int itema=200, itemb=75, itemc=500;
double price;
price = ((itema*2)+(itemb)+(itemc*3));
price = price-(.1*price);
price = price + (0.05*price);
//System.out.println(price);
if(price > 1500)
price = price – (.05*price);
else
System.out.println(“Not eligible for discount”);
System.out.println(price);
}
}
10
Control Statements
Do-while
Why loops in Programming??
do{
To reduce code redundancies //code to be executed
}while(condition);
Executes once and while
Do While
then repeats things
until loop condition
while(condition){
is true.
//code to be executed
}
Repeats things until For
While loop condition is
true.
for(init; condition; Incr/decr){
// code to be executed
Repeats things till }
For the given number of
times. 11
Jump Statements
Break Continue
Syntax: Syntax:
break; continue;
To leave control from loop (for, If continue statement is true.
while, do-while) based on specified
Any statement after continue will not
condition.
get executed in that particular
Similarly from switch case. iteration of loop.
12
Program 3
13
Import java.util.Scanner*;
else if (finalscore>60)
public class students_marks{
public static void main(String[] args) {
While System.out.println(“Good”);
else if (finalscore >40)
int maths, phy, che, eng, compsc; System.out.println(“Average”);
else
double finalscore;
System.out.println(“Poor”);
boolean result=true;
Scanner marks = new Scanner(System.in); System.out.println(“Any more students?
(true or false)”);
while(result)
result = marks.nextBoolean();
{
System.out.println(“Enter Marks for Maths: “); }
}
maths=marks.nextInt();
}
System.out.println(“Enter Marks for Phy: “);
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “);
eng=marks.nextInt();
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80) 14
import java.util.*;
else if (finalscore>60)
Do-while
public class students_marks{ System.out.println(“Good”);
public static void main(String[] args) { else if (finalscore >40)
int maths, phy, che, eng, compsc; System.out.println(“Average”);
else
double finalscore; System.out.println(“Poor”);
boolean result;
When to exit from loop?
Scanner marks = new Scanner(System.in);
do System.out.println(“Any more students?
(true or false)”);
{ result = marks.nextBoolean();
System.out.println(“Enter Marks for Maths: “);
maths=marks.nextInt(); } while(result);
}
System.out.println(“Enter Marks for Phy: “); }
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “);
eng=marks.nextInt();
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80) 15
System.out.println(“V.Good”);
Import java.util.*;
public class students_marks{
public static void main(String[] args) {
For
int maths, phy, che, eng, compsc, studentnumber; else if (finalscore>60)
double finalscore; System.out.println(“Good”);
Scanner marks = new Scanner(System.in); else if (finalscore >40)
System.out.println(“Average”);
studentnumber = marks.nextInt();
else
for(int i=0; i<studentnumber; i++) System.out.println(“Poor”);
{
System.out.println(“Enter Marks for Maths: “); }
}
maths=marks.nextInt();
}
System.out.println(“Enter Marks for Phy: “);
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “); When you know total number of
eng=marks.nextInt(); students in class.
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80)
System.out.println(“V.Good”);
16
THANK YOU….
17