Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

--java code-- here is test public class BackpackerTest public static void main(String[] args) // a few cities to visit City paris = new City (Paris,

--java code--

here is test

image text in transcribedimage text in transcribedimage text in transcribed

public class BackpackerTest public static void main(String[] args) // 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()); 11 expected "Paris (start)" 1/ try going to Rome t.visit(rome, 2); System.out.println(t.getCurrentCity()); // expected "Rome" System.out.println(t.getJournal()); // 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(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.sendPostcards Home (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.sendPostcards Home (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 If we ask for money again now, nothing should happen - Mom and Dad will have no more sympathy for us until we send more postcards. t.callHomeForMoney(); System.out.println(t.getCurrentMoney () ); // still just 181 6. You can start with the Backpacker class as long as you at least have the getCityName() and hostelCost() methods working (though you won't be able to implement everything until you have the remaining methods correct as well). Remember, you don't have to implement it all at once. Implement one feature at a time, and test as you go. 7. In the first step of the sample test code we called the method getCurrentCity(), which is supposed to return the name of the city that the Backpacker is currently in. In order to return the correct value, you'll have to keep track of the Backpacker's current city, suggesting you need an instance variable to store the current city whenever visit() is called. How should it be initialized? We then called the method getJournal() that returns a string with names of all the cities visited. So we need a String variable for the journal that we can update each time we visit a new city. How is it supposed to be initialized? Once you have these two variables correctly initialized, you can implement the two accessors easily and write the statements in visit() that will keep them updated. After you get this much done, run your simple test and make sure you get the values you expect. 8. Next it makes sense to look at money. We have an accessor, getCurrentMoney (), suggesting that we need an instance variable to keep track of the funds. Be sure to initialize it in the constructor. For updating the funds in visit(), work carefully through the examples in the previous section. In particular, look what happened when we had 400 euros and visited Paris for a week: maximum number of nights we can afford with 400 euros = 5 number of nights we're staying = 7 number of hostel nights we actually purchase = the smaller of these two numbers Tip: To implement the logic of the third step, you can use the method Math.min, which returns the smaller of two given numbers: int x = Math.min(3, 4); // now x is 3 9. There is also an accessor getNightsInStation(). There doesn't seem to be any way to calculate this value from the instance variables we already have (city, journal, funds) so we probably need another one. Add the code to update it in visit(). 10. The remaining two methods are slightly more subtle. Notice that for callHomeForMoney () to do the right thing, it has to know how many postcards had been sent home. Can this be calculated from information we already have? No - we need another instance variable to count postcards. This variable is used (and then zeroed) in callHome ForMoney (), but it has to be updated in sendPostcards Home(). The logic can be implemented with Math.min (as in step 8). 11 Download the anchoolror imort it into our roint o u did in lobo 1 ond and run it If thoro public class BackpackerTest public static void main(String[] args) // 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()); 11 expected "Paris (start)" 1/ try going to Rome t.visit(rome, 2); System.out.println(t.getCurrentCity()); // expected "Rome" System.out.println(t.getJournal()); // 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(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.sendPostcards Home (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.sendPostcards Home (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 If we ask for money again now, nothing should happen - Mom and Dad will have no more sympathy for us until we send more postcards. t.callHomeForMoney(); System.out.println(t.getCurrentMoney () ); // still just 181 6. You can start with the Backpacker class as long as you at least have the getCityName() and hostelCost() methods working (though you won't be able to implement everything until you have the remaining methods correct as well). Remember, you don't have to implement it all at once. Implement one feature at a time, and test as you go. 7. In the first step of the sample test code we called the method getCurrentCity(), which is supposed to return the name of the city that the Backpacker is currently in. In order to return the correct value, you'll have to keep track of the Backpacker's current city, suggesting you need an instance variable to store the current city whenever visit() is called. How should it be initialized? We then called the method getJournal() that returns a string with names of all the cities visited. So we need a String variable for the journal that we can update each time we visit a new city. How is it supposed to be initialized? Once you have these two variables correctly initialized, you can implement the two accessors easily and write the statements in visit() that will keep them updated. After you get this much done, run your simple test and make sure you get the values you expect. 8. Next it makes sense to look at money. We have an accessor, getCurrentMoney (), suggesting that we need an instance variable to keep track of the funds. Be sure to initialize it in the constructor. For updating the funds in visit(), work carefully through the examples in the previous section. In particular, look what happened when we had 400 euros and visited Paris for a week: maximum number of nights we can afford with 400 euros = 5 number of nights we're staying = 7 number of hostel nights we actually purchase = the smaller of these two numbers Tip: To implement the logic of the third step, you can use the method Math.min, which returns the smaller of two given numbers: int x = Math.min(3, 4); // now x is 3 9. There is also an accessor getNightsInStation(). There doesn't seem to be any way to calculate this value from the instance variables we already have (city, journal, funds) so we probably need another one. Add the code to update it in visit(). 10. The remaining two methods are slightly more subtle. Notice that for callHomeForMoney () to do the right thing, it has to know how many postcards had been sent home. Can this be calculated from information we already have? No - we need another instance variable to count postcards. This variable is used (and then zeroed) in callHome ForMoney (), but it has to be updated in sendPostcards Home(). The logic can be implemented with Math.min (as in step 8). 11 Download the anchoolror imort it into our roint o u did in lobo 1 ond and run it If thoro

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions