HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Graduation planner

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
plannergraduationstackoverflow

Problem

I know this is not very good. I am currently in my first programming class (Java). It seems like it works, but I am sure there are things I have not thought of.

The goal is to have a user enter

  • the number of credit units each class they need to take is worth, one by one



  • the number of credit units to complete in a term



The program should calculate

  • the number of remaining credit units



  • the number of terms it will take to complete the degree



  • the cost



```
import java.util.ArrayList;
import java.util.Scanner;

public class GraduationPlanner {

public static void main(String[] args) {
int sum = 0;
String creditUnits;
int unitsLeft;
int plannedUnits = 0;
int termsLeft = 0;
double tuitionCost = 0;
final double TUITION_RATE = 2890;
ArrayList cu = new ArrayList();
Scanner in = new Scanner(System.in);
System.out.println("Please input the Credit Units for each course. Press enter after each course. Press Q when finished: ");
while (in.hasNextInt()) {
int temp = (in.nextInt());
if (temp < 0) {
System.out.print("ERROR! Please enter a number 1-9.: ");
} else {
cu.add(temp);
}
}
System.out.println(cu);
Scanner ex = new Scanner(System.in);
System.out.print("Please enter the number of Credit Units you will complete per term: ");
if (ex.hasNextInt()) {
plannedUnits = ex.nextInt();
}
for (int a : cu)
{
sum += a;
}
unitsLeft = (sum - plannedUnits);

termsLeft = (sum / plannedUnits);
tuitionCost = (termsLeft * TUITION_RATE);
System.out.println("After this term there are " + unitsLeft + " credit units remaining.");
System.out.println("You will complete your degree in " + termsLeft + " terms.");
System.out.print("It will cost you " + tuitionCost + " to complet

Solution

I have been teaching Java to new students for some years, and I have seen a lot of beginner Java code. For a complete beginner, your code isn't horrible -- and that's high praise! For example, your variable naming is fairly decent, especially for a beginner. (But cu isn't a good name. Change that.) I can't see anything in your code that immediately strikes me as Don't do that!!!, and that is rare for beginner code :-)

There are of course a lot of things to improve upon. I'm only going to state some of the parts I consider most urgent, because you need to learn one step at a time. Once you master the basic principles, you can move on.

The small steps

First of all, you should divide your code into logical segments. Preferably, this should be various methods/functions, but start by doing logical divisions inside your main function. Group the code lines into logical units, and separate those units with a single empty line. You don't want to create your variables earlier than needed. If you keep them close to where they are used, i.e. make them part of the code they logically belong to, it's easier to remember what they are for, and to avoid using them in the wrong place. For example, move int sum = 0; down to right above

for (int a : cu) {
        sum += a;
}


Note that I changed your for loop to adhere to the Java conventions, like the rest of your code does.

Functions

After that, you should split your code into functions. Try to make a function perform a single logical computation. Knowing what "a single computation" really means is hard, but you will get it with experience. The old Unix mantra is nice: "Do one thing, and do it well."

Do not fall for the temptation to move variables into class scope when you introduce functions. Pass them as parameters instead.

The reason you want to use functions is to reduce complexity. Which of these two snippets is easier to read?

int sum = calculateSum(numbers);


or...

int sum = 0;
for (int i : numbers) {
    sum += i;
}


Exactly. Neither of the snippets are rocket science, but the former is much easier to read. Functions are also used to make the code easier to reuse and Bob Loblaw, but don't think too much about that yet.

Note that since you want a function to do a single item of work, you don't want functions that do both computations and input/output. In your case, you should probably do I/O in main and call functions to do the real calculation.

When you have incorporated these changes and are happy with your program, you could post it as a new question for further guidelines.

Code Snippets

for (int a : cu) {
        sum += a;
}
int sum = calculateSum(numbers);
int sum = 0;
for (int i : numbers) {
    sum += i;
}

Context

StackExchange Code Review Q#29757, answer score: 8

Revisions (0)

No revisions yet.