tag:blogger.com,1999:blog-18646242.post-1131127186021749142005-11-04T09:24:00.000-08:002005-11-04T09:59:46.050-08:00Java Math Tutor.<sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i=""><sums i="">/*<br />* Created on Oct 29, 2005<br />*/<br />One of my friends was preparing for cat, which has quantitative tests. So he<br /> needed to work on his calculations. I wrote this for him. It gives arithmatic<br /> problems and their answers. You may set the hardness. Again I hope I have commented<br /> it well enough to understand the code.<br />package name.shabda.cat;<br /><br />/**<br />* @author shabda<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 />}<br />And we need a class to display the problems. So here we go.<br />//CatGui<br />/*<br /> * Created on Nov 3, 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 />import java.awt.Dimension;<br />import java.awt.FlowLayout;<br />import java.awt.event.ActionEvent;<br />import java.awt.event.ActionListener;<br />import java.awt.event.WindowAdapter;<br />import java.awt.event.WindowEvent;<br /><br />import javax.swing.BoxLayout;<br />import javax.swing.JButton;<br />import javax.swing.JFrame;<br />import javax.swing.JMenu;<br />import javax.swing.JMenuBar;<br />import javax.swing.JMenuItem;<br />import javax.swing.JPanel;<br />import javax.swing.JTextArea;<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 CatGui {<br /> Cat engine;<br /> JButton add,sub,mult,div;<br /> JTextArea result;<br /> JPanel buttons,display;<br /> JMenuBar theMenu;<br /> JMenuItem easy=new JMenuItem("easy");<br /> JMenuItem hard=new JMenuItem("hard");<br /> JMenuItem harder=new JMenuItem("harder");<br /> /**<br /> *<br /> */<br /> public CatGui() {<br /> engine=new Cat();<br /> add=new JButton("add");<br /> sub=new JButton("sub");<br /> mult=new JButton("mult");<br /> div=new JButton("div");<br /> buttons=new JPanel();<br /> buttons.setLayout(new FlowLayout());<br /> buttons.add(add);<br /> buttons.add(sub);<br /> buttons.add(mult);<br /> buttons.add(div);<br /> result=new JTextArea(26,20);<br /> result.setPreferredSize(new Dimension(200,300));<br /> result.setEditable(false);<br /> display=new JPanel();<br /> display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));<br /> display.add(result);<br /> display.add(buttons); <br /> addActionListeners();<br /> theMenu=new JMenuBar();<br /> JMenu hardness=new JMenu("how hard?");<br /> hardness.add(easy);<br /> hardness.add(hard);<br /> hardness.add(harder);<br /> theMenu.add(hardness);<br /> <br /> }<br /> public void addActionListeners(){<br /> add.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> result.setText(engine.stringAdd());<br /> <br /> }});<br /> sub.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> result.setText(engine.stringSub());<br /> <br /> }});<br /> mult.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> result.setText(engine.stringMult());<br /> <br /> }});<br /> div.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> result.setText(engine.stringDiv());<br /> <br /> }});<br /> easy.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> engine.hardness=1;<br /> <br /> }});<br /> hard.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> engine.hardness=2;<br /> <br /> }});<br /> harder.addActionListener(new ActionListener(){<br /><br /> public void actionPerformed(ActionEvent arg0) {<br /> engine.hardness=3;<br /> <br /> }});<br /> }<br /> public static void main(String[] args){<br /> CatGui theGui=new CatGui();<br /> JFrame f=new JFrame();<br /> f.getContentPane().add(theGui.display);<br /> f.setJMenuBar(theGui.theMenu);<br /> f.pack();<br /> f.setVisible(true);<br /> f.addWindowListener(new WindowAdapter(){<br /> public void windowClosing(WindowEvent arg0) {<br /> System.exit(0);<br /> }<br /> });<br /> <br /> }<br /><br />}<br /><br /></sums></sums></sums></sums></sums></sums></sums></sums></sums></sums></sums></sums>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.com