Question

java code

need this part java code

There is one public constructor: public Backpacker (int initialFunds, City initialCity) Constructs a Backpacker starting out

here is test

public class BackpackerTest public static void main(String[] args) 11 a few cities to visit City paris = new City (Paris, 7t.visit(rome, 2); System.out.println(t.getCurrentMoney ()); // expected 400 When we go back to Paris for a week, its a bit d


0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Main Class - BackpackerTest

public class BackpackerTest {
   public static void main(String[] args) {
      
       City paris = new City("Paris", 75);
       City rome = new City("Rome", 50);
      
       Backpacker t = new Backpacker(500, paris);
       System.out.println(t.getCurrentCity());
       System.out.println(t.getCurrentJournal());
       System.out.println(t.getCurrentMoney());  
      
       t.visit(rome,2);
       System.out.println(t.getCurrentCity());
       System.out.println(t.getCurrentJournal());  
       System.out.println(t.getCurrentMoney());  
      
      
       t.visit(paris,7);
       System.out.println(t.getCurrentMoney());
       System.out.println(t.getNightsInStation());
      
       t.visit(paris,7);
       System.out.println(t.getCurrentMoney());
       System.out.println(t.getNightsInStation());
      
       t.sendPostCardHome(1);
       System.out.println(t.getCurrentMoney());
      
       t.sendPostCardHome(12);
       System.out.println(t.getCurrentMoney());
      
       System.out.println(t.isSol());               //Expected True.
      
       t.callHomeForMoney();
       System.out.println(t.getCurrentMoney());
   }

}

// Backpacker Class -


public class Backpacker {

   public static int current_fund;
   public static int initial_fund;
   public static int number_of_nights;
   public static int number_of_nights_wo_money_t = 0;
   public static int number_of_nights_wo_money = 0;
   public static String current_city;
   public static String current_jouranal;
   public boolean journey_started = false;
   public int current_city_cost;
   public static int paris_postcard_cost = 4;
   public static int sympathy_factor = 30;
   public static int total_cards ;

   // Main Method
   public static void main(String[] args) {

   }

   // Constructor of class which initializes the City and Funds.
   public Backpacker(int initialFunds, City initialCity) {

       // Initialize current funds to initial funds
       current_fund = initialFunds;

       // Initialize current city to Initial City.
       current_city = initialCity.city_name;

       // Initial Funds for city

       current_city_cost = initialCity.city_cost;

       // Initialize No Of Nights.
       number_of_nights = 0;
   }

   // Return current City.
   public String getCurrentCity() {
       return current_city;

   }

   // Return Current amount of money.
   public int getCurrentMoney() {
       return current_fund;
   }

   // Get Journal
   // public String[] getJournal(){

   // }

   public boolean isSol(){
       //Initialize local variable.
       boolean sol = false;
      
       if (current_city == "Paris") {
           if (paris_postcard_cost > current_fund) {
               sol = true;
           } else {
               sol = false;
           }
       }
       return sol;
   }

   // Method to return Journal.
   public String getCurrentJournal() {

       // If No of nights not initialized add CityName(Start) to Journal Entry.
       if (number_of_nights == 0) {

           current_jouranal = current_city + "(Start)";
       }

       // If No of nights initialized then add CurrentCityName and Number_of_nights to
       // Journal Entry.
       else {

           current_jouranal = current_jouranal + "," + current_city + "(" + number_of_nights + ")";
       }

       return current_jouranal;

   }

   // Assign current City and No of Nights.
   public void visit(City city, int no_of_nights) {

       current_city = city.city_name;

       number_of_nights = no_of_nights;

       current_city_cost = city.city_cost;

       if (current_city_cost * no_of_nights > current_fund) {

           int possible_nights = current_fund / current_city_cost;

           number_of_nights_wo_money = number_of_nights - possible_nights;

           current_fund = current_fund - possible_nights * city.city_cost;
       }

       else {
           current_fund = current_fund - number_of_nights * city.city_cost;
       }

   }

   // Return the number of nights the Backpacker has spent in Train wihtout money
   public int getNightsInStation() {
       number_of_nights_wo_money_t = number_of_nights_wo_money_t + number_of_nights_wo_money;
       return number_of_nights_wo_money_t;
   }

   public void sendPostCardHome(int no_of_cards) {

       if (current_city == "Paris") {
           int cost = paris_postcard_cost * no_of_cards;

           int max_cards = current_fund / paris_postcard_cost;

           if (max_cards < no_of_cards) {

               cost = paris_postcard_cost * max_cards;
              
               total_cards = total_cards + max_cards;
           }
           else
           {
               total_cards = total_cards + no_of_cards;
           }
          
           current_fund = current_fund - cost;
       }
   }
  
   public void callHomeForMoney() {
      
       //Money from home is n time ( no of cards) * sypmathy factor.
       int home_money = total_cards * sympathy_factor;
      
       current_fund = current_fund + home_money;
   }

}

// Class City  

public class City {
  
   public String city_name;
   public int city_cost;
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
   }
  
   public City( String s, int f) {
       city_name = s;
       city_cost = f;
   }
}

******************************************************************************************************************************************

Code Screenshots

// Code in Left Panel , Output in Right Panel

Hello.java D Factorial.java ob PrintStream.class Test.java Backpacker Test.java X Backpacker.java o e Console x 2 Problems D

Add a comment
Know the answer?
Add Answer to:
java code need this part java code here is test There is one public constructor: public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Java] Please test your code in the link I provide before you post your answer. The...

    [Java] Please test your code in the link I provide before you post your answer. The output should be looked like exact same as the tester. http://www.codecheck.it/files/17033122188mcxvjz8n8qbk0k9fyfrd3w95 Use the following file: LinkedListUtilTester.java import java.util.LinkedList; public class LinkedListUtilTester { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); list.add("7"); list.add("8"); list.add("9"); list.add("10"); list.add("11"); list.add("12"); list.add("13"); list.add("14"); list.add("15"); LinkedListUtil.shrink(list, 3); System.out.println(list); System.out.println("Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]"); System.out.println(LinkedListUtil.reverse(list)); System.out.println("Expected:...

  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • in JAVA -- need to use an arraystack for this and can only manipulate smartstring.java aka...

    in JAVA -- need to use an arraystack for this and can only manipulate smartstring.java aka the one that says :               public class SmartString implements SmartStringADT at the beginning //for SmartStringTest.java: import static org.junit.Assert.*; import org.junit.Test; public class SmartStringTest {    //Test insert method    @Test    public void testinsert1() {        SmartString evaluator = new SmartString();        evaluator.insert(0, "Hello");        evaluator.insert(4, ", how are you?");        assertEquals("Hello, how are you?", evaluator.toString());    }   ...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT