Thursday, May 21, 2009

Mark Brozel Macbeth Review



    Source code  

import java.io. *;


public class Fibonacci {BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
opcionSalir int;
public Fibonacci ( ) {
mostrarMenu ();}



fibonacci_pd public static long (long n) throws
NumeroException {if (n \u0026lt;0) {throw new NumeroException
("Can not enter negative numbers");}



if (n == 0 n;}

long [] a = new long [(int) n +1];
a [0] = 0;
a [1] = 1;
for (int i = 2; i \u0026lt;= n; i + +) {
a [i] = a [i-1] + a [i-2];
}
return to [(int) n];
}

private void mostrarMenu () {

opcionSalir = 2 ;

int option;

do {String [] menu = {
"\\ n -------------- Fibonacci --------------" ,
"1 .- Enter Value"

""
"2 .- Exit"
"-------------------------
---------------"};
for (int i = 0; i \u0026lt;menu.length; i + +) {System.out.println
(menu [i]);}


System.out.println ();
pedirOpcion option = ();
System.out.println ();
procesarOpcion (option);
System.out.println ();}

while (option! = opcionSalir)
System.out.println ("Program complete.");



} private void procesarOpcion (int option) {
switch (option) {case 1
:
calculate ();
break;

case 2:
System.exit (0);
break;


}} private int

pedirOpcion () {int
option = 0;

do {option = pedirEntero ("Options");
if (option \u0026lt;1 & & option> opcionSalir) {
System.out.println ("Invalid option, repeat");}


} while (choice \u0026lt;1 catch (IOException ex) {System.out.println
(
"Generic Error Input-Output with the keyboard");

} catch (NumberFormatException ex) {
System.out.println ("Invalid format for number") ;

} return integer;


} public void calculate () {System.out.println
(
"\\ n Please enter the number from which you want to calculate the Fibonacci sequence:");



try {long number = Long.parseLong (teclado.readLine ());
System.out.println (
"\\ nThe value of the Fibonacci function for the number" + number +
"is:" + fibonacci_pd (number));

} catch (NumeroException ex) {
System.err.println ("You can only enter numbers");


} catch (IOException ex) {
System . err.println (ex.getMessage ());

} catch (NumberFormatException ex) {
System.err.println (ex.getMessage ());}



} public static void main (String [] args ) {
new Fibonacci ();
}}


NUMEROEXCEPTION


CLASS public class extends NumeroException Exception {public

NumeroException (String message) {
super (message);
}}







previous Code import java.io. *;

public class Fibonacci {

/ **
* fibonacci function is implemented with dynamic programming technique.
* It is noted that the number is positive.
* @ param n * @ throws long
NumeroException

* @ return long * / public static long
fibonacci_pd (long n) throws
NumeroException {if (n \u0026lt;0) {throw new NumeroException
("Can not enter negative numbers ");

} if (n == 0) {throw new NumeroException
(" The number must be greater than 0 ");}

long [] f = new long [(int) n + 1 ];
f [0] = 0;
f [1] = 1;
for (int i = 2; i \u0026lt;n + 1, i + +) {
f [i] = f [i - 1] + f [i - 2];

} return f [(int) n];}


/ ** * Function Main
. It asks the user a number to calculate its value by
Fibonacci function * * @
param String args []
* /
public static void main (String [] args) {BufferedReader reader
= new BufferedReader (new InputStreamReader (System.in));
System.out.println (
"\\ n Please enter the number from which you want to calculate the Fibonacci sequence:");

try {long number = Long.parseLong (reader.readLine ());
System.out.println (
"\\ nThe value of the Fibonacci function for the number" + number +
"is:" + fibonacci_pd (number));}

catch (NumberFormatException NFE) {
System.err.println ("You can only enter numbers");
main (args);

} catch (NumeroException ne) {System.err.println
(ne.getMessage ());
main (args);

} catch (IOException ioe) {System.err.println
(ioe.getMessage ());

}}



} Exception used in the code above


NumeroException
public class extends Exception {public

NumeroException (String message) {
super (message);
}}





0 comments:

Post a Comment