Chris Pollett>Old Classes>PIC 20, Winter 2000>Hw2>Hw2 Solutions
// Program Name: Pattern.java // // Purpose: This program searches the command line arguments to // to see if it contains the pattern 7 7 9 // It outputs has pattern if the command line arguments // have this pattern. And outputs no has pattern otherwise. // Pattern search is done by modeling a a simple // three state finite automata. // // Known Bugs: None. public class Patterns { public static void main( String args[]) { int state =0; // state keeps track of current state // 0 is the start state int value; //contains current command line arg boolean accept=false; //flag to say if we accept/reject //input acc : { //labelled break used if we accept input for(int i=0; i< args.length; i++) { value = Integer.parseInt(args[i]); switch(state) { case 0 : if(value ==7 ) state++; break; case 1 : if(value == 7) state++; else state--; break; case 2: if(value == 9) {accept =true; break acc;} if(value != 7) state = 0; break; default : //do nothing } // end switch } // end for } //end acc block if(accept) System.out.println("has pattern"); else System.out.println("no has pattern"); } //end main } //end Patterns // Program Name: Triangles.java // // Purpose: Draw Sierpinski's triangle to the nesting depth supplied by // the user in a dialog box. Triangle drawing is implemented // using a recursive method drawTri below. // // Known Bugs: None. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Triangles extends JApplet implements ActionListener { JButton toSee; //button to click to draw Triangle boolean showTriangle = false; // flag used by paint method // to determine if should draw triangle // triangle should only be draw if we have // gotten a nesting depth from user int nest, x[] = {60,110,160} , y[] = {140,40,140}; //x,y coordinates of largest triangle // nest controls the nesting depth to which we draw // triangle // Name: init() // // Purpose: Adds button to applet and sets up handler // for button click events. Also sets applet background color to white. public void init() { Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.setBackground(Color.white); toSee = new JButton( "Click for Triangles"); toSee.addActionListener( this); c.add( toSee); } // Name: paint(Graphics g) // // Purpose: This method explicitly calls JApplet's version of paint // so as to redraw the button on the applet. It then draws // the Sierpinski triangle to the nesting depth provided // if the showTriangle flag has been set. // public void paint( Graphics g) { super.paint(g); if (!showTriangle) return; drawTri(x,y,g,nest); } // Name: actionPerformed(ActionEvent e) // // Purpose: This method should be called when when applet's button is clicked // It prompts the user for a nesting depth, sets the showTriangle // to true so that paint will draw the triangle then calls repaint. public void actionPerformed( ActionEvent e) { showTriangle =true; nest = Integer.parseInt(JOptionPane.showInputDialog( "Enter integer nesting depth:")); repaint(); } // Name: drawTri(int xPoints[], int yPoints[], Graphics g, int nest) // // Purpose: draws the Sierpinski Triangle with outermost triangle coordinates // given by xPoints[] and yPoints[]. The triangle is drawn to // the nesting depth provided in nest. The algorithm // works by drawing the outermost triangle using drawPolygon // it then computes the midpoint sof the sides of this triangle // and uses these midpoints to compute three subtriangles // which it recursively calls with one less nesting depth private void drawTri(int xPoints[],int yPoints[], Graphics g, int nest) { int xx[][], yy[][]; xx = new int [3][3]; yy = new int [3][3]; int nNest = nest-1; if(nest <= 0) return; g.drawPolygon(xPoints,yPoints,3); for(int i=0; i<3; i++) { xx[i][i]=xPoints[i]; yy[i][i]=yPoints[i]; } xx[0][1]=xx[1][0]=(xPoints[0]+xPoints[1])/2; yy[0][1]=yy[1][0]=(yPoints[0]+yPoints[1])/2; xx[0][2]=xx[2][0]=(xPoints[0]+xPoints[2])/2; yy[0][2]=yy[2][0]=(yPoints[0]+yPoints[2])/2; xx[1][2]=xx[2][1]=(xPoints[1]+xPoints[2])/2; yy[1][2]=yy[2][1]=(yPoints[1]+yPoints[2])/2; for(int i=0; i<3; i++) { drawTri(xx[i],yy[i],g,nNest);} } }