Computer Science 202
Introduction to Programming
Fall 2013, The College of Saint Rose
RunsScored BlueJ Project
Click here to download a BlueJ project for RunsScored.
RunsScored Source Code
The Java source code for RunsScored is below. Click on a file name to download it.
/**
* Program to report on a baseball score, giving combined
* score and average runs per inning for a single game.
*
* @author Jim Teresco
* CSC 202, The College of Saint Rose
* Fall 2012
*
* $Id: RunsScored.java 2217 2013-10-18 14:00:31Z terescoj $
*/
import java.util.Scanner;
public class RunsScored
{
public static void main(String args[]) {
// define a named constant for the number of innings, so we can
// change it here in one place to use our program for games with
// different numbers of innings. As a named constant it follows
// the ALL_CAPS_WITH_UNDERSCORES naming convention and is preceded
// by the "final" keyword to tell Java that we are about to give
// this name its "final" (and only) value.
final int NUMBER_OF_INNINGS = 9;
// we'll need a Scanner to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// read in visiting team name and score
System.out.print("Enter visiting team name and number of runs scored: ");
String visitingTeam = keyboard.next();
int visitingTeamScore = keyboard.nextInt();
// read in home team name and score
System.out.print("Enter home team name and number of runs scored: ");
String homeTeam = keyboard.next();
int homeTeamScore = keyboard.nextInt();
// compute and report total runs scored
System.out.println(visitingTeam + " and " + homeTeam + " combined for " +
(visitingTeamScore + homeTeamScore) + " runs.");
// compute average runs per inning
double runsPerInning = (visitingTeamScore + homeTeamScore)/(1.0*NUMBER_OF_INNINGS);
// report average runs per inning
System.out.println("That's " + runsPerInning + " runs per inning, on average.");
// now as a mixed number
int wholeRuns = (visitingTeamScore + homeTeamScore)/NUMBER_OF_INNINGS;
int fracRuns = (visitingTeamScore + homeTeamScore)%NUMBER_OF_INNINGS;
System.out.println("Or, " + wholeRuns + " " + fracRuns + "/" +
NUMBER_OF_INNINGS +", as a mixed number.");
}
}