Posts

Java Beginner: Lesson 9 - switch

Image
In this lesson we will cover the switch statement. The switch statement same as if statement is a conditional statement, so it checks if a condition is fullfilled, but differently from if statement it is used for a well known set of values and for specific variables. If statement can be used to test complex conditions which can involve even more than one variable, while the switch statement is used to check the value of a specific variable. Example code: switch(variableToTest){ case "value1" : System.out.println("Variable 1 choonsen"); break; case "value2": System.out.println("Variable 2 choonsen"); break; default : System.out.println("Invalid input"); } The example code above shows the structure of a switch statement in case we would test a string variable. So if the string variable with name ...

Exercise Beginner: Base of the number

Image
Request: You are given a string (less than 255 characters). Write a program that determines whether the given string is a number in some number system with a base not more than 16, and, if so, determine the base of the number system, otherwise print -1. Solution First we declare an array of strings which will hold all the possible values of digits that system numbers with base till 16 can have. Than we get the number to be analyzed from user. Being that in the request we have that the given string must not have more than 255 characters this is the first controll tha we make, if the string is larger than 255 characters we print the value -1, which means that we do not have a valid input and we return by ending the method execution. We declare an integer variable highestIndex intialized with the value -1 and a boolean variable isValidDigit. The highestIndex variable will store the value of the biggest valid base for the digits of the string taken in input, and the boolean variable isV...

Exercise Beginner: Product of numbers

Image
Request: Find the product in a segment of numbers when there is given the beginning of the segment and the ending of the segment. Solution This exercise can have different solutions, in this case we will show the one by using the for loop. If you are not familiar with the for loop follow the lesson 7 first. One possible solution of this problem is shown in the example code bellow: Example code: import java.util.Scanner; public class ProductOfSegment { public static void main(String[] args) { System.out.println("Give the starting number of the segment"); Scanner scanner = new Scanner(System.in); int firstNumber = scanner.nextInt(); System.out.println("Give the ending number of the segment"); int lastNumber = scanner.nextInt(); int product = 1; for( int i= firstNumber; i As it can be seen is a straightforward solution, where after taking the starting element and ending element from us...

Java Beginner: Lesson 8

Image
Array After haveing some knowledges about data types, if condition and loops we will see arrays in this lesson. We will abandon the HelloWorld application that we have used previously and we will create a new one. If you need help on how to create a new project refresh your memory from lesson 1 . I will name the project on this lesson Array, you can name it whatever you would like to. Being that we have seen loops in previous lessons we will make a simple iteration into an array and edit some elements of the array. Array in java is a data structure which can hold inside it elements of the type which we declare that it can hold. For example, we will have a data structure which can hold integers if we declare an array as shown in the example bellow: Example code: int myArray[]; But how many elements can this data structure hold? We have not determined it yet because we have just declared the array and not initialized or instantiated it. There are different ways to give a value ...

Java Beginner: Lesson 7

Image
for loop After showing the while and do while loops in the previous lessons we will see the for loop in this lesson. We will see 2 types of implementation of the for loop in this lesson. The first way to implement the for loop, an unusual one, is the one shown bellow, editing the code of the previous lessons to replace do while with for to keep the same functionality we get: Example code: // TODO code application logic here Scanner scanner = new Scanner(System.in); double nr1=0; double nr2=0; String operation; double result =0; for( ; true ; ){ System.out.println("**********NEW OPERATION*********"); System.out.println("Give the first number: "); nr1= scanner.nextDouble(); System.out.println("Give the operation: "); operation = scanner.next(); System.out.println("Give the second number: "); ...

Java Beginner: Lesson 6

Image
do while loop Same as the while loop, shown in the lesson 5 , there are other loops which make possible to repeat an action or a set of actions. In this lesson we will cover the do while loop. In the main method which in the previous lesson was as follow: Example code: public static void main(String[] args) { // TODO code application logic here Scanner scanner = new Scanner(System.in); double nr1=0; double nr2=0; String operation; double result =0; while( true ){ System.out.println("**********NEW OPERATION*********"); System.out.println("Give the first number: "); nr1= scanner.nextDouble(); System.out.println("Give the operation: "); operation = scanner.next(); System.out.println("Give the second number: "); nr2= scanner.nextDouble(); if(operation.equalsIgnoreCase("+...

Java Beginner: Lesson 5

Image
while loop In order to repeat a certain action or set of actions like in most programming languages even in Java there are loops that make possible such an action. In this lesson we will see the while loop. In this lesson we will continue customizing our hello world application with which we have worked in the previouse lessons we will add a while loop into it. This loop will help us repeat operation and not exit the application after the first operation. The state of the main method before we add the for loop is as shown bellow. Example code: public static void main(String[] args) { // TODO code application logic here Scanner scanner = new Scanner(System.in); double nr1=0; double nr2=0; String operation; double result =0; System.out.println("Give the first number: "); nr1= scanner.nextDouble(); System.out.println("Give the operation: "); operation = sca...