Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package space; import java.util.ArrayList; public class Mission implements Comparable { private String destination; private float cost; public Mission(String destination, Float cost) { this.destination = destination;

image text in transcribedimage text in transcribedimage text in transcribed

package space;

import java.util.ArrayList;

public class Mission implements Comparable

{

private String destination;

private float cost;

public Mission(String destination, Float cost)

{

this.destination = destination;

this.cost = cost;

}

public String getDestination()

{

return destination;

}

public float getCost()

{

return cost;

}

// Compare by cost, then by destination.

public int compareTo(Mission that)

{

}

// Let compareTo() do the work. This method should just be 1 line.

public boolean equals(Object x)

{

}

// As you saw in lecture, create an ArrayList. Add destination and

// cost to the list. Return the list's hash code.

public int hashCode()

{

}

public String toString()

{

return "Mission to " + destination + " will cost " + cost;

}

}

package space;

import java.util.*;

/*

* During iteration, missions that cost

* Then expensive missions (that cost >= $1 trillion) are presented in the natural sorting order

* of the Mission class.

*/

public class MissionOrganizer implements Iterable

{

private final static float ONE_TRILLION = 1.0e12f;

// Insert declarations for 2 collections: 1 for cheap missions, 1 for expensive missions.

MissionOrganizer()

{

// Create the 2 mission collections.

}

// Returns true if either cheapMissions or expensiveMissions contains m.

public boolean contains(Mission m)

{

}

// Only adds if mission is not yet contained in this collection. Adds to

// cheapMissions or expensiveMissions, depending on whether the mission's

// cost is = $1 trillion.

public void add(Mission mission)

{

}

// Create an ArrayList. Add all the cheap missions to the ArrayList; then

// add all the expensive missions. (Hint: look in the API page for ArrayList for a

// method that adds all members of a collection.) The ArrayList will then contain

// all the missions, in the desired order. Return the ArrayList's iterator. This

// technique was shown in lecture, in the presentation of the Roster class, and will

// be on the final exam.

public Iterator iterator()

{

}

// All costs are uneducated guesses.

private final static Mission[] TEST_MISSIONS =

{

new Mission("Alderaan", 1E16f),

new Mission("High Earth Orbit", 3E8f),

new Mission("Moon", 2.5E10f),

new Mission("Alpha Centauri", 1E14f),

new Mission("Asteroids", 7E11f),

new Mission("Uranus", 9E11f),

new Mission("Jupiter", 2E11f),

new Mission("Low Earth Orbit", 1E8f),

new Mission("Cetaganda", 13E16f),

new Mission("Mars", 8E10f),

new Mission("Neptune", 1.0E12f),

new Mission("Barrayar", 1E16f),

new Mission("Saturn", 8E11f),

};

public static void main(String[] args)

{

MissionOrganizer organizer = new MissionOrganizer();

for (Mission m: TEST_MISSIONS)

organizer.add(m);

for (Mission m: organizer)

System.out.println(m);

}

/*

Expected output:

Mission to High Earth Orbit will cost 3.0E8

Mission to Moon will cost 2.49999995E10

Mission to Asteroids will cost 6.9999998E11

Mission to Uranus will cost 8.9999999E11

Mission to Jupiter will cost 1.99999996E11

Mission to Low Earth Orbit will cost 1.0E8

Mission to Mars will cost 8.0E10

Mission to Saturn will cost 7.9999998E11

Mission to Neptune will cost 1.0E12

Mission to Alpha Centauri will cost 1.0E14

Mission to Alderaan will cost 1.00000003E16

Mission to Barrayar will cost 1.00000003E16

Mission to Cetaganda will cost 1.30000001E17

*/

}

Mission.java This simple class contains the destination and cost of a space mission Complete 3 methods in this class: compareTo0 Compare by cost. If costs are equal, compare alphabetically by destination. equals0- Implement this just a few lines, 1 of which calls compareTo0. hashCode0 Do this as you saw in lecture, by creating an ArrayList. Add both instance variables to the ArrayList, and return the hashCode of the ArrayList These Mission methods are simple, and it's tempting to move on without testing them if they compile correctly. If you think you should test them now, write a mainO method that creates some instances of Mission, compares them, calls equals on them, and computes their hash codes. Scribe: Did you test this class or did you move on to the next part of the lab? MissionOrganizer.java This class will only ever contain Missions, so it doesn't need to be generic. The class should be iterable, so that you can iterate over all its members in correct order like this: for (Mission m: myMissionOrganizer) System.out.println(m); Notice that the class' declaration says "implements Iterable". The class will need a method iterator () that returns an iterator over the class' Missions. You saw something similar with the Roster class that we covered in lecture. UNSA has a huge budget. Any mission that costs less than 1 trillion dollars (1.0 x 10 12) is considered cheap; any mission that costs more than that is expensive. Cheap missions should always appear first, in the order they were added to the MissionOrganizer Expensive missions should always appear after all cheap missions, in increasing order by cost; if 2 expensive missions have the same cost, they should appear in alphabetical order by destination (note this is the natural sorting order of the Mission class) The MissionOrganizer class should use an appropriate collection type to store its cheap missions and a different appropriate collection type to store its expensive missions. Scribe: considering the requirements for ordering of cheap missions, what is a good collection type for storing them? Considering the requirements for ordering of expensive missions, what is a good collection type for storing them? Declare these 2 instance variables, and initialize them in the ctor Complete the 3 unfinished methods of MissionOrganizer (contains, add, and iterator) as instructed by the comments in the starter file. Below the iterator0 method is an array of test missions, in chaotic order as you would expect from a large international organization. In mainO, a TreeMissionOrganizer is constructed and populated from the test array, and then the organizer's missions are printed out in an enhanced for-loop. Scribe: Why is this possible? (Hint: Because class implements interface -). The correct output appears in a comment below main(). The costs are slightly different from the costs you see in the TEST_MISSIONS array because of float rounding. Don't worry about that. Now add 4 missions to the end of TEST_MISSIONS: 2 cheap missions and 2 expensive ones. Get creative with the destinations. If you aren't feeling creative, Google "stars near earth". Driver: paste the new TEST_MISSIONS into your lab report. Before you rerun the app: what output do you expect? Driver: put your expected output into your report. Now run the app. Scribe: does the output match what you expect? If not, was your expectation wrong or was your code wrong? If your output was wrong, fix your code. Mission.java This simple class contains the destination and cost of a space mission Complete 3 methods in this class: compareTo0 Compare by cost. If costs are equal, compare alphabetically by destination. equals0- Implement this just a few lines, 1 of which calls compareTo0. hashCode0 Do this as you saw in lecture, by creating an ArrayList. Add both instance variables to the ArrayList, and return the hashCode of the ArrayList These Mission methods are simple, and it's tempting to move on without testing them if they compile correctly. If you think you should test them now, write a mainO method that creates some instances of Mission, compares them, calls equals on them, and computes their hash codes. Scribe: Did you test this class or did you move on to the next part of the lab? MissionOrganizer.java This class will only ever contain Missions, so it doesn't need to be generic. The class should be iterable, so that you can iterate over all its members in correct order like this: for (Mission m: myMissionOrganizer) System.out.println(m); Notice that the class' declaration says "implements Iterable". The class will need a method iterator () that returns an iterator over the class' Missions. You saw something similar with the Roster class that we covered in lecture. UNSA has a huge budget. Any mission that costs less than 1 trillion dollars (1.0 x 10 12) is considered cheap; any mission that costs more than that is expensive. Cheap missions should always appear first, in the order they were added to the MissionOrganizer Expensive missions should always appear after all cheap missions, in increasing order by cost; if 2 expensive missions have the same cost, they should appear in alphabetical order by destination (note this is the natural sorting order of the Mission class) The MissionOrganizer class should use an appropriate collection type to store its cheap missions and a different appropriate collection type to store its expensive missions. Scribe: considering the requirements for ordering of cheap missions, what is a good collection type for storing them? Considering the requirements for ordering of expensive missions, what is a good collection type for storing them? Declare these 2 instance variables, and initialize them in the ctor Complete the 3 unfinished methods of MissionOrganizer (contains, add, and iterator) as instructed by the comments in the starter file. Below the iterator0 method is an array of test missions, in chaotic order as you would expect from a large international organization. In mainO, a TreeMissionOrganizer is constructed and populated from the test array, and then the organizer's missions are printed out in an enhanced for-loop. Scribe: Why is this possible? (Hint: Because class implements interface -). The correct output appears in a comment below main(). The costs are slightly different from the costs you see in the TEST_MISSIONS array because of float rounding. Don't worry about that. Now add 4 missions to the end of TEST_MISSIONS: 2 cheap missions and 2 expensive ones. Get creative with the destinations. If you aren't feeling creative, Google "stars near earth". Driver: paste the new TEST_MISSIONS into your lab report. Before you rerun the app: what output do you expect? Driver: put your expected output into your report. Now run the app. Scribe: does the output match what you expect? If not, was your expectation wrong or was your code wrong? If your output was wrong, fix your code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions