20
Create a MIDP application to select movies from a given list using ChoiceGroup c lass. Also include commands Exit and Process - according to the choice. soln: import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class CheckBoxExample extends MIDlet implements CommandListener { private Display display; private Form form; private Command exit, process; private ChoiceGroup movies; private int index; Alert alert; public CheckBoxExample() { form = new Form("Movies"); movies= new ChoiceGroup("Select Movies of your choice", Choice.MULTIPLE); exit = new Command("Exit", Command.EXIT, 1); process = new Command("Process", Command.SCREEN, 2); } public void startApp() { display = Display.getDisplay(this); movies.append("300", null); movies.append("Beautiful Mind", null); movies.append("Forest Gump", null); movies.append("Wal to Remember", null); index = form.append(movies); form.addCommand(exit); form.addCommand(process); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp(){} public void destroyApp(boolean unconditional){ notifyDestroyed(); } public void commandAction(Command c, Displayable displayable){ String label = c.getLabel(); if (label.equals("Exit")){ destroyApp(false); }else { StringItem message[] = new StringItem[movies.size()]; boolean get[] = new boolean[movies.size()]; movies.getSelectedFlags(get); for (int i = 0; i < get.length; i++) { if (get[i]) { message[i] = new StringItem("Your Choice is: ", movies.getString(i)); form.append(message[i]);

PMRC

Embed Size (px)

DESCRIPTION

PMRC

Citation preview

Page 1: PMRC

Create a MIDP application to select movies from a given list using ChoiceGroup class. Also include commands �Exit� and �Process�- according to the choice.soln:import javax.microedition.lcdui.*;import javax.microedition.midlet.*; public class CheckBoxExample extends MIDlet implements CommandListener {private Display display;private Form form;private Command exit, process;private ChoiceGroup movies;private int index;Alert alert; public CheckBoxExample() {form = new Form("Movies");movies= new ChoiceGroup("Select Movies of your choice", Choice.MULTIPLE);exit = new Command("Exit", Command.EXIT, 1);process = new Command("Process", Command.SCREEN, 2);} public void startApp() {display = Display.getDisplay(this);movies.append("300", null);movies.append("Beautiful Mind", null);movies.append("Forest Gump", null);movies.append("Wal to Remember", null); index = form.append(movies);form.addCommand(exit);form.addCommand(process);form.setCommandListener(this);display.setCurrent(form);} public void pauseApp(){} public void destroyApp(boolean unconditional){notifyDestroyed();} public void commandAction(Command c, Displayable displayable){String label = c.getLabel(); if (label.equals("Exit")){destroyApp(false);}else {StringItem message[] = new StringItem[movies.size()]; boolean get[] = new boolean[movies.size()]; movies.getSelectedFlags(get); for (int i = 0; i < get.length; i++) { if (get[i]) { message[i] = new StringItem("Your Choice is: ", movies.getString(i)); form.append(message[i]);

Page 2: PMRC

} } form.delete(index); form.removeCommand(process); } }} ---------------------------------------------------------------------------------------------------------------- 2)Create a MIDP application to select movies from a given list using List class. Also include commands � Exit � and �Process� soln:import javax.microedition.midlet.*;import javax.microedition.lcdui.*; public class listmovieexclusive extends MIDlet implements CommandListener{Display d;List list;Alert alert; public Command Exit,Process; public listmovieexclusive(){list=new List("movies",Choice.IMPLICIT);}public void startApp(){d=Display.getDisplay(this);Exit=new Command("Exit",Command.EXIT,1);Process=new Command("Show",Command.SCREEN,0);list.append("swades",null);list.append("HARRY POTTER",null);list.append("CSI",null);list.addCommand(Exit);list.addCommand(Process);list.setCommandListener(this); d.setCurrent(list); } public void pauseApp(){}public void destroyApp(boolean b){} public void commandAction(Command c , Displayable d){if(c==Exit){

Page 3: PMRC

notifyDestroyed();destroyApp(true);}else{Display d1=Display.getDisplay(this); String selection = list.getString(list.getSelectedIndex()); alert = new Alert("Option Selected", selection, null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); d1.setCurrent(alert); }} } ----------------------------------------------------------------------------------------------------------------- 3)Create a MIDlet with simple alert UI component containing an image(The image file name is f.png which is to be placed in the resource folder) soln:import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*; public class alert extends MIDlet { private Display display; public Form form; private Image img; public alert() { form = new Form(" Alert"); try{ img = Image.createImage("/1.png"); }catch(Exception e){ System.out.println(e.getMessage()); } } public void startApp() { display = Display.getDisplay(this); Alert success = new Alert("OK", "Alert Message Here!", img, AlertType.INFO); success.setTimeout(Alert.FOREVER); success.setImage(img); display.setCurrent(form); display.setCurrent(success);

Page 4: PMRC

} public void pauseApp() {} public void destroyApp(boolean unconditional) { notifyDestroyed(); } } --------------------------------------------------------------------------------------------------------------- 4. Create a MIDlet which shows a list of students. Show the selected student name on the same screen. Add commands �Show� and �Back�( use IMPLICIT list) soln:import javax.microedition.midlet.*;import javax.microedition.lcdui.*; public class Student extends MIDlet implements CommandListener{ private Display display; private StudentList student; private List list; private Command show; public Student() { student= new StudentList(); list = new List("Student List", Choice.IMPLICIT); show = new Command("Show", Command.OK, 1); list.append("Anusmita", null); list.append("Neelam", null); list.append("Sandeep", null); list.append("Suman", null); list.append("Saurabh", null); list.addCommand(show); display = Display.getDisplay(this); } public void startApp(){ list.setCommandListener(this); display.setCurrent(list); } public void pauseApp() {} public void destroyApp(boolean unconditional){ notifyDestroyed(); }

Page 5: PMRC

public void commandAction(Command c, Displayable d){ String label = c.getLabel(); if(label.equals("Show")){ display.setCurrent(student); } } class StudentList extends Canvas implements CommandListener{ private Command back; public StudentList(){ back = new Command ( "Back", Command.BACK, 1); addCommand (back); setCommandListener (this); } public void paint(Graphics g){ int width = getWidth(); int height = getHeight(); String str = list.getString(list.getSelectedIndex()); g.setColor(0, 0, 255); g.drawString(str, width/2, height/2, Graphics.HCENTER | Graphics.TOP); } public void commandAction(Command c, Displayable d){ String label = c.getLabel(); if(label.equals("Back")){ display.setCurrent(list); } } }} ----------------------------------------------------------------------------------------------------------- 5)Create a MIDlet showing list of chocolates available in a shop using multiple choice list. Show the list of selected chocolates. Add commands �Exit�, �Show� soln:import javax.microedition.midlet.*;import javax.microedition.lcdui.*; public class Chocolate extends MIDlet implements CommandListener{Display d;List list;Alert alert; public Command Exit,Show; public Chocolate(){list=new List("Chocolate",Choice.MULTIPLE);}public void startApp(){d=Display.getDisplay(this);

Page 6: PMRC

Exit=new Command("Exit",Command.EXIT,1);Show=new Command("Show",Command.SCREEN,0);list.append("Twix",null);list.append("Diary Milk",null);list.append("Polo",null);list.addCommand(Exit);list.addCommand(Show);list.setCommandListener(this); d.setCurrent(list); } public void pauseApp(){}public void destroyApp(boolean b){} public void commandAction(Command command, Displayable Displayable) { if (command == Show) { boolean choice[] = new boolean[list.size()]; StringBuffer message = new StringBuffer(); list.getSelectedFlags(choice); for (int i = 0; i < choice.length; i++) { if (choice[i]) { message.append(list.getString(i)); message.append(" . "); } } Alert alert = new Alert("Choice", message.toString(), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); d.setCurrent(alert);}else{notifyDestroyed();destroyApp(true);} }} ----------------------------------------------------------------------------------------------------------------6)Create a MIDlet that displays a downloading bar on mobile screen. Add a string �do not close the window, downloading continues�. Change the string to � download complete� upon completion(use ItemStateListener) import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*; public class ga extends MIDlet implements CommandListener{

Page 7: PMRC

private Display display; private Form fmMain; private Command cmExit; private Command cmStop; private Gauge gaProgress; private Timer tm; private DownloadTimer tt; public ga() { display = Display.getDisplay(this); gaProgress = new Gauge("Download Progress", false, 20, 1); cmExit = new Command("Exit", Command.EXIT, 1); cmStop = new Command("Stop", Command.STOP, 1); fmMain = new Form(""); fmMain.append(gaProgress); fmMain.addCommand(cmStop); fmMain.setCommandListener(this); } public void startApp() { display.setCurrent(fmMain); tm = new Timer(); tt = new DownloadTimer(); tm.scheduleAtFixedRate(tt, 0, 1000); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == cmExit) { destroyApp(false); notifyDestroyed(); } else if (c == cmStop) { tm.cancel(); fmMain.removeCommand(cmStop); fmMain.addCommand(cmExit); gaProgress.setLabel("Download Cancelled!"); } } private class DownloadTimer extends TimerTask { public final void run() { if (gaProgress.getValue() < gaProgress.getMaxValue()) gaProgress.setValue(gaProgress.getValue() + 1); else { fmMain.removeCommand(cmStop);

Page 8: PMRC

fmMain.addCommand(cmExit); gaProgress.setLabel("Download Complete!"); cancel(); } } }} ---------------------------------------------------------------------------------------------------------- 7)Write a MIDP application to draw the diagram using Canvas import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Student extends MIDlet { private Display display; private MyCanvas canvas; public Student() { display = Display.getDisplay(this); } protected void startApp() { canvas=new MyCanvas(); display.setCurrent(canvas); }

Page 9: PMRC

protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } } class MyCanvas extends Canvas { public void paint(Graphics g) { int x=100; int y=100; g.setColor(0x000000); g.drawRect(x,y,50,50); g.setColor(0xff0000); g.fillTriangle(125,100,100,150,150,150); g.setColor(0xffff00); g.drawArc(110,120,30,30,0,360); }} --------------------------------------------------------------------------------------------------------- 8)Write a MIDP application that draws a rectangle with dotted line and draws triangles inside the rectangle.soln:import javax.microedition.midlet.*;

Page 10: PMRC

import javax.microedition.lcdui.*; public class Rectangle extends MIDlet{ private Display display; public void startApp(){ Canvas canvas = new CanvasRectangle(); display = Display.getDisplay(this); display.setCurrent(canvas); } public void pauseApp(){} public void destroyApp(boolean unconditional){}} class CanvasRectangle extends Canvas { public void paint(Graphics g) { int width = getWidth(); int height = getHeight(); int x= width/2; int y=height/2; g.setStrokeStyle(Graphics.DOTTED); g.drawRect(x-50,y-25,100,50); g.setStrokeStyle(Graphics.SOLID); g.drawLine(x-50,y,x+25,y-25); g.drawLine(x+25,y-25,x+50,y+25); g.drawLine(x+50,y+25,x-50,y); }} --------------------------------------------------------------------------------------------------------------9)Create a MIDlet for banking. Accept userid and password on form1, validates them and goes to form2 where you can have options � deposit� and �withdraw�. According to the selection made go to from3 and show the amount in your account(CustomItem) import javax.microedition.midlet.*;import javax.microedition.lcdui.*; public class bankval extends MIDlet implements CommandListener { Form f,f1;private Display display;private TextField userid,pass,acc,money,opt;private Command ok,exit,submit,cancel;private Alert a1;int count;

Page 11: PMRC

public bankval() { display=Display.getDisplay(this); f=new Form("welcome to banking"); userid=new TextField("username","",30,TextField.ANY); pass=new TextField("password","",10,TextField.PASSWORD); ok=new Command("ok",Command.OK,1); exit=new Command("exit",Command.EXIT,2); f.addCommand(ok); f.addCommand(exit); f.append(userid); f.append(pass); f.setCommandListener(this); } public void startApp() {display.setCurrent(f); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c,Displayable d) { int flag=0;String msg="submitted";a1=new Alert(msg);String label=c.getLabel();if(label.equals("ok")) { if(userid.getString().equals("")||pass.getString().equals("")) { msg=("plzz fill in the details"); a1.setString(msg); display.setCurrent(a1); }

Page 12: PMRC

else { for(int i=0;i<userid.size();i++) { if((userid.getString().charAt(i) == '@' )) { flag++; } } if(flag!=1) { msg="submit failed.username must contain only 1 @"; a1.setString(msg); display.setCurrent(a1); } if(pass.size() != 10) { msg="password length error"; a1.setString(msg); display.setCurrent(a1); } else{ int num=0; String pwd=pass.getString(); for(int i=0;i<pass.size();i++) { char c1=pwd.charAt(i); int cno=(int)c1; if(cno>=48&&cno<=57) {num++; } } if(num<4) {msg="submit failed.please chk password..minimum 4 numbers needed"; a1.setString(msg); display.setCurrent(a1); } } if(msg.equals("submitted")) {

Page 13: PMRC

display=Display.getDisplay(this); f1=new Form("processing"); acc=new TextField("account balance","",5,TextField.NUMERIC); opt=new TextField("Choose W/D (W:Withdraw or D: Deposit)","",2,TextField.INITIAL_CAPS_SENTENCE); money=new TextField("money that you want to deposit/withdraw deping on the above option","",5,TextField.NUMERIC); submit=new Command("submit",Command.OK,1); cancel=new Command("cancel",Command.CANCEL,1); f1.addCommand(submit); f1.addCommand(cancel); f1.append(acc); f1.append(money); f1.append(opt);count=1; f1.setCommandListener(this); display.setCurrent(f1); } } } if(count==1) {label=c.getLabel(); if(label.equals("submit")) { int t3=Integer.parseInt(acc.getString()); int t4=Integer.parseInt(money.getString()); String trans=opt.getString(); if(trans.equals("W") && ((t3-t4) >0)) { acc.setString("" +(t3-t4)); display=Display.getDisplay(this); Form f2=new Form("Result"); f2.append("Account Balance after Transaction is" +Integer.parseInt(acc.getString())); display.setCurrent(f2); } else{display=Display.getDisplay(this); Form f2=new Form("Result"); f2.append("SORRY... not enough balance"); display.setCurrent(f2); } if(trans.equals("D")) { acc.setString("" +(t3+t4)); display=Display.getDisplay(this); Form f2=new Form("Result"); f2.append("Account Balance after Transaction is" +Integer.parseInt(acc.getString())); display.setCurrent(f2); } }

Page 14: PMRC

if(label.equals("cancel")) { notifyDestroyed(); } } if(label.equals("exit")) { notifyDestroyed(); } } } --------------------------------------------------------------------------------------------------------------10) Write a MIDlet that moves characters �Welcome� on a mobile screen trhough arrow keys from keypads using GameCanvas class. soln: import javax.microedition.midlet.*;import javax.microedition.lcdui.*;/// first version - key pressed is captured/*class cankey extends Canvas{int x=getWidth()/2;int y=getHeight()/2; cankey() {} public void paint(Graphics g) { g.setColor(0xffffff); g.fillRect(0,0,getWidth(),getHeight()); g.setColor(0x00ff00); g.drawString("Welcome",x,y,Graphics.TOP|Graphics.LEFT); } protected void keyPressed(int k)

Page 15: PMRC

{ switch(k){ case -1: y-=25; break; case -2: y+=25; break; case -3: x-=25; break; case -4: x+=25; break;} repaint(); } }*/////Second version - where game action keys are capturedclass cankey extends Canvas{int x=getWidth()/2;int y=getHeight()/2; cankey() {} public void paint(Graphics g) { g.setColor(0xffffff); g.fillRect(0,0,getWidth(),getHeight()); g.setColor(0x00ff00); g.drawString("Welcome",x,y,Graphics.TOP|Graphics.LEFT); } protected void keyPressed(int k){ switch(getGameAction(k)){ case UP: y-=25; break;case DOWN: y+=25;

Page 16: PMRC

break;case LEFT: x-=25; break;case RIGHT: x+=25; break;} repaint(); } } public class lowkey extends MIDlet { Displayable d; public void startApp(){ d=new cankey(); Display.getDisplay(this).setCurrent(d);} public void pauseApp() {} public void destroyApp(boolean unconditional){} } --------------------------------------------------------------------------------------------------------------- 11) Add records of two employees to the RecordStore( name and email together, eg: [email protected]; name is abc and email [email protected]) Extract the names of the employees and display on the mobile screen. soln:import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.rms.*;public class rms extends MIDlet implements CommandListener{private Display display; private Command exitCommand;public rms() { display=Display.getDisplay(this);exitCommand=new Command("exit",Command.SCREEN,1);}public void startApp(){TextBox t=new TextBox("Name and email",null,295,TextField.ANY);

Page 17: PMRC

RecordStore rs=null;byte[] nameEmail1=null;byte[] nameEmail2=null;try { rs=RecordStore.openRecordStore("RMS",true);}catch(RecordStoreException ex) { ex.printStackTrace();}try { nameEmail1="[email protected]".getBytes(); nameEmail2="[email protected]".getBytes(); rs.addRecord(nameEmail1,0,nameEmail1.length); rs.addRecord(nameEmail2,0,nameEmail2.length);rs.closeRecordStore();}catch(Exception e) {}String result1=new String(nameEmail1);int position1=result1.indexOf('@');result1="Name:"+result1.substring(0,position1)+"\n"+"E-mail"+result1.substring(0,result1.length());String result2=new String(nameEmail2);int position2=result2.indexOf('@');result2="Name:"+result2.substring(0,position2)+"\n"+"E-mail"+result2.substring(0,result2.length());String result=result1+result2; t.setString(result);t.addCommand(exitCommand);t.setCommandListener(this);display.setCurrent(t);}public void pauseApp() {}public void destroyApp(boolean unconditional) {}public void commandAction(Command c,Displayable s){ if(c==exitCommand){destroyApp(false); notifyDestroyed();}}} --------------------------------------------------------------------------------------------------------------12. Create a MIDlet too draw : a) Smiling and crying face toggleb) Blinking eyes soltn 4 blinking eyes:-import javax.microedition.midlet.*;import javax.microedition.lcdui.*; class Can extends Canvas{int x,y;int cnt=0; public Can() { x=100; y=100; }public void paint(Graphics g) { if(cnt==2) cnt=1; else cnt++;

Page 18: PMRC

g.setColor(255,0,0);g.drawArc(x,y,100,100,0,360);g.drawArc(x+20,y+50,60,30,0,-180); if(cnt==1){ g.setColor(0x000000); g.fillArc(x+20,y+20,20,20,0,360);g.fillArc(x+60,y+20,20,20,0,360); } if(cnt==2){ g.setColor(0xff0000); g.fillArc(x+20,y+20,20,20,0,360);g.fillArc(x+60,y+20,20,20,0,360); }}}public class conc extends MIDlet { Can ca; public void startApp() { ca=new Can(); Display.getDisplay(this).setCurrent(ca); try{Thread t=new Thread(); while(true) {t.sleep(500); ca.repaint(); } }catch(Exception e){} } public void pauseApp() { } public void destroyApp(boolean n) { } } --------------------------------------------------------------------------------------------------------------

Page 19: PMRC

13.SIES (All alphabets should keep changing colors )soltn:-import javax.microedition.midlet.*;import javax.microedition.lcdui.*; class can1 extends Canvas{ int i; can1() { i=0; }public void paint(Graphics g) { g.setColor(0,0,0); Font f=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE); g.setFont(f); g.setColor(i,i,23); g.drawString("S",100,100,Graphics.TOP|Graphics.LEFT); g.setColor(i,100,i); g.drawString("I",110,100,Graphics.TOP|Graphics.LEFT); g.setColor(i,0,255); g.drawString("E",115,100,Graphics.TOP|Graphics.LEFT); g.setColor(108,i,100); g.drawString("S",125,100,Graphics.TOP|Graphics.LEFT); i+=5; if(i>255) i=0; }} public class strcolor extends MIDlet{ can1 d1; Thread t=new Thread(); public void startApp() { d1=new can1(); Display.getDisplay(this).setCurrent(d1);Thread t=new Thread(); for(int i=0;i<500;i++){ try{ t.sleep(100); d1.repaint(); } catch(Exception e){}} }public void pauseApp() {}

Page 20: PMRC

public void destroyApp(boolean n) {} }