Results 1 to 6 of 6
Thread: Help: Methods and Arguments
- 12-11-2010, 10:23 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Help: Methods and Arguments
Hi I'm writing a program about Kitchen Sales.
In this program, it will have to work out the total amount and the current unit price, the kind of fittings chosen from Basic, Standard and Deluxe and whether the units are to be installed or not.
There will a method that works out the basic price for the number of cupboard units the user input. This method will then be passes the number of of units and the unit price and return the result of multiplying them together.
The answer is passes back to the main method and it will print out this basic price.
The result of the calculation should then be passes by the main method to a second method that works out the total cost taking into account the kind of cupboard installed. The cost is unchanged for basic units, is multiplied by 2 for standard units and multiplied by 3 for deluxe units.
if the kitchen is to be installed(1000 added) otherwise nothing will be added to the cost
This is what I have done
Basically the main method should ask for the base price, number of units and pass them to the first method to work out the basic cost. This basic cost will be returned to main which prints it out. The main should then asks for the kind of fitting and whether it is to be installed or not and passes the information along with basic cost to the second method. It then returns the final overall estimate to the main.Java Code:public static void main(String[] param) { fittedkitchen(); System.exit(0); } public static void fittedkitchen() { String cupboardunits = getText("Please type in the number of cupboard units."); int numofunits = Integer.parseInt(cupboardunits); String pricePerUnits = getText("Please type in the individual unit price."); int unitPrice = Integer.parseInt(pricePerUnits); JOptionPane.showMessageDialog(null,"The basic price is " + calculatePrice(numofunits,unitPrice)); String fittings = getText("Type in the fittings according to the number given:\n\n1.Basic\n2.Standard\n3.Deluxe"); int fitted = JOptionPane.showConfirmDialog(null, "Is the kitchen to be installed ?","", JOptionPane.YES_NO_OPTION); int cost =0; if(fittings.equalsIgnoreCase("B")){ cost =1; }else if(fittings.equalsIgnoreCase("S")){ cost =2; }else if(fittings.equalsIgnoreCase("D")){ cost =3; } //if ((number-1) <= types.length) //{ int price = cost*calculatePrice(numofunits,unitPrice); if(fitted == 0) { price = price + 1000; } else { } JOptionPane.showMessageDialog(null, price + " pounds"); } public static int calculatePrice(int unit, int price){ return unit * price; } public static String getText(String msg) { String txt = null; while (txt == null || txt.trim().equals("")) { txt = JOptionPane.showInputDialog(msg); } return txt; }
But the I only have one extra method to work the base price. How should I do the rest?
Any help would be greatly appreciated.
- 12-11-2010, 11:22 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
So this is the algorithm in pseudocode, right?
See if you can write that in Java, and let us know what problems you have.Java Code:basePrice = quantity * unitPrice if fittings = Basic price = basePrice else if fittings = Standard price = basePrice * 2 else if fittings = Deluxe price = basePrice * 3 else bad value for fittings if installed price += 1000
-Gary-
- 12-11-2010, 11:30 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
what do you meant by that? I have written that operation in the program. what I need help is how to pass to another new methods and not compact everything in main method.
- 12-11-2010, 11:30 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Sorry, I should have read your post more carefully before replying. Your problem is not with the algorithm, but with source code structure and code flow. Once again, in pseudocode:
Hope that helps.Java Code:main method: get number of units get unit price call computeBasePrice method print out base price get fitting type get installation (Yes/No) call computeFinalPrice method print out final price computeBasePrice method: return numberOfUnits * unitPrice computeFinalPrice method // pretty much the pseudocode from my other post
-Gary-
- 12-11-2010, 11:38 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Some method declarations to get you started:
Java Code:public int computeBasePrice(int quantity, int unitPrice) { // TODO write this part }Since we don't really want to make our methods static, consider something like this in main():Java Code:public int computeFinalPrice(int basePrice, String fittings, boolean installed) { // TODO write this part }
Hope that helps.Java Code:public static void main(String[] args) { Kitchen kitchen = new Kitchen(); // TODO code to get quantity and unit price int basePrice = kitchen.computeBasePrice(quantity, unitPrice); // TODO code to print base price // TODO code to get fittings and install option int finalPrice = kitchen.computeFinalPrice(basePrice, fittings, installed); // TODO code to print final price }
-Gary-
- 12-12-2010, 12:14 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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 -
Testing arguments
By and0rsk in forum New To JavaReplies: 6Last Post: 10-10-2010, 11:17 AM -
Sorting objects with 2 strings arguments and 2 int arguments
By tirwit in forum New To JavaReplies: 8Last Post: 09-23-2010, 12:07 AM -
Passign arguments
By ninjalord918 in forum AWT / SwingReplies: 4Last Post: 08-03-2010, 11:11 PM -
passing arguments
By mac in forum New To JavaReplies: 3Last Post: 04-07-2010, 11:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks