Results 1 to 3 of 3
Thread: Calling Methods between classes?
 03222010, 01:51 AM #1Member
 Join Date
 Mar 2010
 Posts
 2
 Rep Power
 0
Calling Methods between classes?
Hi,
So I have this program that has been written and I need to call the method from the second class in the first class. I get an error about parse, but don't know any other way to do it. The problem areas are in getting length and width, thus far. It will ask for the length of the first pizza, but then will give an error. I've worked and worked on this all weekend and it's due tomorrow afternoon...ANY help would be greatly appreciated. Thank you.
Java Code:package edu.uwec.cs.jimeneam.lab5; import javax.swing.JOptionPane; public class Lab5Start { public static void main (String [] args) { //  general variables String inputString = ""; // input string for variables String outputString = ""; // output string for message dialog char continueChar = ' '; // character to hold 'Y' to continue or 'N' to stop //  variables for first pizza double firstLength = 0.0; // length of first pizza if rectangular double firstWidth = 0.0; // width of first pizza if rectangular double firstArea = 0.0; // area of first pizza double firstCost = 0.0; // cost of first pizza double firstCostPerArea = 0.0; // cost per unit area of first pizza //  variables for second pizza double secondLength = 0.0; // length of second pizza if rectangular double secondWidth = 0.0; // width of second pizza if rectangular double secondArea = 0.0; // area of second pizza double secondCost = 0.0; // cost of second pizza double secondCostPerArea = 0.0; // cost per unit area of second pizza // initialize to go through loop first time continueChar = 'Y'; //  main processing loop while (continueChar != 'N') { // get the length and width of the first pizza firstLength = inputDimension("length", 1); //inputString = JOptionPane.showInputDialog(null, "Enter the length of the first pizza in inches"); firstLength = Double.parseDouble(inputString); firstWidth = inputDimension("width,", 1); //inputString = JOptionPane.showInputDialog(null, "Enter the width of the first pizza in inches"); //firstWidth = Double.parseDouble(inputString); // get the cost of the first pizza inputString = JOptionPane.showInputDialog(null, "Enter the cost of the first pizza"); firstCost = Double.parseDouble(inputString); // get the dimension(s) of the second pizza secondLength = inputDimension("Length", 2); secondLength = Double.parseDouble(inputString); secondWidth = inputDimension("Width", 2); secondWidth = Double.parseDouble(inputString); //inputString = JOptionPane.showInputDialog(null, "Enter the length of the second pizza in inches"); //secondLength = Double.parseDouble(inputString); //inputString = JOptionPane.showInputDialog(null, "Enter the width of the second pizza in inches"); //secondWidth = Double.parseDouble(inputString); // get the cost of second pizza inputString = JOptionPane.showInputDialog(null, "Enter the cost of the second pizza"); secondCost = Double.parseDouble(inputString); // calculate the area of the first pizza, and format that area firstArea = firstLength * firstWidth; firstArea = ((int)(firstArea * 100)) / 100.0; // calculate the area of the second pizza, and format that area secondArea = secondLength * secondWidth; secondArea = ((int)(secondArea * 100)) / 100.0; // calculate the cost per area of the first pizza, and format that cost per area firstCostPerArea = firstCost / firstArea; firstCostPerArea = ((int)(firstCostPerArea * 10000)) / 10000.0; // calculate the cost per area of the second pizza, and format that cost per area secondCostPerArea = secondCost / secondArea; secondCostPerArea = ((int)(secondCostPerArea * 10000)) / 10000.0; // initialize the output string outputString = ""; // append the information for the first pizza to the output string outputString += "\n First pizza: "; outputString += " Rectangle with length " + firstLength + " inches, width " + firstWidth + " inches"; outputString += ", Area of " + firstArea + " sq. inches, Cost of $" + firstCost + ", Cost per square inch of $" + firstCostPerArea; // append the information for the second pizza to the output string outputString += "\n Second pizza: "; outputString += " Rectangle with length " + secondLength + " inches, width " + secondWidth + " inches"; outputString += ", Area of " + secondArea + " sq. inches, Cost of $" + secondCost + ", Cost per square inch of $" + secondCostPerArea; // generate and append a comparison string to the output string if (secondCostPerArea < firstCostPerArea) { outputString += "\n Pizza 2 is a better value"; } else if (firstCostPerArea < secondCostPerArea) { outputString += "\n Pizza 1 is a better value"; } else { outputString += "\n Pizza 1 and Pizza 2 are of equal value"; } // output the output string to a message window JOptionPane.showMessageDialog(null, outputString); // ask the user if they want to continue inputString = JOptionPane.showInputDialog(null, "Do you want to continue? Enter Y to continue, N to quit"); continueChar = inputString.charAt(0); while (continueChar != 'Y' && continueChar != 'N') { inputString = JOptionPane.showInputDialog(null, "Do you want to continue? Enter Y to continue, N to quit"); continueChar = inputString.charAt(0); } } // end  while loop } // end  main method // other methods go here // method to input a dimension in general public static double inputDimension(String dimensionName, int pizzaNumber) { // declare local variables for an input string and the dimension as a general value String inputString = ""; double dimension = 0.0; // display an input box to get the "dimensionName" dimension for pizza "pizzaNumber" inputString = JOptionPane.showInputDialog(null,"Enter the " + dimensionName + " of pizza " + pizzaNumber + ":"); // parse that input string into the dimension variable dimension = Double.parseDouble(inputString); // return the dimension return dimension; } // method to calculate area of pizza } // end  class Lab5
 03222010, 01:55 AM #2Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 8
I only see one class here and no indication of what method you want to call from what other class or where or why. A clue please?
Gary
 03222010, 02:22 AM #3Member
 Join Date
 Mar 2010
 Posts
 2
 Rep Power
 0
Sorry, I mispoke. I meant two methods within a class. The other method being:
Java Code:public static double inputDimension(String dimensionName, int pizzaNumber)
Similar Threads

Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04162012, 11:00 PM 
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02272010, 10:12 PM 
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12172007, 07:22 AM 
Calling jar classes from java executable
By SteM in forum Advanced JavaReplies: 1Last Post: 11272007, 09:21 AM 
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11152007, 10:53 AM
Bookmarks