Przekształcenie algorytmu z typu tablicowego na ArrayList - NWD i NWW

0

Cześć,

mam problem z tym co zwraca metoda CalculateGreatestCommonDivisor, a mianowicie nie zwraca mi nic specjalnego przy wywołaniu jej w widoku.
Widok raczej dobrze jest skonstruowany, ale coś jest z metodą w modelu. Pozostałe metody w modelu zakomentowałem, aby mi nie bruździły zanim dojde co i jak przy obliczaniu NWD.

Mój Model:


package Model;
import java.util.ArrayList; 
import java.util.List;
/**
 *
 * Class used to calculate two math formulas.
 *
 * @author anonymous
 * @version 1.24
 *
 */
public class LeastGreatestCommon {

    
    
   
    
    int result;     // result of counting Least Common Multiple  **
    int result2;    // result of counting Greatest Common Divisor **
     
    /**
     * Method counting Greatest Common Divisor from Euclidean algorithm
     *
     * @param a the first value of arguments
     * @param b the second value of arguments
     * @return returns the value of the largest common divisor for two input
     * values
     * @throws NegativeValueException where is one or more negative values
     */
    public int gcd(int a, int b) throws NegativeValueException {
        if (a < 0) {
            throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
        }
        return b == 0 ? a : gcd(b, a % b);
    }

    /**
     * Method counting Least Common Multiple from Euclidean algorithm
     *
     * @param a the first value of arguments
     * @param b the second value of arguments
     * @return returns the value of the least common multiple for two input
     * values
     * @throws NegativeValueException where is one or more negative values
     */
    private int lcm(int a, int b) throws NegativeValueException {
        if (a < 0) {
            throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
        }
        return a * b / (gcd(a, b));
    }

    /**
     * Method counting Greatest Common Divisor of n numbers
     *
     * @param r the number of arguments that will be used to calculate GCD
     * @param tab_tmp the value of the array
     * @return returns the value of the Greatest common divisor for all input
     * values from the input stream or defined arguments
     * @throws NegativeValueException where is one or more negative values
     */
   public int calculateGreatestCommonDivisor(int r, int number) throws NegativeValueException {
        
        List<Integer> getGreatestCommonDivisor = new ArrayList<Integer>();
               
        
        for (int i = 0 ; i < r ; ++i) {
         getGreatestCommonDivisor.add(i);
         result2 = gcd(result2, getGreatestCommonDivisor.get(i));  
        //  getGreatestCommonDivisor.remove(i);
        }
        return result2;
    }

    /**
     * Method counting Least Common Multiple of n numbers
     *
     * @param r the number of arguments that will be used to calculate LCM
     * @param tab_tmp the value of the array
     * @return returns the value of the Least Common Multiple for all input
     * values from the input stream or defined arguments
     * @throws NegativeValueException where is one or more negative values
     */
//    public int calculateLeastCommonMultiple(int r, int[] tab_tmp) throws NegativeValueException {
//
//        int i;                      // integer representing the i-element array 
//        int[] tab = tab_tmp;        // element of the array **
//        int r_tmp = r;              // number of elements in the array **
//        r = tab.length;
//        result = lcm(tab[0], tab[1]);
//
//        for (i = 1; i < r; i++) {
//            if (tab[i] < 0) {
//                throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
//            }
//            result = lcm(result, tab[i]);
//        }
//        return result;
//    }
}

Mój Widok:

package View;

import Model.LeastGreatestCommon;
import Model.NegativeValueException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Main view class
 *
 * @author anonymous
 * @version 1.23
 *
 */
public class View {

    /**
     * The main method of counting calculate based on the given arguments value
     * of GCD and LCM
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NegativeValueException {

        int ResultOfTheMultiple;
        int ResultOfTheDivisor;
        //int r_tmp;

        LeastGreatestCommon leastGreatestCommon = new LeastGreatestCommon();  // Creating new object of LeastGreatestCommon class
//        Scanner scanner = new Scanner(System.in);
       ArrayList<Integer> getGreatestCommonDivisor = new ArrayList<>();
        
        if (args.length == 0) {   // Interaction with the user via the console

            int r;
            int number = 0;
            Scanner input = new Scanner(System.in);   // Creating Scanner object class associated with the input stream of object
            System.out.println("How many numbers will we count? ");
            r = input.nextInt();
            //int[] tab = new int[r];
            //System.out.println(r);
            for (int i = 0; i < r; i++) {
                System.out.println("Enter the number ");
                number = input.nextInt();
                
            }
            
            System.out.println("The array consists of " + r + " elements\n");
            for (int i = 0; i < r ; ++i) {
                System.out.print(number + " , ");
            }
                        
            System.out.println("\n");

            try {
             //  getGreatestCommonDivisor.forEach(s -> System.out.println(s));
              ResultOfTheDivisor = leastGreatestCommon.calculateGreatestCommonDivisor(r, number);
//                for (Integer calculations : ResultOfTheDivisor) {
//                System.out.println(calculations);
//            }
            // ResultOfTheDivisor.forEach(System.out::println);
//ResultOfTheMultiple = leastGreatestCommon.calculateLeastCommonMultiple(r, tab);
                
                System.out.println("The Greatest Common Divisor of these numbers is " + ResultOfTheDivisor);
            //    System.out.println("The Least Common Multiple of these numbers is " + ResultOfTheMultiple);
            } catch (NegativeValueException ex) {
               //  System.out.println(e);
               System.out.println(ex.getMessage());
            }
        }

//        if (args.length > 1) {  // Interaction with the user using arguments in the project properties
//
//            System.out.println("Arguments were entered");
//            System.out.println("\n");
//            int[] tab_tmp = new int[args.length];
//            r_tmp = Integer.parseInt(args[0]);
//
//            for (int i = 0; i < args.length; i++) {
//                tab_tmp[i] = Integer.parseInt(args[i]);
//            }
//
//            try {
//                ResultOfTheDivisor = leastGreatestCommon.calculateGreatestCommonDivisor(r_tmp, tab_tmp);
//                ResultOfTheMultiple = leastGreatestCommon.calculateLeastCommonMultiple(r_tmp, tab_tmp);
//                System.out.println("The Greatest Common Divisor of these numbers is " + ResultOfTheDivisor);
//                System.out.println("The Least Common Multiple of these numbers is " + ResultOfTheMultiple);
//            } catch (NegativeValueException ex) {
//                System.out.println(ex.getMessage());
//            }
//        }
    }
};

Mój Wyjątek:

package Model;

/**
 *
 * Exception class that's is thrown when number is negative
 *
 * @author anonymous
 * @version 1.23
 *
 */
public class NegativeValueException extends Exception {

    /**
     * Public constructor of NegativeValueException class
     *
     * @param msg Exception message that is returned when exception occurs.
     */
    public NegativeValueException(String msg) {
        super(msg);
    }
}

0

Wyjaśnij może czego oczekujesz od tej funkcji, obecnie przekazujesz do niej dwie liczby: r - ilość wprowadzonych liczb oraz number - ostatnia wprowadzona liczba.
P.S. Masz dziwną obsługę błędów:

  • rzucasz wyjątek gdy trafi się liczba ujemna - a można to sprawdzić zwykłym if-em podczas wczytywania (wypadałoby też poinformować użytkownika, że oczekujesz liczb >=0).,
  • ignorujesz realnie możliwy błąd - zły format liczby.
0

Oczekuje, żeby ta metoda policzyła mi wykorzystując metode gcd() największy wspólny dzielnik, korzystając z pól r, czyli ilość argumentów z których będziemy liczyć wyrażenie oraz number czyli liczby oddzielone spacją, dla których będziemy liczyć NWD.

0

Porównaj te trzy swoje wypowiedzi:

  • sygnatura funkcji w kodzie: public int calculateGreatestCommonDivisor(int r, int number),
  • fragment ostatniego postu oraz number czyli liczby oddzielone spacją,
  • tytuł Przekształcenie algorytmu z typu tablicowego na ArrayList.
    Drugie zignorowałem, bo jest bez sensu.
            for (int i = 0; i < r; i++) {
                System.out.println("Enter the number ");
                number = input.nextInt();
                getGreatestCommonDivisor.add(number);
            }
            ...
           ResultOfTheDivisor = leastGreatestCommon.calculateGreatestCommonDivisor(r, getGreatestCommonDivisor.add);
           ...
           public int calculateGreatestCommonDivisor(int r, ArrayList<Integer> numbers)
1

Nie prowadź dyskusji w komentarzach.
Co to jest integer? Ma być

ArrayList<Integer>

1 użytkowników online, w tym zalogowanych: 0, gości: 1