Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 9 June 2013

JAVA Program: Finding Smallest Number (Demonstration of Recursion)

Posted on 12:46 by Unknown
Hello, everyone. I want to present to you another program I did in my latest programming class at college. What we're looking at is a demonstration of finding the smallest number in any given set of values; only instead of using a for loop, we are using recursion.

A main method is provided to demonstrate this method. The result is at the bottom of the code.


/*
 * Data Structures and Object-Oriented Programming - Assignment 10
 * Question 24.24 of Introduction to Java Programming (by Daniel Liang)
 * 
 * Find the Smallest Number (Divide and Conquer Approach)
 * 
 * This program finds the smallest number of a list using a
 * divide and conquer approach; specifically, recursion, with
 * the original list split in two.
 * 
 * Programmed by Gregory Desrosiers
 * 
 * Start Date: April 28, 2013
 * End Date: May 5, 2013
 * 
 * File Name: Chapter24Exercise24.java
 * Teacher: Amin Ranj Bar
 * 
 * *SPECIAL*: Regardless if the list size is odd, the program is able to
 * find the smallest value in the list.
 */

import java.util.*;

public class Chapter24Exercise24
{
public static void main(String[] args)
{
List<Double> values = new ArrayList<Double>();

Random randomizer = new Random();

for (int x = 0; x < 125 ; x ++)
        values.add((double)randomizer.nextInt(150));

System.out.println("Test Case Values:");

for (int x = 0; x < values.size() ; x ++)
System.out.println(values.get(x));

System.out.println("\nThe smallest number in the list is " + smallestNumberInList(values)+ ".\n");

}

public static double smallestNumberInList(List<Double> numberList)
{
if (numberList.size() == 2)
{
double smallestNumber = numberList.get(0);

for(int x = 0; x < numberList.size(); x++)
{
if (numberList.get(x) < smallestNumber)
smallestNumber = numberList.get(x);
}

return smallestNumber;
}
else
{
if (numberList.size() > 2)
{
if (smallestNumberInList(numberList.subList(0, numberList.size() / 2)) < 
smallestNumberInList(numberList.subList(numberList.size() / 2, numberList.size())))
return smallestNumberInList(numberList.subList(0, numberList.size() / 2));
else
return smallestNumberInList(numberList.subList(numberList.size() / 2, numberList.size()));
}

// The statement below should not be there, but this is to prevent a compiler error.
// And this statement is invoked when the initialized number list only has one value.

return numberList.get(0); 
}
}
}

Console Output:

Test Case Values:
26.0
128.0
126.0
55.0
67.0
49.0
85.0
44.0
2.0
64.0
25.0
48.0
75.0
121.0
40.0
115.0
91.0
87.0
2.0
12.0
21.0
149.0
32.0
118.0
98.0
46.0
97.0
72.0
119.0
5.0
142.0
113.0
107.0
47.0
91.0
84.0
4.0
86.0
50.0
97.0
92.0
32.0
7.0
139.0
101.0
80.0
147.0
88.0
111.0
60.0
80.0
111.0
100.0
23.0
118.0
106.0
71.0
133.0
1.0
8.0
59.0
40.0
39.0
143.0
68.0
81.0
90.0
8.0
43.0
39.0
133.0
14.0
44.0
116.0
41.0
58.0
146.0
125.0
78.0
12.0
26.0
90.0
48.0
22.0
92.0
33.0
98.0
73.0
27.0
70.0
99.0
27.0
118.0
103.0
46.0
94.0
40.0
124.0
94.0
128.0
8.0
38.0
66.0
33.0
79.0
91.0
130.0
27.0
90.0
42.0
17.0
63.0
82.0
46.0
35.0
140.0
78.0
14.0
53.0
94.0
126.0
147.0
88.0
119.0
16.0


The smallest number in the list is 1.0.

That does it for this program. I hope you have a fun afternoon; I'll be in touch.

Follow me through the following pages and be sure to like my e-book page on Facebook for support!!

E-book: https://www.facebook.com/TheAspergerComputerGregDes

Facebook: https://www.facebook.com/pages/Gregory-Desrosiers/171954446270382?ref=hl
Twitter: http://www.twitter.com/GregoryDes
Google+: Search up "Gregory Desrosiers" in the Search Engine, then click on the profile that has a photo of me with two thumbs up.
YouTube: http://www.youtube.com/user/Bammer2001
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ▼  June (12)
      • "The Asperger Computer" outline and Spreading the ...
      • JAVA Program - Essay Class (From Chapter 11 of Ton...
      • JAVA Program: Circle Cannon Shooter (Shooting Game...
      • Message from my College English teacher
      • How to Play a DOS Game using DOSBox
      • JAVA Program: Finding Smallest Number (Demonstrati...
      • JAVA Program: Entering Non-Duplicate Numbers to a ...
      • Need some help on publicity with my e-book
      • JAVA Program: The PlayStation Class [SonyPlaystati...
      • The Asperger Computer - Second Edition Preview: Th...
      • Am I Happy on My Brother's 21st Birthday?
      • JAVA Program: The Nintendo GameCube Game Class [In...
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile