0

I need help understanding how i can link variables from a class to my main class, it seems to work during certain instances of code I've used before but I'm not clear on how it works. in this case, the Dollar variable needs to pull in an integer from my main method in the tester class that comes from a scanner input. Also how do I implement my while loop and have it call other methods so that it prints the desired variable. the point of this program is too find out the max bars you can get with a fixed amount of money, assuming each is 1 dollar, each bar has one coupon, and 6 coupons get you a free bar(with the 1 coupon inside). Im a beginner and I cant figure out where to go next.

 public class VendingMachine 
        {
            public static int dollars;
            public static int coupons = dollars; 
            public static int bars = dollars; 

    public VendingMachine (int x) 
                    {
                    dollars = x;

                    }   
    public VendingMachine()
                    {
                    this (0); 
                    }

    public static void Bars() 
        {   
            int newbars;
            newbars = coupons/6;
            bars = bars + newbars;

        }

    public static void Coupons()
        {
            int newcoupons;
            newcoupons = (coupons % 6) +(coupons/6);
            coupons = newcoupons + coupons;
        }


    }

    import java.util.*;



    public class VendMachineTest{ 


    public static void main(String[]args, int coupons)

    {
        Scanner user_input = new Scanner(System.in);

        int dollars;
        System.out.println("Amount of Cash?");
        dollars = user_input.nextInt();

        VendMachineTest totalbars = new VendMachineTest();
        while ( coupons >= 6)
        {       
            VendMachineTest.Bars();
            VendMachineTest.Coupons();
        }
    }

    System.out.println( dollars +""+coupons+""+bars+"");
    }   

2 Answers 2

1

The code posted has many syntax errors. To correct these errors you should consider the following:

  • Although not an error, The method names should start with a lowercase character (as per good programming practice)
  • You are declaring two public classes in a class, in which it is giving an error (That assumes that you are using the two classes in one file)
  • You are declaring the variables (coupons and bars) as a unassigned variable (using public int coupons, bars; will suffice).
  • Your main method is taking a int (coupons) which will not register with the JVM as a main method so it will not allow you to run the program.
  • The program creates a new VendMachineTest Object which is not used during the application.

  • In your VendingMachine class you are dividing an int by an int which will produce a double (possible loss of precision)

You want to use inner classes to display the values.

public class VendMachineTest{

    public static void main(String[] args){
            //insert your test method here
    }

    private class VendingMachine{
        public double coupons, dollars, bars; // declare the variable
        // values declared here can be used in the VendMachineTest
    }

}

Also, it would be better if you didn't declare the methods in the VendingMachine class as static. If you create a new VendingMachine object

VendingMachine vendingMachine = new VendingMachine(dollars);

You can use vendingMachine.getBars() rather than using the class name.

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what your program is supposed to do but....

  1. You shouldn't be using "static", this is really used for constant variables.

    public static int dollars;
    public static int coupons = dollars; 
    public static int bars = dollars; 
    
  2. This constructor has no function in your program

    public VendingMachine()
    {
       this (0); 
    }
    
  3. "%" is the modulo operator this is used to get the remainder...I don't think thats what your are wanting to do here.

    newcoupons = (coupons % 6) +(coupons/6);
    
  4. Here you want to create an instance of "VendingMachine" not the tester class.

    VendMachineTest totalbars = new VendMachineTest();
    

Perhaps you were trying to achieve something along the lines of this?

public class VendingMachine 
{
public int dollars;
public int coupons;  
public int mybars = 0;

public VendingMachine (int d,int c) 
{
    dollars = d;
    coupons = c;
}   

public void buy_One_Bar() 
{   

    if(coupons >5) //I get a free bar?
    {
        mybars++; // mybars = mybars + 1;
        coupons = coupons - 6;

    }else{
        dollars--; //Decrease amount of dollars by 1
        mybars++; //You gain a bar
        coupons++; //You gain a coupon

    }

}

public int getBars()
{
    return mybars;
}

public int getDollars()
{
    return dollars;
}

public int getCoupons()
{
    return coupons;
}

    }

import java.util.Scanner;

public class VendMachineTest{ 

public static void main(String[]args)

{
   Scanner user_input = new Scanner(System.in);

   int starting_dollars;
   int starting_coupons;

   System.out.println("Amount of Cash?");
   starting_dollars = user_input.nextInt();
   System.out.println("How many coupons do you have?");
   starting_coupons = user_input.nextInt();

VendingMachine vm = new VendingMachine(starting_dollars, starting_coupons);

vm.buy_One_Bar(); //Buy some bars

//Print out what you have...
System.out.println("Your Dollars :"+ vm.getDollars());
System.out.println("Amount of Coupons:"+ vm.getCoupons());
System.out.println("Amount of Bars you have:"+ vm.getBars());
}


}   

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.