Вы находитесь на странице: 1из 10

4

Create a midlet to accept a number from the user and then find if the number is a prime or not. Show appropriate messages. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class PrimeNumberMidlet extends MIDlet implements CommandListener{ Display display; // The display for this MIDlet TextBox primeTextBox; // The textbox to enter number Command exitCommand, checkCommand; // The exit command, command to chek for prime // midlet constructor public PrimeNumberMidlet() { primeTextBox = new TextBox("Prime Number", null, 256, TextField.NUMERIC); checkCommand = new Command("Check", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.EXIT, 0); primeTextBox.addCommand(checkCommand); primeTextBox.addCommand(exitCommand); primeTextBox.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(primeTextBox); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { Alert msgAlert; if (c == exitCommand) { notifyDestroyed(); } if (c == checkCommand) { if(isPrime(Integer.parseInt(primeTextBox.getString()))){ msgAlert = new Alert("Check Prime", "Pime Number", null, AlertType.INFO); display.setCurrent(msgAlert, s); } else{ msgAlert = new Alert("Check Prime", "Not a Pime Number", null, AlertType.ERROR); msgAlert.setTimeout(Alert.FOREVER); display.setCurrent(msgAlert, s); } } } boolean isPrime(int number){ for (int i=2; i <= Math.sqrt(number); i++ ){ if (number % i == 0){ return false; } } return true; } }

Design a login form which accepts the username and password and display success/failure through alerts. import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class LoginMidlet extends MIDlet implements CommandListener { private private private private Display display; Form form; Command exitCommand, loginCommand; TextField unameTextField, pwdTextField;

public LoginMidlet() { form = new Form("Login"); loginCommand = new Command("Login", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.EXIT, 1); unameTextField = new TextField("Username:", null, 10, TextField.ANY); pwdTextField = new TextField("Pasword:", null, 10, TextField.PASSWORD | TextField.SENSITIVE); form.append(unameTextField); form.append(pwdTextField); form.addCommand(loginCommand); form.addCommand(exitCommand); form.setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { notifyDestroyed(); } if (c == loginCommand) { Alert alert; if(unameTextField.getString().equals("admin") && pwdTextField.getString().equals("admin")){ alert = new Alert("Login Status", "Login Successful!", null, AlertType.INFO); } else{ alert = new Alert("Login Status", "Login unsuccessful. Please try again!", null, AlertType.ERROR); } display.setCurrent(alert); } } public void startApp() { if(display == null) display = Display.getDisplay(this); display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

Create a midlet to open a URL entered by the user in the TextBox. Add Open command to open the URL. import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class URLMidlet extends MIDlet implements CommandListener { Display display; // The display for this MIDlet Command exitCommand, openCommand; // The exit command TextBox textBox; public URLMidlet() { textBox = new TextBox("URL", "", 256, TextField.URL); openCommand = new Command("Open", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.EXIT, 0); textBox.addCommand(openCommand); textBox.addCommand(exitCommand); textBox.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(textBox); } } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } if (c == openCommand) { try{ this.platformRequest(textBox.getString()); }catch(ConnectionNotFoundException cnf){ System.out.println(cnf.getMessage()); } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

Create a midlet to make a call to a number entered by the user in the TextBox. Add Call command to make a call. import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class PhoneCallMidlet extends MIDlet implements CommandListener { Display display; // The display for this MIDlet Command exitCommand, callCommand; // The exit command TextBox textBox; public PhoneCallMidlet() { textBox = new TextBox("Call", "", 256, TextField.NUMERIC); callCommand = new Command("Call", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.EXIT, 0); textBox.addCommand(callCommand); textBox.addCommand(exitCommand); textBox.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(textBox); } } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } if (c == callCommand) { try{ this.platformRequest("tel:" + textBox.getString()); }catch(ConnectionNotFoundException cnf){ System.out.println(cnf.getMessage()); } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

Create a midlet to accept email id from user as a password and display it on the click of a button. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class EmailMidlet extends MIDlet implements CommandListener { Display display; // The display for this MIDlet Command exitCommand, showCommand; // The exit command TextBox emailTextBox; public EmailMidlet() { emailTextBox = new TextBox("Email-ID", "", 256, TextField.EMAILADDR | TextField.PASSWORD); exitCommand = new Command("Exit", Command.EXIT, 0); showCommand = new Command("Show", Command.SCREEN, 0); emailTextBox.addCommand(exitCommand); emailTextBox.addCommand(showCommand); emailTextBox.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(emailTextBox); } } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } if (c == showCommand) { display.setCurrent(new Alert(("Email", emailTextBox.getString(), null, AlertType.INFO)); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

10

Create a midlet to display following: True if mobile supports color False otherwise Number of colors supported by the device Flash Backlight support Vibration support import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class DisplayCapabilitiesMidlet extends MIDlet implements CommandListener { Display display; // The display for this MIDlet Command exitCommand; // The exit command TextBox msgTextBox; // The textbox to display Hello... String msgString; // The string for diplaying message public DisplayCapabilitiesMidlet() { msgTextBox = new TextBox("Display Capabilities", "", 256, TextField.ANY); exitCommand = new Command("Exit", Command.EXIT, 0); msgTextBox.addCommand(exitCommand); msgTextBox.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(msgTextBox); } msgString = "isColor = " + display.isColor() + "\n"; msgString = msgString + "numColors = " + display.numColors() + "\n"; msgString = msgString + "flashBacklight = " + display.flashBacklight(2000) + "\n"; msgString = msgString + "vibrate = " + display.vibrate(2000); msgTextBox.setString(msgString); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

11

Create a midlet to offer three options namely Airplane, Car and Hotel. Only one option can be selected at a time. After selecting the option display the appropriate alert message. import java.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class TravelListMidlet extends MIDlet implements CommandListener { Display display; private List travelList; private Command exitCommand, nextCommand; public TravelListMidlet() throws IOException { String[] elementsString = {"Airplane", "Car", "Hotel"}; travelList = new List("Reservation type", List.EXCLUSIVE, elementsString, null); nextCommand = new Command("Next", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.EXIT, 0); travelList.addCommand(nextCommand); travelList.addCommand(exitCommand); travelList.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(travelList); } } public void commandAction(Command c, Displayable s) { if (c == nextCommand || c == List.SELECT_COMMAND) { int index = travelList.getSelectedIndex(); Alert alert = new Alert("Your selection", "You chose " + travelList.getString(index) + ".", null, AlertType.INFO); display.setCurrent(alert, travelList); } else if (c == exitCommand) notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

12

Design a form to display following items in a midlet:

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class LayoutMidlet extends MIDlet implements CommandListener { Display display; // The display for this MIDlet Command exitCommand, itemCommand; // The exit command Form form; // The form containing String Items public LayoutMidlet() { form = new Form("StringItem"); // creates a new form exitCommand = new Command("Exit", Command.EXIT, 0); StringItem oneStringItem = new StringItem("Layout:", "Left", Item.PLAIN); oneStringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_AFTER); StringItem twoStringItem = new StringItem("Layout:", "Center", Item.PLAIN); twoStringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER); StringItem threeStringItem = new StringItem("Layout:", "Right", Item.PLAIN); threeStringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_RIGHT | Item.LAYOUT_NEWLINE_AFTER); form.append(oneStringItem); form.append(twoStringItem); form.append(threeStringItem); form.addCommand(exitCommand); form.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(form); } } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

13

Write a MIDlet application which will accept date from the user and will display day of the week (Mon, Tue, etc.) for the entered date. import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.util.Calendar; public class DateFieldMidlet extends MIDlet implements CommandListener { private private private private Display display; Form form; Command exitCommand, dayCommand; DateField dobDateField;

public DateFieldMidlet() { form = new Form("DateField"); dayCommand = new Command("Day", Command.SCREEN, 1); exitCommand = new Command("Exit", Command.EXIT, 2); dobDateField = new DateField("Date of Birth:", DateField.DATE); form.append(dobDateField); form.addCommand(dayCommand); form.addCommand(exitCommand); form.setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { notifyDestroyed(); } if (c == dayCommand) { Alert alert; String msgString; String[] daysString ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; Calendar cal = Calendar.getInstance(); cal.setTime(dobDateField.getDate()); msgString = "Day of Week: " + daysString[cal.get(Calendar.DAY_OF_WEEK)-1]; alert = new Alert("Alert", msgString, null, AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(form); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

15

Design a canvas based MIDP application which generates concentric circles from smallest to largest circle on click of command button. import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; public class ConcentricCirclesCanvas extends Canvas { public void paint(Graphics g) { int w , h; int INIT_RADIUS = 5; w = getWidth(); h = getHeight(); g.setColor(255, 255, 255); //white g.fillRect(0, 0, w, h); // white background g.setColor(0, 0, 0); //black int rmax; if (w > h){ rmax = h/2; } else{ rmax = w/2; } for(int r = INIT_RADIUS; r<=rmax; r+=10){ g.drawArc(w/2-r, h/2-r, 2*r, 2*r, 0, 360); } } } import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class ConcentricCirclesMIDlet extends MIDlet implements CommandListener { Display display; Command exitCommand; ConcentricCirclesCanvas ccc; public ConcentricCirclesMIDlet() { ccc = new ConcentricCirclesCanvas (); exitCommand = new Command("Exit", Command.EXIT, 0); house.addCommand(exitCommand); house.setCommandListener(this); } public void startApp() { if(display == null){ display = Display.getDisplay(this); display.setCurrent(house); } } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { notifyDestroyed(); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }

Вам также может понравиться