in Java. i think you need just 2 classes so I\'ll provide them please. you don\'
ID: 3914158 • Letter: I
Question
in Java. i think you need just 2 classes so I'll provide them please. you don't have to test just show me how please. Its commented //TODO
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
import lib280.list.LinkedList280;
public class PerformanceByRank {
public static LinkedList280<Crew> readCrewData(String path) {
Scanner infile = null;
try {
infile = new Scanner(new File(path));
}
catch (FileNotFoundException e) {
System.out.println("Error: File not found!");
}
// Initialize output list.
LinkedList280<Crew> pirateCrew = new LinkedList280<Crew>();
// While there is more stuff to read...
while(infile.hasNext()) {
// Read the three values for a Crew record
int rank = infile.nextInt();
double pay = infile.nextDouble();
int sacks = infile.nextInt();
// Create a crew object from the data
Crew c = new Crew(rank, pay, sacks);
// Place the new Crew instnace in the linked list.
pirateCrew.insertFirst(c);
}
// Close the input file like a good citizen. :)
infile.close();
// Return the list of Crew objects.
return pirateCrew;
}
public static void main( String args[] ) {
// Read the data for Jack's pirate crew.
// If you are getting a "File Not Found" error here, you may adjust the
// path to piratecrew.txt as needed.
LinkedList280<Crew> pirateCrew = readCrewData("A1-PirateCrew/piratecrew.txt");
// TODO Write your solution here.
}
}
other class
/**
* This class stores information about one of Tractor Jack's pirate crew.
*/
public class Crew {
/**
* The crew member's rank
*/
protected int rank;
/**
* The crew member's guaranteed annual pay (number golden guineas)
*/
protected double pay;
/**
* The number of sacks of grain plundered by this crew member this year.
*/
protected int grainSacks;
public Crew(int rank, double pay, int grainSacks) {
this.rank = rank;
this.pay = pay;
this.grainSacks = grainSacks;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
public int getGrainSacks() {
return grainSacks;
}
public void setGrainSacks(int grainSacks) {
this.grainSacks = grainSacks;
}
public String toString() {
return "Rank: " + new Integer(this.rank) + ", Pay: " + new Double(this.pay) + ", Plunder: " + new Integer(this.grainSacks);
}
}
Explanation / Answer
public static void main( String args[] ) {
// Read the data for Jack's pirate crew.
// If you are getting a "File Not Found" error here, you may adjust the
// path to piratecrew.txt as needed.
LinkedList280<Crew> pirateCrew = readCrewData("A1-PirateCrew/piratecrew.txt");
LinkedList280<Crew>[] piratesByRank = new LinkedList280<Crew>[10];
for (int i = 0; i<piratesByRank.length; i++){
piratesByRank[i] = new LinkedList280<Crew>();
}
for (int i = 0; i<pirateCrew.size(); i++){
int j = pirateCrew.get(i).getRank();
piratesByRank[j].add(pirateCrew.get(i))
}
for (int i = 0; i<10; i++){
double sumSacks = 0;
double amt = 0;
for (int j = 0; j < piratesByRank[i].size(); j++){
sumSacks = sumSacks + piratesByRank[i].get(j).getGrainSacks();
amt = amt + piratesByRank[i].get(j).getPay();
}
double avg = sumSacks/amt;
System.out.println("Jack's rank " + i + " crew member were paid of " + avg + " guineas per sack of graing plundered");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.