★ wanayoo — archive 1999 http://www.webdeveloper.com/java/java_programming_grounds_up_2.htmlNouvelle recherche | Portail wanayoo
internet.com

Go to WebDeveloper Home

Seek and you shall

or search all of internet.com
Looking for a Site Map?
Vote for WD!
Please keep
voting for us
as a Top 100
Web site!

The Web Developer Network
WebDeveloper.com
Streaming Media World
WebReference.com
Web Developer's Virtual Library
BrowserWatch
Java Boutique
Web Developer's Journal
JavaScript Source
ScriptSearch
Web Developer Forums
Web Developer News

internet.com
Internet News
Internet Stocks
Internet Technology
Web Developer
Internet Marketing
ISP
Downloads
Internet Resources
International

Search internet.com
Advertising Info
Corporate Info
Internet Trade Shows

internet.commerce
Be an Affiliate
Be a Partner
Software Store
Computer Help
Register a Domain
Be Domain Registrar
e-solutions
Internet Jobs
A/V Network
Map Your Website
Rent E-mail Lists
Bookstore
Press Release dist.
Sell Ad Space
Internet Research
Venture Capital
Web Publishing
Build Your Intranet
Expert Advice
Get e-Biz Intell.
Content for Websites

  All Net Research -- in one place
J A V A  T U T O R I A L
WebDeveloper.com

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.


FIGURE 1
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.

[ Click here to move to the next part of the article ]

Fast Jump to Anywhere on WebDeveloper.com:

When you're ready to play with the big boys



Copyright © 1999 internet.com Corporation
All Rights Reserved. Legal Notices.
Contact the WebDeveloper.com staff

Last modified: Wed Sep 22 06:10:52 EDT 1999

http://www.internet.com

 

Refresh Daily
Join Editor-in-Chief David Fiedler The Editor With No Time and find truth, justice, and a clue or two.


Browse by Category
[ Site Map ]

ActiveX / VBscript
Animated GIF Archive
Browsers
CGI / Perl
Database Connectivity
Design / Graphics
E-Commerce
HTML-Advanced: DHTML, CSS
HTML / Site Authoring Tools
Intranet/Groupware
Java
JavaScript
Multimedia: Audio / Video / Streaming Technologies
Opinions
Refresh Daily: Editorial Column
Security
Servers & Server Tools
Site Design / Graphics
Site Management / Marketing / Log File Analysis
Tutorials
VRML / 3D
XML