Thursday, December 16, 2010

FINAL EXAM CS36O

FINAL EXAM CS36O
December 16, 2010




import java.awt.Point;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

public class PaintPanel extends JPanel
{
private int pointCount = 0; //count number of points

//array of 10000 java.awt.Point references
private Point[] points = new Point[10000];

//set up GUI and register mouse event handler
public PaintPanel()
{
//handle frame mouse motion event
addMouseMotionListener(

new MouseMotionAdapter() //anonymous inner class
{
//store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if( pointCount < points.length )
{
points[ pointCount ] = event.getPoint(); //find point
pointCount++; //increment number of points in array
repaint(); //repaint JFrame
} //end if
} //end method mouseDragged
} //end anonymous inner class
); //end call to addMouseMotionListener
} //end PaintPanel constructor

//draw ovals in a 4-by-4 bounding box at expecified locations on window
public void paintComponent( Graphics g )
{
super.paintComponent( g ); //clears drawing area

//draw all points in array

for( int i = 0; i < pointCount; i++ )
g.fillOval( points[ i ].x, points[i].y, 4, 4 );
} //end method paintComponent
} //end class PaintPanel







import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Painter
{
public static void main( String[] args )
{
//create JFrame
JFrame application = new JFrame( "A simple paint program." );

PaintPanel paintPanel = new PaintPanel(); //create paint panel
application.add( paintPanel, BorderLayout.CENTER); //in center

//create a label and place it in SOUTH of BorderLayout
application.add( new JLabel( "Drag the mouse to draw" ),
BorderLayout.SOUTH );

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.setSize(400, 200); //set frame size
application.setVisible( true ); //display frame
} //end main
} //end class Painter

Thursday, December 2, 2010

Assignment #20 CS360

Assignment #20
"Wildcard Test"
12-02-10




//WildcardTest.java

//Wildcard test program.

import java.util.ArrayList;

public class WildcardTest

{
public static void main( String[] args )

{
//create, initialize and output ArrayList of integers, then
//display total of the elements

Integer[] integers = { 1, 2, 3, 4, 5 };

ArrayList integerList = new ArrayList ();

//insert elements in intergerList
for( Integer element : integers )
integerList.add( element );

System.out.printf( "integerList contains: %s\n", integerList );
System.out.printf( "Total of the elements in integerList: %.0f\n\n",
sum( integerList ) );

//create, initialize and output ArrayList of Doubles, then
//display total of the elements
Double[] doubles = {1.1, 3.3, 5.5 };
ArrayList< Double > doubleList = new ArrayList< Double >();

//Insert Elements in doubleList
for( Double element : doubles )
doubleList.add( element );

System.out.printf( "doubleList contains:%s\n", doubleList );
System.out.printf( "Total of the elements in doubleList: %.1f\n\n",
sum( doubleList ));

//create, initialize and output ArrayList of Numbers containing
//both Integers and Doubles, then display total of the elements
Number[] numbers = {1, 2.4, 3, 4.1 }; //Integers and Doubles
ArrayList< Number > numberList = new ArrayList< Number >();

//Insert elements in numberList
for( Number element : numbers )
numberList.add( element );

System.out.printf( "numberList contains: %s\n", numberList );
System.out.printf( "Total of the elements in numberList: %.1f\n",
sum( numberList));

}//end main

//total the elements; using a wildcard in the ArrayList parameter
public static double sum( ArrayList< ? extends Number > list )
{
double total = 0; //initialize total

//calculate sum
for( Number element : list )
total += element.doubleValue();

return total;

}//end method sum
}//end class WildcardTest