Question: In this problem, I need to calculate the average masses of 8 planets. I can only change the code inside the getMeanPlanetaryMass() function under the

In this problem, I need to calculate the average masses of 8 planets. I can only change the code inside the getMeanPlanetaryMass() function under the MassAverager class. I have created basic code, however, I need help. Any idea how to access the getMass() method for each of the created planets? The line that creates errors is highlighted below. Thanks!

// Class Planet

package planets; public class Planet {

// NASA publishes masses of planets at http://nssdc.gsfc.nasa.gov/planetary/factsheet/. // Mass units are 10^24 kg.

private final static float MASS_UNIT = 1.0e24f; private final static Planet[] THE_PLANETS = { new Planet("Mercury", 0.33f * MASS_UNIT), new Planet("Venus", 4.87f * MASS_UNIT), new Planet("Earth", 5.97f * MASS_UNIT), new Planet("Mars", 0.642f * MASS_UNIT), new Planet("Jupiter", 1898 * MASS_UNIT), new Planet("Saturn", 568 * MASS_UNIT), new Planet("Uranus", 86.8f * MASS_UNIT), new Planet("Neptune", 102f * MASS_UNIT) // No more Pluto :-( };

private String name; private float massKg; public Planet(String name, float massKg) { this.name = name; this.massKg = massKg; }

public String getName() { return name; }

public float getMass() { return massKg; }

public static Planet[] getAll() { return THE_PLANETS; }

}

// Class MassAverager

package planets; public class MassAverager { // Complete this. Retrieve the array of planets, then compute average mass. public float getMeanPlanetaryMass() { // Fetch the array of planet objects // Compute the sum of the masses // Divide by the size of the array // Planet.getAll(); float sumMasses = 0 ; for(int i=0;i<8;i++) { sumMasses = sumMasses + Planet[i].getMass; /* Any idea how to access the getMass() method for each of the created planets? ************ Errors Here*********** */

} float average = sumMasses/8; return average; } public static void main(String[] args) { MassAverager averager = new MassAverager(); System.out.println(averager.getMeanPlanetaryMass()); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!