Computing Prime Classes
The class of students entering college in 1999 and graduating in 2003 is a special class because both 1999 and 2003 are prime number. Between, 1900 and 2100, how many such classes exist?
Enter the two years in the applet below to find out.
/* Applet that prints out "prime classes" between any two give years. What is a prime class? If both the entering and graduating years of a class are prime numbers then we have a prime class. For example, Class of 2003 entered in 1999. Both 1999 and 2003 are prime classes. Credits: This problem was communicated to me via a student (class of 2003) who heard Haverford President Tom Tritton describe it. You can use this applet to find out how many prime classes are there between 1900 and 2100 */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class PrimeClasses extends Applet implements ActionListener { // GUI Elements TextField firstNumber = new TextField(5); TextField secondNumber = new TextField(5); Button goButton = new Button("Go"); // program variables int year1=0, year2=0; public void init() { makeGUI(); } // init private boolean isPrime(int n) { // See if n is a prime number for (int divisor=2; divisor*divisor <= n; divisor++) { if (n % divisor == 0) return false; } return true; } // isPrime public void paint(Graphics g) { int lastPrime = 0, x = 20, y = 20; for (int year = year1; year <= year2; year++) { // See if year is a prime number if (isPrime(year)) { if (year - lastPrime == 4) { // we got a prime class, print it g.drawString(lastPrime + ", " + year, x, y); y = y + 15; } lastPrime = year; } // if... } // for... } // paint public void actionPerformed (ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Go")) { // deal with the action // read the start and end numbers processInput(); repaint(); } } // actionperformed public void processInput() { String s; Integer i; // Get the text in Start Field s = firstNumber.getText(); // Convert it to Integer object i = new Integer(s); // gets its int value year1 = i.intValue(); // Get the text in End Field s = secondNumber.getText(); // Convert it to Integer object i = new Integer(s); // gets its int value year2 = i.intValue(); } // processInput private void makeGUI() { Label startLabel = new Label("Start :", Label.RIGHT); Label endLabel = new Label("End :", Label.RIGHT); Panel p1 = new Panel(); Panel p2 = new Panel(); Panel p3 = new Panel(); Panel p11 = new Panel(); Panel p21 = new Panel(); Panel p = new Panel(); // add the labels and textfields to the panels and applet p1.setLayout(new GridLayout(1, 2)); p1.setBackground(Color.cyan); p1.add(startLabel); p11.setLayout(new FlowLayout(FlowLayout.LEFT)); p11.add(firstNumber); p1.add(p11); p2.setLayout(new GridLayout(1, 2)); p2.setBackground(Color.cyan); p2.add(endLabel); p21.setLayout(new FlowLayout(FlowLayout.LEFT)); p21.add(secondNumber); p2.add(p21); p3.setBackground(Color.black); p3.add(goButton); p.setLayout(new GridLayout(3, 1)); p.add(p1); p.add(p2); p.add(p3); // add the panel to the applet this.setLayout(new BorderLayout()); this.setBackground(Color.yellow); add(p, "South"); // register listener(s), if any goButton.addActionListener(this); } // makeGUI } // PrimeClasses