1

I'm trying to create a new object using a user input. I tried assigning the user input to variables but don't know how to add the variables to the new object in the when I declare the new object. This is just the part of my code that i need help with. part i need help with is line 8. i know i can just put something random and when i use my set methods it will overwrite but that's not what I want. thank you in advance

   Scanner input = new Scanner(System.in);
   System.out.println("Enter the name of the Soda: ");
   String soda = input.nextLine();
   System.out.println("Inventory count?: ");
   int product = input.nextInt();
   System.out.println("Cost of Soda?: ");
   double cost = input.nextDouble();
   SodaMachine one = new SodaMachine(String soda, int product, double cost);
   one.setCostOfBottle(cost);
   one.setInventory(product);
   one.setNameOfSoda(soda);

1 Answer 1

1

It depends in how is SodaMachine class implemented. If the constructor of SodaMachine expects String, int, double parameters, call the constructor passing them without specifying the type. Note that it's not necessary to set (using setCostOfBottle(), ...) the members again, because that's why you are passing the variables to the constructor.

SodaMachine one = new SodaMachine(soda, product, cost);

If your constructor doesn't accept parameters:

SodaMachine one = new SodaMachine();

and set the members later:

one.setCostOfBottle(cost);
one.setInventory(product);
one.setNameOfSoda(soda);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.