Friday, April 29, 2011

lab15

//seriously change it a little so teach doesent eat me
import java.awt.*;
import java.applet.*;
public class Lab15 extends Applet
{
 private int numBars;
 int barHeight[];
 int sortDelay;
 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);
  clearRedBars(g);
  sortBars(g);
 }
 public void showFrame(Graphics g)
 {
  g.fillRect(0,0,1300,20);
  g.fillRect(0,0,20,1000);
  g.fillRect(0,880,1000,20);
  g.fillRect(980,0,20,880);
 }
 public void displayRandomBars(Graphics g)
 {
  g.setColor(Color.red);
  for(int k = 0; (k+1) <= 47; k++)
  {
   g.fillRect(30+(k*20),barHeight[k],10,(880 - barHeight[k]));
  }
 }
 public void sortBars(Graphics g)
 {
  int b;
  g.setColor(Color.blue);
  for(int k = 0; k < 47; k++)
  {
   Expo.delay(20);
   for(int d = 0; d < 46; d++)
   {
    if(barHeight[d] < barHeight[d+1])
    {
    b = barHeight[d];
    barHeight[d] = barHeight[d+1];
    barHeight[d+1] = b;
    }
    g.fillRect(30+(k*20),barHeight[k],10,(880 - barHeight[k]));
   }

  }
 }
 public void clearRedBars(Graphics g)
 {
  g.setColor(Color.white);
  for(int k = 0; (k+1) <= 47; k++)
  {
   Expo.delay(40);
   g.fillRect(30+(k*20),barHeight[k],10,(880 - barHeight[k]));
  }
 }
}

Friday, April 22, 2011

Palindrome Finder

CHANGE THE CODE A LITTLE SO I DONT GET IN TROUBLE
public class Lab14
{
 public static void main(String args[])
 {
  System.out.println("100 POINT VERSION,");
  boolean finished = false;
  do
  {
   System.out.println("===============================================================");
   System.out.print("Enter a string ===>> ");
   Palindrome p = new Palindrome(Expo.enterString());
   p.displayData();
   System.out.print("Do you wish to repeat this program [Y/N]? ===>> ");
   char repeat = Expo.enterChar();
   finished = (repeat != 'Y' && repeat != 'y');
   System.out.println();
  }
  while (!finished);
 }
}
class Palindrome
{
 private String str;
 private String str2 = "";
 private int d = 0;
 public Palindrome(String s)
 {
  str = s;
 }
 public boolean isPal()
 {
  int s = str.length() - 1;
  int k = (s/2);
  for (int i = s; i >= 0; i--)
  {
   str2 += str.charAt(i);
  }
  if(str.equals(str2)){
   d = 1;
   return true;
  }
  else{
  return false;
  }
 }
 public boolean almostPal()
 {
  purge();
  if(d == 1){
   return false;
  }
  else if(str.equals(str2)){
   return true;
  }
  else{
  return false;
  }
 }
 public String purge()
 {
  str = str.replace(" ","");
  str2 = str2.replace(" ","");
  str = str.replace(",","");
  str2 = str2.replace(",","");
  str = str.replace("'","");
  str2 = str2.replace("'","");
  str = str.replace(".","");
  str2 = str2.replace(".","");
  str = str.replace("!","");
  str2 = str2.replace("!","");
  str = str.toUpperCase();
  str2 = str2.toUpperCase();
  return "";
 }
 public void displayData()
 {
  System.out.println();
  System.out.println("String:        " + str);
  System.out.println("Palindrome:      " + isPal());
  System.out.println("Almost Palindrome:    " + almostPal());
  System.out.println("===============================================================");
  System.out.println();
  d = 0;
 }
}