A227A/D242: 3D Environments in Java                                   a

Quiz 1.

 

Name: ____________________________________________________

 

1.  Suppose we have a class called MyChild.  MyChild has a method called gotoSchool().

Fill in the code for the method gotoSchool (below) that will make all children go to school.

 

class MyMother  {

           

      MyChild[] children;

 

      //***** Contructor

      public MyMother(){

      children = new MyChild[4];

      for(int i=0; i<children.length; i++)

          children[i] = new MyChild();

 

      }

     

      //*********************

      public void gotoSchool(){

 

 

     for(int i=0; i<children.length; i++)

          children[i]. gotoSchool();

 

           

           

           

           

           

      }

}

 

2.  Rewrite the following code to make it faster:

 

      for(int i=0; i<100; i++){

          double ang = Math.PI/180.*30.;

          double d = Math.cos(Math.PI/180.*i) * Math.sin(ang);

      }

 

 

double ang = Math.PI/180.*30.;

sinang = Math.sin(ang);

double d;

double div = Math.PI/180.;

for(int i=0; i<100; i++){

          d = Math.cos(div*i) * sinang;

      }

 

 

 

 

 

 

 

 

 

 

3.  Draw the result of this code:

 

     //********************

      public void draw(Graphics g){

 

            Polygon poly = new Polygon();

 

            poly.addPoint(0, 0);

            poly.addPoint(0, 100);

            poly.addPoint(100, 100);

 

            g.drawPolygon(poly);

 

      }

 

 

 

  1. Can we write a DXF file from an applet?  (Explain why or why not)

 

No, because the browser will throw a security exception.

 

 

 

  1. Write the code that would reverse the order of an array of points[] (that is, the first to become the last, the second to become the last-1, etc.).

 

 

 

// Make a copy of points array

MyPoint[] copy;

copy = new MyPoint[points.length];

 

// Reverse the points

for(int j=0; j<points.length; j++)

     copy[points.length -1-j] = new MyPoint(points[j].x, points[j].y, points[j].z);

 

// Copy back to points

for(int j=0; j<points.length; j++)

     points[j] = new MyPoint(copy[j].x, copy[j].y, copy[j].z);