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

Prompting for five test scores and printing the average

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

Problem

I am not sure how to properly make this readable. I have been trying to find examples online but I cannot find any that are specific to this simple program.

```
import java.util.Scanner;

public class KifahUnit3 {

public static void main(String[] args) {

Scanner stdIn = new Scanner(System.in);
String first; //first name
String last; //last name
String full; //full name
Boolean nameMatch = false;
int score1;
int score2;
int score3;
int score4;
int score5;
int totalScore;
int x;
double scoreAverage;
System.out.print("Please enter your first name: ");
first = stdIn.next();
System.out.print("Please enter your last name: ");
last = stdIn.next();
System.out.println(first + " " + last);
nameMatch = first.equals(last);
if (nameMatch == true) {
System.out.println(
"Hello, " + first + " " + last + " "
+ "your first and last name are the same.");
} else {
System.out.println(
"Hello, " + first + " " + last + " "
+ "your first and last name are different.");
full = first + last;
System.out.println("The length of your first name is: " + first.length());
System.out.println("The length of your full name is: " + full.length());
System.out.println("Your initials are " + first.charAt(0) + last.charAt(0));

System.out.println("Please enter your test scores");
System.out.print("Score 1= ");
score1 = stdIn.nextInt();
System.out.print("Score 2= ");
score2 = stdIn.nextInt();
System.out.print("Score 3= ");
score3 = stdIn.nextInt();
System.out.print("Score 4= ");
score4 = stdIn.nextInt();
System.out.print("Score 5= ");
score5 = stdIn.next

Solution

Firstly - Java Code Convention

Read the Java code convention, see this cheatsheet

The Java Code convention has a more detailed description -

Why use code conventions (From the link) -

Code conventions are important to programmers for a number of reasons:

  • 80% of the lifetime cost of a piece of software goes to maintenance.



  • Hardly any software is maintained for its whole life by the original


author.

  • Code conventions improve the readability of the software,


allowing engineers to understand new code more quickly and thoroughly.

Secondly - Refactoring

Start by using arrays - this will really reduce the amount of code you wrote. For example:

// Declare this constant at the top of the class 
private static final int NUMBER_OF_SCORES = 5;

int[] score = new int[NUMBER_OF_SCORES];

System.out.println("Please enter your test scores");

// Read all Scores
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  score[i] = stdIn.nextInt();
}


With arrays, you can also easily calculate the total score and output each score -

// Total Score
int totalScore = 0;
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  totalScore += score[i];
}

// Output All the scores
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  System.out.print("score " + i + " = " + score[i]);
}


You can also introduce a Person class - example. (Take note of the getInitials function)

public class Person {

    private final String name;
    private final String surname;

    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getInitials() {
        return "" + name.charAt(0) + surname.charAt(0));
    }
}


With the introduction of a Person class, the main function can transform into the for example. (Also splitting up the main function into smaller parts)

public static final int NUMBER_OF_SCORES = 5;

public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
    System.out.print("Please enter your first name: ");
    // Read First Name
    String firstName = stdIn.next();
    System.out.print("Please enter your last name: ");
    // Read Last Name
    String lastName = stdIn.next();
    System.out.println("Hello " + firstName + " " + lastName);
    if (firstName.equals(lastName)) {
        System.out.println("Your first and last name are the same.");
    } else {
        // Now you know you have a valid person.
        Person person = new Person(firstName, lastName);
        System.out.println("Your first and last name are different.");
        processName(person);
        readAndProcessScores();
    }
}

private static void processName(Person person) {
    String full = person.getName() + person.getSurname();
    System.out.println("The length of your first name is: " + person.getName().length());
    System.out.println("The length of your full name is: " + full.length());
    System.out.println("Your initials are " + person.getInitials());
}

public static void readAndProcessScores() {
    Scanner stdIn = new Scanner(System.in);

    int[] score = new int[NUMBER_OF_SCORES];
    System.out.println("Please enter your test scores");
    // Read all Scores
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        System.out.print("Score " + i + " = ");
        score[i] = stdIn.nextInt();
        System.out.println();
    }

    // Output All the scores
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        System.out.print("score " + i + " = " +score[i] + " ");
    }

    // Total Score
    int totalScore = 0;
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        totalScore += score[i];
    }

    double scoreAverage = (((double) totalScore) / NUMBER_OF_SCORES);
    System.out.println("Total = " + totalScore);
    System.out.println("Your average is:  " + scoreAverage);
}


This code itself can be refactored even more, but this I leave to you :)
Try doing the following -

-
Introduce one more class.

-
Extract the calculate total score into its own function. Use this declaration:

public static double calculateAverage(int[] scores)


-
Add some comments to the methods :)

General tips

  • Always use good descriptive variable names.



  • Declare variables as needed (Close to where they are used).



Hope this helps!

Code Snippets

// Declare this constant at the top of the class 
private static final int NUMBER_OF_SCORES = 5;

int[] score = new int[NUMBER_OF_SCORES];

System.out.println("Please enter your test scores");

// Read all Scores
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  score[i] = stdIn.nextInt();
}
// Total Score
int totalScore = 0;
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  totalScore += score[i];
}

// Output All the scores
for (int i = 0; i < NUMBER_OF_SCORES; i++) {
  System.out.print("score " + i + " = " + score[i]);
}
public class Person {

    private final String name;
    private final String surname;

    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getInitials() {
        return "" + name.charAt(0) + surname.charAt(0));
    }
}
public static final int NUMBER_OF_SCORES = 5;

public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
    System.out.print("Please enter your first name: ");
    // Read First Name
    String firstName = stdIn.next();
    System.out.print("Please enter your last name: ");
    // Read Last Name
    String lastName = stdIn.next();
    System.out.println("Hello " + firstName + " " + lastName);
    if (firstName.equals(lastName)) {
        System.out.println("Your first and last name are the same.");
    } else {
        // Now you know you have a valid person.
        Person person = new Person(firstName, lastName);
        System.out.println("Your first and last name are different.");
        processName(person);
        readAndProcessScores();
    }
}

private static void processName(Person person) {
    String full = person.getName() + person.getSurname();
    System.out.println("The length of your first name is: " + person.getName().length());
    System.out.println("The length of your full name is: " + full.length());
    System.out.println("Your initials are " + person.getInitials());
}

public static void readAndProcessScores() {
    Scanner stdIn = new Scanner(System.in);

    int[] score = new int[NUMBER_OF_SCORES];
    System.out.println("Please enter your test scores");
    // Read all Scores
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        System.out.print("Score " + i + " = ");
        score[i] = stdIn.nextInt();
        System.out.println();
    }

    // Output All the scores
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        System.out.print("score " + i + " = " +score[i] + " ");
    }

    // Total Score
    int totalScore = 0;
    for (int i = 0; i < NUMBER_OF_SCORES; i++) {
        totalScore += score[i];
    }

    double scoreAverage = (((double) totalScore) / NUMBER_OF_SCORES);
    System.out.println("Total = " + totalScore);
    System.out.println("Your average is:  " + scoreAverage);
}
public static double calculateAverage(int[] scores)

Context

StackExchange Code Review Q#103947, answer score: 12

Revisions (0)

No revisions yet.