Posts

Showing posts with the label Coding problems

How to Print Pyramid Pattern of Alphabets in Java program

In earlier programming tutorials, I have taught you how to print pyramid pattern of stars and numbers in Java, and in this tutorial, you will learn printing pyramid pattern of alphabets. If you understand the logic of previous programs then this one won't be difficult for you because we will use the same logic of printing rows and columns using nested loop in Java. Actually, pyramid pattern is nothing but a matrix where you need to print rows and columns. Though, you need to learn where to print those values and when to move to next row. Once you know this trick of advancing, you can print any kind of pyramid pattern in Java. The one, we'll see in this tutorial is the simplest of one but I'll give you some tough one for exercise to develop your creativity and coding skill. Read more �

How to calculate GCF and LCM of two numbers in Java? Example

This week's programming exercise is to write a Java program to calculate GCF and LCM of two numbers . The GCF , stands for Greatest common factor and LCM stands for Lowest common multiplier, both are popular mathematical operation and related to each other. The GCF is the largest number which divides both the number without leaving any remainder e.g. if two numbers are 24 and 40 then their GCF is 8 because 8 is the largest number which divides both 24 and 40 perfectly, without leaving any remainder. Similarly, LCM is the lowest number which is perfectly divisible by the two number, for example, if given number is 40 and 24 then their LCM is 120 because this is the lowest number which is perfectly divisible by both 40 and 24. Read more �

Java Object Oriented Analysis and Design Problem - Vending Machine Part 2

This is the second part of Java tutorial to show how to create Vending Machine in Java. In the first part, we have discussed problem statement and the solution itself, but unit testing and design document was still pending, which we'll see in this article. As I said, there are multiple ways to implement Vending machine in Java e.g. you could have easily used state design pattern to implement a vending machine, in fact, it's one of the best examples of State design pattern. Vending machine behaves differently on different states e.g. return a product if the machine is not empty, otherwise, it just returns coins, so it ideally fits in state design pattern. Though, In our solution, I have not used the state design pattern but have just coded the solution with an if else block. This is easier because of a limited number of state and not many state transition but in more real world scenario, state design pattern is better as it uses Polymorphism and removes the logic we have put ins...