Thursday, September 30, 2010

Fibonacci Calculator & Fibonacci Test




/* Fibonacci Calculator*/

public class FibonacciCalculator
{
//reverse 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)
);
}
}

------------------------------------------------

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

Factorial Calculator & Factorial Test





/* Factorial Calculator */

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 factorials 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));
}
}

-----------------------------

/* Factorial Test*/

public class FactorialTest
{
//calculate factorials of 0 - 10

public static void main( String args[])
{
FactorialCalculator factorialCalculator=new FactorialCalculator();
factorialCalculator.displayFactorials();
}
}

Thursday, September 16, 2010

Assignment # 7 LinearSearchTest



/* 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 sequentionally
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 input 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 from 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 +
" was 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 Label Frame & Label Test



/*
Assignment 6 Gabriel Cuevas
*/

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 );
}
}

SECOND PART

/*
Assignment 6 Main Function Gabriel Cuevas
*/

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 AverageCarFuel.java










/* 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 Addition.java




//Assignment 4 Addition.java


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) ;
}
}

Assignment #3 Shapes






/* Assignment 3 */

//Shapes.java
//Demostrates drawing different shapes.
import java.awt.Graphics;
import javax.swing.JPanel;

public class Shapes extends JPanel
{
private int choice; //user's choice of which shape to draw

//constructor sets the user's choice
public Shapes(int userChoice)
{
choice = userChoice;
} //end Shapes constructor

// draws a cascade of shapes starting from the top-left corner
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for ( int i = 0; i < 10; i++)
{
//pick the shape based on the user's choice
switch (choice)
{
case 1: //draw rectangles
g.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case 2: //draw ovals
g.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
} // end switch
} // end for
} // end method paintCompnent
} //end class Shapes

----------------------------------------------------------------------------


/*

Test for Shapes

*/

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShapesTest
{
public static void main ( String args[])
{
//obtain user's choice

String input = JOptionPane.showInputDialog(
"Enter 1 to draw rectangles\n"+
"Enter 2 to draw ovals");

int choice=Integer.parseInt( input ); //converts input to int

//create the panel with the user's input
Shapes panel = new Shapes(choice);

JFrame application = new JFrame(); // creates new JFrame

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300,300);
application.setVisible(true);
}
}

Assignment #2 - ArrayOnFrame



/*
Assignment 2
*/

import javax.swing.*;

public class ArrayOnFrame
{

private static void createAndShowGUI()
{
int[]anArray;

anArray = new int[10];

anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;

JFrame frame = new JFrame("ArrayOnFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("element at index0:" + anArray[0] );
frame.getContentPane().add(label);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

Assignment #1 Hello World


/*
Assignment 1 HelloWorldSwing
*/
/*
*HelloWorldSwing.java required no other files.
*/
import javax.swing.*;

public class HelloWorldSwing {
/**
*Create the GUI and show it.
*/
private static void createAndShowGUI () {
//Create and set up the window.
JFrame frame = new JFrame ("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add the ubiquitous " Hello World" label.
JLabel label = new JLabel("Hello Gabriel");
frame.getContentPane().add(label);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main (String[] args) {
//Schedule a job for the event-displacing thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI ();
}
});
}
}