Tuesday, January 10, 2012

Tutorial: Sum of All Possible Subsets of A Number in Java

by eturo

Before I will end my day, I would like to share a program source code to solve a problem which I think it's a little bit complicated. This was given when I once attended a seminar-workshop in our region. I made a Java applet for the said problem as shown below.

Computing All Subset Sums

Problem: Given an array containing some integers (there may be duplicates), write a" routine that returns all the possible sums that can be formed by using numbers in the array. For instance, if the array contains 4 and 6, the possible sums are n0, 4, 6, and 10." If the array contains 1, 3, 5, and 7, the possible sums are 0, 1, 3, 5, 7, 4, 6, 8, 8, 10, 12, 9, 11, 13, 15, and 16. Notice that 8 appears twice in the answer (1+7 and 3+5).". 


The Design:

 

Tutorial: A Guessing Game in Java

by eturo


Today, I want to introduced a simple guessing game I made in Java. This game is for everybody wherein the player has to guessed a random number from 1 to 10 which is generated by the program. If the player guessed the correct random number then he/she wins, otherwise he/she will be a loser. The game has also the ability to ask the player if he/she wants to continue the game.

In creating this simple game program, I used predefined (built-in) methods of classes such as the random() method of the Math class. I used looping statements (do-while, while, for loops) to create iteration such as in continuing the game. I also used conditional statements such as if-else and nested-if to compare the guessed number of the player with the random number generated by the computer.

How to Play the Game?

You need to register first your name. (see the image below)


After entering your name, the computer will ask you to enter any guessed number from number 1 to 10. (see the picture below)

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...