tag:blogger.com,1999:blog-18646242.post-1131124308363388472005-11-04T08:54:00.000-08:002005-11-04T09:11:48.373-08:00Java Math TutorOne 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.<br />/*<br /> * Created on Oct 29, 2005<br /> *<br /> * TODO To change the template for this generated file go to<br /> * Window - Preferences - Java - Code Style - Code Templates<br /> */<br />package name.shabda.cat;<br /><br />/**<br /> * @author shabda<br /> *<br /> * TODO To change the template for this generated type comment go to<br /> * Window - Preferences - Java - Code Style - Code Templates<br /> */<br />public class Cat {<br /> final int sums=10;//The number of questions asked.<br /> 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.<br /> int hardness=1;//how hard do you want the questions. Heigher values give harder questions.<br /> public int[][] doMultiply(){//returns a array containing arithmatic problems on multiplication.<br /> //Like for 2+3=5, arr[0][0]=2,arr[0][1]=3,arr[0][2]=5.<br /> int[][] arr=new int[sums][x];<br /> for(int i=0;i<sums;i++){<br /> int howHard=(int) Math.pow(10,hardness);<br /> arr[i][0]=(int) (Math.random()*100*howHard);<br /> arr[i][1]=(int) (Math.random()*100*howHard);<br /> arr[i][2]=arr[i][0]*arr[i][1];<br /> }<br /> return arr;<br /> }<br /> public double[][] doDivide(){//returns a array containing arithmatic problems on Division.<br /> int howHard=(int) Math.pow(10,hardness);<br /> double[][] arr=new double[sums][x];<br /> for(int i=0;i<sums;i++){<br /> arr[i][0]=(int) (Math.random()*100*howHard);<br /> arr[i][1]=(int) (Math.random()*100*howHard);<br /> arr[i][2]=arr[i][0]/arr[i][1];<br /> }<br /> return arr;<br /> }<br /> public int[][] doSum(){//returns a array containing arithmatic problems on addition.<br /> int howHard=(int) Math.pow(10,(hardness*2));<br /> int[][] arr=new int[sums][x];<br /> for(int i=0;i<sums;i++){<br /> arr[i][0]=(int) (Math.random()*100*howHard);<br /> arr[i][1]=(int) (Math.random()*100*howHard);<br /> arr[i][2]=arr[i][0]+arr[i][1];<br /> }<br /> return arr;<br /> }<br /> public int[][] doSub(){//returns a array containing arithmatic problems on subtraction.<br /> int howHard=(int) Math.pow(10,(hardness*2));<br /> int[][] arr=new int[sums][x];<br /> for(int i=0;i<sums;i++){<br /> arr[i][0]=(int) (Math.random()*100*howHard);<br /> arr[i][1]=(int) (Math.random()*100*howHard);<br /> arr[i][2]=arr[i][0]-arr[i][1];<br /> }<br /> return arr;<br /> }<br /> public String stringMult(){//Lets convert the array to a nice string.<br /> String data="";<br /> int[][] arr=doMultiply();<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"*"+arr[i][1];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> for(int i=0;i<5;i++){<br /> data+="\n";<br /> }<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"*"+arr[i][1]+"="+arr[i][2];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> return data;<br /> }<br /> public String stringDiv(){//Lets convert the array to a nice string.<br /> String data="";<br /> double[][] arr=doDivide();<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"/"+arr[i][1];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> for(int i=0;i<5;i++){<br /> data+="\n";<br /> }<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"/"+arr[i][1]+"="+arr[i][2];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> return data;<br /> }<br /> public String stringSub(){//Lets convert the array to a nice string.<br /> String data="";<br /> int[][] arr=doSub();<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"-"+arr[i][1];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> for(int i=0;i<5;i++){<br /> data+="\n";<br /> }<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"-"+arr[i][1]+"="+arr[i][2];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> return data;<br /> }<br /> public String stringAdd(){//Lets convert the array to a nice string.<br /> String data="";<br /> int[][] arr=doSum();<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"+"+arr[i][1];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> for(int i=0;i<5;i++){<br /> data+="\n";<br /> }<br /> for(int i=0;i<sums;i++){<br /> String temp=""+arr[i][0]+"+"+arr[i][1]+"="+arr[i][2];<br /> data+=temp;<br /> data+="\n";<br /> }<br /> return data;<br /> }<br /> public static void main(String[] args){//Just a check.<br /> Cat app=new Cat();<br /> System.out.println("Div");<br /> System.out.println(app.stringDiv());<br /> System.out.println("Add");<br /> System.out.println(app.stringAdd());<br /> System.out.println("Sub");<br /> System.out.println(app.stringSub());<br /> }<br /><br />}<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18646242-113112430836338847?l=bytecodes.blogspot.com'/></div>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.com0