
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS Properties
HTML 4.01 Tags
Site Management
WD Forums

    Web Video
    Expression Web



    Forum, Blog, Wiki & CMS

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

    The Coffee Lounge
    Computer Issues

Java Programming ... From the Grounds Up Part 2

Java Programming ... From the Grounds Up
Part 2

by Mark C. Reynolds

The code, boss...the code!

Figure 1 shows the Java code listing for an application that can generate prime numbers. At the beginning of the code, a class called Primes is declared. This class has a private variable called start that will set the value at which the program starts looking for primes, and a public variable known as count that tracks the number of primes it should find. Line 11 shows that within the class, the method Primes takes a single-integer argument which is used to initialize the private variable start. This is the only way that start can be set, since a private variable is inaccessible outside its class.

Primes.java, the source code for a Java application that generates prime numbers.

 1: /** 2: Prime number generator 3: @author Mark C. Reynolds 4: @version 1.0 5: */ 6: 7: class Primes { 8: private int start; // no default 9: public int count = 100; // default value 10: 11: Primes(int sta) { // constructor with one arg 12: start = sta; 13: } 14: 15: private boolean isPrime(int p) { 16: int i; 17: for(i=2;i<(p/2);i++) { 18: if ( i*(p/i) == p ) return(false); 19: } 20: return(true); 21: } 22: 23: public void Generate(int arr[]) { // make 24: primes 25: int sta = start; 26: int cnt = count; 27: int idx = 0; 28: 29: while ( cnt > 0 ) { 30: if ( isPrime(sta) ) { 31: arr[idx++] = sta; 32: cnt; 33: } 34: sta++; 35: } 36: } 37: } 38: 39: class Mclass { 40: public static void main(String argv[]) { 41: 42: Primes p; 43: int arrp[]; 44: int len; 45: int sta = 2; 46: int cnt = 0; // dummy default 47: int i; 48: 49: len = argv.length; 50: if ( len > 0 ) { 51: sta = Integer.parseInt(argv[0]); 52: if ( len > 1 ) { 53: cnt = Integer.parseInt(argv[1]); 54: } 55: } 56: p = new Primes(sta); 57: if ( len > 1 ) p.count = cnt; // set if user 58: specified a 59: count 60: arrp = new int[p.count]; 61: p.Generate(arrp); 62: 63: for(i=1;i<=p.count;i++) 64: System.out.println("Prime " + i + 65: " is " + arrp[i-1]);: 66: } 67: } 68: 

Line 11 also shows an example of a method (Prime) having the same name as its class (Prime). This special kind of method, called a constructor, permits the programmer to initialize an object in the Primes class before using it. In line 15, the method isPrime takes an integer p and returns a boolean value (set to true or false) indicating whether or not that integer is prime, using brute force procedures. If we had developed a highly sophisticated method of testing for primality, it might be in our commercial interests to retain the rights to that algorithm, so in that spirit the method isPrime has been declared private. The public method Generate accepts an array name and generates count primes by methodically incrementing up through the integers beginning at the value set for start and testing each one to see if it is prime. Any primes found are loaded into the array passed as the argument to Generate. Since Generate is a public method, it may be called freely by any Java code. Since it is also part of the Primes class, it is permitted to call the private isPrime method. A Java programmer using the Primes class may therefore create a list of primes, but is not permitted to access the function that tests for primality.

This class definition is followed by the definition of the Mclass class. This class contains a single member, a method function known as main. This function is akin to the main() at the heart of C and C++, except that it takes a single argument, an array of elements of type String. Java handles much of its string handling capabilities within its String classes. The section Advanced Java Classes explains the predefined Java classes used in this example in greater detail.

[ < Java Programming ... From the Grounds Up:
Part 1 ]
[ Java Programming ... From the Grounds Up:
Part 3 > ]

HTML5 Development Center

Recent Articles