Friday, November 04, 2005

Java Math Tutor.

/*
* Created on Oct 29, 2005
*/
One of my friends was preparing for cat, which has quantitative tests. So he
needed to work on his calculations. I wrote this for him. It gives arithmatic
problems and their answers. You may set the hardness. Again I hope I have commented
it well enough to understand the code.
package name.shabda.cat;

/**
* @author shabda
*/
public class Cat {
final int sums=10;//The number of questions asked.
final int x=3;//Humm. Why 3? Coz for basic maths operations we need 3 values like 2+3=5.1st 2,2nd 3, third 5.
int hardness=1;//how hard do you want the questions. Heigher values give harder questions.
public int[][] doMultiply(){//returns a array containing arithmatic problems on multiplication.
//Like for 2+3=5, arr[0][0]=2,arr[0][1]=3,arr[0][2]=5.
int[][] arr=new int[sums][x];
for(int i=0;i<sums;i++){
int howHard=(int) Math.pow(10,hardness);
arr[i][0]=(int) (Math.random()*100*howHard);
arr[i][1]=(int) (Math.random()*100*howHard);
arr[i][2]=arr[i][0]*arr[i][1];
}
return arr;
}
public double[][] doDivide(){//returns a array containing arithmatic problems on Division.
int howHard=(int) Math.pow(10,hardness);
double[][] arr=new double[sums][x];
for(int i=0;i<sums;i++){
arr[i][0]=(int) (Math.random()*100*howHard);
arr[i][1]=(int) (Math.random()*100*howHard);
arr[i][2]=arr[i][0]/arr[i][1];
}
return arr;
}
public int[][] doSum(){//returns a array containing arithmatic problems on addition.
int howHard=(int) Math.pow(10,(hardness*2));
int[][] arr=new int[sums][x];
for(int i=0;i<sums;i++){
arr[i][0]=(int) (Math.random()*100*howHard);
arr[i][1]=(int) (Math.random()*100*howHard);
arr[i][2]=arr[i][0]+arr[i][1];
}
return arr;
}
public int[][] doSub(){//returns a array containing arithmatic problems on subtraction.
int howHard=(int) Math.pow(10,(hardness*2));
int[][] arr=new int[sums][x];
for(int i=0;i<sums;i++){
arr[i][0]=(int) (Math.random()*100*howHard);
arr[i][1]=(int) (Math.random()*100*howHard);
arr[i][2]=arr[i][0]-arr[i][1];
}
return arr;
}
public String stringMult(){//Lets convert the array to a nice string.
String data="";
int[][] arr=doMultiply();
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"*"+arr[i][1];
data+=temp;
data+="\n";
}
for(int i=0;i<5;i++){
data+="\n";
}
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"*"+arr[i][1]+"="+arr[i][2];
data+=temp;
data+="\n";
}
return data;
}
public String stringDiv(){//Lets convert the array to a nice string.
String data="";
double[][] arr=doDivide();
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"/"+arr[i][1];
data+=temp;
data+="\n";
}
for(int i=0;i<5;i++){
data+="\n";
}
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"/"+arr[i][1]+"="+arr[i][2];
data+=temp;
data+="\n";
}
return data;
}
public String stringSub(){//Lets convert the array to a nice string.
String data="";
int[][] arr=doSub();
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"-"+arr[i][1];
data+=temp;
data+="\n";
}
for(int i=0;i<5;i++){
data+="\n";
}
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"-"+arr[i][1]+"="+arr[i][2];
data+=temp;
data+="\n";
}
return data;
}
public String stringAdd(){//Lets convert the array to a nice string.
String data="";
int[][] arr=doSum();
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"+"+arr[i][1];
data+=temp;
data+="\n";
}
for(int i=0;i<5;i++){
data+="\n";
}
for(int i=0;i<sums;i++){
String temp=""+arr[i][0]+"+"+arr[i][1]+"="+arr[i][2];
data+=temp;
data+="\n";
}
return data;
}
public static void main(String[] args){//Just a check.
Cat app=new Cat();
System.out.println("Div");
System.out.println(app.stringDiv());
System.out.println("Add");
System.out.println(app.stringAdd());
System.out.println("Sub");
System.out.println(app.stringSub());
}

}
And we need a class to display the problems. So here we go.
//CatGui
/*
* Created on Nov 3, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package name.shabda.cat;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
* @author shabda
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CatGui {
Cat engine;
JButton add,sub,mult,div;
JTextArea result;
JPanel buttons,display;
JMenuBar theMenu;
JMenuItem easy=new JMenuItem("easy");
JMenuItem hard=new JMenuItem("hard");
JMenuItem harder=new JMenuItem("harder");
/**
*
*/
public CatGui() {
engine=new Cat();
add=new JButton("add");
sub=new JButton("sub");
mult=new JButton("mult");
div=new JButton("div");
buttons=new JPanel();
buttons.setLayout(new FlowLayout());
buttons.add(add);
buttons.add(sub);
buttons.add(mult);
buttons.add(div);
result=new JTextArea(26,20);
result.setPreferredSize(new Dimension(200,300));
result.setEditable(false);
display=new JPanel();
display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
display.add(result);
display.add(buttons);
addActionListeners();
theMenu=new JMenuBar();
JMenu hardness=new JMenu("how hard?");
hardness.add(easy);
hardness.add(hard);
hardness.add(harder);
theMenu.add(hardness);

}
public void addActionListeners(){
add.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
result.setText(engine.stringAdd());

}});
sub.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
result.setText(engine.stringSub());

}});
mult.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
result.setText(engine.stringMult());

}});
div.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
result.setText(engine.stringDiv());

}});
easy.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
engine.hardness=1;

}});
hard.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
engine.hardness=2;

}});
harder.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
engine.hardness=3;

}});
}
public static void main(String[] args){
CatGui theGui=new CatGui();
JFrame f=new JFrame();
f.getContentPane().add(theGui.display);
f.setJMenuBar(theGui.theMenu);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});

}

}

0 Comments:

Post a Comment

<< Home