Saturday, September 22, 2007

Scribe's Scratches



Well there's the slideshow, didn't upload like it was on the smart software and I tried in 5 different formats...
If you want some more practice I attempted to make an applet in JAVA. I must say it's one of the ugliest computer programs I've ever made and it was a heck of a time making it work online (even though it is still sketchy even now seems to work all the time on ei6 and from time to time completely kill firefox). For any computer programmer people out there who look at my code I know it could have been done better with arrays but I remembered that JAVA arrays were kinda funky compared to VB so I didn't bother re-reading that part of my book and just avoided it. Here is the program (hopefully) the code is under it for anyone else who wants to compile and use it (but I doubt it). I'll try to make a smoother, better looking, more complete(more angles) and more stable program some other time. I didn't this time because there are just too many little things that went wrong. Spent an hour debugging and the bug was a single equals sign. ugh.





import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;


public class TrigQGenerator extends Applet{

Boolean firstQuestionAsked = false;

Color bgColor;

URL path;

//for future versions w/ images
MediaTracker mTracker;

Button submit;
Button squareRootButton;
Button newQuestion;

TextField answerArea;

Label questionDisplay;
Label answerDisplay;

Label rightLabel;
Label wrongLabel;


int right, wrong;
int rand;

String question = "Questions will appear here";
String answer;
String textToDisplay;

final String answer1 = "1/2";
final String answer2 = "sqr(2)/2";
final String answer3 = "sqr(3)/2";
final String answer4 = "1/sqr(3)";
final String answer5 = "1";
final String answer6 = "sqr(3)";
final String squareRoot = "sqr(";

final String question1 = "What is the sine of pi/6?";
final String question2 = "What is the sine of pi/4?";
final String question3 = "What is the sine of pi/3?";

final String question6 = "What is the cosine of pi/6?";
final String question5 = "What is the cosine of pi/4?";
final String question4 = "What is the cosine of pi/3?";

final String question7 = "What is the tangeant of pi/6?";
final String question8 = "What is the tangeant of pi/4?";
final String question9 = "What is the tangeant of pi/3?";

String[] answers;
String[] questions;

public void init(){
mTracker = new MediaTracker(this);

setLayout(null);

submit = new Button("Submit");
submit.setBounds(20,100,100,40);
submit.addActionListener(new ButtonSubmitListener());

squareRootButton = new Button("Square Root");
squareRootButton.setBounds(120, 100, 100, 40);
squareRootButton.addActionListener(new SquareRootButtonListener());

newQuestion = new Button("New Question");
newQuestion.setBounds(220,100,100,40);
newQuestion.addActionListener(new ButtonNewQuestionListener());

rightLabel= new Label("Right: 0");
rightLabel.setBounds(20,60,100,40);
rightLabel.setBackground(Color.green);

wrongLabel = new Label("Wrong: 0");
wrongLabel.setBounds(220,60,100,40);
wrongLabel.setBackground(Color.red);

add(squareRootButton);
add(submit);
add(rightLabel);
add(wrongLabel);
add(newQuestion);

answerArea = new TextField("Answer Here");
answerArea.setBounds(120, 60, 100, 40);
add(answerArea);

questionDisplay = new Label("Questions will appear here");
questionDisplay.setBounds(20,20,300,40);
add(questionDisplay);



try {
//gets the path the applet is in
path = getDocumentBase();
}catch (Exception e) {
}

bgColor = Color.black;
setBackground(bgColor);

//this TC block waits for everything to be loaded
try {
mTracker.waitForAll();
}catch (InterruptedException e) {
}
}
public void stop(){

}
public void paint(Graphics g){

}
public void generateQuestion(){
String currentQuestion = questionDisplay.getText();
while (currentQuestion.equals(question)){
rand = (int)(Math.random()*9);

if (rand == 1) {
question = question1;
answer = answer1;
}
if (rand == 2) {
question = question2;
answer = answer2;
}
if (rand == 3) {
question = question3;
answer = answer3;
}
if (rand == 4) {
question = question4;
answer = answer1;
}
if (rand == 5) {
question = question5;
answer = answer2;
}
if (rand == 6) {
question = question6;
answer = answer3;
}
if (rand == 7) {
question = question7;
answer = answer4;
}
if (rand == 8) {
question = question8;
answer = answer5;
}
if (rand == 9) {
question = question9;
answer = answer6;
}
}
displayQuestion();
}

class SquareRootButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event){
String currentText = answerArea.getText();
currentText = currentText + squareRoot;
answerArea.setText(currentText);
}
}
class ButtonSubmitListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (questionDisplay.getText() == "Questions will appear here") {
} else {
if (answer.equals(answerArea.getText())){
rightAnswer();
} else {
wrongAnswer();
}
}
}
}
class ButtonNewQuestionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
firstQuestionAsked = true;
generateQuestion();
answerArea.setText("");
}
}
public void displayQuestion(){
questionDisplay.setText(question);

}
public void rightAnswer(){
right++;

textToDisplay = Integer.toString(right);
textToDisplay = "Right: " + textToDisplay;
rightLabel.setText( textToDisplay);
}
public void wrongAnswer(){
wrong++;
textToDisplay = Integer.toString(wrong);
textToDisplay = "Wrong: " + textToDisplay;
wrongLabel.setText(textToDisplay);
}
}

THE NEXT SCRIBE IS TIMMY!

2 comments:

«Craig» said...

LOL haha, nice try Graeme... I take it by the enormous amount of coding you almost had it... Darn, now it just takes up a heck of a lot of space on the blog...

Grey-M said...

lol man that's not enourmous. This program is takes up 60kb, that game me and Miles made last year was 35Mb.