Professor Kit Romano's Web Site

 



Site Index

About Me

Welcome
Definition of Program
Variable Types
Understanding Memory
Declaring Variables
Simple Commands
Concatenation and Strings
Sleep-Threads-Try Catch
Campus Scenes
Pictures
Java simplified
Application Shell Styles
Reading Data From File
Overview of Applets
Excellent Graphics Applet
Online Reference
Nested if statements
Arrays
Sorting Arrays
Using Strings
Functions
Top 20 Replies 
First program 211
Important Class Example
JAVA in SDK Environment
Understanding OOP
OOP simple example
Recursion example
Example of Inheritance
     and use of JTextArea



*** Web Related **********
Build Student Web Page ?
What is FTP
Download WS ftp95
Download Java Plug In
Applets from jbuilder to sdk
Practice Problems

 

Programming Examples

 

 

                                                

 

Program 2-4

// Number generation gane  by A Thatcher
// uses arrays , random numbers and decision making

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;

public class Application1
{
  public Application1()
  {
   String input;
   String numPick[]=new String[10];
   int luck[]=new int[10];
   int random[]=new int[10];
   int num,temp,yes,x=0,count=0,hitCount=0,missCount=0;

     //user chooses number of integers he wishes to pick
   input=JOptionPane.showInputDialog("How many numbers do you  
            wish to pick (1-10)?");
   num=Integer.parseInt(input);

        if (num>10)       // if num > than 10, user must rechoose
        input=JOptionPane.showInputDialog("How many numbers do you wish to pick
                   (you cannot choose more than 10)?");
        num=Integer.parseInt(input);

      if(num==0)

       {      //if num is 0, user gets fairwell message, application exited

        JOptionPane.showMessageDialog(null, "Thank you.  Pleas play again.");
        System.exit(0);
       }

   temp=num-1;

   do
    {
     count++; // input integers; put chosen integers into string array
     numPick[x]=JOptionPane.showInputDialog("Enter pick # "+count+
          ".  You may pick any number through 25.");

     luck[x]=Integer.parseInt(numPick[x]);       //convert input[](string) to luck[] (int)

         //if choice is greater than 25, user rechooses
      if(luck[x]>=26)
          numPick[x]=JOptionPane.showInputDialog("Enter pick # "+count+
          ".  Make sure your pick does not exceed 25!!");
      luck[x]=Integer.parseInt(numPick[x]);

     random[x]=1+(int) (Math.random()*25);    //choose random integer and put into
                        array

     temp--;
     x++;
    }while(temp>=0);

      temp=num-1;
      x=0;
      do                      //compare input integers to random integers
       {
         yes=0;

         for(int a=0;a<10;a++)
          {
            if(luck[x]==random[a])yes++;
          }

        if(yes>=1)hitCount=hitCount+yes;       //add number luck[x] hit to hitCount
         else missCount++; //add one to missCount if luck[x] didn't hit any of random[]

        temp--;
        x++;
       } while (temp >=0);

   //show number guessed right and wrong
   JOptionPane.showMessageDialog(null,"Your guessed "+hitCount+" right, and "+missCount+" wrong.");
   }

  public static void main (String args[])
  {
   Application1 app=new Application1();
   System.exit(0);
  }

}