Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am not sure why my Java code is not returning the right funds or the city name

ID: 3887369 • Letter: I

Question

I am not sure why my Java code is not returning the right funds or the city name.

Here is the code:

Traveler Class

package hw1;

/**

*

* @author morgansmith

*

*/

/**

* Model of a traveler for use with city class

*/

public class Traveler {

/**

* Sympathy factor per postcard sent home

*/

public static final int SYMPATHY_FACTOR = 30;

/**

* The balance of funds available

*/

private int funds;

/**

* Nights spent at the train station

*/

private int nightsAtStation = 0;

/**

* Number of postcards sent home

*/

private int postcards;

/**

* Number of nights in a city

*/

private int numberNights;

/**

* Name of the city

*/

private String name;

/**

* Current city the traveler is in.

*/

private City currentCity = new City(name, funds);

/**

* String of a journal of where the traveler has gone.

*/

private String journal;

/**

*

* @param initialFunds

* @param initialCity

*/

public Traveler(int initialFunds, City initialCity) {

funds = initialFunds;

name = currentCity.getName();

journal = name + " (Start), ";

}

/**

* Returns City name

* @return

* city name

*/

public String getCurrentCity() {

return name;

}

/**

* Balance of funds

* @return

* funds

*/

public int getCurrentFunds() {

return funds;

}

/**

* Returns journal string

* @return

* journal

*/

public String getJournal() {

return journal;

}

/**

* Returns true if they cannot afford to send a postcard

* @return

* true if not enough funds

*/

public boolean isSOL() {

return (funds > currentCity.costToSendPostcard());

}

/**

* Returns the number of nights at the train station

* @return

* nights at station

*/

public int getTotalNightsInTrainStation() {

return nightsAtStation;

}

/**

*

* @param c

* @param numNights

*/

public void visit(City c, int numNights) {

currentCity = c;

numberNights = numNights;

journal = journal + currentCity.getName() + " (" + numberNights + "),";

funds = funds - (currentCity.maxLengthOfStay(numberNights) * currentCity.lodgingCost());

nightsAtStation = nightsAtStation + (numberNights - (currentCity.maxLengthOfStay(numberNights)));

}

/**

*

* @param howMany

*/

public void sendPostcardsHome(int howMany) {

funds = (funds - (howMany * currentCity.maxNumberOfPostcards(howMany)));

postcards = postcards + howMany;

}

/**

* Increases funds in relation to number of post cards.

*/

public void callHomeForMoney() {

funds = (funds + (postcards * SYMPATHY_FACTOR));

}

City class:

package hw1;

/**

*

* @author morgansmith

*

*/

/**

*

* Model of a city for use with traveler class

*

*/

public class City {

/**

* Cost to send a postcard from this city

*/

public static final double RELATIVE_COST_OF_POSTCARD = 0.05;

/**

* Name of the city

*/

private String name;

/**

* Cost of lodging

*/

private int lodgingCost;

/**

*

* @param givenName

* @param givenLodgingCost

*/

public City(String givenName, int givenLodgingCost) {

name = givenName;

lodgingCost = givenLodgingCost;

}

/**

* Returns the name of the city

* @return

* city name

*/

public String getName() {

return name;

}

/**

* Returns the cost of lodging

* @return

* lodging cost

*/

public int lodgingCost() {

return lodgingCost;

}

/**

* Returns the cost to send a postcard

* @return

* cost to send a postcard

*/

public int costToSendPostcard() {

double costToSend = (lodgingCost * RELATIVE_COST_OF_POSTCARD);

return ((int) Math.round(costToSend));

}

/**

* Returns the max length of stay based on funds

* @param funds

* @return

* max length of stay

*/

public int maxLengthOfStay(int funds) {

return (funds / lodgingCost);

}

/**

* Returns the max number of postcards based on funds

* @param funds

* @return

* max number of postcards

*/

public int maxNumberOfPostcards(int funds) {

double maxNumber = (funds / Math.ceil(lodgingCost * RELATIVE_COST_OF_POSTCARD));

return (int) maxNumber;

}

}

}

Explanation / Answer

//Modified Traveler class.Modifications are in bold letters
public class Traveler {
   /**
   * Sympathy factor per postcard sent home
   */
   public static final int SYMPATHY_FACTOR = 30;
   /**
   * The balance of funds available
   */
   private int funds;
   /**
   * Nights spent at the train station
   */
   private int nightsAtStation;
   /**
   * Number of postcards sent home
   */
   private int postcards;
   /**
   * Number of nights in a city
   */
   private int numberNights;
   /**
   * Name of the city
   */
   private String name;
   /**
   * Current city the traveler is in.
   */
   //Note: Declaration of City variable
   //Not to intialize the currentCity object
   //without arguments
   private City currentCity ;
   /**
   * String of a journal of where the traveler has gone.
   */
   private String journal;
   /**
   *
   * @param initialFunds
   * @param initialCity
   */
   public Traveler(int initialFunds, City initialCity) {
       //assign the initialFunds to funds
       funds = initialFunds;
       //set initialCity to the currentCity

       currentCity = initialCity;
       name = currentCity.getName();
       journal = name + " (Start), ";
   }
   /**
   * Returns City name
   * @return
   * city name
   */
   public String getCurrentCity() {
       return name;
   }
   /**
   * Balance of funds
   * @return
   * funds
   */
   public int getCurrentFunds() {
       return funds;
   }
   /**
   * Returns journal string
   * @return
   * journal
   */
   public String getJournal() {
       return journal;
   }
   /**
   * Returns true if they cannot afford to send a postcard
   * @return
   * true if not enough funds
   */
   public boolean isSOL() {
       return (funds > currentCity.costToSendPostcard());
   }
   /**
   * Returns the number of nights at the train station
   * @return
   * nights at station
   */
   public int getTotalNightsInTrainStation() {
       return nightsAtStation;
   }

   /**
   *
   * @param c
   * @param numNights
   */
   public void visit(City c, int numNights) {
       currentCity = c;
       numberNights = numNights;
       journal = journal + currentCity.getName() + " (" + numberNights + "),";
       funds = funds - (currentCity.maxLengthOfStay(numberNights) * currentCity.lodgingCost());
       nightsAtStation = nightsAtStation + (numberNights - (currentCity.maxLengthOfStay(numberNights)));
   }
   /**
   *
   * @param howMany
   */
   public void sendPostcardsHome(int howMany) {
       funds = (funds - (howMany * currentCity.maxNumberOfPostcards(howMany)));
       postcards = postcards + howMany;
   }

   /**
   * Increases funds in relation to number of post cards.
   */
   public void callHomeForMoney() {
       funds = (funds + (postcards * SYMPATHY_FACTOR));
   }
}

Note:Check the code with your main method class since the main method class not available.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote