I recently wrote a short program for an exercise in a textbook. After completion however I re-structured the program to use methods instead of one long main method.

My question:

Which way of programming should I be using? I am leaning more towards the methods as it is much more re-useable although longer.

Any help is much appreciated, thanks.

First attempt with one long main method:

/**
 * Absolute Java 6.1
 * 
 * A program that takes input: degree of difficulty and seven judges scores.
 * Outputs the overall score for a particular dive where:
 * Score = (7 judges score-maxscore-minscore) x score multipler of 0.6 x a degree of difficulty
 * 
 *
 */

import java.util.Scanner;

public class DivingScores
{
	public static final double MIN_DEGREE_SCORE = 1.2;
	public static final double MAX_DEGREE_SCORE = 3.8;
	public static final double SCORE_MULTIPLIER = 0.6;
	
	public static final int MAX_DIVE_SCORE = 10;
	public static final int MIN_DIVE_SCORE = 0;
	public static final int JUDGES = 7;
	
	public static void main(String[] args)
	{
		
		Scanner keyboard = new Scanner(System.in);
		
		//	Get degree of difficulty.
		double degreeOfDifficulty;
		
		do
		{
			System.out.print("Enter a degree of difficulty: ");
			degreeOfDifficulty = keyboard.nextDouble();
			
			if ((degreeOfDifficulty < MIN_DEGREE_SCORE) || (degreeOfDifficulty > MAX_DEGREE_SCORE))
			{
				System.out.println();
				System.out.println("Degree must be between 1.2 and 3.8");
				System.out.println();
			}
		} while ( (degreeOfDifficulty < MIN_DEGREE_SCORE) || (degreeOfDifficulty > MAX_DEGREE_SCORE) );
		
		//	Get each judges score.
		double[] judgesScores = new double[JUDGES];
		
		for ( int index = 0; index < JUDGES; index++ )
		{
			double score;
			
			do
			{
				System.out.println();
				System.out.print("Enter judge " + (index+1) + " score: ");
				
				score = keyboard.nextDouble();
				
				if ( (score < MIN_DIVE_SCORE) || (score > MAX_DIVE_SCORE) )
				{
					System.out.println();
					System.out.println("Score must be between 0 - 10");
					System.out.println();
				}
				
			} while ( (score < MIN_DIVE_SCORE) || (score > MAX_DIVE_SCORE) );
			
			judgesScores[index] = score;
		}
		

		//	Sort judges scores into ascending order.
		for ( int index = 0; index < JUDGES; index++)
		{
			for ( int otherIndex = (index+1); otherIndex < JUDGES; otherIndex++ )
			{
				if ( judgesScores[otherIndex] < judgesScores[index] )
				{
					double temp = judgesScores[index];
					judgesScores[index] = judgesScores[otherIndex];
					judgesScores[otherIndex] = temp;
				}
			}
		}

		//	Calculate the final dive score.
		double finalScore = 0;
		for ( int index = 1; index < (JUDGES-1); index++ )
		{
			finalScore += judgesScores[index];
		}
		
		System.out.println();
		System.out.println("Dive score: " + (finalScore*degreeOfDifficulty*SCORE_MULTIPLIER));
	}
}

Second attempt with multple methods:

/**
 * Absolute Java 6.1
 * 
 * A program that takes input: degree of difficulty and seven judges scores.
 * Outputs the overall score for a particular dive where:
 * Score = (7 judges score-maxscore-minscore) x score multipler of 0.6 x a degree of difficulty
 * 
 *
 */

import java.util.Scanner;

public class DivingScoresAlternate
{
	public static final double MIN_DEGREE_SCORE = 1.2;
	public static final double MAX_DEGREE_SCORE = 3.8;
	public static final double SCORE_MULTIPLIER = 0.6;
	
	public static final int MAX_DIVE_SCORE = 10;
	public static final int MIN_DIVE_SCORE = 0;
	public static final int JUDGES = 7;
	
	/**
	 * Gets a degree score as user input and ensures the score is within correct range.
	 * 
	 * @return double A degree score between 1.2 - 3.8.
	 */
	public static double getDegree()
	{
		Scanner keyboard = new Scanner(System.in);
		double degreeOfDifficulty;
		
		do
		{
			System.out.print("Enter a degree of difficulty: ");
			degreeOfDifficulty = keyboard.nextDouble();
			
			if ((degreeOfDifficulty < MIN_DEGREE_SCORE) || (degreeOfDifficulty > MAX_DEGREE_SCORE))
			{
				System.out.println();
				System.out.println("Degree must be between 1.2 and 3.8");
				System.out.println();
			}
		} while ( (degreeOfDifficulty < MIN_DEGREE_SCORE) || (degreeOfDifficulty > MAX_DEGREE_SCORE) );
		
		return degreeOfDifficulty;
	}
	
	/**
	 * Gets 7 judges scores and ensures each score is within correct range.
	 * 
	 * @return double[] array of 7 judges scores between 0 - 10.
	 */
	public static double[] getJudgesScores()
	{
		Scanner keyboard = new Scanner(System.in);
		
		double[] judgesScores = new double[JUDGES];
		
		for ( int index = 0; index < JUDGES; index++ )
		{
			double score;
			
			do
			{
				System.out.println();
				System.out.print("Enter judge " + (index+1) + " score: ");
				
				score = keyboard.nextDouble();
				
				if ( (score < MIN_DIVE_SCORE) || (score > MAX_DIVE_SCORE) )
				{
					System.out.println();
					System.out.println("Score must be between 0 - 10");
					System.out.println();
				}
				
			} while ( (score < MIN_DIVE_SCORE) || (score > MAX_DIVE_SCORE) );
			
			judgesScores[index] = score;
		}
		
		return judgesScores;
	}
	
	/**
	 * Sorting algorithm for an array of doubles. Sorts into ascending order.
	 * 
	 * @param array Is the array to be sorted.
	 * @return An array of doubles in ascending order
	 */
	public static double[] sortAscending(double[] array)
	{
		for ( int index = 0; index < array.length; index++)
		{
			for ( int otherIndex = (index+1); otherIndex < array.length; otherIndex++ )
			{
				if ( array[otherIndex] < array[index] )
				{
					double temp = array[index];
					array[index] = array[otherIndex];
					array[otherIndex] = temp;
				}
			}
		}
		
		return array;
	}
	
	/**
	 * Calculates the final score for a dive. Formula:
	 * Score = (Sum of all the judges scores - MAXscore - MINscore)* degree of difficulty * 0.6
	 * 
	 * @param judgesScores Is the array of each judges scores.
	 * @param degreeOfDifficulty is a degree of difficulty for a dive.
	 * @return double The final score of a dive
	 */
	public static double getFinalScore(double[] judgesScores, double degreeOfDifficulty)
	{
		double finalScore = 0;
		for ( int index = 1; index < (JUDGES-1); index++ )
		{
			finalScore += judgesScores[index];
		}
		
		return (finalScore*degreeOfDifficulty*SCORE_MULTIPLIER);
	}
	
	public static void main(String[] args)
	{
		double degree = getDegree();
		double[] judgesScores = getJudgesScores();
		
		double diveScore = getFinalScore(judgesScores, degree);
		
		System.out.println();
		System.out.println("Dive Score: " + diveScore);
	}
}

Recommended Answers

All 2 Replies

Methods. Keep in mind that a method should do one thing and do it well. Some of your code can be broken down better; for example, you should have a method which gets one judge's score and another method that calculates the overall total score. The overall method would call the method that calculates one judge's score. The way you have it right now, if the methodology behind finding a judge's score changed, it would cause more problems. Consider, for example, if you wanted to change your program so that it got each judges score from a database. All you should need to do is change the mechanism/method that gets one judges score.

Hope that advice is helpful, but yes, methods are definitely the way to go for any code that you intend to be

1) reusable
2) modifiable
3) readable

+1 for the above.
This was a debate that was settled in the industry in the 1960's. Call it modularisation, delegation, encapsulation, whatever. It's best to break code into small understandable units, and in Java that means classes & methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.