Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

java code need this part java code here is test There is one public constructor: public Backpacker (int initialFunds, City initialCity) Constructs a Backpacker starting

java code

need this part java code

image text in transcribed

here is test

image text in transcribedimage text in transcribed

There is one public constructor: public Backpacker (int initialFunds, City initialCity) Constructs a Backpacker starting out with the given amount of money in the given city. The journal is initially a string consisting of "cityname(start)", where cityname is the name of the starting city. There are the following public methods: public String getCurrentCity ( ) Returns the name of the Backpacker's current city. public int getCurrentMoney () Returns the amount of money the Backpacker currently has. public String getJournal() Returns the Backpacker's journal. The journal is a string of comma-separated values of the form cityname(number_of_nights) containing the cities visited by the Backpacker, in the order visited, along with the number of nights spent in each. The first value always has the form cityname(start) for the starting city. For example, if a Backpacker starts in Paris, spends 5 nights in Rome, and then spends 2 nights in Prague, the journal string would be: Paris (start), Rome (5), Prague (2). public boolean is SOL() Returns true if Backpacker doesn't have enough money to send postcard from the current city. public int getNightsInStation () Returns the number of nights the Backpacker has spent in a train station when visiting a city without enough money for hostels. public class BackpackerTest public static void main(String[] args) 11 a few cities to visit City paris = new City ("Paris", 75); City rome = new City ("Rome", 50); // start out in Paris Backpacker t = new Backpacker (500, paris); // initial state System.out.println(t.getCurrentCity()); // expected "Paris" System.out.println(t.getJournal()); 1/ expected "Paris (start)" // try going to Rome t.visit(rome, 2); System.out.println(t.getCurrentCity () ); // expected "Rome" System.out.println(t.getJournal ()); 11 expected "Paris (start), Rome (2)" // back to Paris for a week t.visit (paris, 7); System.out.println(t.getCurrentCity()); // expected "Paris" System.out.println(t.getJournal()); // "Paris (start), Rome (2), Paris (7)" Next, what happens to the funds if we follow the same itinerary as above? For Rome, we can determine (from the City object) that we have enough money for 10 nights. We're staying 2 nights, which is less than 10, so we buy lodging for 2 nights. That costs 2 * 50, or 100 euros, so we expect to have 400 left. t = new Backpacker (500, paris); // start over // initial state System.out.println(t.getCurrentMoney ()); // expected 500 // visit a city t.visit(rome, 2); System.out.println(t.getCurrentMoney ()); // expected 400 When we go back to Paris for a week, it's a bit different. We determine (from the City object) that 400 euros is enough for only 5 nights. The desired number is 7 nights, and the smaller of these values is 5. So we buy lodging for only 5 nights, at a cost of 5 * 75 = 375 euros. We should have 25 euros left. This also implies that we slept in the train station for 7 - 5 = 2 nights. t.visit(paris, 7); System.out.println(t.getCurrentMoney ()); // expected 25 System.out.println(t.getNightsInStation()); // expected 2 Paris is a lot of fun, so we stay another week. This time we discover we have only enough money for zero nights lodging. The smaller of zero and 7 is zero, so we purchase zero nights lodging at a cost of zero times 75. So we still have 25 euros, but we've slept another 7-0 = 7 nights in the train station. t.visit(paris, 7); System.out.println(t.getCurrentMoney () ); // expected 25 System.out.println(t.getNightsInStation () ); // expected 9 Ok, we're tired, but are we SOL? The cost of a postcard from Paris is 4 euros. Is that more than we have left? The expression 4 > 25 is false, so we are not yet SOL. Let's send a postcard home! It will cost us just 4 euros. t.sendPostcardsHome (1); System.out.println(t.getCurrentMoney ()); // expected 21 Before we call home and ask for more money, let's really get on Mom and Dad's good side. Try sending 12 postcards! With 21 euros, the maximum number of postcards we can send is 5. The smaller of 5 and 12 is 5, so that's the number we actually send. We should see our funds go down by 5 times the postcard cost, or 20 euros, leaving us one euro. t.sendPostcardsHome (12); System.out.println(t.getCurrent Funds()); // expected 1 At this point we're technically SOL: we have 1 euro, the cost of sending a postcard is 4 euros, and the expression 4> 1 is true. System.out.println(t.isSOL()); // expected true But, we've sent a total of 6 postcards home. When we call and ask for money, our funds should go up by 6 times SYMPATHY_FACTOR, 6 * 30 = 180, leaving us sitting pretty with 181 euros. t.callHomeForMoney (); System.out.println(t.getCurrentMoney ()); // expected 181

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions