Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Coding Competition

800445Dec 4 2008 — edited Dec 5 2008
Hey everyone,

This is the link to a coding competition I just entered for my college. I just thought I'd post the link and see if anyone else felt like whipping something up, I'm interested to see where other people decide to go with this.
Here's my submission:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class GoodbyeGUI implements ActionListener, Runnable{
public JPanel goodbyePanel;
public JTextArea goodbyeTextArea;
public JFrame container;
public JButton button;
public MyCanvas canvas;
public GoodbyeReader reader = null;
public String finalFileName, finalText = null;
public int counter = 0;
public TimerThread thread;
public ArrayList<String> strings = new ArrayList<String>();
public ArrayList<Runnable> threads = new ArrayList<Runnable>();
public BufferedWriter finalWriter;
public GoodbyeGUI(String fileName)
{
finalFileName = fileName;
new Thread(this).run();
}
public void run()
{
goodbyePanel = new JPanel();
goodbyePanel.setLayout(new GridLayout(1, 2));
goodbyePanel.setPreferredSize(new Dimension(500, 165));
goodbyeTextArea = new JTextArea();
goodbyeTextArea.setEditable(false);
goodbyeTextArea.setLineWrap(true);
goodbyePanel.add(goodbyeTextArea);
button = new JButton("Next Step");
button.addActionListener(this);
goodbyePanel.add(button);
canvas = new MyCanvas();
goodbyePanel.add(canvas);
canvas.setBackground(Color.WHITE);
container = new JFrame("Goodbye World!");
container.setContentPane(goodbyePanel);
canvas.setPreferredSize(new Dimension(500, 165));
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setResizable(false); 
container.pack(); 
container.setVisible(true);
Scanner scan = null;
reader = new GoodbyeReader("goodbyeWorld.txt");
thread = new TimerThread();
threads.add(thread);
new Thread(thread).run();
}
public void actionPerformed(ActionEvent e)
{
if(finalText == null)
reader.readFile();
else if(counter < finalText.length())
{
JOptionPane.showMessageDialog(null,
"Reading next letter from log file.");
if(counter <=5)
canvas.myString+=finalText.charAt(counter);
else
canvas.otherString+=finalText.charAt(counter);
canvas.paint(canvas.getGraphics());
counter++;
}
else
{
thread.time = false;
try{Thread.sleep(1000);}catch(Exception ee){}
JOptionPane.showMessageDialog(null, 
"Operation completed. Total time: "+thread.getSecs()+" seconds.");
}
}
public class GoodbyeReader implements Runnable {
public String fileName;
ArrayList<Integer> count = new ArrayList<Integer>();
BufferedWriter writer = null;
public GoodbyeReader(String fileName)
{
this.fileName = fileName;
new Thread(this).run();
}
public void run()
{
try{
writer = new BufferedWriter(new FileWriter(new File(finalFileName)));
JOptionPane.showMessageDialog(null, "Reading and displaying contents of original file \""+fileName+"\".");
String text = new Scanner(new File(fileName)).nextLine();
goodbyeTextArea.setText(text);
}
catch(Exception e){};
}
public void readFile()
{
Scanner scan = null;
JOptionPane.showMessageDialog(null, "Attempting to decipher file \""+fileName+"\".");
try{
scan = new Scanner(new File(fileName));
}
catch(Exception e){};
for(int i = 0; i < count.size(); i++)
strings.add(scan.findInLine("[\\w\\s]"));
try{
String toWrite = scan.findInLine("[\\w\\s!]");
strings.add(toWrite);
JOptionPane.showMessageDialog(null, "Found next letter: "+toWrite);
Thread.sleep(1000);
CheckerThread thread = new CheckerThread(toWrite, count.size());
threads.add(thread);
new Thread(thread).run();
if(thread.isRight())
writer.write(toWrite);
}
catch(Exception e){e.printStackTrace();};
count.add(1);
if(!scan.hasNext())
try{
writer.close();
JOptionPane.showMessageDialog(null, "All letters written to log file!!");
JOptionPane.showMessageDialog(null, "Attempting to read and display contents of log file.");
File finalFile = new File(finalFileName);
scan = new Scanner(finalFile);
finalText = scan.nextLine();
finalFile.setReadOnly();
finalWriter = new BufferedWriter(new FileWriter(finalFile));
}
catch(Exception e){e.printStackTrace();System.out.println("Oops, a segfault.");};
}
}
public class TimerThread implements Runnable{
public ArrayList<Integer> millis = new ArrayList<Integer>();
public int counter = 0;
public boolean time = true;
public void run()
{
while(time)
{
try{Thread.sleep(1);}catch(Exception e){};
counter++;
millis.add(counter);
}
}
public int getSecs()
{
int secs = 0;
for(int i: millis)
if(i%1000 == 0)
secs++;
return secs;
}
}
public class CheckerThread implements Runnable{
public String check;
public int index;
public String[] correct = {"H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"};
boolean isRight = true;
public CheckerThread(String check, int index)
{
this.check = check;
this.index = index;
}
public void run()
{
isRight = correct[index].equals(check);
if(isRight)
System.out.println(check+ " is the next letter in the sentence.");
else
System.out.println("Error! "+check+" is not the next letter!");
}
public boolean isRight()
{
return isRight;
}
}
public class MyCanvas extends Canvas{
public String myString = "", otherString = "";
public MyCanvas()
{
super();
}
public void paint(Graphics g)
{
if(!"".equals(myString))
{
setFont(g.getFont().deriveFont(50F));
g.drawString(myString, 10, 50);
if(!"".equals(otherString))
g.drawString(otherString, 10, 100);
}
try{
Thread.sleep(1000);}catch(Exception e){};
}
public void update(Graphics g)
{
paint(g);
}
}
public static void main(String[] args)
{
new GoodbyeGUI("helloWorld.txt");
}
}
Which reads from this file:
H----*)(*$)%*#$(*^($%*)^*#)%^*#)%$*$)%#--e----@#*$(@#*$(@*%^$@%^@*$%@#$%@#--l--#$%&*()^&*^%$#@$^&%^-l-----@#$%@%^&$%^&(^&(*)*()^&$@%%@##$---o---@#@$%%^^&(^&&@#@#@%$#$%--- ----@#$@%#%^$&$%&^*$^&----W-)())*(%$$#%$^$--o---#$%@#$^#$^&*$^*())++++---r-@#$%@#$^%#$^%@#%$#%#$%&^^&^^--l-#@$%$^*&(&^#@@#$@-d----@#$%@#$%@#$%@$#%^^^%$--------!
(Before you criticize it, look at the point of the contest ;) )

Hope to see some other ideas!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2009
Added on Dec 4 2008
20 comments
617 views