INDEX Sr. No. Practical Signature 1 Write a program in J2ME to perform the simple calculator operations such as: a. Addition b. Subtraction c. Multiplication d. Division 2 Write a program in J2ME to create a simple Quiz which content 3 to 4 questions and also display the score. 3 Write a program in J2ME to create a currency converter and also display the result. 4 Write a program in J2ME to generate a calendar. 5 Write a program in J2ME to demonstrate simple animation through snake movement. 6 Write a program in J2ME to create a simple application with an address book with the help of Record Store. 7 Write a program in J2ME to create a simple database application with an address book with the following operations: a. Insert b. Delete c. Update d. Retrieve 8 Program in J2MEto perform the following tasks: a. Create a form1 that contain Label & TextBox,Next Button. b.Create a form2 that contain checkBox, radioButton ,TextArea& Next, Back Button. c.Create a form3 that contains Display of all above forms. 9 Write a ticker program to continuously scroll a message on the mobile screen. PRACTICAL No. 1 AIM:Write a program in J2ME to perform the simple calculator operations such as - a. Addition b. Subtraction c. Multiplication d. Division Program: import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.io.*; public class calculator extends MIDlet implements CommandListener { private Form form; private Display display; private TextField input1, input2; private Command add, sub, mul,div; private StringItem item; public calculator() { } public void startApp() { display = Display.getDisplay(this); Form form = new Form("Calculator"); form.append("Hello everybody"); item = new StringItem("Result", ""); input1 = new TextField("First Number:", "", 30, TextField.NUMERIC); input2 = new TextField("Second Number", "", 30, TextField.NUMERIC); form.append(input1); form.append(input2); add = new Command("Addition", Command.OK, 1); sub = new Command("Subtraction", Command.OK, 1); mul = new Command("Multiplication", Command.OK, 1); div = new Command("Division", Command.OK, 1); form.addCommand(add); form.addCommand(sub); form.addCommand(mul); form.addCommand(div); form.append(item); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean uncondn) { notifyDestroyed(); } private void calculate() { int one=Integer.parseInt(input1.getString()); int two= Integer.parseInt(input2.getString()); int result=one+two; item.setText( result + "" ); } private void calculate1() { int one = Integer.parseInt(input1.getString()); int two = Integer.parseInt(input2.getString()); int result = one - two; item.setText(result + ""); } private void calculate2() { int one = Integer.parseInt(input1.getString()); int two = Integer.parseInt(input2.getString()); int result = one * two; item.setText(result + ""); } private void calculate3() { int one = Integer.parseInt(input1.getString()); int two = Integer.parseInt(input2.getString()); int result = one / two; item.setText(result + ""); } public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if (label.equals("Addition")) { calculate(); } else if (label.equals("Subtraction")) { calculate1(); } else if (label.equals("Multiplication")) { calculate2(); form.append("The Answer is:"); } else if (label.equals("Division")) { calculate3(); form.append("The Answer is:"); } } } Output: PRACTICAL No. 2 Write a program in J2ME to create a simple Quiz which content 3 to 4 questions and also display the score. Program: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class QuizMidlet extends MIDlet implements CommandListener { private Display display; private Form form1,form2,form3,form4,form5; private ChoiceGroup ch1,ch2,ch3,ch4; private Command next; private Command back; private Command ok; private Command exit; private StringItem st; int count=0; public QuizMidlet() { display=Display.getDisplay(this); next=new Command("Next",Command.OK,1); back=new Command("Back",Command.BACK,1); st=new StringItem("Total correct answers","0"); form1=new Form("1.J2ME is?"); ch1=new ChoiceGroup("",Choice.EXCLUSIVE); ch1.append("mobile java", null); ch1.append("java automatic", null); ch1.append("standard java", null); ch1.append("for serverside", null); form1.append(ch1); form1.addCommand(next); form1.setCommandListener(this); form2=new Form("2.What is JAVA?"); ch2=new ChoiceGroup("",Choice.EXCLUSIVE); ch2.append("Object Oriented Program",null); ch2.append("Assembly Language",null); ch2.append("Modeling Language",null); ch2.append("System Software",null); form2.append(ch2); form2.addCommand(next); form2.addCommand(back); form2.setCommandListener(this); form3=new Form("3.Root of 625?"); ch3=new ChoiceGroup("",Choice.EXCLUSIVE); ch3.append("15", null); ch3.append("35", null); ch3.append("45", null); ch3.append("25", null); form3.append(ch3); form3.addCommand(next); form3.addCommand(back); form3.setCommandListener(this); form4=new Form("4.Class is a collection of ?"); ch4=new ChoiceGroup("",Choice.EXCLUSIVE); ch4.append("variables", null); ch4.append("objects ", null); ch4.append("operations", null); ch4.append("interfaces", null); form4.append(ch4); form4.addCommand(next); form4.addCommand(back); form4.setCommandListener(this); form5=new Form("Score"); exit=new Command("Exit",Command.SCREEN,1); ok=new Command("Submit",Command.OK,2); form5.addCommand(ok); form5.addCommand(exit); form5.setCommandListener(this); } public void startApp() { display.setCurrent(form1); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command cmd,Displayable displayable) { if(displayable==form1) { if(cmd==next) display.setCurrent(form2); } else if(displayable==form2) { if(cmd==next) display.setCurrent(form3); else if(cmd==back) display.setCurrent(form1); } else if(displayable==form3) { if(cmd==next) display.setCurrent(form4); else if(cmd==back) display.setCurrent(form2); } else if(displayable==form4) { if(cmd==next) { if(ch1.getSelectedIndex()==0) count++; if(ch2.getSelectedIndex()==0) count++; if(ch3.getSelectedIndex()==3) count++; if(ch4.getSelectedIndex()==1) count++; st.setText(String.valueOf(count)); form5.append(st); display.setCurrent(form5); } } else if(displayable==form5) { if(cmd==ok) { display.setCurrent(form5); } else if(cmd==exit) { destroyApp(false); notifyDestroyed(); } } } } Output: PRACTICAL No. 3 Write a program in J2ME to create a currency converter and also display the result. Program: import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class currency extends MIDlet implements CommandListener { private Display display; private Form Input; private TextField inr; private Command convert; public currency() { display = Display.getDisplay(this); Input = new Form("INR to USD Converter"); inr = new TextField("Enter INR:", null, 10, TextField.DECIMAL); convert = new Command("Convert", Command.SCREEN, 1); Input.append(inr); Input.addCommand(convert); Input.setCommandListener(this); } public void startApp() { /* Display form */ display.setCurrent(Input); } public void pauseApp() { /* pass */ } public void destroyApp(boolean unconditional) { /* pass */ } public void commandAction(Command c, Displayable d) { String text = null; long rs = 0; int paise = 0; long dollor = 0; long cent = 0; String final_dollar = null; if (c == convert) { text = inr.getString(); paise = text.indexOf('.'); if (paise == -1) { rs = Long.parseLong(text) * 100; } else { rs = Long.parseLong(text.substring(0, paise)) * 100; paise = Integer.parseInt(text.substring((paise+1), text.length())); rs = rs + paise; } dollor = rs * 100 / 4470; cent = dollor % 100; dollor = dollor / 100; final_dollar = String.valueOf(dollor) + "." + String.valueOf(cent); Alert alert = new Alert("US Dollor", final_dollar + "$", null,AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } } Output: PRACTICAL No. 4 Write a program in J2ME to generate a calendar. Program: import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import java.util.Date; import java.util.TimeZone; public class calender extends MIDlet { private Form form; private Display display; private DateField calender; private static final int DATE = 0; public calender(){ calender = newDateField("DateIn:",DateField.DATE,TimeZone.getTimeZone("GMT")); } public void startApp() { display = Display.getDisplay(this); Form form = new Form("Calender"); form.append(calender); display.setCurrent(form); } public void pauseApp(){} public void destroyApp(boolean destroy) { notifyDestroyed(); } } Output: PRACTICAL No. 5 Write a program in J2ME to demonstrate simple animation through snake movement. Program: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MySnake extends MIDlet implements CommandListener { DrawSnake ds; Display dis; public MySnake() { dis=Display.getDisplay(this); ds=new DrawSnake(); } public void startApp() { dis.setCurrent(ds); } public void commandAction(Command c,Displayable d) {} public void pauseApp() {} public void destroyApp(boolean unconditional) {} } class DrawSnake extends Canvas implements Runnable, CommandListener { int x,y,dir; Thread th; Command quit; public DrawSnake() { x=0; y=0; dir=0; quit=new Command("QUIT",Command.SCREEN,1); addCommand(quit); setCommandListener(this); th=new Thread(this); th.start(); } public void commandAction(Command c,Displayable d) { if(c==quit){} //notifyDestroyed(); } public void keyPressed(int cd) { switch(getGameAction(cd)) { case Canvas.UP: y-=5; dir=1; break; case Canvas.DOWN: y+=5; dir=2; break; case Canvas.LEFT: x-=5; dir=3; break; case Canvas.RIGHT: x+=5; dir=4; break; } } public void run() { while(true) { if(dir==1) y-=5; else if(dir==2) y+=5; else if(dir==3) x-=5; else if(dir==4) x+=5; repaint(); try { th.sleep(500); } catch (Exception e) { } } } public void paint(Graphics g) { g.setColor(0x1F2F1F); g.fillRect(x, y, 10, 10); g.drawString("TYMCA SEM V",25,25, Graphics.TOP | Graphics.LEFT); } } Output: PRACTICAL No. 6 Write a program in J2ME to create a simple application with an address book with the help of RecordStore. Program: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; public class addressbook extends MIDlet implements CommandListener { private Display disp; private Form frm; private Command add, exit, delete,update,retrived; private TextField txtrecid, txtname,txttelno; private RecordStore rs; String recid, name, telno; public addressbook() { disp = Display.getDisplay(this); exit = new Command("Exit",Command.SCREEN,1); add = new Command("Add",Command.SCREEN,1); delete = new Command("Delete",Command.SCREEN,1); update = new Command("Update",Command.SCREEN,1); retrived = new Command("Retrived",Command.SCREEN,1); try { rs = RecordStore.openRecordStore("recorddemo", true); } catch(Exception e) { e.printStackTrace(); } txtrecid = new TextField("Record Id","" , 32, TextField.ANY); txtname = new TextField("Name","" , 32, TextField.ANY); txttelno = new TextField("Telephone No.","" , 32, TextField.ANY); frm = new Form("Customer Details"); frm.append(txtrecid); frm.append(txtname); frm.append(txttelno); frm.addCommand(exit); frm.addCommand(add); frm.addCommand(delete); frm.addCommand(update); frm.addCommand(retrived); frm.setCommandListener(this); } public void startApp() { disp.setCurrent(frm); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command cmd, Displayable d) { if(cmd==exit) { destroyApp(true); notifyDestroyed(); } else if(cmd==add) { recid = txtrecid.getString(); name = txtname.getString(); telno = txttelno.getString(); String data = (recid+";"+name+";"+telno); System.out.println(data); byte b[] = data.getBytes(); try { int result = rs.addRecord(b, 0, b.length); if(result>=1) { Alert a = new Alert("Add","Record Added",null,AlertType.INFO); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); txtrecid.setString(""); txtname.setString(""); txttelno.setString(""); } else { Alert a = new Alert("Error","Record not Added: ",null,AlertType.ERROR); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); } } catch(Exception e) { e.printStackTrace(); } } else if(cmd==retrived) { try { byte b[] = new byte[rs.getRecordSize(Integer.parseInt(txtrecid.getString()))]; rs.getRecord(Integer.parseInt(txtrecid.getString()),b,0); String data = new String(b); System.out.println(data); recid = data.substring(0, data.indexOf(";")-1); data = data.substring(data.indexOf(";")+1, data.length()); System.out.println(data); name = data.substring(0,data.indexOf(";")); telno = data.substring(data.indexOf(";")+1, data.length()); txtname.setString(name); txttelno.setString(telno); } catch(InvalidRecordIDException e) { Alert a = new Alert("Retrival Error","Record not Found",null,AlertType.ERROR); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); } catch(Exception e) { e.printStackTrace(); } } else if(cmd==delete) { try { rs.deleteRecord(Integer.parseInt(txtrecid.getString())); txtname.setString(""); txttelno.setString(""); txtrecid.setString(""); Alert a = new Alert("Delete","Record Deleted",null,AlertType.INFO); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); } catch(InvalidRecordIDException e) { Alert a = new Alert("Error","No Record to Delete",null,AlertType.ERROR); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); } catch(Exception e) { e.printStackTrace(); } } else if(cmd==update) { try { recid = txtrecid.getString(); name = txtname.getString(); telno = txttelno.getString(); String data = (recid+";"+name+";"+telno); System.out.println(data); byte b[] = data.getBytes(); rs.setRecord(Integer.parseInt(recid), b, 0, b.length); Alert a = new Alert("Update","Record Updated",null,AlertType.INFO); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); txtrecid.setString(""); txtname.setString(""); txttelno.setString(""); } catch(InvalidRecordIDException e) { Alert a = new Alert("Error","No Record to Update",null,AlertType.ERROR); a.setTimeout(Alert.FOREVER); disp.setCurrent(a); } catch(Exception e) { e.printStackTrace(); } } } } Output: PRACTICAL No. 7 Write a program in J2ME to create a simple database application with an address book with the following operations: a. Insert b. Delete c. Update d. Retrieve Program: import java.io.*; importjava.util.*; importjavax.microedition.midlet.*; importjavax.microedition.lcdui.*; import javax.microedition.io.*; public class MySQLConn extends MIDlet implements CommandListener { private String username; private String url="http://localhost:8080/jj/getConnection"; private Display display; private Command exit=new Command("EXIT", Command.EXIT,1); private Command connect=new Command("Connect", Command.SCREEN,1); private Form menu; privateTextField tb1; privateTextField tb2; DBConndb; publicMySQLConn() throws Exception { display=Display.getDisplay(this); } public void startApp() { displayMenu(); } public void displayMenu() { menu=new Form("Connect"); tb1=new TextField("Please input username:","",30,TextField.ANY); tb2=new TextField("Please input password:","",30,TextField.PASSWORD); menu.append(tb1); menu.append(tb2); menu.addCommand(exit); menu.addCommand(connect); menu.setCommandListener(this); display.setCurrent(menu); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command command,Displayable screen) { if(command==exit) { destroyApp(false); notifyDestroyed(); } else if(command==connect) { db=new DBConn(this); db.start(); db.connectDb(tb1.getString(),tb2.getString()); } } public class DBConn implements Runnable { MySQLConnmidlet; private Display display; String db; String user; String pwd; publicDBConn(MySQLConnmidlet) { this.midlet=midlet; display=Display.getDisplay(midlet); } public void start() { Thread t=new Thread(this); t.start(); } public void run() { StringBuffersb=new StringBuffer(); try { HttpConnection c=(HttpConnection) Connector.open(url); c.setRequestProperty("User-Agent","Profile/MIDP-1.0, Configuration/CLDC1.0"); c.setRequestProperty("Content-Language","en-US"); c.setRequestMethod(HttpConnection.POST); DataOutputStreamos=(DataOutputStream)c.openDataOutputStream(); os.writeUTF(user.trim()); os.writeUTF(pwd.trim()); os.flush(); os.close(); DataInputStream is=(DataInputStream)c.openDataInputStream(); intch; sb=new StringBuffer(); while((ch=is.read())!=-1) { sb.append((char)ch); } showAlert(sb.toString()); is.close(); c.close(); } catch(Exception e) { showAlert(e.getMessage()); } } public void connectDb(String user,Stringpwd) { this.user=user; this.pwd=pwd; } private void showAlert(String err) { Alert a=new Alert(""); a.setString(err); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } }; } Servlet Code for JDBC Connection: import java.io.*; importjavax.servlet.*; importjavax.servlet.http.*; import java.sql.*; public class getConnection extends HttpServlet { Connection conn; public void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); DataInputStreamin=newDataInputStream((InputStream)request.getInputStream()); String user=in.readUTF(); String pwd=in.readUTF(); System.out.println("received data is==>"+user+pwd); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn=DriverManager.getConnection("jdbc:odbc:milu"); Statement stmt=conn.createStatement(); inti=stmt.executeUpdate("insert into Login Values('"+user+"','"+pwd+"')"); if(i!=0) { out.println("Hi, Milind, Data has Inserted Successfully"); } else { out.println("iData is Not inserted"); } } catch(Exception e) { e.printStackTrace(); } out.println("Your Name is: "+user+"\n Your Password is:"+pwd); in.close(); out.close(); out.flush(); } public void doget(HttpServletRequestrequest,HttpServletResponse response) throwsServletException, IOException { doPost(request,response); } } Output: PRACTICAL No. 8 Program in J2MEto perform the following tasks: a. Create a form1 that contain Label &TextBox, Next Button. b. Create a form2 that contain checkBox, radioButton ,TextArea& Next, Back Button. c. Create a form3 that contains Display of all above forms. Program: import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.io.*; public class Commander extends MIDlet implements CommandListener { private CanvasDemo cd; public Commander() { } Display display = null; Form f1 = null; Form f2 = null; Form f3 = null; private ChoiceGroup gender; private int currentIndex; private int genderIndex; private ChoiceGroup hobbies; private int hobbiesIndex; private TextField tbName, tbInstitute, tbPhone, tbCel, tbCity, tbArea; public Ticker ticName; Command firstFormCommand = new Command("1st Form", "Go to First Form", Command.SCREEN, 0); Command secondFormCommand = new Command("2nd Form", "Go to Second Form", Command.SCREEN, 0); Command thirdFormCommand = new Command("3rd Form", "Go to Third Form", Command.SCREEN, 0); Command exitCommand = new Command("Exit", Command.EXIT, 1); Command backCommand1 = new Command("Back1", Command.BACK, 1); Command backCommand2 = new Command("Back2", Command.BACK, 1); public void startApp() { cd=new CanvasDemo(this); display = Display.getDisplay(this); f1=new Form("Form 1"); f1.addCommand(secondFormCommand); f1.addCommand(exitCommand); f1.setCommandListener(this); tbName=new TextField("Enter Your Name : ","",20,TextField.ANY); tbInstitute=new TextField("Name of Institute : ","",20,TextField.ANY); tbPhone=new TextField("Phone No. : ","",20,TextField.ANY); tbCel=new TextField("Mobile No. : ","",20,TextField.ANY); tbCity=new TextField("City : ","",20,TextField.ANY); ticName=new Ticker("Ankita Baraskar"); f1.append(tbName); f1.append(tbInstitute); f1.append(tbPhone); f1.append(tbCel); f1.append(tbCity); f1.setTicker(ticName); f2=new Form("Form 2"); f2.addCommand(thirdFormCommand);
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-