1
\$\begingroup\$

I've been listening to some programmers and they use terms like good and bad code. I am not really sure what they mean by that. I mean I went on r/badcode and saw some stuff I knew was definitely bad code. But I don't really understand if bad code is repeatedly typing the same code instead of looping or just bad layouts.

    import java.io.IOException;
    import java.util.Scanner;
    class Covid19ExpoGrowth {
    public static void main(String[] args) throws IOException {
    // I will use the formula y = a(1 + r)^x
    // a = initial value
    // r = growth rate
    // x = time interval
    int a;
    double r;
    int x;

    // Workout exponential growth
    System.out.println("Let us see the Exponential growth of Covid-19.");
    System.in.read();
    System.out.println("We will set the initial value of Covid as 88,000 as that was the amount of cases in february");
    a = 88000;
    System.in.read();

    System.out.println("We need to find the growth rate of Covid-19, we can use the growthRate method");
    System.in.read();
    r = growthRate(14000, 88000);
    r = Math.round(r * 10.0) / 10;
    System.out.println("Using the growthRate method, we worked out the growth rate is: " + r + " rounded to no d.p");
    System.in.read();

    System.out.println("The time interval will be from February 01 2020 - April 16 2020, 45 days");
    x = 45;
    System.in.read();

    System.out.println("Now we have all the values let us use the method calculateExpoGrowth");
    System.in.read();
    double roundedExpo;
    roundedExpo = Math.round(calculateExpoGrowth(a, r, x) * 100) /10;
    System.out.println("The Exponential growth of Covid 19 is: " + roundedExpo + " rounded to 2 d.p");

    System.out.println("You can now work out your own Exponential Growth");

    // User works out chosen exponential growth
    while (true) {
        System.out.println("Please enter the initial value");
        Scanner scan = new Scanner(System.in);
        a = scan.nextInt();

        Scanner scan_gr = new Scanner(System.in);System.out.println("Enter WORKOUT GROWTH RATE if you would like to workout the growth rate," +
                "if you already know it press enter");
        String gr = scan_gr.nextLine();
        if (gr.equals("WORKOUT GROWTH RATE")) {
            int pastValue;
            int presentValue;

            System.out.println("Please enter the past value");
            pastValue = scan.nextInt();

            System.out.println("Please enter the present value");
            presentValue = scan.nextInt();

            r = growthRate(pastValue, presentValue);
        } else {
            System.out.println("Please type in the growth rate");
            r = scan.nextDouble();
        }
        System.out.println("Now finally, type in the time interval");
        x = scan.nextInt();
        double roundedExpo2;

        roundedExpo2 = Math.round(calculateExpoGrowth(a, r, x) * 100) / 10;

        System.out.println("The program has calculated the Exponential growth is: " + roundedExpo2 + " rounded to 2 d.p");
    }
}
public static double growthRate(int pastVal, int presentVal){
    // To work out growth rate you:

    double newVal = presentVal - pastVal;
    // Subtract the past value from the present value

    return newVal / pastVal;
    // Divide the new value by the past value

}
public static double calculateExpoGrowth(int a, double r, int x){
    // Exponential growth formula
    return (a * Math.pow((1 + r), x));

}
}
\$\endgroup\$
6
  • 4
    \$\begingroup\$ Welcome to Code Review! Please edit your question to give it a more descriptive title which reflects what your code does. \$\endgroup\$
    – Null
    Commented Apr 20, 2020 at 20:02
  • \$\begingroup\$ Is your code correct? Have you ever seen 2 decimal place results in your output? Have you checked the results by hand? Math.round(calculateExpoGrowth(a, r, x) * 100) /10 doesn't look correct. \$\endgroup\$
    – AJNeufeld
    Commented Apr 20, 2020 at 22:34
  • \$\begingroup\$ As @AJNeufeld stated, your calculations are incorrect. You wanted a code critique, so here's mine. Indent your code properly. Don't use so many comments, especially where the comment just repeats what the code does. If you comment, your comment should explain why you coded in a certain way. Use blank lines to break up logical groups of code within a method, not every line in the method. I find your main method too large, but since your process is linear, it's not horrible. \$\endgroup\$ Commented Apr 21, 2020 at 8:49
  • \$\begingroup\$ When I receive the output I get a huge number so I had to round it. If there is a better way please let me know. \$\endgroup\$ Commented Apr 21, 2020 at 10:40
  • \$\begingroup\$ Can I receive help with my calculations as I am not really sure about the formulas I use. Thanks, \$\endgroup\$ Commented Apr 21, 2020 at 10:41

1 Answer 1

0
\$\begingroup\$

Your formula was assuming 14,000 cases a day, rather than 14,000 cases a month. I removed all rounding, choosing to round for display only.

Here's a partial result of the last test run I did. I formatted the output manually to fit in the answer.

Let us see the Exponential growth of Covid-19.
We will set the initial value of Covid as 
88,000 as that was the amount of cases in 
February.

We need to find the growth rate of Covid-19, 
we can use the growthRate method.
We'll assume a linear growth rate of 14,000 
cases a month.

Using the growthRate method, we worked out 
the growth rate is 0.005303.

The time interval will be from February 1, 
2020 - April 16, 2020, 45 days.
Now we have all the values let us use the 
method calculateExpoGrowth

The Exponential growth of Covid 19 is 
111,647 cases by April 16, 2020.

I cleaned up the code a lot. Here it is.

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Covid19ExpoGrowth {
    public static void main(String[] args) {
        try {
            calculateGrowth();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println();
        System.out.println("You can now work out your "
                + "own Exponential Growth");

        // User works out chosen exponential growth
        Scanner scan = new Scanner(System.in);
        while (getUserInput(scan));
        scan.close();
    }

    private static void calculateGrowth() throws IOException {
        // I will use the formula y = a(1 + r)^x
        // a = initial value
        // r = growth rate
        // x = time interval
        int a;
        double r;
        int x;

        // Workout exponential growth
        System.out.println("Let us see the Exponential "
                + "growth of Covid-19.");
        System.out.println(
                "We will set the initial value of Covid "
                + "as 88,000 as that was the amount of "
                + "cases in February.");
        System.out.println();
        a = 88_000;
        System.out.println("We need to find the growth "
                + "rate of Covid-19, we can use the "
                + "growthRate method.");
        System.out.println("We'll assume a linear growth rate "
                + "of 14,000 cases a month.");
        System.out.println();
        r = growthRate(a, 14_000d + a) / 30d;
        String output = String.format("%.6f", r);
        System.out.println("Using the growthRate method, "
                        + "we worked out the growth "
                        + "rate is: " + output + ".");
        System.out.println();
        System.out.println("The time interval will be "
                + "from February 1, 2020 - April 16, 2020,"
                + " 45 days.");
        x = 45;
        System.out.println("Now we have all the values "
                + "let us use the method calculateExpoGrowth");
        System.out.println();
        double roundedExpo = calculateExpoGrowth(a, r, x);
        DecimalFormat formatter = new DecimalFormat(
                "###,###,###");
        output = formatter.format(roundedExpo);
        System.out.println("The Exponential growth of "
                + "Covid 19 is " + output 
                + " cases by April 16, 2020.");
    }

    private static boolean getUserInput(Scanner scan) {
        int a;
        double r;
        int x;

        System.out.println("Please enter the initial value");
        String value = scan.nextLine().trim();
        if (value.isEmpty()) {
            return false;
        }

        a = Integer.valueOf(value);

        System.out.println("Enter WORKOUT GROWTH RATE if you "
                + "would like to workout the growth rate,"
                + "if you already know it press enter");
        String gr = scan.nextLine().trim();
        if (gr.equalsIgnoreCase("WORKOUT GROWTH RATE")) {
            int pastValue;
            int presentValue;

            System.out.println("Please enter the past value");
            pastValue = Integer.valueOf(scan.nextLine().trim());

            System.out.println("Please enter the present value");
            presentValue = Integer.valueOf(scan.nextLine().trim());

            System.out.println("Please enter the days");
            int days = Integer.valueOf(scan.nextLine().trim());

            r = growthRate(pastValue, presentValue) / days;
        } else {
            System.out.println("Please type in the growth rate");
            r = Double.valueOf(scan.nextLine().trim());
        }
        System.out.println("Now finally, type in the time "
                + "interval");
        x = Integer.valueOf(scan.nextLine().trim());
        double roundedExpo2 = calculateExpoGrowth(a, r, x);
        DecimalFormat formatter = new DecimalFormat(
                "###,###,###");
        String output = formatter.format(roundedExpo2);
        System.out.println(
                "The program has calculated the Exponential "
                + "growth is: " + output + ".");
        return true;
    }

    public static double growthRate(double pastVal, 
            double presentVal) {
        double newVal = presentVal - pastVal;
        return newVal / pastVal;
    }

    public static double calculateExpoGrowth(int a, double r, 
            int x) {
        // Exponential growth formula
        return (a * Math.pow((1d + r), (double) x));
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot man I am really glad someone would take time out of their day to write this, you really me I feel like such an idiot lol. Thanks and sorry for taking long to reply I have been busy with another project. Edit: Also I am real glad you showed that equals Ignore Case method thats gonna help alot \$\endgroup\$ Commented Apr 23, 2020 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.