# Exponential growth calculator for COVID-19

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.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.out.println("We need to find the growth rate of Covid-19, we can use the growthRate method");
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.out.println("The time interval will be from February 01 2020 - April 16 2020, 45 days");
x = 45;

System.out.println("Now we have all the values let us use the method calculateExpoGrowth");
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) {
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;

pastValue = scan.nextInt();

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));

}
}

• Welcome to Code Review! Please edit your question to give it a more descriptive title which reflects what your code does.
– Null
Commented Apr 20, 2020 at 20:02
• 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. Commented Apr 20, 2020 at 22:34
• 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. Commented Apr 21, 2020 at 8:49
• 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. Commented Apr 21, 2020 at 10:40
• Can I receive help with my calculations as I am not really sure about the formulas I use. Thanks, Commented Apr 21, 2020 at 10:41

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;

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;

pastValue = Integer.valueOf(scan.nextLine().trim());

presentValue = Integer.valueOf(scan.nextLine().trim());

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));
}
}

• 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 Commented Apr 23, 2020 at 17:41