Create a java program that will Answer all three questions below the following q
ID: 3678110 • Letter: C
Question
Create a java program that will Answer all three questions below the following questions with the following requirements using for loops and do while statements.
Problem: The following is a program about PIN checking for bank.
Question 1: Rewrite this program by using for loop, and add a trying times limitation, which means the user is allowed to try entering PIN at most 3 times. If still not matching
in the 3 trying, the program will print out a message and stops.
The output will look like the following:
Question 2: Redo Question 1, but using do...while loop.
Question 3: What are the difference about the 3 loops (for, while, do...while)? Analyze
the advantages and disadvantages.
Explanation / Answer
//Question:1
//program about PIN checking for bank, assuming correct pin number is 2113
// Pincheck.java
import java.util.*;
class Pincheck
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your PIN number");
int pin=scan.nextInt();
int choice=0;
if(pin != 2113)
{
if(choice==0)
{
System.out.println("Your PIN is wrong,you have only two chances left");
choice++;
}
for(int i=0;i<2;i++)
{
System.out.println("Enter your PIN number");
pin=scan.nextInt();
if(choice==1)
{
System.out.println("Your PIN is wrong,you have only one chance left");
choice++;
}
else
{
System.out.println("Your PIN is wrong,you have no chances left");
}
}
}
}
}
//Question:2 using do while
//program about PIN checking for bank, assuming correct pin number is 2113
// Pincheck.java
import java.util.*;
class Pincheck
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your PIN number");
int pin=scan.nextInt();
int choice=0;
if(pin != 2113)
{
if(choice==0)
{
System.out.println("Your PIN is wrong,you have only two chances left");
choice++;
}
int i=0;
do
{
System.out.println("Enter your PIN number");
pin=scan.nextInt();
if(choice==1)
{
System.out.println("Your PIN is wrong,you have only one chance left");
}
else
{
System.out.println("Your PIN is wrong,you have no chances left");
}
choice++;
i++;
} while(i<2);
}
}
}
//Question No:3
//differences between for,while and do while loops.
Answer:
for loop:
for loop is the simplest loop since initialization,condition and iteration will be done in a single
statement.This loop may or may not be executed atleast once.Hence this loop is also called
"Entry level controlled loop".
while loop:
In this loop initialization,condition and iteration will be done separately in three
statements.This loop may or may not be executed atleast once.Hence this loop is also called
"Entry level controlled loop".
do while loop:
In this loop also initialization,condition and iteration will be done separately in three
statements.This loop must always be executed atleast once.Hence this loop is also called
"Exit level controlled loop".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.