Download AP Computer Science I

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
AP Computer Science I
Java Unit Lab Assignment # 33a
The "Graphical Linked List" Program
80 & 100 Point Versions
Assignment Purpose:
The purpose of this program is to demonstrate logical understanding of linked lists,
without yet knowing how to implement any type of linked list class.
Write a program that draws a graphical linked list. You will be drawing a linked list of six nodes. The
linked list must be drawn in the sequence that a linked list is formed. For this assignment you will first
draw a linked list as a stack for 80 points. For the 100-point version you will make a second drawing
that display a linked list drawn as a queue. The graphics display needs to have a time delay so that
the sequence of the linked list creation can be viewed.
You will be provided with a special GfxNode class, which is designed to draw linked lists. It is not
important that you understand the implementation of the methods in the GfxNode class. Your
mission is to know how to use the methods properly. The GfxNode class is shown below, but only
the public method headers and comments are provided. Understanding the implementation of the
GfxNode class is not necessary for completing this assignment.
GfxNode Public Methods Headers and Comments
// GfxNode constructor instantiates an object and
// stores its Top-Left coordinate (tlx,tly) information, as
// well as the length and width of the node object. A node object
// with two fields is drawn at the specified coordinate.
public GfxNode(Graphics g, int tlx, int tly, char ltr, int clr, int dt)
// Method getColor a private helper method to make it easier to use colors
// in a graphics program.
private Color getColor(int clr)
{
Color temp = Color.white;
switch (clr)
{
case 0: temp = Color.black; break;
case 1: temp = Color.red;
break;
case 2: temp = Color.green;
break;
case 3: temp = Color.blue; break;
case 4: temp = Color.orange; break;
case 5: temp = Color.cyan;
break;
case 6: temp = Color.magenta;
break;
case 7: temp = Color.yellow; break;
case 8: temp = Color.pink; break;
case 9: temp = Color.white; break;
}
return temp;
}
Exposure Java Chapter XXXIII Lab Assignments Page 1 07-04-03
// Method getX returns the top-left X-coordinate of a linked list node.
public int getx()
// Method getY returns the top-left Y-coordinate of a linked list node.
public int gety()
// Method drawPointer draws a vertical pointer down to an existing node.
// The first pointer to a node uses OffSet value 1 and the second
// pointer to the same node uses OffSet value 2. The result is that
// the second pointer is moved farther to the right.
public void drawPointer(Graphics g, char ltr, int offSet, int clr)
// Method enterData draws a letter in the Data field of the GfxNode.
public void enterData(Graphics g, char ltr, int clr)
// Method drawLink draws a link from the current sourceNode to the
// endNode in the specified color (clr).
public void drawLink(Graphics g, GfxNode endNode, int clr)
// Method drawNull draws a diagonal g.drawLine in the Next
// field of a list node, to indicate a NULL value.
public void drawNull(Graphics g, int clr)
// Method drawLetter upper-case Letter characters. The characters
// are drawn in a 9x9 pixel box.
// The (x,y) parameters indicate the coordinate of the top-left corner
// of the box. Only capital letters and numbers are drawn.
public void drawLetter(Graphics g, char ltr, int x, int y)
// Method delay allows viewing the sequence in which the linked lists are drawn/
private void delay(double n)
The use of the GfxNode methods is explained in Chapter 33 with a variety of program examples.
Make sure that you understand how to use this class before you start the assignment. This program
needs to be written as a graphics application program. The student file already has the main method
completed. The paint method is also completed. The goal of this assignment revolves around
completing the drawStack and drawQueue methods.
80 Point Version
The 80 point version draws only one linked list which is drawn in a stack LIFO manner.
100 Point Version
The 100 point version starts by drawing the stack and then also draws a second linked list that will
behave like a queue FIFO.
Exposure Java Chapter XXXIII Lab Assignments Page 2 07-04-03
Provided Student Program for Lab33a
// Lab33ast.java
// This is the student Version of the Lab33a assignment.
import java.awt.*;
import java.awt.event.*;
public class Lab33ast
{
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
gfx.setSize(1000,750);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.show();
}
}
class GfxApp extends Frame
{
private int td = 200;
// time delay to slow down graphics display
public void paint (Graphics g)
{
g.setFont(new Font("ARIAL",Font.BOLD,28));
g.drawString("LAB 33A 80/100 POINT VERSION",300,50);
g.setFont(new Font("ARIAL",Font.BOLD,20));
g.drawString("DRAWING A LINKED LIST AS A STACK",50,215);
// g.drawString("DRAWING A LINKED LIST AS A QUEUE",50,415);
drawStack(g);
// drawQueue(g);
}
// for 100 point version only
// for 100 point version only
public void drawStack(Graphics g)
{
g.setFont(new Font("ARIAL",Font.BOLD,20));
}
/* for 100 point version only
public void drawQueue(Graphics g)
{
}
*/
}
Exposure Java Chapter XXXIII Lab Assignments Page 3 07-04-03
80 Point Execution
In the 80-point, as well as the 100-point execution you will only see the finished display on hardcopy.
Make sure that you select a sufficient delay-time to demonstrate the sequence of the linked list
creation. It is not sufficient that your final picture matches the pictures that follow. The essence of
this assignment is that you demonstrate knowledge of the proper step sequence of creating linked
lists as a stack and for 100 points as a queue as well.
Exposure Java Chapter XXXIII Lab Assignments Page 4 07-04-03
100 Point Execution
Exposure Java Chapter XXXIII Lab Assignments Page 5 07-04-03
AP Computer Science I
Java Unit Lab Assignment # 33b
The "Polynomial" program
80 & 100 Point Versions
Assignment Purpose:
The purpose of this program is to demonstrate knowledge of the Java syntax that is required to
create and traverse a simple linked list, which is created with dynamic memory allocation.
Write a program that will evaluate polynomial functions of the following type:
Y = a1Xn + a2Xn-1 + a3Xn-2 + . . . an-1X2 + anX1 + a0X0
where
X, the coefficients ai, and n are to be given.
This program has to be written, such that each term of the polynomial is stored in a linked list node.
You are expected to create nodes for each polynomial term and store the term information. These
nodes need to be linked to each previously created node. The result is that the linked list will access in
a LIFO sequence. When you display the polynomial, it will be displayed in reverse order from the
keyboard entry sequence.
80 Point Version
This program has a 80 point and a 100-point version. The 80-point version displays the contents of
each polynomial term, and is not concerned with normal mathematical appearance. The result is a
polynomial display like:
Y
=
1X^0 + 0X^1 + 0X^2 + 1X^3
100 Point Version
The 100-point version makes the display follow mathematical conventions and does not display terms
with zero coefficients, nor powers of 1 or 0. The polynomial example, above, is shown again as it would
appear with the 100-point version.
Y
=
1 + X^3
Exposure Java Chapter XXXIII Lab Assignments Page 6 07-04-03
Normal polynomials should work with real number coefficients. For the sake of this program, assume
that you are strictly dealing with integers and that the result of the polynomial is an integer as well. You
will be provided with a special PolyNode class. The PolyNode class is very similar to the ListNode
class that you learned about in chapter 33 and in class. The ListNode class is more general and works
with object data members. Such a class is very practical for many different situations. For this
assignment, early in your linked list learning, a class has been created strictly for working with a linked
list that will store the coefficient and the degree of each term in the polynomial..
// Lab33bst.java
// The student version of the Lab33b assignment.
import java.io.*;
public class Lab33bst
{
public static void main (String args[]) throws IOException
{
}
}
class PolyNode
{
private int coeff;
// coefficient of each term
private int degree;
// degree of each term
private PolyNode next; // link to the next term node
public PolyNode (int c, int d, PolyNode initNext)
{
coeff = c;
degree = d;
next = initNext;
}
public int getCoeff()
{
return coeff;
}
public int getDegree()
{
return degree;
}
public PolyNode getNext()
{
return next;
}
public void setCoeff (int newCoeff)
{
coeff = newCoeff;
}
public void setDegree (int newDegree)
{
degree = newDegree;
}
public void setNext (PolyNode newNext)
{
next = newNext;
}
}
Exposure Java Chapter XXXIII Lab Assignments Page 7 07-04-03
You are expected to add various methods that are not provided in the student version. The sample
execution will indicate which methods you need to write. Everything could be finished in the main
method of the program, but hopefully you realize by now that such an approach is rather poor
program design.
Lab33b 80 Point Version
One Required Output
Lab33b 80 Point Version
#####
ENTER-POLY METHOD
#####
Enter the degree of the polynomial
Enter
Enter
Enter
Enter
Enter
#####
coefficient
coefficient
coefficient
coefficient
coefficient
for
for
for
for
for
X^4
X^3
X^2
X^1
X^0
If
If
If
If
If
ENTER-XVALUE METHOD
no
no
no
no
no
DISPLAY-POLY METHOD
4
exists,
exists,
exists,
exists,
exists,
enter
enter
enter
enter
enter
0
0
0
0
0
===>>
===>>
===>>
===>>
===>>
1
2
3
4
5
#####
Enter X value of the polynomial
#####
term
term
term
term
term
===>>
====>>
2
#####
Y = 5X^0 + 4X^1 + 3X^2 + 2X^3 + 1X^4
#####
COMPUTE-POLY METHOD
#####
DISPLAY-VALUE METHOD
#####
#####
Y(2) = 57
Exposure Java Chapter XXXIII Lab Assignments Page 8 07-04-03
Lab33b 100 Point Version
Two Required Outputs
Lab33b 100 Point Version
#####
ENTER-POLY METHOD
#####
Enter the degree of the polynomial
Enter
Enter
Enter
Enter
Enter
coefficient
coefficient
coefficient
coefficient
coefficient
#####
for
for
for
for
for
X^4
X^3
X^2
X^1
X^0
If
If
If
If
If
ENTER-XVALUE METHOD
no
no
no
no
no
term
term
term
term
term
4
exists,
exists,
exists,
exists,
exists,
enter
enter
enter
enter
enter
0
0
0
0
0
===>>
===>>
===>>
===>>
===>>
1
2
3
4
5
#####
Enter X value of the polynomial
#####
===>>
DISPLAY-POLY METHOD
====>>
2
#####
Y = 5 + 4X + 3X^2 + 2X^3 + X^4
#####
COMPUTE-POLY METHOD
#####
#####
DISPLAY-VALUE METHOD
#####
Y(2) = 57
Lab33b 80 Point Version
#####
ENTER-POLY METHOD
#####
Enter the degree of the polynomial
Enter
Enter
Enter
Enter
Enter
#####
coefficient
coefficient
coefficient
coefficient
coefficient
for
for
for
for
for
X^4
X^3
X^2
X^1
X^0
If
If
If
If
If
ENTER-XVALUE METHOD
no
no
no
no
no
DISPLAY-POLY METHOD
4
exists,
exists,
exists,
exists,
exists,
enter
enter
enter
enter
enter
0
0
0
0
0
===>>
===>>
===>>
===>>
===>>
1
0
0
0
5
#####
Enter X value of the polynomial
#####
term
term
term
term
term
===>>
====>>
2
#####
Y = 5 + X^4
#####
COMPUTE-POLY METHOD
#####
DISPLAY-VALUE METHOD
#####
#####
Y(2) = 21
Exposure Java Chapter XXXIII Lab Assignments Page 9 07-04-03
Exposure Java Chapter XXXIII Lab Assignments Page 10 07-04-03