Thursday, September 30, 2010

Assignment #10 CS360

Assignment #10
"Fibonacci Calculator & Fibonacci Test"
09-30-10





/*

Fig 15.5
9/18/07
*/

public class FibonacciCalculator
{
//recursive declaration of method Fibonacci
public long fibonacci( long number )
{
if (( number == 0 ) || (number == 1 )) //base cases
return number;
else //recursion step
return fibonacci( number - 1) + fibonacci( number - 2 );
}

public void displayFibonacci()
{
for (int counter = 0; counter <= 10; counter++)
System.out.printf( "Fibonacci of %d is %d\n", counter, fibonacci( counter ) );
}
}


~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|


/*
Fig 15.6
9/18/07
*/

public class FibonacciTest
{
public static void main( String args[] )
{
FibonacciCalculator fibonacciCalculator = new FibonacciCalculator();
fibonacciCalculator.displayFibonacci();
}
}

Assignment #9 CS360

Assignment #9
"Factorial Calculator & Factorial Test"
09-30-10




/*

*/

public class FactorialCalculator
{
//recursive declaration on method factorial
public long factorial( long number )
{
if (number <= 1 ) //test for base case
return 1; //base cases: 0!=1 and 1!=1

else //recursion step
return number * factorial(number - 1);
}

//output factoriasl for values 0-10
public void displayFactorials()
{
//calculate factorials of 0 through 10
for (int counter = 0; counter <= 10; counter++)
System.out.printf( "%d! = %d\n", counter, factorial(counter ));
}
}



~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|



/* Fig 15.4
9/18/07
*/

public class FactorialTest
{
//calculate factorials of 0-10
public static void main( String args[] )
{
FactorialCalculator factorialCalculator = new FactorialCalculator();
factorialCalculator.displayFactorials();
}
}

Thursday, September 23, 2010

Assignment #8 CS360

Assignment #8
"Binary Array & Binary Search Test"
09-23-10






/*
Assignment #8
*/

import java.util.Random;
import java.util.Arrays;

public class BinaryArray
{
private int[] data; //array of any value
private static Random generator = new Random();

//create array of any size and fill with random int
public BinaryArray( int size)
{
data = new int[ size ]; //crate space for array

//fill array with int 10-99
for(int i=0; i data [i] = 10 + generator.nextInt( 90 );

Arrays.sort( data );
}

//perform binary search in data
public int binarySearch( int searchElement )
{
int low = 0; //low end of search area
int high = data.length - 1; //high end of search area
int middle = (low + high + 1)/2; //middle element
int location = -1; //return value; -1 if not found

do //loop to search element
{
//print remaining elements of array
System.out.print( remainingElements( low, high ) );

//output spaces for alignment
for(int i=0; i System.out.print( "" );
System.out.println( "*" ); //indicate current middle

//if the element is found at the middle
if(searchElement == data[ middle ])
location = middle; //location is the current middle

//middle element is too high
else if(searchElement high = middle - 1; // eliminate the higher half
else //middle element too low
low = middle + 1; //eliminate the lower half

middle = (low + high + 1)/2; //recalculate the middle
}

while( ( low <= high ) && (location == -1) );

return location; //return location of search key

}

//method to output certain values in array
public String remainingElements( int low, int high )

{

StringBuilder temporary = new StringBuilder();

//output spaces for alignment
for(int i=0; i<=high; i++)
temporary.append("");

//output element left in array
for(int i=low; i<=high; i++)
temporary.append( data[ i ] + "");

temporary.append( "\n");
return temporary.toString();
}

//method to output values in array
public String toString()
{
return remainingElements( 0,data.length - 1);
}
}




~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|




import java.util.Scanner;

public class BinarySearchTest
{
public static void main(String args[] )
{
//create Scanner object to imput data
Scanner input = new Scanner( System.in );

int searchInt; //Search key
int position; //location of search key in array

//create array and output it
BinaryArray searchArray = new BinaryArray( 15 );
System.out.println( searchArray );

//get input from user
System.out.print( "Please enter an integer value (-1 to quit): ");
searchInt = input.nextInt(); //read an int from user
System.out.println();

//repeadtedly ubput an integer; -1 terminates the program
while(searchInt != -1)
{
//use binary search to try to find integer
position = searchArray.binarySearch( searchInt );

//return value of -1 indicates integer was not found
if(position == -1)
System.out.println( "The integer" + searchInt + "was not found.\n");

else
System.out.println( "The integer" + searchInt + "was found in position"
+ position + ".\n" );

//get input from user
System.out.print( "Please enter an integer value (-1 to quit): ");
searchInt = input.nextInt(); //read an int from user
System.out.println();
}
}
}

Thursday, September 16, 2010

Assignment #7 CS360

Assignment #7
"Linear Array & Linear Search Test"
09-16-10







/*

LinearArray
*/

import java.util.Random;

public class LinearArray
{
private int[] data; //array of values
private static Random generator = new Random();

//Create array of any size and fill with random int
public LinearArray( int size)
{
data = new int[ size ]; //creates spaces for array

//fills array with ints 10-99
for (int i=0; i data[i] = 10 + generator.nextInt(90);
}

//perform a linear search on the data
public int linearSearch( int searchKey )
{
//loop thourgh array sequentionaly
for (int index=0; index if( data[ index ] == searchKey)
return index; //return inex int
return -1; //integer not found
}

//method to output values in array
public String toString()
{
StringBuilder temporary = new StringBuilder();

//iterate through array
for (int element : data)
temporary.append( element+"");

temporary.append("\n"); //add endline
return temporary.toString();
}
}



~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|~|



/*
LinearSearchTest
*/

import java.util.Scanner;

public class LinearSearchTest
{
public static void main( String args[] )
{
//create scanner object to put data
Scanner input = new Scanner( System.in );

int searchInt; //search key
int position; //location of search key in array

//create array and output it
LinearArray searchArray = new LinearArray( 10 );
System.out.println( searchArray ); //print array

//get input form user
System.out.print( "Please enter an integer value (-1 to quit):");
searchInt = input.nextInt(); //read first int from user

//repeatedly input an integer: -1 terminates the program
while (searchInt != -1 )
{
//perform linear search
position = searchArray.linearSearch( searchInt);

if(position == -1) //integer was not found
System.out.println( "The Integer " + searchInt +" is not found.\n");

//get input from user
System.out.print( "Please enter an integer value (-1 to quit):");
searchInt = input.nextInt(); //read next int from user
}
}
}

Thursday, September 9, 2010

Assignment #6 CS360

Assignment #6
"Label Test"
09-02-10




/*
Assignment 6
*/

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class LabelFrame extends JFrame
{
private JLabel label1; //Just for text
private JLabel label2; //Constructed with text and icon
private JLabel label3; // with added text and icon

public LabelFrame()
{
super( "Testing JLabel" );
setLayout (new FlowLayout() );

//JLabel constructor with string argument
label1 = new JLabel( "Label with text" );
label1.setToolTipText( "This is label1" );
add( label1);

//JLabel constructor with string, Icon and alignment arguments
Icon bug = new ImageIcon( getClass().getResource( "bug1.jpg" ) );
label2 = new JLabel( "Label with text and icon", bug,
SwingConstants.LEFT);
label2.setToolTipText( "This is label2" );
add( label2 );

//JLabel constructor with no arguments
label3 = new JLabel();
label3.setText( "Label with icon and text at bottom" );
label3.setIcon( bug );
label3.setHorizontalTextPosition( SwingConstants.CENTER);
label3.setVerticalTextPosition( SwingConstants.BOTTOM);
label3.setToolTipText( "This is label3" );
add( label3 );
}
}





/*
Assignment 6 Main Function
*/

import javax.swing.JFrame;

public class LabelTest
{
public static void main( String args[] )
{
LabelFrame labelFrame = new LabelFrame() ; //creates LabelFrame
labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
labelFrame.setSize( 275, 180 );
labelFrame.setVisible( true );
}
}

Assignment #5 CS360

Assignment #5
"Average Car Fuel"
09-02-10




/*
Assignment 5
*/

import javax.swing.JOptionPane;

public class AverageCarFuel
{
public static void main( String args[] )
{
String milesTravled =
JOptionPane.showInputDialog( "Enter number of miles traveled" );
String gallonsUsed =
JOptionPane.showInputDialog( "Enter gallons used" );

double miles = Integer.parseInt( milesTravled );
double gallons = Integer.parseInt( gallonsUsed);

//gac = gas average for car
double gac = miles / gallons;

JOptionPane.showMessageDialog( null, "Your gas average is " + gac,
"Your Gas Average", JOptionPane.PLAIN_MESSAGE);
}
}

Thursday, September 2, 2010

Assignment #4.1 CS360

Assignment #4.1
"Average"
09-02-10



//Assignment 5


import javax.swing.JOptionPane;

public class Average
{
public static void main(String args[] )
{
//obtain user input from JOptionPaneinput dialogs
String firstNumber =
JOptionPane.showInputDialog( "Enter first interger" ) ;
String secondNumber =
JOptionPane.showInputDialog( "Enter second integer" ) ;
String thirdNumber =
JOptionPane.showInputDialog( "Enter third integer" ) ;

//convert string into int values
int number1 = Integer.parseInt( firstNumber ) ;
int number2 = Integer.parseInt( secondNumber ) ;
int number3 = Integer.parseInt( thirdNumber ) ;

int ave = (number1 + number2 + number3) / 3 ;

//display results in JOptionPane mssg dialog
JOptionPane.showMessageDialog( null, "The average is " + ave,
"Average of Three Integers", JOptionPane.PLAIN_MESSAGE) ;
}
}

Assignment #4 CS360

Assignment #4 CS360
"Addition"
09-02-10




//Assignment 4


import javax.swing.JOptionPane;

public class Addition
{
public static void main(String args[] )
{
//obtain user input from JOptionPaneinput dialogs
String firstNumber =
JOptionPane.showInputDialog( "Enter first interger" ) ;
String secondNumber =
JOptionPane.showInputDialog( "Enter second integer" ) ;

//convert string into int values
int number1 = Integer.parseInt( firstNumber ) ;
int number2 = Integer.parseInt( secondNumber ) ;

int sum = number1 + number2;

//display results in JOptionPane mssg dialog
JOptionPane.showMessageDialog( null, "The sum is " + sum,
"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE) ;
}
}