patternjavaMinor
Grade report builder
Viewed 0 times
gradereportbuilder
Problem
I am starting out with Java programming and get limited feedback from my professor. Following is an assignment that I handed in, and just re-factored. I wanted to see if people with Java experience could give some pointers on improving how it looks. Last semester I took a C++ course so splitting sections up into public/private methods/classes/objects is pretty foreign to me. Feedback on commenting is also welcome; I'm not sure about how much I should be commenting.
Note: All user input is taken is as a string as per the Professor's request. Hence the
```
import java.util.Scanner;
public class ReportBuilder {
static Scanner keyboard = new Scanner(System.in);
public static int numericalInputs = 7;
private static String firstName;
private static String lastName;
static String reportName;
public static void main(String[] args) {
firstName = "Empty";
lastName = "Empty";
reportName = "Empty";
System.out.println("This program will generate a report based on the input");
System.out.println("If you wish to exit enter \"-1\"");
System.out.println("Please enter your first name:");
firstName = FirstInput.main();
System.out.println(firstName + ", please enter your last name one character at a time");
System.out.println("When you are finished, type 'Stop'");
lastName = SecondInput.main();
System.out.println("Hi, " + firstName + " " + lastName + ".");
System.out.println("Enter the name of the report:");
reportName = keyboard.next();
if (reportName.equals("-1")) {
exit();
}
else {
System.out.println("Please enter seven numbers one at a time");
ThirdInputv2.main();
}
keyboard.close();
}
public static void exit() {
System.out.println("Good bye!");
System.exit(0);
}
}
public class FirstInput {
public static Stri
Note: All user input is taken is as a string as per the Professor's request. Hence the
parseDoubles flying around.```
import java.util.Scanner;
public class ReportBuilder {
static Scanner keyboard = new Scanner(System.in);
public static int numericalInputs = 7;
private static String firstName;
private static String lastName;
static String reportName;
public static void main(String[] args) {
firstName = "Empty";
lastName = "Empty";
reportName = "Empty";
System.out.println("This program will generate a report based on the input");
System.out.println("If you wish to exit enter \"-1\"");
System.out.println("Please enter your first name:");
firstName = FirstInput.main();
System.out.println(firstName + ", please enter your last name one character at a time");
System.out.println("When you are finished, type 'Stop'");
lastName = SecondInput.main();
System.out.println("Hi, " + firstName + " " + lastName + ".");
System.out.println("Enter the name of the report:");
reportName = keyboard.next();
if (reportName.equals("-1")) {
exit();
}
else {
System.out.println("Please enter seven numbers one at a time");
ThirdInputv2.main();
}
keyboard.close();
}
public static void exit() {
System.out.println("Good bye!");
System.exit(0);
}
}
public class FirstInput {
public static Stri
Solution
Just a few comments:
Actually I did not plan on doing this, but whatever. Here's my version of the code.
```
import java.util.Scanner;
/**
* [describe what the program is doing]
*/
public class ReportBuilder {
static Scanner keyboard = null;
/**
* main program flow
*/
public static void main(String[] args) {
keyboard = new Scanner(System.in);
System.out.println("This program will generate a report based on the input");
System.out.println("If you wish to exit enter \"-1\"");
System.out.println("Please enter your first name:");
String firstName = getFirstName();
System.out.println(firstName + ", please enter your last name one character at a time");
System.out.println("When you are finished, type 'Stop'");
String lastName = getLastName();
System.out.println("Hi, " + firstName + " " + lastName + ".");
System.out.println("Enter the name of the report:");
String reportName = readString();
System.out.println("Please enter seven numbers one at a time");
double[] numbers = getNumberArray(7);
System.out.println("");
printReport(reportName, numbers);
keyboard.close();
}
/**
* handle exit condition each time a keyboard input is read
*/
private static String readString() {
String input = keyboard.next();
if ("-1".equals(input)) {
System.out.println("Good bye!");
System.exit(0);
}
return input;
}
private static String getFirstName() {
String firstName = readString();
return firstName.replaceAll("[0-9]+ ?","");
}
/** This method takes in the last name one character at a time then stops when instructed.
*/
private static String getLastName() {
String lastName = "";
while (true) {
String input = readString();
// When the user enters "Stop" no more input is accepted
if (input.equalsIgnoreCase("Stop")) {
break;
}
// The validateLast method of CheckNum is called to see if the user input contains a number.
if (isNumber(input)) {
System.out.println("Please do not enter any numbers");
}
// add character to lastName
lastName += input.substring(0, 1);
}
return lastName;
}
/** This method collects each number entered by the user then stores it in an array
* After each number is entered: the max value, minimum value, average, and total are displayed
*/
private static double[] getNumberArray(int numberOfInputs) {
double[] inputs = new double[numberOfInputs];
double min = 9999, max = 0, sum = 0, avg = 0;
for (int i = 0; i < numberOfInputs; i++) {
String userInput = readString();
if (isNumber(userInput)) {
inputs[i] = Double.parseDouble(userInput);
min = Math.min(min, inputs[i]);
max = Math.max(max, inputs[i]);
sum = sum + inputs[i];
avg = sum / (i+1);
System.out.println("The running total is " + sum);
System.out.println("The max value so far is " + max
+ " and the min value is " + min);
System.out.println("The average value is " + avg);
if (i < numberOfInputs) {
System.out.println("Please enter the next number:");
}
} else {
System.out.println("Please enter a number!");
i--;
}
}
return inputs;
}
/** This method prints out the final report to screen.
*/
private static void printReport(String name, double[] numbers) {
double min = 9999, max = 0, sum = 0, avg = 0;
double[] mins = new double[numbers.length];
double[] maxs = new double[numbers.length];
double[] sums = new double[numbers.length];
double[] avgs = new double[numbers.length];
// Header
System.out.println("Report ID: " + name);
System.out.println("Input" + "\t" + "Max" + "\t" + "Min" + "\t" + "Total" + "\t" + "Average");
System.out.println("-------
- in Java, comments describing methods are ususally placed right before the method declaration, not after it. Also, use
/** ... */, so they are turned into Javadoc comments.
- Its odd how you create separate classes for each of the input tasks, and how those classes define a
mainmethod. In Java, themainmethod describes the entry point to your program, like themainmethod in your first class. Calling other methodsmainmay lead to confusion.
- Rather than having several classes with lots of static variables, try to convert those classes to simple methods of your main class, and make those variables local to those methods.
Actually I did not plan on doing this, but whatever. Here's my version of the code.
```
import java.util.Scanner;
/**
* [describe what the program is doing]
*/
public class ReportBuilder {
static Scanner keyboard = null;
/**
* main program flow
*/
public static void main(String[] args) {
keyboard = new Scanner(System.in);
System.out.println("This program will generate a report based on the input");
System.out.println("If you wish to exit enter \"-1\"");
System.out.println("Please enter your first name:");
String firstName = getFirstName();
System.out.println(firstName + ", please enter your last name one character at a time");
System.out.println("When you are finished, type 'Stop'");
String lastName = getLastName();
System.out.println("Hi, " + firstName + " " + lastName + ".");
System.out.println("Enter the name of the report:");
String reportName = readString();
System.out.println("Please enter seven numbers one at a time");
double[] numbers = getNumberArray(7);
System.out.println("");
printReport(reportName, numbers);
keyboard.close();
}
/**
* handle exit condition each time a keyboard input is read
*/
private static String readString() {
String input = keyboard.next();
if ("-1".equals(input)) {
System.out.println("Good bye!");
System.exit(0);
}
return input;
}
private static String getFirstName() {
String firstName = readString();
return firstName.replaceAll("[0-9]+ ?","");
}
/** This method takes in the last name one character at a time then stops when instructed.
*/
private static String getLastName() {
String lastName = "";
while (true) {
String input = readString();
// When the user enters "Stop" no more input is accepted
if (input.equalsIgnoreCase("Stop")) {
break;
}
// The validateLast method of CheckNum is called to see if the user input contains a number.
if (isNumber(input)) {
System.out.println("Please do not enter any numbers");
}
// add character to lastName
lastName += input.substring(0, 1);
}
return lastName;
}
/** This method collects each number entered by the user then stores it in an array
* After each number is entered: the max value, minimum value, average, and total are displayed
*/
private static double[] getNumberArray(int numberOfInputs) {
double[] inputs = new double[numberOfInputs];
double min = 9999, max = 0, sum = 0, avg = 0;
for (int i = 0; i < numberOfInputs; i++) {
String userInput = readString();
if (isNumber(userInput)) {
inputs[i] = Double.parseDouble(userInput);
min = Math.min(min, inputs[i]);
max = Math.max(max, inputs[i]);
sum = sum + inputs[i];
avg = sum / (i+1);
System.out.println("The running total is " + sum);
System.out.println("The max value so far is " + max
+ " and the min value is " + min);
System.out.println("The average value is " + avg);
if (i < numberOfInputs) {
System.out.println("Please enter the next number:");
}
} else {
System.out.println("Please enter a number!");
i--;
}
}
return inputs;
}
/** This method prints out the final report to screen.
*/
private static void printReport(String name, double[] numbers) {
double min = 9999, max = 0, sum = 0, avg = 0;
double[] mins = new double[numbers.length];
double[] maxs = new double[numbers.length];
double[] sums = new double[numbers.length];
double[] avgs = new double[numbers.length];
// Header
System.out.println("Report ID: " + name);
System.out.println("Input" + "\t" + "Max" + "\t" + "Min" + "\t" + "Total" + "\t" + "Average");
System.out.println("-------
Code Snippets
import java.util.Scanner;
/**
* [describe what the program is doing]
*/
public class ReportBuilder {
static Scanner keyboard = null;
/**
* main program flow
*/
public static void main(String[] args) {
keyboard = new Scanner(System.in);
System.out.println("This program will generate a report based on the input");
System.out.println("If you wish to exit enter \"-1\"");
System.out.println("Please enter your first name:");
String firstName = getFirstName();
System.out.println(firstName + ", please enter your last name one character at a time");
System.out.println("When you are finished, type 'Stop'");
String lastName = getLastName();
System.out.println("Hi, " + firstName + " " + lastName + ".");
System.out.println("Enter the name of the report:");
String reportName = readString();
System.out.println("Please enter seven numbers one at a time");
double[] numbers = getNumberArray(7);
System.out.println("");
printReport(reportName, numbers);
keyboard.close();
}
/**
* handle exit condition each time a keyboard input is read
*/
private static String readString() {
String input = keyboard.next();
if ("-1".equals(input)) {
System.out.println("Good bye!");
System.exit(0);
}
return input;
}
private static String getFirstName() {
String firstName = readString();
return firstName.replaceAll("[0-9]+ ?","");
}
/** This method takes in the last name one character at a time then stops when instructed.
*/
private static String getLastName() {
String lastName = "";
while (true) {
String input = readString();
// When the user enters "Stop" no more input is accepted
if (input.equalsIgnoreCase("Stop")) {
break;
}
// The validateLast method of CheckNum is called to see if the user input contains a number.
if (isNumber(input)) {
System.out.println("Please do not enter any numbers");
}
// add character to lastName
lastName += input.substring(0, 1);
}
return lastName;
}
/** This method collects each number entered by the user then stores it in an array
* After each number is entered: the max value, minimum value, average, and total are displayed
*/
private static double[] getNumberArray(int numberOfInputs) {
double[] inputs = new double[numberOfInputs];
double min = 9999, max = 0, sum = 0, avg = 0;
for (int i = 0; i < numberOfInputs; i++) {
String userInput = readString();
if (isNumber(userInput)) {
inputs[i] = Double.parseDouble(userInput);
min = Math.min(min, inputs[i]);
max = Math.max(max, inContext
StackExchange Code Review Q#28479, answer score: 3
Revisions (0)
No revisions yet.