d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > New Coder > Progress Thread
12Next
Add Reply New Topic New Poll
Member
Posts: 7,755
Joined: Sep 26 2011
Gold: 129.34
Mar 4 2014 11:01pm
I'm a new coder looking to get better and find people of like mind. I'm currently developing a game in java to kill some free time and help learn better the language. Here is a copy of the most recent assignment i had to turn in for class, let me know what you think. It grabs 2 numbers from the user and finds the greatest common divisor of both.

//Michael Scott
//Date: 3/4/2014
import java.util.Scanner;
public class DivisorFinderApp
{
public static void main(String[] args)
{
//instantiate variables and scanner
Scanner sc = new Scanner(System.in);
String choice = "y";
int num1 = 0;
int num2 = 0;
int output = 0;

//Intro
System.out.println("Greatest Common Divisor Finder");
System.out.println();

//begin loop for continue function
while (choice.equalsIgnoreCase("y"))
{
//get 2 numbers
System.out.print("Enter the first number: ");
num1 = sc.nextInt();
System.out.print("Enter the second number: ");
num2 = sc.nextInt();

//calculate and display output
output = getGreatestCommonDivisor(num1, num2);
System.out.println("Greatest common divisor: " + output);
System.out.println();

//prompt to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}

}
public static int getGreatestCommonDivisor(int num1, int num2)
{
int buffer = 0;
int x = num1;
int y = num2;

while (x != 0)
{
while (y >= x)
{
y = y - x;
}
buffer = x;
x = y;
y = buffer;
}
return y;

}
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 4 2014 11:09pm
pls to use code tags for posting code :o
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 4 2014 11:38pm
Quote (mikey00004 @ Mar 5 2014 12:01am)
I'm a new coder looking to get better and find people of like mind. I'm currently developing a game in java to kill some free time and help learn better the language.  Here is a copy of the most recent assignment i had to turn in for class, let me know what you think. It grabs 2 numbers from the user and finds the greatest common divisor of both.


If you are looking at improvements to the algorithm? I would say Euclid did it best:

Code
public static int euclid(int A, int B)
{
int a = Math.max(A,B);
int b = Math.min(A,B);

while (b != 0)
{
int t = b;
b = a % b;
a = t;
}

return a;
}


Just to highlight the performance boost. Running your algorithm vs "mine" with this input
Code
gcd(1073741824,2)


Yields
Code
Mine: iterations (1) 2
Yours: iterations (536870912) 2


Pretty significant, don't you think?

Code
gcd(1073741824,1231236)


yields

Code
Mine: iterations (8) 4
Yours: iterations (1001) 4


This post was edited by Minkomonster on Mar 4 2014 11:55pm
Member
Posts: 3,415
Joined: Jun 17 2006
Gold: 38,177.00
Mar 6 2014 07:08am
use the modulo (%) function to find the biggest common divisor. it would be easier.

edit : instead of using a ton of whiles just use a
while(true){
System.out.println("whatever your options are for the menu");
sc.nextInt();
switch(choice)

case 0 : //quit program
return;

case 1 :
//stuff to do here
break;

case 2 :
//stuff to do here
break;

//as many cases as options in your menu
}

This post was edited by Lord_balthamos on Mar 6 2014 07:14am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 6 2014 06:10pm
Quote (Lord_balthamos @ Mar 6 2014 08:08am)
use the modulo (%) function to find the biggest common divisor. it would be easier.

edit : instead of using a ton of whiles just use a
while(true){
System.out.println("whatever your options are for the menu");
sc.nextInt();
switch(choice)

case 0 : //quit program
return;

case 1 :
//stuff to do here
break;

case 2 :
//stuff to do here
break;

//as many cases as options in your menu
}


He uses just as many while loops in his implementation for input fetching as you did in your "improvement" with the exception of his being cleaner and more readable. There is nothing wrong with how he gathers input. Please don't suggest an improvement to someone unless you have a solid reason as to why it is an improvement.
Member
Posts: 7,755
Joined: Sep 26 2011
Gold: 129.34
Mar 7 2014 12:04am
Thanx for comments and suggestions guys i appreciate it a lot. By the way how do I write in code block in jsp so that the indentation looks nice and like its supposed too?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 7 2014 12:06am
Quote (mikey00004 @ Mar 7 2014 02:04am)
Thanx for comments and suggestions guys i appreciate it a lot.  By the way how do I write in code block in jsp so that the indentation looks nice and like its supposed too?


click the button next to the quote button before the text size/color button.

else use the [CODE][CODE] tags (remember to use / to close the ending tag like everything else)
Member
Posts: 3,415
Joined: Jun 17 2006
Gold: 38,177.00
Mar 7 2014 02:42am
Quote (Minkomonster @ Mar 7 2014 01:10am)
He uses just as many while loops in his implementation for input fetching as you did in your "improvement" with the exception of his being cleaner and more readable. There is nothing wrong with how he gathers input. Please don't suggest an improvement to someone unless you have a solid reason as to why it is an improvement.


the way he gathers input will work, only if he chooses to make bigger programs the menu will be in one place where you can call the methods you'll need and be more organised with my method.

This post was edited by Lord_balthamos on Mar 7 2014 02:46am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 7 2014 12:46pm
Quote (Lord_balthamos @ Mar 7 2014 04:42am)
the way he gathers input will work, only if he chooses to make bigger programs the menu will be in one place where you can call the methods you'll need and be more organised with my method.


You suggested a broken way of doing things. If your code were to replace his, 0 would not be a value a user could input because it would exit from the function, as well as you would have a minimum of 11 case statements just to do what he does with a few input reads. If he ever needed to expand this to other mathematical functions it would be highly unsuitable. Please don't suggest such garbage.

This post was edited by AbDuCt on Mar 7 2014 12:48pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 7 2014 01:23pm
Quote (AbDuCt @ Mar 7 2014 01:46pm)
You suggested a broken way of doing things. If your code were to replace his, 0 would not be a value a user could input because it would exit from the function, as well as you would have a minimum of 11 case statements just to do what he does with a few input reads. If he ever needed to expand this to other mathematical functions it would be highly unsuitable. Please don't suggest such garbage.


He's suggesting the use of a switch statement to handle general menu selections. At it's core, it's not a bad design (setting aside the fact that it breaks OOP design), and with tweaks it could be applied to the OPs implementation to handle user input. But for what reason? It's a single user input statement that checks for a single value to continue looping. It's a prime example where s while loop is satisfactory. Implementing the switch statement just adds useless fluff.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll