p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java_objects thread: Re: Array Question


Message #1 by Jacquie Barker <jjbarker@e...> on Wed, 05 Jun 2002 05:20:23 -0400

Craig Trail wrote:

> I have to find the pretax average of an array item

I'm not sure what you mean:  the pretax average price of all items in the array?  My answer
below assumes this interpretation; but, I'd check back with whomever gave you the
requirements to begin with to clarify what "pretax average of an array item" really means
in plain English.

> and I have no idea how
> it is done. Any ideas would be apprecriated.

Assuming that you want to compute the average price of all items, you need to step through
the array of CatalogItem objects, and for each item (itemList[i]), call whatever method
will return the pretax price.  Compute a running total of these prices, then divide by the
total number of items.  You can write a method called pretaxAveragePrice() based on your
code for the expensive() method.

>
> public class Assignment_5
>
> {
>     private static final int MAX_ITEMS = 3; //Sets final array length
>
>     private static CatalogueItem[] itemList; // sets array
>
>     private static StringTokenizer split;// sets tokenizer to read the
> input text
>
>     NumberFormat cash = NumberFormat.getCurrencyInstance(new Locale
> ("en","AU"));
>
>     private static void main (String[] args) throws IOException
>
>     {
>         String temp1 = ""; // sets String for first item
>         String temp2 = ""; // sets String for second item
>         double temp3 = 0; // sets Double for third item
>         String inString = "";
>         int preTotal = 0;
>         double minPrice, maxPrice;
>
>         BufferedReader stdin = new BufferedReader (new InputStreamReader
> (System.in));
>
>         itemList = new CatalogueItem[MAX_ITEMS]; // sets array length
>
>         System.out.println ("Enter five Items of data:");
>
>         for(int i = 0; i < MAX_ITEMS;i++) // sets the length of the
> question
>
>         {
>             inString = stdin.readLine(); // read the input lines
>
>             // sets the String Tokenizer and the symbols that can be used
> to seperate the
>             // text to be read.
>             split = new StringTokenizer (inString, ".,;:/!?()\n\t");
>
>             temp1 = split.nextToken(); // sets the first token to be read
>             temp2 = split.nextToken(); // sets the second token to be read
>             temp3 = Double.parseDouble (split.nextToken()); // sets the
> third token to be read
>
>             // create an instance of CatalogueItem element of the array
>             itemList[i] = new CatalogueItem(temp1, temp2, temp3);
>         } // end of for loop
>
>
>             System.out.println();
>             System.out.println ();
>             System.out.println("LISTING OF ALL GOODS");
>             System.out.println();
>             System.out.println("Cat \tDesciption \tEx Tax \tTax \tInc
> Tax");
>             System.out.println ();
>             System.out.println("--- \t---------- \t------ \t--- \t--------
> ");
>             System.out.println();
>
>             for(int i = 0;i <= MAX_ITEMS -1;i++)
>
>                 System.out.println(itemList[i].getCatCode() + "\t" +
> itemList[i].getDesc()
>                 + "\t\t" + itemList[i].getPrice() + "\t" + itemList
> [i].getTaxAmount()
>                 +" \t" + itemList[i].getTaxIncl());
>
>             System.out.println ();
>             System.out.println ();
>             System.out.println ("CHEAPEST GOODS IN CATALOGUE");
>             System.out.println ();
>
>             minPrice = cheapest(itemList, MAX_ITEMS);
>
>             System.out.println("Cat \tDesciption \tEx Tax \tTax \tInc
> Tax");
>             System.out.println ();
>             System.out.println("--- \t---------- \t------ \t--- \t--------
> ");
>             System.out.println();
>
>             for(int i = 0;i <  MAX_ITEMS;i++)
>                if(itemList[i].getTaxIncl() == minPrice)
>                   System.out.println(itemList[i].getCatCode() + "\t" +
> itemList[i].getDesc()
>                   + "\t\t" + itemList[i].getPrice() + "\t" + itemList
> [i].getTaxAmount()
>                   +" \t" + itemList[i].getTaxIncl());
>
>
>             System.out.println ();
>             System.out.println ();
>             System.out.println ("MOST EXPENSIVE GOODS IN CATALOGUE");
>             System.out.println ();
>
>             maxPrice = expensive (itemList, MAX_ITEMS);
>
>             System.out.println("Cat \tDesciption \tEx Tax \tTax \tInc
> Tax");
>             System.out.println ();
>             System.out.println("--- \t---------- \t------ \t--- \t--------
> ");
>             System.out.println();
>
>             for(int i = 0;i <  MAX_ITEMS;i++)
>                if(itemList[i].getTaxIncl() == maxPrice)
>                   System.out.println(itemList[i].getCatCode() + "\t" +
> itemList[i].getDesc()
>                   + "\t\t" + itemList[i].getPrice() + "\t" + itemList
> [i].getTaxAmount()
>                   +" \t" + itemList[i].getTaxIncl());
>
>     }
>
>     public static double cheapest (CatalogueItem[] itemList,int size)
>
>     {
>         double minPrice = itemList[0].getTaxIncl();
>
>         for(int i =1; i < size; i++)
>
>         {
>             if(itemList[i].getTaxIncl() < minPrice)
>             minPrice = itemList[i].getTaxIncl();
>         }
>
>         return minPrice;
>      }
>
>      public static double expensive (CatalogueItem[] itemList, int size)
>
>      {
>         double maxPrice = itemList[0].getTaxIncl();
>
>         for (int i = 1; i < size;i++)
>
>         {
>             if (itemList[i].getTaxIncl() > maxPrice)
>             maxPrice = itemList[i].getTaxIncl();
>         }
>
>         return maxPrice;
>     }
> }


  Return to Index