Skip to main content

Posts

Showing posts from April, 2021

Spring Boot + Spring Data REST Example

In this articles we’ll go through some of the basics of Spring Data REST and see an example of using Spring Boot with Spring Data REST. Table of contents Spring Data REST Using @RepositoryRestResource to customize REST endpoints Changing the Base URI Accessing Data JPA custom query Using @RestResource annotation to change query method path Spring Data REST Spring Data REST builds on top of the Spring Data repositories and automatically exports those as REST endpoints. It takes the features of Spring HATEOAS and Spring Data and automatically combines them together. In the Spring Boot + Spring Data JPA + MySQL + Spring RESTful example you can see how using Spring Data JPA requires that you just create a Repository interface. No need to write DAO implementation classes Spring takes care of automatically implementing this repository interface. Spring Data REST goes one step further, you don’t even need to create a Controller class with mappings (GET,

Tic-Tac-Toe Game in Python

In this article we’ll see how to develop tic-tac-toe game in Python. The tic-tac-toe game we are going to develop is a two player game played in the command line. Steps in the program are as follows- Give an option to the player 1 to choose symbol (‘X’ or ‘O’). Create a tic-tac-toe board with index numbers making it easy for the players to input the index where they want their symbol to be placed. Give each player a chance to place the symbol alternatively until one of the players has won the game or the game is drawn. Tic-tac-toe game – Python code def user_choice(): choice = ' ' flag = False while not choice.isdigit() or not flag: choice = input('Please input a number (0-10): ') if choice.isdigit() and int(choice) in range(0, 10): flag = True return int(choice) else: print('Enter number between 0 and 10') flag = False def display_board(board): print('\n') print(' ' + boa

Java Continue Statement With Examples

Continue statement in Java is used to force the iteration of the loop. As soon as a continue statement is encountered in a loop, remaining statements in the loop that follow the continue statement are not executed and the control jumps to the next iteration of the loop. What happens after the control jumps to the beginning of the loop differs based on the type of loop used. For while loop and do-while loop , continue statement causes the transfer of control to the condition of the loop. In case of for loop , continue statement causes the transfer of control initially to the increment part of the for loop and then to the condition that controls the loop. Java continue statement examples 1- Using continue statement in a for loop to print only odd numbers between 1-10. public class ContinueDemo { public static void main(String[] args) { for(int i = 0; i <=10; i++){ // even case don't print the number // go to next iteration if(i%2 == 0)

Java Ternary Operator With Examples

In the post conditional operators in Java we talked about the Conditional-AND and Conditional-OR operators, here we'll talk about another conditional operator known as ternary operator in Java (?:) . Ternary operator uses three operands thus the name ternary operator and it can be used in place of if-else statement or switch-case statement to make code more compact and readable. Java ternary operator syntax Ternary operator in Java has the following form- result = boolean_expression ? Operand1 : Operand2 First operand is always a boolean expression returning either true or false. If the boolean expression is true then the evaluation of Operand1 is assigned to result, if the expression is false then the evaluation of Operand2 is assigned to result. Ternary operator Java example As already said ternary operator can be used in place of if-else statement to make the code more compact. For example the following if-else statement- String str; if(age >= 18){ str

Java break Statement With Examples

Sometimes you may need to continue to the next iteration with in the loop with out executing the statements or you may need to terminate the loop altogether. For these two scenarios Java provides two control statements- continue and break . In this post we’ll go through Java break statement in detail along with usage examples. Break statement in Java break statement in Java has the following three uses- Exiting a loop - When the break statement is encountered with in a loop, the loop is terminated and the control transfers to the statement immediately following the loop. In switch statement - break statement is used to terminate the switch statement too. Used with label to work as “goto” statement - You can use labelled break statement to exit out of the labelled block. Java break statement examples Here are some examples showing the usage of break statement to exit a loop. Generally break statement is used with if-else statement . 1- Using break statem

Java switch case Statement With Examples

Switch statement is Java's selection statement which can have a number of possible execution paths. Another selection statement if-else-if ladder in Java also executes one of the code block based on the evaluation of the condition, switch-case in Java provides an easier and more readable alternative to do the same rather than using a large if-else-if ladder. Syntax of the Java switch-case statement is- switch(expression) { case value1: // statements break; case value2: // statements break; . . . case valueN : // statements break; default: // statements } The body of a switch statement is known as a switch block . A switch block contains one or more case labels and an optional default label . Table of contents How does Java switch statement work Type of switch statement expression Java switch-case example Executing default section in switch statement Java switch-case- Removing break statement Using String in Java switch state

Java if-else Statement With Examples

In Java programming language there are two selection statements if-else and switch statement to control the execution flow. This post talks about if and if-else statements in detail with examples. Java if-else statement Java if-else statement is used to evaluate a condition and take the execution path based on whether the evaluated condition is true or false. Java if statement is very flexible and provides many combinations. if statement if-else statement if-else-if statement nested if statement Java if statement You can have just the if statement (else is optional). Syntax of the Java if statement- if(condition){ //statement(s) } Here condition is a boolean expression. If condition is evaluated to true if block is executed, if condition evaluates to false then the if block is not executed. If there is only a single statement with in the if condition then the curly braces are optional. Java if statement examples public class IfDemo { public static vo

for Loop in Java With Examples

Loop in any programming language is used to repeatedly execute a block of code until a condition to terminate the loop is satisfied. In Java there are three types of loops- while loop , do-while loop and for loop. In this post we’ll see how to use for loop in Java. Java for loop In Java there are two forms of for loop. Traditional for loop Enhanced for loop (for-each loop) Syntax of the traditional for loop is as follows- for(initialization; condition; increment\decrement) { // body } Block of code that is executed with in a loop is enclosed with in curly braces. If there is only one statement that has to be executed with in a for loop then curly braces are optional. Java for loop execution flow Following image shows the execution flow of the for loop. Initialization - When the execution of the for loop starts, first step is initialization which is executed only once in the beginning. Initialization step is generally used to set the initial value of th

while Loop in Java With Examples

while loop in Java repeatedly executes a block of statement while the given condition is true. Syntax of the while loop in Java is as follows- while (condition) { // body } Block of code that is executed with in a loop is enclosed in curly braces. If only a single statement is executed with in while loop then curly braces are optional. Java while loop execution flow Following image shows the execution flow of the while loop. The while statement evaluates the condition which is a boolean expression and it must return a boolean value. If the boolean expression evaluates to true, then block of code in the while loop is executed. The while statement continues testing the expression and executing its block until the expression evaluates to false. When condition controlling the while loop becomes false, loop is terminated and control passes to the next line of code immediately following the loop. One thing to note about the while loop is that the conditional expression is

do-while loop in Java With Examples

A do-while loop in Java repeatedly executes a block of statement while the given condition is true. Java do-while loop is similar to while loop except one difference that the condition in the do-while loop is evaluated at the bottom rather than at the top (as is the case with while loop). Since the condition in do while loop is evaluated after the loop body, statements within the do while block are always executed at least once . Syntax of the do-while loop in Java is as follows- do { //body } while (expression); Block of code that is executed with in a loop is enclosed with in curly braces. Java do while loop execution flow Following image shows the execution flow of the do while loop. In each iteration of the do-while loop body of the loop is executed first and then the condition is evaluated. Condition must be a boolean expression, if the expression evaluates to true then the loop is repeated, if expression evaluates to false then the loop terminates. Java do whi

Private Methods in Java Interface

In Java 8 two new features were added to the interfaces in Java- Default methods and static methods . Java 9 added one more capability of adding private methods to the interfaces in Java . Interface private method in Java Private methods in Java interfaces can be defined using private modifier the same way it is done for a Java class. private methodName(arguments){ .. .. } You can add both private methods and private static methods in interfaces, Java 9 onward. Why do we need private methods in interfaces With the inclusion of default and static methods one problem cropped up in interfaces. What if there is a common code in default methods or static methods? There was no proper way to keep that common code in a separate method without exposing it to the implementing classes. You could only keep the common code in another default or static method but by default all the methods in interfaces were public thus exposing any such code to the implementing classes. Consider