The following AirData.txt contains: This is the lab: Exercise 1. Open the input
ID: 3763146 • Letter: T
Question
The following AirData.txt contains:
This is the lab:
Exercise 1.
Open the input file AirData.txt in NetBeans. Note that each line of the file contains an airline name, number of revenue-miles flown (in thousands), and number of passenger-miles flown (also in thousands).
Compile and run the program AirDataListTester. In its current state, all it does is read successive lines from the AirData.txt file and “echo print” them. Then it calls the printData method of the AirDataList class. Only the headings are printed because no objects have been added to the ArrayList yet.
In the main method of class AirDataListTester, write statements (in the locations indicated) to
create a Scanner object to scan the current line of input
use the Scanner object you created to extract the 3 tokens of the current line
create an AirData object passing the tokens to the constructor
add the object to AirDataList object list.
(For reference, see files InputDemo2.java and BankTester2.java)
Make no changes anywhere else in the program. When this is done, the program should print out a table of the data read from the input file.
Exercise 2.
Now add a method to the AirDataList class that will compute and print each airline’s share of the total revenue miles and of the total passenger miles.
An airline’s share of the total revenue miles is defined as the revenue miles for that airline divided by the total revenue miles of all the airlines, expressed as a percent.
The share of the total passenger miles is similarly defined.
Here is the algorithm:
1. First we need to compute the total revenue miles and the total passenger miles
a. Declare and initialize two accumulators, one for the total revenue miles and one for the total passenger miles
b. For each AirData object on the list:
i."get" the next AirData object.
ii.get the revenue miles for that object and add it to the accumulator for the total revenue miles
iii.get the passenger miles for that object and add it to the accumulator for the total passenger miles
2. Now that we have the totals we can compute each airline's share
Traverse the list again and for each AirData object on it:
i.get the next object.
ii.get the revenue miles for that airline and divide it by the total revenue miles to get that airline's share of the total revenue miles. Convert to a percent.
iii.Compute that airline's share of the total passenger miles (hint: see ii.)
iv.Print the airline name and its shares of the total revenue miles and total passenger miles
Write a statement in main to call this method.
Explanation / Answer
/*
* A class to store data for an airline
*/
class AirData
{
// instance vars
private String name; // airline name
private int revenueMiles; // annual revenue miles (in 1000's)
private int passengerMiles; // annual passenger miles (in 1000's)
/**
* Creates an AirData object.
*
* @param name the airline name
* @param revenueMiles the number of revenue miles flown
* @param passengerMiles the number of passenger miles flown
*/
public AirData(String name, int revenueMiles, int passengerMiles)
{
this.name = name;
this.revenueMiles = revenueMiles;
this.passengerMiles = passengerMiles;
}
/**
* Returns the airline name.
*
* @return the airline name
*/
public String getName()
{
return name;
}
/**
* Returns the airline's revenue miles flown.
*
* @return the revenue miles
*/
public int getRevMiles()
{
return revenueMiles;
}
/**
* Returns the airline's passenger miles flown.
*
* @return the passenger miles
*/
public int getPassMiles()
{
return passengerMiles;
}
} // end of AirData class definition
------------------------------------------------------------------------------------------------------------
/**
* A class to implement a list of AirData objects
*/
import java.util.ArrayList;
class AirDataList
{
// instance var
private ArrayList<AirData> list; // list of AirData objects
/**
* Creates an empty list
*/
public AirDataList()
{
list = new ArrayList<AirData>();
}
/**
* Appends an AirData object to the list.
*
* @param current the object to be appended to the list
*/
public void addToList(AirData current)
{
list.add(current); // calls add method of ArrayList class
}
//New method
/**The method printShars that calculates the total revenue and total passenger
* miles and then prints the share of airline in total of revenues and passenger
* miles */
public void printShars()
{
double totalRev=0;
double totalPassenger=0;
for (int i = 0; i < list.size(); i++)
{
//Add the revenue miles to totalRev
totalRev+=list.get(i).getRevMiles();
//Add the passenger miles to totalPassenger
totalPassenger+=list.get(i).getPassMiles();
}
System.out.printf("%-20s%-10s%-10s ",
"Airline","Rev.Share(%)","Passenger.Share(%)");
System.out.printf("%-20s%-10s%-10s%n","=======","=======",
"==============");
for (int i = 0; i < list.size(); i++)
{
//calculate the percent of revunue share percent
double revShare=(list.get(i).getRevMiles()/totalRev)*100;
//calculate the percent of passenger share percent
double passShare=(list.get(i).getPassMiles()/totalPassenger)*100;
System.out.printf("%-20s%-10.2f%-10.2f ",
list.get(i).getName(),
revShare,passShare);
}
}
/**
* Converts the list to a multi-line string, with each line containing the
* data for one airline.
*
* @return the String containing all the data on the list
*/
public String toString()
{
// headings
String out =
String.format("%28s%18s%n", "Revenue Miles", "Passenger Miles") +
String.format("%12s%16s%18s%n", "Airline", "(in 1000's) ",
"(in 1000's) ") +
String.format("%12s%16s%18s%n","=======","=============",
"===============");
// for each AirData object on the list...
for (int i = 0; i < list.size(); i++)
{
AirData air = list.get(i); // get next AirData obj
String name = air.getName(); // get airline name
int revMiles = air.getRevMiles(); // get revenue miles
int passMiles = air.getPassMiles(); // get passenger miles
// concatenate data to output string
out = out + String.format("%12s", name)
+ String.format("%16s", revMiles)
+ String.format("%18s", passMiles) + " ";
}
return out + " ";
}
} // end of AirDataList class definition
------------------------------------------------------------------------------------------------------------
/**The modified test program that will prints the airlines
* of AirData class objects.
* And then prints the share of airlines in total revunues
* and passenger miles*/
//AirDataListTester.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class AirDataListTester
{
public static void main(String[] args) throws IOException
{
//Create an instance of AirDataList
AirDataList list = new AirDataList();
// create Scanner object to read each line of file until eof
Scanner infile = new Scanner(new File("AirData.txt"));
System.out.println("Data entered: ");
//while not eof...
while (infile.hasNext())
{
// read passenger name from file
String passenger=infile.next();
// read revunue miles from file
int revunueMiles=infile.nextInt();
// read passenger miles from file
int passengerMiles=infile.nextInt();
// Create an object of class AirData with name, revunue miles
//passenger miles
list.addToList(new AirData(passenger, revunueMiles, passengerMiles));
}
System.out.println();
// print the list
System.out.println(list.toString());
//call the method printShars that print the percent of shares
//of airlines in total revenue and passenger miles
list.printShars();
}
} // end of AirDataListTester class definition
------------------------------------------------------------------------------------------------------------
input file AirData.txt
American 26851 2210871
Continental 9316 622534
Delta 21515 1862276
Northwest 20803 1924288
USAir 9855 1542800
TransWorld 16228 1188124
United 35175 3673152
------------------------------------------------------------------------------------------------------------
Sample Data entered:
Revenue Miles Passenger Miles
Airline (in 1000's) (in 1000's)
======= ============= ===============
American 26851 2210871
Continental 9316 622534
Delta 21515 1862276
Northwest 20803 1924288
USAir 9855 1542800
TransWorld 16228 1188124
United 35175 3673152
Airline Rev.Share(%)Passenger.Share(%)
======= ======= ==============
American 19.21 16.98
Continental 6.67 4.78
Delta 15.40 14.30
Northwest 14.89 14.77
USAir 7.05 11.85
TransWorld 11.61 9.12
United 25.17 28.20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.