Friday, November 04, 2005

Java Math Tutor

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.
/*
* Created on Oct 29, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package name.shabda.cat;

/**
* @author shabda
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
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 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 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 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 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 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 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 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 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 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 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 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 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());
}

}

0 Comments:

Post a Comment

<< Home