Learning Java

Hey, readers! In  this post, we’ll continue our explanation about Java. If you haven’t read  the previous post, just click on the previous post button. Alright so let’s get started.

document-2178656_1920.jpg

When we need to execute a set of statements based on a condition then we need to use control flow statements. There are four types of control statements that you can use in java programs based on the requirement:

a) if statement

if(condition){
  Statement(s);
}

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.

b) nested if statement

When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the conditions( condition_1 and condition_2) are true.

c) if-else statement

This is how an if-else statement looks:

if(condition) {
   Statement(s);
}
else {
   Statement(s);
}

The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.

d) if-else-if statement

if-else-if statement is used when we need to check multiple conditions. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder. This is how it looks:

if(condition_1) {
   /*if condition_1 is true execute this*/
   statement(s);
}
else if(condition_2) {
   /* execute this if condition_1 is not met and
    * condition_2 is met
    */
   statement(s);
}
else if(condition_3) {
   /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
   statement(s);
}
.
.
.
else {
   /* if none of the condition is true
    * then these statements gets executed
    */
   statement(s);
}

in if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.

 


 

There may be a situation when you need to execute a block of code several number of times. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages. Java programming language provides the following types of loop to handle looping requirements.

While loop

while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.It tests the condition before executing the loop body.

while(Boolean_expression) {
   // Statements
}

Do while loop

do while loop is similar to a while loop, except that a do while loop is guaranteed to execute at least one time. Like a while statement, except that it tests the condition at the end of the loop body.

do {
   // Statements
}while(Boolean_expression);

 

For loop

for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. A for loop is useful when you know how many times a task is to be repeated.

for(initialization; Boolean_expression; update) {
   // Statements
}


Here are some exercise to improve your knowledge :

  • Make a program of 4 input which is A, B, C, and D. Which  A must be smaller than C, B must be greater than D but smaller than C. If the input is incorrect, the system should show “input is incorrect”. and if the input is already correct then system should print the geometrical number from A to C with the range of B. and if the number is divisible by D, the system output should show @. It was confusing at first, and I need to read this question thoroughly. And there comes a conclusion that D<B<C and A<C so here’s the code :
import java.util.Scanner;


public class Week6_1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);

System.out.print("A = ");
int A = scan.nextInt();
System.out.print("B = ");
int B = scan.nextInt();
System.out.print("C = ");
int C = scan.nextInt();
System.out.print("D = ");
int D = scan.nextInt();

int n = 1;
int x = A;

if (A > C || B < D || B > C) {
System.out.println("INPUT IS INCORRECT");
}
else
while(x <= C){
if (x%D==0){
System.out.print(" @ ");
}
else {
System.out.print(" " + x);
}
x = A + (B*n);
n++;
} 
} 
}

output :

A = 2
B = 6
C = 25
D = 4
2 @ 14 @
A = 10
B = 3
C = 20
D = 21
INPUT IS INCORRECT
  • There’s a chocolate stall which offer a free chocolate bar if their customers exchange four chocolates wrapper. Make a program which output display the amount of customer’s chocolate bonus based on how much the customer has the chocolate wrapper. If the customer has 4 chocolate wrapper, he gets one extra chocolate bar; and if the customer has 7 chocolate wrapper, he gets a couple of chocolate bars.
import java.util.Scanner;

public class w6_2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int bonus = 0;
int wrapper;

System.out.println("How much chocolate wrapper do you have ? ");
wrapper = scan.nextInt();

while (wrapper>=4){
wrapper = wrapper - 4;
bonus = bonus + 1;
wrapper ++;

}
System.out.println("Chocolate bonus : " + bonus);
}
}

output :

How much chocolate wrapper do you have ? 
7
Chocolate bonus : 2

 

  • Make a program to calculate and display the following series of numbers according to n inputted by user! Example n = 5 → 1 – 2 + 3 – 4 + 5 = 3
import java.util.Scanner;
public class w6_4 {
public static void main(String[] args) {
System.out.println("Input N ");

Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int i = 1;
int total = 1;
int a = 0;
System.out.print("1");
for(i=2; i<=n; i++){
if (i%2 == 0){
System.out.print("-" + i ); 
a = a - i; 
}

else {
System.out.print("+" + i );
a = a + i;
}
}
total = a + total;

System.out.println("= " + total);

}
}

output :

Input N 
5
1-2+3-4+5= 3
Input N 
10
1-2+3-4+5-6+7-8+9-10= -5

 

  • Make a program so that it can display shapes like the image below, where many line according to user input.
  • input lines : 5                                          input lines : 3
    +++++                                                                  +++
    ++++                                                                    ++
    +++                                                                      +
    ++
    +

 

import java.util.Scanner;

public class w6_9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int baris; 

System.out.println("Input lines : ");
baris = scan.nextInt();
for(int a = baris-1; a>=0; a--){
for (int i = a; i >=0; i--) {
System.out.print("+");
} 
System.out.println(""); 
}
}
}

output :

Input lines : 
5
+++++
++++
+++
++
+

 

There’s a new rule on ABBA penalty shootout as follows:

  • Each team has 5 chances to make a penalty
  • If the A team that gets the first chance, then the kicking sequence is:
  • A – B – B – A – A – B – B – A – A – B
  • The team that has the most scores is the one who wins
  • If each team has kicked 5 times and the score is atie, then the program should display a sudden death
import java.util.Scanner;

public class w6_7 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = "";
String firstKick = "";
int teamA = 0;
int teamB = 0;

System.out.print("First Turn (Team A/B):");
input = s.next();
firstKick = input;

if (firstKick.equalsIgnoreCase("a")) {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
}

System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
}
} else if (i % 2 == 1) {
System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
}

System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
}
}
}
} else if (firstKick.equalsIgnoreCase("b")) {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
}

System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
}
} else if (i % 2 == 1) {
System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
}

System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
}
}
}
}

int counter = 0;
while (teamA == teamB) {
if (firstKick.equalsIgnoreCase("a")) {
if (counter % 2 == 1) {
System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
break;
}

System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
break;
}
} else if (counter % 2 == 0) {
System.out.print("B goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamB++;
break;
}

System.out.print("A goal ?");
input = s.next();
if (input.equalsIgnoreCase("y")) {
teamA++;
break;
}
}
}
}

System.out.println("Score Team A: " + teamA);
System.out.println("Score Team B: " + teamB);
if (teamA > teamB) {
System.out.println("Winner Team A");
} else {
System.out.println("Winner team B");
}
}
}

output :

First Turn (Team A/B):a
A goal ?y
B goal ?y
B goal ?y
A goal ?y
A goal ?y
B goal ?y
B goal ?y
A goal ?y
A goal ?y
B goal ?y
B goal ?y
Score Team A: 5
Score Team B: 6
Winner team B

 



Thanks for visiting my site. Don’t forget to give a thumb up and leave  a comment below.

Have a nice day!


“Computers are good at following instructions, but not at reading your mind”

Donald Knuth



Self – reflection :

The question about ABBA penalty pattern was kinda difficult for me because it took a long time for me to think of the code and I need some help to finish that question. I have to learn more about Java so I could think of the code easily. Thanks to our lecturer, Mr. Mychael and Mrs. Citra.




source :

https://beginnersbook.com/2017/08/if-else-statement-in-java/

https://www.learnjavaonline.org/en/Conditionals

https://www.tutorialspoint.com/java/if_else_statement_in_java.htm

https://www.tutorialspoint.com/java/java_loop_control.htm

https://beginnersbook.com/2015/03/for-loop-in-java-with-example/

Leave a comment