Results 1 to 3 of 3
Thread: ArrayLists Of Objects help!
- 11-14-2012, 02:41 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
ArrayLists Of Objects help!
I'm new to this forums thing but I can't seem to figure this out, the point is to 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, along with the name of the airline. The bottom is my output and all I see is a whole mess of zero's. What seems to be wrong.
American 26851 2210871
Continental 9316 622534
Delta 21515 1862276
Northwest 20803 1924288
USAir 9855 1542800
TransWorld 16228 1188124
United 35175 3673152
=======================
// File: AirDataListTester.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 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
*/
class AirDataList {
// instance var
private ArrayList<AirData> list; // list of AirData objects
/**
* Creates an empty list
*/
public AirDataList() {
list = new ArrayList<>();
}
/**
* 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);
}
public int totRevMiles(){
int rev = 0;
for (int i = 0; i < list.size(); i++){
AirData revMiles = list.get(i);
rev = rev + revMiles.getRevMiles();
}
return rev;
}
public int totPassMiles(){
int pass = 0;
for (int i = 0; i < list.size(); i++){
AirData passMiles = list.get(i);
pass = pass + passMiles.getPassMiles();
}
return pass;
}
/**
* 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 = " Revenue Miles Passenger Miles\n"
+ " Airline (in 1000's) (in 1000's)\n"
+ " ======= ============= ===============" + "\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 + Align.right(name, 12) + Align.right(revMiles, 16)
+ Align.right(passMiles, 18) + "\n";
}
return out + "\n";
}
public String anotherString(){
String out = " Shares of Revenue Shares of Passengers\n "
+ " Airline (in %) (in %)\n"
+ " ======= ================= ====================" + "\n";
for (int i = 0; i < list.size(); i++){
AirData air = list.get(i);
String name = air.getName();
int totRev = ((air.getRevMiles()/totRevMiles())*100);
int totPop = ((air.getPassMiles()/totPassMiles())*100);
out = out + Align.right(name, 12) + Align.right(totRev, 21)
+ Align.right(totPop, 24) + "\n";
}
return out + "\n";
}
} // end of AirDataList class definition =====================================
public class AirDataListTester {
public static void main(String[] args) throws IOException
{
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:\n");
while (infile.hasNext()) // while not eof...
{
// read next line
String line = infile.nextLine();
// "echo print" data entered
System.out.println(line);
// 1. create a Scanner object associated with String "line"
Scanner filescan = new Scanner(line);
// 2. extract tokens from current line
while (filescan.hasNext()){
String name = filescan.next();
int revenueMiles = filescan.nextInt();
int passengerMiles = filescan.nextInt();
// 3. create AirData object passing the tokens to the constructor
AirData next = new AirData(name,revenueMiles,passengerMiles);
// 4. add object to list
list.addToList(next);
}
}
System.out.println();
System.out.println(list.toString()); // print the list
System.out.println(list.anotherString());
}
} // end of AirDataListTester class definition
======================================
run:
Data entered:
American 26851 2210871
Continental 9316 622534
Delta 21515 1862276
Northwest 20803 1924288
USAir 9855 1542800
TransWorld 16228 1188124
United 35175 3673152
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
Shares of Revenue Shares of Passengers
Airline (in %) (in %)
======= ================= ====================
American 0 0
Continental 0 0
Delta 0 0
Northwest 0 0
USAir 0 0
TransWorld 0 0
United 0 0
BUILD SUCCESSFUL (total time: 1 second)
- 11-14-2012, 02:57 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Re: ArrayLists Of Objects help!
Found the answer!
- 11-14-2012, 11:01 PM #3
Similar Threads
-
I don't get ArrayLists
By Gio!? in forum New To JavaReplies: 10Last Post: 05-03-2012, 12:50 PM -
Arraylists
By talia in forum New To JavaReplies: 5Last Post: 01-30-2012, 06:44 PM -
adding objects into arrayLists
By socboy6579 in forum New To JavaReplies: 19Last Post: 12-10-2010, 06:59 AM -
ArrayLists
By Freakzoyd in forum New To JavaReplies: 4Last Post: 11-12-2010, 04:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks