I haven’t really worked on this page in about 4 years, and now that im working far away from home and have no friends and plenty of time on my hands,

I decided to do something productive with my time and that is to teach what I have learned in my java class. I think the best way to learn (instead of

taking tutorials) is to just look at the code. The first program I will start off with is called Stadium. This program is rather basic and doesn’t go into any

kind of interface; it basically makes sure you know C++. If you don’t understand this program, maybe you should take some C++ classes.

 

Before getting into the lessons, you should be aware of symbols I have created to better document what the code means or does. First of all, if a word or group of words is highlighted in yellow, for example: ‘public class Stadium’ that means that is the specific part of the code I’m explaining right beside the forward slash symbols. If a part of the code is highlighted in red, for example: public static void main(String[] str), that means that part of the code was created in the ‘design tab’ part of JBUILDER. This means you should be in design mode to create the code (not typing it w/ your fingers!?). Last but not least, you should take note of coding highlighted in blue. I highlighted it in blue because it is very important and often times skipped over in writing the code.

 

One last note, if you are going to Old Dominion University and taking Java with Dr. Crouch, you should not be here. The Old Dominion Honor Code forbids you to be here! Anyway, on to the code.

 

Program #1 Stadium (Data Types, Operators & Expressions)

Program #2 Applet Swing Classes, Program Control Structures, and Methods

Program #3 Graphics Drawing Classes

Program #4 Animation and Graphic Images

Program #5 (I’m probably not going to include this program because #6 is very similar)

Program #6 Databases, JDBC, and Complex Queries

 

Upcoming additions this link will tell you what new things I’m adding to the page. For example, the design of this page is horrid and I will also be providing new program codes and program outputs. If I get permission from my previous prof, I might even put the interfaces up.

 

 

Program #1 (stadium)

 

/*what the program does: calculates the total expenses of the different types of seating in a stadium. Inputs: user inputs how many people have bought seats at 5 different locations. Each location has a different price. Price of the seat is not an input, it is static. This program also includes two other files which are necessary for the program to run: NumericData and NumericInput. They called inside the following code and they basically format the numbers (if I remember correctly), classify them, and makes sure they are numbers.

 

import java.text.*;

import java.io.*;

 

public class Stadium

  {

  final static int NUMBER_OF_POSITIONS=5; // final is very similar to the constant variable in C++

 

  // an array of promts is defined

 

  final static String seating[] = {"Number of Box Seats:         \t",

                                   "Number of Side Line Seats:   \t",

                                   "Number of End Zone Seats:    \t",

                                   "Number of Folding Chairs:    \t",

                                   "Number of End Zone Standing: \t",

  };

public static void main(String[] str)

  {

  /* this is where all of the variable are defined. theSize represents the amount

     seats sold, the rate represents how much it costs per seat, the expenses represents

     how much it costs total for the different sections, number of people is the

     variable used to get the total amount of people that purchased tickets, the percentage

     represents what percentage of people bought tickets - where? */

 

  int theSize[] = new int[NUMBER_OF_POSITIONS]; // please note this is the same variable as the ‘final static int’ above

  double rate[] = {620.00, 65.00, 25.00, 15.00, 5.00}; // these are the static prices of the seats.

  double expenses[] = new double[NUMBER_OF_POSITIONS];

  int numberOfPeople; // this variable holds the total amount of people that have bought tickets

  double thePercentage[]=new double[NUMBER_OF_POSITIONS]; /* this variable provides the percentage (how many people bought tickets for a type of seat devided by total amount of tickets bought */

  double totalExpenses; // holds total expenses or actually total amount of money made by the stadium

 

  //below are the methods (functions)

 

  enterData(theSize); // when this statement executes, it looks through the code below to find the ‘enterData’ function. Once that function is complete, it comes back and performs the next       function (calculateExpenses). When part of functions are capitalized (ex: enterData), it is important to make sure all correlating functions are Capitolized in the same way, for example, when you find the function called in the below code (public static void enterData(int theSize[])), you will notice enterData is capitalized in the same way. This means FUNCTIONS AND VARIABLES IN JAVA ARE CASE SENSITIVE

  calculateExpenses(theSize, rate, expenses); /* now that you have the values for theSize, you need theSize again for the next function, you also need rate and expenses which are arrays defined above*/

  numberOfPeople=calculateNumberOfPeople(theSize); this calculates the total number of people. The value returned is used to calculate the percentages.

  calculatePercent(theSize, numberOfPeople, thePercentage);

  totalExpenses=calculateTotalExpenses(expenses);

  printReport(theSize, rate, expenses, numberOfPeople, totalExpenses, thePercentage);

  }

 

public static void enterData(int theSize[]) // note that you need the highlighted information inside a function call. You didn’t need these when calling the function above.

  {

  NumericData returnValue = new NumericData(); // calls up a function (which is another file I will provide soon)

  System.out.println("Please fill in the following information\n");

    for(int i=0; i<NUMBER_OF_POSITIONS; i++) // this means (since number of positions is 5) – the loop will go through 5 times.

    {

 

      /*the following asks the user to input the amount of people at each

        different seating area.*/

 

System.out.println("Please Enter the " + seating[i]); // if you go up to the top of the code, there are 5 different types of seating. The loop displays a different type of seat each time it goes through.

        returnValue = NumericInput.getInt(); // calls up a function (which is another file I will provide soon)

 

        if(returnValue.ioFlag==true)

            theSize[i] = returnValue.intValue; // records the values the user inputs as theSize. Once the loop is complete there should be 5 values inside the array ‘theSize’

        else

        {

          /* if per say they enter something besides a numeric value, they are asked

          to try again*/

 

          System.out.println("Invalid Entry, Please Retry");

          i--;

        }

      }

    }

 

private static void calculateExpenses(int theSize[], double rate[], double expenses[])

  {

  // the following calculates the expenses by multiplying the rate by the amount of

  // people in each seating area

  for(int i=0; i<NUMBER_OF_POSITIONS; i++)

    {

    expenses[i]=theSize[i]*rate[i]; //note that you have a different value per variable each time you go through the loop.

    }

  }

 

private static int calculateNumberOfPeople(int theSize[])

{

  int numberOfPeople=0;

 

  // this function calculates the total # of people which is necessary for the next

  // function because it calulates the percentage of 'who sat where'.

 

  for(int i =0; i<NUMBER_OF_POSITIONS; i++)

  {

    numberOfPeople+=theSize[i]; // this adds all of the values stored in theSize to get the total amount of seats bought

  }

  return(numberOfPeople);

}

 

 

 

private static void calculatePercent(int theSize[], int numberOfPeople, double thePercentage[])

  {

  for(int i=0; i<NUMBER_OF_POSITIONS; i++)

    {

    thePercentage[i]=theSize[i]*100/numberOfPeople; //note that there are 5 values stored in thePercentage when loop is complete

    }

  }

 

 

private static double calculateTotalExpenses(double expenses[])

{

  double totalExpenses=0;

  for(int i=0; i<NUMBER_OF_POSITIONS; i++)

  {

    totalExpenses+=expenses[i];

  }

  return(totalExpenses);

}

 

 

public static void printReport(int theSize[], double rate[], double expenses[], int numberOfPeople, double totalExpenses, double thePercentage[])

{

 

StringBuffer outputBuffer = new StringBuffer(100);

int intLength, fill, doubleLength, doubleLength2, doubleLength3;

 

// below prints out the main heading

  System.out.println("\t\t              Seats Sold      Percent       Price Per      Expenses");

  System.out.println("\t\t                                Sold          Seat\n");

 

  /*the first decimal format (below) allows the program to print out a number with

  commas and two values to the right of the decimal point. I used this for total expenses.

   The second decimal format was used to print out a number with just commas. i used this for

   the total amount of people*/

  DecimalFormat fmt= new DecimalFormat("#,##0.00");

  DecimalFormat fmt2 = new DecimalFormat("#,###");

  for(int i=0; i<NUMBER_OF_POSITIONS; i++)

    {

    outputBuffer.delete(0,99);

 

    /*the following 4 lines declare an object, assign values of amount of people (the size),

    initializes strings*/

    Integer classInteger = new Integer(theSize[i]);

    String intString = new String();

    intString = classInteger.toString();

    intLength = intString.length();

 

    /*the following 4 lines declare an object, assign values of the percentage of people

    (thePercentage), initializes strings*/

    Double classDouble = new Double(thePercentage[i]);

    String doubleString = new String();

    doubleString = classDouble.toString();

    doubleLength=doubleString.length();

 

    /*the doubleString2 object is initialized with the string that is generated

      when the 'format' method write the value of the double precision test value*/

    String doubleString2 = new String(fmt.format(rate[i]));

    doubleLength2 = doubleString2.length();

 

    String doubleString3 = new String(fmt.format(expenses[i]));

    doubleLength3 = doubleString3.length();

 

    outputBuffer.append(" ");

    outputBuffer.append(seating[i]);  // this sets up the format of the diff kinds

    outputBuffer.append("  ");        // of seats

 

    fill = 10-intLength;

    for(int j=0;j<fill;j++)

 

    outputBuffer.append(" ");

    outputBuffer.append(intString);   // sets up format of Seats Sold

    outputBuffer.append("   ");

 

    fill = 10-doubleLength;

    for(int j = 0; j<fill; j++)

    outputBuffer.append(" ");          // sets up format of Percent Sold

    outputBuffer.append(doubleString);

    outputBuffer.append("  ");

 

    fill = 15-doubleLength2;

    for(int j = 0; j<fill; j++)

    outputBuffer.append(" ");           // sets up format of Price per Seat

    outputBuffer.append(doubleString2);

    outputBuffer.append("  ");

 

    fill = 15-doubleLength3;

    for(int j = 0; j<fill; j++)

    outputBuffer.append(" ");          // sets up format of Expenses

    outputBuffer.append(doubleString3);

    outputBuffer.append("  ");

 

 

 

 

    System.out.println(outputBuffer);       //prints out the above formats

    }

System.out.println("    Total:\t\t\t      " +fmt2.format(numberOfPeople) + "        100.0                        " +fmt.format(totalExpenses));

}

}

 

1