CS 100: Lecture L9

February 23

| Back to Lecture Index |


Introduction to instance methods via the class Disk:

public class Disk
// An instance of this class is a disk with center (xc,yc) and radius r.
{
   private double xc;
   private double yc;
   private double r;
   
   // Constructor
   public Disk(double xcVal, double ycVal, double rVal)
   {
      xc = xcVal;
      yc = ycVal;
      r  = rVal;
   }
   
   
   // Yields the center-to-center distance between this disk and q.
   public double dist(Disk q)
   {
      double delx = xc-q.xc;
      double dely = yc-q.yc;
      double d    = Math.sqrt(delx*delx + dely*dely);
      return d;
   }
   
   // Yields the area of this disk.
   public double area()
   {
      return Math.PI*r*r;
   }
   
   // Pretty prints the parameters of this disk.
   public void println()
   {
   System.out.println("xc = " + xc + "  yc = " + yc + "  r = " + r);
   }
   
   // Yields the minimum distance between a point in this disk and a point in q.
   public double sep(Disk q)
   {
      double c2c;  // The center-to-center distance
      c2c = Math.sqrt((xc-q.xc)*(xc-q.xc)+(yc-q.yc)*(yc-q.yc));
      // Equivalent to  c2c = dist(q);
      if (c2c<=r+q.r)
         {return 0;}
      else
         {return c2c - r - q.r;}
   }
   
   
   // Yields true if every point in q is in this disk.
   public boolean contain(Disk q)
   {
      double d = this.dist(q);
      return (d+q.r<=r);
   }
   
   // Yields a disk whose center is randomly selected from the set
   // {(x,y) | x1<=x<x2, y1<=y<y2} and whose radius is randomly selected from 
   // the interval [0,rMax).
   public static Disk random(double x1, double x2, double y1, double y2, double rMax)
   {
      double a = x1 + Math.random()*(x2-x1);
      double b = y1 + Math.random()*(y2-y1);
      double rho = Math.random()*rMax;
      Disk D = new Disk(a,b,rho);
      return D;
   }      
   
   
   // Resets the center of this disk to (x0,y0).
   public void move(double x0, double y0) 
   {
      xc = x0;
      yc = y0;
   }
   
   // Resets the radius of this disk to r0.
   public void scale(double r0)
   {
      r = r0;
   }
   
   // Yields the x-coordinate of this disk's center.
   public double get_xc()
   {
      return xc;
   }
   
   // Yieds the y-coordinate of this disk's center.
   public double get_yc()
   {
      return yc;
   }
   
   // Yields the radius of this disk.
   public double get_r()
   {
      return r;
   }
}

Examples that use the methods in Disk:

import java.io.*;
public class ShowDisk
{	
	public static void main(String args[])
	{
           TokenReader in = new TokenReader(System.in);
	   
	   // Illustrate the constructor and the println method.
	   Disk D1 = new Disk(1.0,2.0,3.0);
	   System.out.print("D1: ");
	   D1.println();
	   System.out.print("D2: ");
	   Disk D2 = new Disk(4.0,6.0,3.0);
	   D2.println();
	   
	   // Illustrate the dist method.
	   double d12 = D1.dist(D2);
	   double d21 = D2.dist(D1);
	   System.out.println("d12 = " + d12);
	   System.out.println("d21 = " + d21);
	   
	   // Illustrate the area method.
	   double area = D1.area();
	   System.out.println("The area of D1 = " + area);
	   
	   // Illustrate the sep method.
	   D1 = new Disk(10,10,5);
	   System.out.print("\nD1: ");
	   D1.println();
	   D2 = new Disk(30,30,3);
	   System.out.print("D2: ");
	   D2.println();
	   double sep12 = D1.sep(D2);
	   System.out.println("The separation between D1 and D2 is " + sep12);
		
	   // Illustrate the contain method.
	   D1 = new Disk(0,0,5);
	   System.out.print("\nD1: ");
	   D1.println();
	   D2 = new Disk(1,1,3);
	   System.out.print("D2: ");
	   D2.println();
	   if (D1.contain(D2))
	      {System.out.println("D1 contains D2");}
	   else
	      {System.out.println("D1 does not contain D2");}
	   if (D2.contain(D1))
	      {System.out.println("D2 contains D1");}
	   else
	      {System.out.println("D2 does not contain D1");}  
	      
	   // Illustrate the move and scale methods.
	   D1 = new Disk(0,0,1);
	   System.out.print("\nD1: ");
	   D1.println();
	   D1.move(1,2);
	   System.out.print("D1: ");
	   D1.println();
	   D1.scale(3);
	   System.out.print("D1: ");
	   D1.println();
	   
	   // Illustrate the get methods.
	   double a = D1.get_xc(); 
	   double b = D1.get_yc();
	   double rho = D1.get_r();
	   System.out.println("    xc = " + a + "  yc = " + b + "  r = " + rho);
	
	
	   // Illustrate the random method by estimating the probability that two
	   // random disks intersect. The disk centers are randomly situated in
	   // a 10-by-10 square and the radii are randomly selected from the interval [0,5).
	   
	   Disk Disk1,Disk2;
	   int count=0;
	   for(int k=1;k<=1000;k++)
	   {
	      Disk1 =  Disk.random(0,10,0,10,5);
	      Disk2 =  Disk.random(0,10,0,10,5);
	      if (Disk1.sep(Disk2)>0)
	         {count++;}
	   }
	   System.out.println("\nProb = " + (double)count/1000);
	      
	   in.waitUntilEnter();
	}

}