Java examples: Enhanced For Loop


Advertisements

Calculating average using a perl script


Ok here is the basic perl script for calculating average.

I assume you know how to run the script if you dont ( I will put up a tutorial later so for now dig around in other forms )

Perl Script for calculating average from another file

#!/usr/bin/perl
#calculate averate from a file

use strict;

my $total = 0;
my $count =0;
while (my $line = <>) {
      $total +=$line;
      $count ++=;
}
print "Average = ", $total / $count, "\n";

So the  explaniation for the above code is…

use strict; – allow to find simple mistakes

to find the $average need to calculate

my $total

and the total number of lines

my $count

At the begining of the loop these values are 0.

therefore define the inital value

my $total  = 0;
my $count =0;

So now what you need to do this read each line andadd each value consequtively.

while (my $line = <>) {
   $total += $line;
   $count ++=;
 }
print "Average = ", $total / $count, "\n";

Read the first line.  Then add then and the value read to the total inital value  of 0.

   $total = $line + $total

calculate the total

   $count = $count + 1

calculate the total number of lines read
this can be written in short form as below

   $total += $line;
   $count ++=;

then go back in loop adding value conseutively until there are no more lines to read and then print the line

print "Average = ", $total / $count, "\n";

Hope this is clear I will put a anotated diagram up with more explanation later on.

Java Tutorial – Installing JAVA SE Development Kit (JDK)


Before writing a JAVA program first need to download JDK which allows write JAVA and compile the code.

  1. Download appropriate JAVA SE Development Kit (JDK) for your operating system
  2. Run the Installation
  3. Navigate to C:\Program Files\Java\jdk1.7.0\bin (this location may change in any instance you want to navigate to bin in JDK installation) make sure you have the .exe files there.
  4. copy the location of the bin C:\Program Files\Java\jdk1.7.0\bin
  5. Right click on computer and select properties > Advanced System settings > Environment variablesenvironment variables location
  6. Click on New and Variable name as Path and variable value as location of the bin so in my case it would be C:\Program Files\Java\jdk1.7.0\binUser Variable
  7. Click OK  OK
  8. To make sure compiler is working and ready for JAVA programming open cmd and run JavaC and if Java Compliler is working should get some random code as belowJavaC Working
Special thanks to Bucky Roberts for the clear accompanying video tutorial