I am a beginner JAVA coder, and yes this is a homework assignment for a online class... I am stumped, and cannot get help fast enough when it is 11pm at night :<)
To better explain, I need to be able to have four methods. My first method contains all my variables, so my problem/question is how to call upon all of the variables in my first method so as to use the contents of it in the other three methods created. As of now, each method is independent of each other, so I cannot: Use my Calculations method which requires the variables from my Variables Method!
import javax.swing.JOptionPane;
public class Ch2Lab {
static final double NUM_COMMISSION = 0.02;
// VARIABLES SECTION
public static void Variables()
{
double numStocksBuy; //Number of stocks purchased
double numStocksSell; //Number of stocks sold
double numCostStockBuy; //Price of each stock when purchased
double numCostStockSell; //Price of each stock when sold
double numSumStockBuy; //Total dollar sum of stock purchased
double numSumStockSold; //Total dollar sum of stock sold
double numComPaidBuy; //Total amount paid in commission when purchased
double numComPaidSold; //Total amount paid in commission when sold
double numSumStockComBuy; //Total sum of stock plus commission when purchased
double numSumStockComSell; //Total sum of stock plus commission when sold
double numProfitLoss; //Value for either profit or loss after buy and sell
}
//Section for calculations
public static void Calculations()
{
numSumStockBuy = numSumStocksBuy * numCostStockBuy;
numSumStockSold = numSumStocksSell * numCostStockSold;
numComPaidBuy = ((numStocksBuy * numCostStockBuy) * NUM_COMMISSION);
numComPaidSold = ((numStocksSold * numCostStockSell) * NUM_COMMISSION);
numSumStockComBuy = (numComPaidBuy + numSumStockBuy);
numSumStockComSell = (numComPaidSold + numSumStockSold);
numProfitLoss = (numSumStockComBuy - numSumStockComSell);
}
//Section that gets the input from the user using JOptionPane
public static Input()
{
numStocksBuy = JOptionPane.showInputDialog("Enter the number of" +
"stocks being purchased: ");
numCostStockBuy = JOptionPane.showInputDialog("Enter the buy price of" +
"each stock: ");
numStocksSell = JOptionPane.showInputDialog("Enter the number of" +
"stocks being sold: ");
numCostStockSell = JOptionPane.showInputDialog("Enter the sell price of" +
"each stock: ");
}
public static void Output()
{
System.out.println("The number of shares of stock purchased: " + numStocksBuy);
System.out.println("The cost of each share purchased: " + numCostStockBuy);
System.out.println("Total amount of money paid for the stock: " + numSumStockBuy);
System.out.println("Amount of commission paid on purchase: " + numComPaidBuy);
System.out.println("Total price paid including commission: " + numSumStockComBuy);
System.out.println("The number of shares of stock sold: " + numStocksSell);
System.out.println("The cost of each share sold: " + numCostStockSell);
System.out.println("Total amount of money received for the stock: " + numSumStockSold);
System.out.println("Amount of commission paid on sale: " + numComPaidSold);
System.out.println("Total price received including commission: " + numSumStockComSell);
}
public static void main(String[] args)
{
Variables();
Calculations();
Input();
Output();
}
}