//Here is my titration applet. Eric Mechalke //This program plots a tritration curve for a strong acid(HCl) being titrated with a strong base(NaOH). // Two arrays are used to hold pH vs. volume data. import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class titration extends Applet implements ActionListener { TextField textV=new TextField(" "); //Volume of HCl TextField textM=new TextField(" "); //molarity of HCL TextField textB=new TextField(" "); //molarity of NaOH Label labelY=new Label("Volume of HCl"); Label labelA=new Label("Molarity of HCl"); Label labelB=new Label("Molarity of NaOH"); Button mybutton=new Button("Titrate"); double V, M,BM; int beginX, beginY, endX, endY; double mypH[]=new double [100]; //Holds calculated pH values for plot double myVol[]=new double[100]; //Holds actual volume, in liters,for each point in the titrations(for molarity calc). boolean paintIt=false; double endpointx1, endpointx2, endpointy1, endpointy2; public void init() { textV.setText("25"); textM.setText("0.1"); textB.setText("0.1"); add(labelY); add(textV); add(labelA); add(textM); add(labelB); add(textB); add(mybutton); mybutton.addActionListener(this); } //==================================================================================================== public void fillArray() { String textRead; String myV, myM, myC,myB; double molA, molB, H_remaining, H_molarity, Fb, Vb, Vl; String backtoString; myV=textV.getText(); //Gets the volume of acid to be titrated myM=textM.getText(); //Gets the molarity of the acid to be titrated myB=textM.getText(); //Gets the molarity of the titrant(base). V=Double.parseDouble(myV); M=Double.parseDouble(myM); BM=Double.parseDouble(myB); Vl=V/1000; //Constant Initial volume in liter molA=(Vl*M); // Constant value Initial moles of H+ to titrate molB=1.2*molA; //Constant value. Amount of base needed to titrate 120% Vb=molB/BM; //Constant value. Volume of base to be added System.out.println("********************************************************************"); for (int i=0; i<100; i++) { myVol[i]=Vl+Vb*((double)i/100.0); // Initial volume + fraction of total volume of base to add Fb=molB*((double)i/100.0); // Fb is the fraction of moles base added H_remaining=molA-Fb; // H+ remaining after adding OH- if (H_remaining > 0) // Before equivalance point pH is calculate from pH= -log[H+] { H_molarity= H_remaining/myVol[i]; // molarity, [H+], calc. mypH[i]=-Math.log(H_molarity)/Math.log(10); //System.out.println("pH ="+ mypH[i] + " here is the volume = "+ myVol[i]); } else { // After equivalance point we are finding OH- molarity H_molarity= -H_remaining/myVol[i]; mypH[i]=14.0 + Math.log(H_molarity)/Math.log(10); // pH=pKw-pOH // System.out.println("PH ="+ mypH[i] + " here is the volume = "+ myVol[i]); } } } //============================================================================================================= public void paint(Graphics screen) { beginX=50; beginY=50; endX=500; endY=300; setSize(600, 400); int x1=0; int y1=0; int x2=200; int y2=200; screen.setColor(Color.blue); screen.drawRect(beginX, beginY, endX, endY); //****************************** //draw the xy chart: screen.setColor(Color.black); //****************************** screen.drawString("pH",10, 200); int j=13; //labels pH 13 to 1 String jbacktoString; for (int i=21; i<=294; i=i+21) //d { jbacktoString = Integer.toString(j); screen.drawString(jbacktoString,35, 55+i); j=j-1; } screen.drawString("Volume NaOH Titrant",250, 395); //****************************** //*************************** double x,y,ml, transX, transY, oldTransX, oldTransY; transX=0; transY=0; oldTransX=0; oldTransY=0; screen.setColor(Color.blue); for (int yy=21; yy<294; yy=yy+21) screen.drawLine(50,(50+yy), 550, (50+yy)); // pH lines y-axis if(paintIt==true) { for (int i=0; i<100; i++) //Loop to read array { //x=0; transX =50.0+(500*((double)i/100.0)); transY =350.0 -(21.0*mypH[i])-5; screen.setColor(Color.red); if(oldTransX == 0.0) { System.out.println("No line to print with first data point"); oldTransX=transX; oldTransY=transY; } else { screen.drawLine((int)oldTransX, (int)oldTransY, (int) transX, (int) transY); oldTransX=transX; oldTransY=transY; } } screen.setColor(Color.black); screen.drawLine((int)endpointx1, (int)endpointy1, (int)endpointx2, (int)endpointy2); x=43.0+(500*(83.5/100.0)); ml=(V*M)/BM; jbacktoString = Double.toString(ml); screen.drawString(jbacktoString, (int)x , 375); screen.drawString("end point.", (int)x , 395); paintIt=false; } } //================================================================================================================== public void actionPerformed(ActionEvent event) { String mycaption=event.getActionCommand(); fillArray(); if (mycaption.equals("Titrate")) { String myV, myM, myB; double V, M, Vl, molB=0, molA=0, V_endpt; myV=textV.getText(); //Gets the volume of acid to be titrated myM=textM.getText(); //Gets the molarity of the acid to be titrated myB=textB.getText(); //Gets the molarity of the titrant(base). V=Double.parseDouble(myV); M=Double.parseDouble(myM); BM=Double.parseDouble(myB); Vl=V/1000; //Constant Initial volume in liter molB = molA; //Constant value. Amount of base needed to titrate 120% V_endpt=molB/BM; //Constant value. Volume of base to be added System.out.println("the endpoint volume = "+V_endpt); endpointx1=50.0+(500*(83.5/100.0));; endpointy1= 345; endpointx2=50.0+(500*(83.5/100.0)); //endpointx2=50.0+(500*((double)V_endpt/100.0)); endpointy2=355; paintIt=true; repaint(); //calls the paint portion of our program to happen } } }