Results 1 to 3 of 3
Thread: Calling Methods between classes?
- 03-22-2010, 12:51 AM #1
Member
- 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
- 03-22-2010, 12:55 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-22-2010, 01:22 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
Sorry, I mispoke. I meant two methods within a class. The other method being:
That is the method I need to call under the two sets that get the dimensions for first pizza and second pizza. Sorry for the confusion in my rushed post.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: 04-16-2012, 11:00 PM -
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02-27-2010, 09:12 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
Calling jar classes from java executable
By SteM in forum Advanced JavaReplies: 1Last Post: 11-27-2007, 08:21 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks