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: 3887414 • Letter: I

Question

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

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(numNights) * currentCity.lodgingCost());

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

}

/**

*

* @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

I have done correction which are bold below

---------------------------------------------------------------------------------

public class Test {

public static void main(String[] srgs){

Traveler trvlr = new Traveler(1000000, new City("Damoh", 10));

System.out.println(trvlr.getJournal());

System.out.println(trvlr.getCurrentFunds());

trvlr.visit(new City("Indore", 15), 2);

System.out.println(trvlr.getJournal());

System.out.println(trvlr.getCurrentFunds());

trvlr.visit(new City("Sagar", 12), 5);

System.out.println(trvlr.getJournal());

System.out.println(trvlr.getCurrentFunds());

}

}

----------------------------------------------------------------------------------------------------------------------

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) {

currentCity = 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(funds) * currentCity.lodgingCost());

nightsAtStation = nightsAtStation + (numNights - (currentCity.maxLengthOfStay(funds)));

}

/**

*

* @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));

}

}

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;

}

}

-----------------------------------------------------------------------------------------------

OUTPUT

Damoh (Start)
0
Damoh (Start) ,Indore(2)
2
Damoh (Start) ,Indore(2),Sagar(5)
7