Question by Kat: Help with Arrays in Java?
ok heres some code.. could someone just show me how to call an array.. one with the get messages and one with the computing messages?? I can figure out the rest im sure, I just need a little help to know where to start and how to do it.
Thanks!!
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Lab6Fall09
{
public static void main(String[] args)
{
//variable declarations
int quote = 0;
int MotorHomeType;
int[] numbers;
double optionalFeaturesCost = 0.0;
double discount = 0.0;
double totalHomeCost = 0.0;
double netHomeCost = 0.0;
double taxes = 0.0;
double profit = 0.0;
double basicHomeCost = 48000;
String inputString = “”;
final double PERCENT_PROFIT = .33;
final double STATE_TAX_RATE = .075;
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat(“$ #,##0.00″);
//array declarations
int[] MotorHomeTypeArray = new int[5];
int[] EngineOptionArray = new int[5];
int[] TransmissionOptionArray = new int[5];
int[] CabinetOptionArray = new int[5];
int[] FurnitureOptionArray = new int[5];
double[] optionalFeaturesArray = new double[5];
double[] discountArray = new double[5];
double[] totalHomeCostArray = new double[5];
quote = 1;
do
{
discount = 0.0;
optionalFeaturesCost = 0.0;
totalHomeCost = 0.0;
MotorHomeType = 0;
descriptionMsg();
MotorHomeType = getMotorHomeType(quote);
if(MotorHomeType == 2) //motor home option == 2
{
optionalFeaturesCost = getEngineOption(optionalFeaturesCost);
optionalFeaturesCost = getTransmissionOption(optionalFeaturesCost);
optionalFeaturesCost = getCabinetOption(optionalFeaturesCost);
optionalFeaturesCost = getFurnitureOption(optionalFeaturesCost);
discount = computediscount(optionalFeaturesCost, discount);
}
totalHomeCost = computetotalHomeCost(optionalFeaturesCost, discount, totalHomeCost);
int moreQuotes = getmoreQuotes(quote);
//this shows option 2
if(moreQuotes != 1)
break;
quote++;
}while(quote < 6);
thankYouMsg();
}//End of main method
//defining all methods that are goin to be used
public static void showInputSummary(int arrayCount,
int[]MotorHomeTypeArray,
int[]EngineOptionArray,
int[]TansmissionOptionArray,
int[]CabinetOptionArray,
int[]FurnitureOptionArray,
double[] optionalFeaturesCostArray,
double[] discountArray,
double[] totalHomeCostArray,
DecimalFormat formatter)
/** descriptionMsg
*/
public static void descriptionMsg()
{
JOptionPane.showMessageDialog(null, "This program allows you to select either a standard or a custom built motor home.n" + "The program will total up and display the cost of the motor home you selected.n" + "You will be given up to 5 attempts to pick the motor home you want.");
}//End of description method
/** The get Motor Home feature allows you to choose between the Basic Motor Home and the Motor Home with options. This is the get motor home Method.
@param 1 option for the basic motor home.
@param 2 option for the motor home with optional features.
@return MotorHomeType.
*/
public static int getMotorHomeType(int quote)
{
String inputString = "";
inputString = JOptionPane.showInputDialog("This is quote # " + quote + " for getting a quote.n" + "For Only the Basic Motor Home Enter 1n" +
"For a Motor Home with Optional Features Enter 2");
//Check for motor home option errors
if((Integer.parseInt(inputString) != 1) && (Integer.parseInt(inputString) != 2))
{
JOptionPane.showMessageDialog(null, "Incorrect input for motor home.");
inputString = JOptionPane.showInputDialog("Please re-enter Motor Home Option 1n" +
"Or for Motor Home with Optional Features Enter 2.");
}
return Integer.parseInt(inputString);
}//End of the getMotorHomeType Method
/** The get Engine Option features allows you to choose what engine you want for your motor home. This is the get engine Method.
@param 1 option for the standard diesel engine.
@param 2 option for the heavy-duty turbo charged diesel engine.
@return optionalFeaturesCost.
*/
public static double getEngineOption(double optionalFeaturesCost)
{
String inputString = "";
//Get engine option
inputString = JOptionPane.showInputDialog("For the Standard Diesel Engine Enter 1n" +
"For the Heavy-Duty Turbo Charged Diesel Engine Enter 2");
//Check for engine option errors
if((Integer.parseInt(inputString) !=1) && (Integer.parseInt(inputString) != 2))
{
JOptionPane.showMessageDialog(null, "Incorrect input for engine option.");
inputString = JOptionPane.showInputDialog("Please re-enter Standard Diesel Engine Option 1n" +
"Or for the Heavy-Duty Turbo Charged Diesel Engine Enter 2.");
}
//Calcul
Heres what i want.. replace the original calls to the methods to make them arrays.
like optionalFeaturesCost = getengineOptions(optionalFeaturesCost,array count, engineOptionArray);
where the array count = 0;
quote count = 1;
Best answer:
Answer by David Karr
You’ll have to phrase a more specific question. I can’t tell what you’re looking for.
Arrays are not “called” like a function. You declare an array and initialize it to the array with null entries. You put non-null objects into entries in the array. You get entries from the array, either null or non-null.
Know better? Leave your own answer in the comments!
Technorati Tags: Arrays, Help, Java