Connecting Tech Pros Worldwide Forums | Help | Site Map

Good coding

Newbie
 
Join Date: Oct 2009
Posts: 31
#1: Oct 7 '09
Hi All,

Being new to java i was hoping for some input as to ensure im following best practice.

The following script is for some homework admittedly. It all works and outputs what i want, but to me it looks messy so i really want to know if im overdoing the code?

Thanks in advance

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package weektwohomework;
  7. // include your header
  8.  
  9.  
  10.  
  11. class CDDriver
  12. {
  13.  
  14.     public static void main(String args[])
  15.     {
  16.  
  17.  
  18.                 CD[] cdList = new CD[3];
  19.  
  20.                 cdList[0] = new CD("Kaiser "," up the khazi ", 9.99);
  21.         cdList[1] = new CD("Oasis "," morning glory ",3.99);
  22.         cdList[2] = new CD("Bob Dylan "," Alreet Sunna ",6.99);
  23.  
  24.  
  25.  
  26.  
  27.         System.out.println("Artist \t\tTitle\t\tCost");
  28.         System.out.println("====================================");
  29.  
  30.                 for (int i = 0; i < cdList.length; i++)
  31.                 System.out.println(cdList[i].getArtist() + " " + cdList[i].getTitle() + " " + cdList[i].getCost());
  32.                // System.out.println(cd1.getArtist() + " " + cd1.getTitle() + " " + cd1.getCost());
  33.                 //System.out.println(cd2.getArtist() + " " + cd2.getTitle() + " " + cd2.getCost());
  34.                // System.out.println(cd3.getArtist() + " " + cd3.getTitle() + " " + cd3.getCost());
  35.  
  36.  
  37.                 // set an integer for the highest price
  38.                       double maxPrice = 0;
  39.  
  40.                         //loop through all records (the cdLists length) and increment by 1
  41.                       for (int i=0; i<cdList.length; i++)
  42.  
  43.                           // if the items price which is being checked is higher than the
  44.                           //current value of maxPrice,
  45.                           if (cdList[i].getCost()>maxPrice)
  46.  
  47.                                 //replace its value with that cds price
  48.                           maxPrice = cdList[i].getCost();
  49.  
  50.  
  51.                       // display max price to the screen
  52.                       System.out.println("The highest price is: " + maxPrice);
  53.  
  54.  
  55.                       // set an int to hold the total number of CDs that are counted and set to zero
  56.                       int total = 0;
  57.  
  58.                       //for loop to work through list of cds
  59.                       for( int j = 0; j < cdList.length;j++)
  60.  
  61.                             //add one to total for each item found
  62.                           total = total + 1;
  63.  
  64.                       //output how many have been counted to screen
  65.                       System.out.println("You have " + total + " cd's in your list");
  66.  
  67.  
  68.                       //set variable for total price
  69.                       double totalPrice = 0;
  70.  
  71.                     for ( int k = 0; k < cdList.length; k ++)
  72.  
  73.                         totalPrice = totalPrice + cdList[k].getCost();
  74.  
  75.                      System.out.println("The total value of all of your CD's is: " + totalPrice);
  76.  
  77.     }
  78. }
  79.  
  80.  

myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#2: Oct 7 '09

re: Good coding


It's good that you are trying to learn about coding standard while learning the language. Most people don't.

A glance at your code tells me that it's time you discomposed your very long methods into a number of shorter ones that are a bit specialized. This allows for readability and more importantly reuse.

Also do not comment the obvious. Doing
Expand|Select|Wrap|Line Numbers
  1.  // Add to total
  2. total = total + 1; 
does not say anything that we cannot tell immediately just by looking at the code.

There are many others but those do for now.
Happy coding!

Regards,

Alex.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Oct 7 '09

re: Good coding


You can count the number of CDs on that rack at the same time as you are checking which one is the most expensive one. This will take you less time than going over the same rack twice.
Leaving you time on your hands to call ...
Newbie
 
Join Date: Oct 2009
Posts: 31
#4: Oct 7 '09

re: Good coding


so i could use the same for loop and have each function run concurrently?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Oct 7 '09

re: Good coding


Don't forget the braces {} around the block of code you want repeating.
Reply