HW3 Solutions Page
Return to
homework page.
Here are the javadoc files I created for the four programs we had to
write:
|cs151sec3hw3file1|
|cs151sec3hw3file2|
|cs151sec3hw3file3|
|cs151sec3hw3file4|
4.7
// Driver - used to the canonical form methods of the cs151sec3hw3file1
import java.util.*;
import java.applet.*;
import java.net.*;
/**
Driver is used to test the canonical form methods of cs151sec3hw3file1.
It creates three instances of cs151sec3hw3file1 and attaches
an AppletStub implementation to them, initializes them and test
equal(), toString() and hashCode(). applet's use the
AppletStub method getParameters() when they want to know the parameters
passed to themselves. (AppletStub usual set by browser).
*/
public class Driver
{
public static void main(String[] args)
{
/* a,b,c will eventually contain various versions of our Applet
which we will test
*/
cs151sec3hw3file1 a = new cs151sec3hw3file1();
cs151sec3hw3file1 b = new cs151sec3hw3file1();
cs151sec3hw3file1 c = new cs151sec3hw3file1();
/* set-up a HashMap and use it to create an AppletStub
which set into a and b.
*/
HashMap table = new HashMap();
table.put("categories","horse donkey zebra");
table.put("horse","55");
table.put("donkey","25");
table.put("zebra","15");
Parameters equines = new Parameters(table);
a.setStub(equines);
a.init();
b.setStub(equines);
b.init();
table.put("categories","horse donkey zebra mule");
table.put("mule","5");
equines = new Parameters(table);
c.setStub(equines);
c.init();
// test toString and hashCode method's.
System.out.println("Here's a.toString():\n" + a.toString());
System.out.println("Here's a.hashCode():\n" + a.hashCode());
// now test two things that are equal
if( a.equals(b)) System.out.println("a and b are equal.");
else System.out.println("a and b are not equal.");
// now test two things that are not equal
if( a.equals(c)) System.out.println("a and c are equal.");
else System.out.println("a and c are not equal.");
}
/**
This inner class is set into our test applets so they can
use it to read our test pie chart parameters
*/
public static class Parameters implements AppletStub
{
HashMap table; // table of parameters
/**
Sets the table of parameters
@param param - contains a table of parameters to be used
with getParameters()
*/
public Parameters(HashMap param)
{
table = new HashMap(param);
}
/**
Does nothing.
@param x - new x size
@param y - new y size
*/
public void appletResize(int x, int y){}
/**
@return null
*/
public AppletContext getAppletContext()
{
return null;
}
/**
@return null
*/
public URL getCodeBase()
{
return null;
}
/**
@return null
*/
public URL getDocumentBase()
{
return null;
}
/**
@return false
*/
public boolean isActive()
{
return false;
}
/**
@param name - name of parameter want value of
@return parameter from table
*/
public String getParameter(String name)
{
return (String)table.get(name);
}
}
}
<!-- HTML used to test pie chart applet-->
<applet code="cs151sec3hw3file1.class" width="300" height="200">
<param name="categories" value="trucks suv compact coupe sedan" />
<param name="trucks" value="20" />
<param name="suv" value="30" />
<param name="compact" value="20" />
<param name="coupe" value="20" />
<param name="sedan" value="10" />
</applet>
// cs151sec3hw3file1 -- an applet to draw pie charts
import java.awt.*;
import java.util.*;
/**
This applet can be used to draw a pie chart depending parameters
passed in the HTML. The parameter categories is used to pass a
space separated list of pie chart categories. Each category
and its value are passed as additional categories.
@author Chris Pollett
@version 1.0
*/
public class cs151sec3hw3file1 extends java.applet.Applet
{
/**
Two pie charts are equal if they have the same categories
with same values for each category
@return true - if two pie charts equal
*/
public boolean equals( Object obj)
{
if( obj instanceof cs151sec3hw3file1)
return pieChart.equals(obj.toString());
return false;
}
/**
Sum of values in the pie chart.
@return returns sum of the values in the pie chart
*/
public int hashCode()
{
return sliceTotal;
}
/**
String of all categories and values.
@return string of all categories and values. one/line.
*/
public String toString()
{
return pieChart;
}
/**
Set up the width and the height of the pie chart as
well as the background color. Gets the pie chart parameters
from the HTML file
*/
public void init()
{
Dimension size = getSize();
width =size.width;
height = size.height;
topX = width/SCALE_TOP;
topY = height/SCALE_TOP;
widthPie = SCALE_PIE*topX;
heightPie = SCALE_PIE*topY;
setBackground(Color.white);
getPieChartParameters();
}
/**
Draws the pie chart to the Graphics context.
@param g - graphics context
*/
public void paint(Graphics g)
{
drawSlices(g);
drawLabels(g);
}
/*
Draws each of the slices in the pie chart
*/
void drawSlices(Graphics g)
{
int angle=0;
int angleOffset;
Color curColor;
for(int i=0; i < numCategories; i++)
{
g.setColor(colorSlice[i]);
angleOffset = (FULL_ROTATION*valueSlice[i])/sliceTotal;
g.fillArc(topX, topY, widthPie, heightPie, angle, angleOffset);
angle += angleOffset;
}
}
/*
Draws the names of the categories for the pie chart next
to the appropriate slice.
*/
void drawLabels(Graphics g)
{
double angle=0;
double angleOffset;
int drawX;
int drawY;
int centerX = width/2;
int centerY = height/2;
g.setColor(Color.black);
g.setFont(font);
for(int i=0; i < numCategories; i++)
{
angleOffset = (2*PI*valueSlice[i])/sliceTotal;
drawX = (int)(centerX*(1 +
SCALE_DRAW*Math.cos(angle-angleOffset/2)));
drawY = (int)(centerY*(1 +
SCALE_DRAW*Math.sin(angle-angleOffset/2)));
drawCenter(g, nameSlice[i],drawX,drawY);
angle -= angleOffset;
}
}
/*
Draws onto the context g the String string at location x, y.
*/
void drawCenter(Graphics g, String string, int x, int y)
{
FontMetrics fontMetrics = g.getFontMetrics();
int charWidth = fontMetrics.charWidth('0');
int charHeight = fontMetrics.getHeight();
g.drawString(string, x - charWidth*string.length()/2, y +charHeight/4);
}
/*
Gets the names of the slices (the categories) and their
values from parameters passsed from the HTML file.
*/
void getPieChartParameters()
{
String categories = getParameter("categories");
StringTokenizer tokens = new StringTokenizer(categories);
numCategories= tokens.countTokens();
nameSlice = new String[numCategories];
valueSlice = new int[numCategories];
colorSlice = new Color[numCategories];
int cnt = 0;
sliceTotal = 0;
String next;
String param;
StringBuffer buf = new StringBuffer();
/* using StringBuffer should be faster than using String +
+= as don't keep creating anonymous StringBuffer objects. */
while(tokens.hasMoreTokens())
{
next = tokens.nextToken();
param = getParameter(next);
nameSlice[cnt] = next;
valueSlice[cnt] = Integer.parseInt(param);
colorSlice[cnt] = new Color( (int)(Math.random() * COLOR_RANGE),
(int)(Math.random() * COLOR_RANGE),
(int)(Math.random() * COLOR_RANGE));
sliceTotal += valueSlice[cnt];
buf.append(next);
buf.append(" ");
buf.append(param);
buf.append("\n");
cnt++;
}
pieChart = buf.toString();
}
String[] nameSlice; // array of names of categories
int[] valueSlice; // array of values for each category
Color[] colorSlice; // color of each wedge of pie chart
Font font = new Font( "SansSerif", Font.PLAIN, 12); /* font used to print
categories */
int numCategories; // number of categories
int sliceTotal; // sum of values of all the categories.
int width; // width of applet
int height; // height of applet
int topX; // top x coordinate of bounding rectangle of pie chart
int topY; // top y coordinate of bounding rectangle of pie chart
int widthPie; // width of pie chart
int heightPie; // height of pie chart
final int SCALE_TOP=4; // pic chart starts width/SCALE_TOP picels down
final int SCALE_PIE=2; // width/SCALE_PIE is size of pie chart
final double SCALE_DRAW = .75; //
final int FULL_ROTATION = 360; //360 degrees in acircle
final double PI = Math.PI; // beloved constant
final int COLOR_RANGE=256; // RGB values can be from 0 to 256
String pieChart; //categories and value written as one string
}
5.1 a, b, d
<!-- Three HTML files used to test plot functions -->
<applet code="cs151sec3hw3file2.class" width="300" height="200">
<param name="xorigin" value="150" />
<param name="yorigin" value="100" />
<param name="xratio" value="30" />
<param name="yratio" value="30" />
</applet>
<applet code="cs151sec3hw3file3.class" width="300" height="200">
<param name="xorigin" value="150" />
<param name="yorigin" value="100" />
<param name="xratio" value="
30" />
<param name="yratio" value="30" />
</applet>
<applet code="cs151sec3hw3file4.class" width="300" height="200">
<param name="xorigin" value="150" />
<param name="yorigin" value="100" />
<param name="xratio" value="30" />
<param name="yratio" value="30" />
</applet>
/**
This applet draws the graph of sqrt x to the screen
@author Chris Pollett
@version 1.0
*/
public class cs151sec3hw3file2 extends Plotter
{
/**
@return value of the square root of x.
*/
public double func(double x)
{
return Math.sqrt(x);
}
}
/**
This applet draws the graph of ln x to the screen
@author Chris Pollett
@version 1.0
*/
public class cs151sec3hw3file3 extends Plotter
{
/**
@return - the natural log of x
*/
public double func(double x)
{
return Math.log(x);
}
}
/**
This applet draws the graph of x^3-3x^2+5x+8 to the screen
@author Chris Pollett
@version 1.
*/
public class cs151sec3hw3file4 extends Plotter
{
/**
@return x^3-3x^2+5x+8
*/
public double func(double x)
{
return x*(x*(x-3)+5)+8; //Horner's rule
}
}