GraphicsLab08 - Vernon Computer Science

advertisement
PreAP Computer Science
The Graphics Sorter
Graphics Lab 08
Practice/Perform Major Assignment
80 & 100 Point Versions
Assignment Purpose:
The purpose of this program is to demonstrate knowledge of interfacing a sorting
algorithm with a graphics display.
This assignment is a continuation from Graphics Lab 07. In that lab, you took an array of random
integers and displayed them as vertical bars on the graphics screen using a method you wrote called
displayBars. The random arrangement of the bars represented the random arrangement of the
integers in the array. In chapter 13 you learned the Bubble Sort. You will use this to sort your array.
Then when displayBars is called again, we will see, visually, that the array is sorted.
The displayBars method is more complicated in this assignment as now the “bars” are actual bars
that are 10 pixels thick as opposed to a simple line; however, the focus of this assignment is on the
sorting of the array, so the new displayBars method is provided for you. You are expected to show
knowledge of sorting by using the bar height values and sorting them in ascending order. The
student starter file will show the bars in random order. Your job is to sort the bar heights and then
display the bar sorted from shortest to tallest.
GraphicsLab08 Student Version #1
Do not copy this file, which is provided.
// GraphicsLab08st1.java
// The Graphics Sorter
// This is the student, starting file for the 80 Point Version of Graphics Lab 07.
import java.util.*;
import java.awt.*;
import java.applet.*;
public class GraphicsLab08st1 extends Applet
{
private int numBars;
int barHeight[];
int sortDelay;
int count;
// number of bars to be sorted
// array of bar heights
// delay between comparison iteration
public void init()
{
numBars = 47;
sortDelay = 3000;
barHeight = new int[numBars];
getBarValues();
count = 1;
}
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 1
05-22-13
public void getBarValues()
{
Random rand = new Random(3333);
for (int k = 0; k < numBars; k++)
barHeight[k] = rand.nextInt(591) + 10;
}
// range of 10..600
public void paint(Graphics g)
{
showFrame(g);
displayBars(g);
sortBars(g);
Expo.delay(sortDelay);
showFrame(g);
displayBars(g);
}
public void showFrame(Graphics g)
{
Expo.setColor(g,Expo.white);
Expo.fillRectangle(g,0,0,1000,650);
Expo.setColor(g,Expo.black);
Expo.fillRectangle(g,0,0,1000,15);
Expo.fillRectangle(g,0,0,15,650);
Expo.fillRectangle(g,0,635,1000,650);
Expo.fillRectangle(g,985,0,1000,650);
}
public void displayBars(Graphics g)
{
if (count == 1)
Expo.setColor(g,Expo.red);
else
Expo.setColor(g,Expo.blue);
int x = 25;
int x2 = 35;
int y = 635;
for (int k=0; k<numBars; k++)
{
int y2 = 635 - barHeight[k];
Expo.fillRectangle(g,x,y,x2,y2);
x+=20;
x2+=20;
}
count = 2;
}
public void sortBars(Graphics g)
{
}
private void swap(int x, int y)
{
}
}
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 2
05-22-13
Student, Starting File Current Output
80 Point Version Ending Output
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 3
05-22-13
100 Point Version Specifics
The 100 point version has a different starting file. You are once again sorting, but this time the
sorting process needs to be displayed as it is happening. This cannot be shown in this document.
Your teacher will demonstrate a sample execution.
Do not copy this file, which is provided.
GraphicsLab08 Student Version #2
// GraphicsLab08st2.java
// The Graphics Sorter
// This the student, starting file for the 100 Point Version of Graphics Lab 08.
import java.awt.*;
import java.applet.*;
public class GraphicsLab08st2 extends Applet
{
private int numBars;
// number of bars to be sorted
private int barHeight[]; // array of bar heights
private int sortDelay;
// delay between comparison iteration
public void init()
{
numBars = 47;
sortDelay = 10;
barHeight = new int[numBars];
getBarValues();
}
public void getBarValues()
{
Expo.startSeed(3333);
for (int k = 0; k < numBars; k++)
barHeight[k] = Expo.seedRandom(10,600);
}
public void paint(Graphics g)
{
showFrame(g);
displayRandomBars(g);
sortBars(g);
}
public void showFrame(Graphics g)
{
Expo.setColor(g,Expo.black);
Expo.fillRectangle(g,0,0,1000,20);
Expo.fillRectangle(g,0,630,1000,650);
Expo.fillRectangle(g,0,20,20,630);
Expo.fillRectangle(g,980,20,1000,650);
}
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 4
05-22-13
public void drawBar(Graphics g, int k)
{
int y2 = 630;
int x1 = 35 + k * 20;
int y1 = y2 - barHeight[k];
int x2 = x1 + 10;
Expo.setColor(g,Expo.red);
Expo.fillRectangle(g,x1,y1,x2,y2);
}
public void eraseBar(Graphics g, int k)
{
}
public void displayRandomBars(Graphics g)
{
for (int k = 0; k < numBars; k++)
drawBar(g,k);
}
public void swap(Graphics g, int m, int n)
{
}
public void sortBars(Graphics g)
{
}
}
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 5
05-22-13
The student starting file compiles and has various elements done already. The init method initializes
the variables and constructs the barHeight array. The getBarValues method assigns random values
integers between 10 and 600 to every array element. The showFrame method displays the outline
for the screen. Finally, the displayRandomBars method calls the drawbar method for every array
element to graphically display its value. The result of this is shown below:
Student, Starting File Current Output
This starting output graphically shows the random nature of the array at the start of this program.
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 6
05-22-13
100 Point Version Ending Output
A Bubble Sort needs to compare adjacent bars and swap their locations in the logic of the Bubble sort
algorithm. The picture below shows the completed sort. The command Expo.delay(sortDelay);
method must be used in the inner loop of the Bubble Sort to see the actual sorting process. Without
a delay method the sort will seem instantaneous.
The sortBars method will be a traditional Bubble Sort which will call the swap method. The swap
method is special because it does not just swap the values in the array, it needs to swap the bars as
well. This means erasing the bars for the old position (eraseBar) and redrawing them in the new
position (drawbar).
Exposure Java 2013, PreAPCS Edition
Graphics Lab 08
Page 7
05-22-13
Download