苏州大学java_苏州大学JAVA考试原题及答案,历年同样的卷子。看吧。(卷二)...

本文提供了一组Java MIDlet应用程序示例,包括使用RecordComparator接口对记录存储进行排序,Media API播放.wav和.midi音频文件,以及使用RecordFilter接口搜索记录存储的实现。这些代码实例详细展示了如何在J2ME环境中处理数据存储和媒体播放操作。
摘要由CSDN通过智能技术生成

Q1:John a Midlet developer needs to sort records in a record store. He is using RecordComparator interface to develop a test application which appends some records in a record store and then display sorted records. Write the code for test application

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

public class Midlet extends MIDlet implements CommandListener {

private Display display;

TextField t1 = new TextField("Name", "", 15, TextField.ANY);

TextField t2 = new TextField("E-Mail", "", 15, TextField.EMAILADDR);

TextField t3 = new TextField("Phone", "", 15, TextField.PHONENUMBER);

private Form addform, readform, sortedForm;

Alert alert;

private Command add = new Command("Add", Command.SCREEN, 1);

private Command read = new Command("Read", Command.SCREEN, 1);

private Command sort = new Command(" Sorted Records", Command.SCREEN, 1);

private Command back = new Command("Back", Command.SCREEN, 1);

private RecordStore rs = null;

static final String REC_STORE = "db_8";

public Midlet() {

addform = new Form("AddRecord");

readform = new Form("ReadRecord");

sortedForm = new Form("Sorted Records");

addform.append(t1);

addform.append(t2);

addform.append(t3);

addform.addCommand(add);

addform.addCommand(read);

addform.addCommand(sort);

readform.addCommand(back);

sortedForm.addCommand(back);

addform.setCommandListener(this);

readform.setCommandListener(this);

sortedForm.setCommandListener(this);

display = Display.getDisplay(this);

}

public void destroyApp(boolean unconditional) {

}

public void startApp() {

display.setCurrent(addform);

}

public void pauseApp() {

}

public void db(String str) {

System.err.println("Msg: " + str);

}

public void commandAction(Command command, Displayable displayable) {

if (command == add) {

String str = "";

str = t1.getString();

str = str + " " + t2.getString();

str = str + " " + t3.getString();

try {

rs = RecordStore.openRecordStore(REC_STORE, true);

byte[] rec = str.getBytes();

rs.addRecord(rec, 0, rec.length);

rs.closeRecordStore();

} catch (Exception e) {

db(e.toString());

}

alert = new Alert("RecordStore", "New Record Added.", null, AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert, addform);

t1.setString("");

t2.setString("");

t3.setString("");

} else if (command == read) {

try {

rs = RecordStore.openRecordStore(REC_STORE, true);

// Intentionally make this too small to test code below

byte[] recData = new byte[5];

int len;

for (int i = 1; i <= rs.getNumRecords(); i++) {

if (rs.getRecordSize(i) > recData.length) {

recData = new byte[rs.getRecordSize(i)];

}

len = rs.getRecord(i, recData, 0);

readform.append(new String(recData, 0, len) + "\n");

}

rs.closeRecordStore();

} catch (Exception e) {

db(e.toString());

}

display.setCurrent(readform);

}

if (command == sort) {

readRecords();

}

if (command == back) {

display.setCurrent(addform);

}

}

public void readRecords() {

try {

rs = RecordStore.openRecordStore(REC_STORE, true);

if (rs.getNumRecords() > 0) {

Comparator comp = new Comparator();

RecordEnumeration re = rs.enumerateRecords(null, comp, false);

while (re.hasNextElement()) {

// Calls String constructor that takes an array of bytes as input

String str = new String(re.nextRecord());

sortedForm.append(str + "\n");

}

display.setCurrent(sortedForm);

rs.closeRecordStore();

}

} catch (Exception e) {

db(e.toString());

}

}

}

class Comparator implements RecordComparator {

public int compare(byte[] rec1, byte[] rec2) {

String str1 = new String(rec1), str2 = new String(rec2);

int result = str1.compareTo(str2);

if (result == 0) {

return RecordComparator.EQUIVALENT;

} else if (result 

return RecordComparator.PRECEDES;

} else {

return RecordComparator.FOLLOWS;

}

}

}

Q2. Write a J2me application using Media API to demonstrate playing a sound clip of .wav format.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.io.*;

import javax.microedition.media.*;

public class Midlet extends MIDlet implements CommandListener {

private Display display; // Reference to Display object

private Form fmMain; // The main form

private Command Play;

// Request method GET

// Request method Post

private Command Exit;

public Midlet() {

display = Display.getDisplay(this);

// Create commands

Play = new Command("Play", Command.SCREEN, 3);

Exit = new Command("Exit", Command.EXIT, 1);

// Textfields

// Create Form, add commands & componenets, listen for events

fmMain = new Form("Media Player");

fmMain.addCommand(Exit);

fmMain.addCommand(Play);

fmMain.setCommandListener(this);

}

public void startApp() {

display.setCurrent(fmMain);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

public void commandAction(Command c, Displayable s) {

if (c == Play) {

try {

int id;

InputStream istream = getClass().getResourceAsStream("audio.wav");

Player WAVplayer = Manager.createPlayer(istream, "audio/X-wav");

WAVplayer.start();

} catch (Exception e) {

System.err.println("Msg: " + e.toString());

}

} else if (c == Exit) {

destroyApp(false);

notifyDestroyed();

}

}

}

Q3: John a Midlet developer needs to search records in a record store. He is using RecordFilter interface to develop a test application which searches a record in a record store and then display found records. Write the code for test application which can search a record from record store.

import java.io.*;

import javax.microedition.midlet.*;

import javax.microedition.rms.*;

import javax.microedition.lcdui.*;

public class SimpleSearch extends MIDlet implements CommandListener {

private Display display; // Reference to Display object

private Form fmMain; // The main form

private StringItem siMatch; // The matching text, if any

private Command cmFind; // Command to search record store

private Command cmExit; // Command to insert items

private TextField tfFind; // Search text as requested by user

private RecordStore rs = null; // Record store

static final String REC_STORE = "db_6"; // Name of record store

public SimpleSearch() {

display = Display.getDisplay(this);

// Define textfield, stringItem and commands

tfFind = new TextField("Find", "", 10, TextField.ANY);

siMatch = new StringItem(null, null);

cmExit = new Command("Exit", Command.EXIT, 1);

cmFind = new Command("Find", Command.SCREEN, 2);

// Create the form, add commands

fmMain = new Form("Record Search");

fmMain.addCommand(cmExit);

fmMain.addCommand(cmFind);

// Append textfield and stringItem

fmMain.append(tfFind);

fmMain.append(siMatch);

// Capture events

fmMain.setCommandListener(this);

//--------------------------------

// Open and write to record store

//--------------------------------

openRecStore(); // Create the record store

writeTestData(); // Write a series of records

}

public void destroyApp(boolean unconditional) {

closeRecStore(); // Close record store

}

public void startApp() {

display.setCurrent(fmMain);

}

public void pauseApp() {

}

public void openRecStore() {

try {

// The second parameter indicates that the record store

// should be created if it does not exist

rs = RecordStore.openRecordStore(REC_STORE, true);

} catch (Exception e) {

db(e.toString());

}

}

public void closeRecStore() {

try {

rs.closeRecordStore();

} catch (Exception e) {

db(e.toString());

}

}

public void writeTestData() {

String[] golfClubs = {

"Wedge...good from the sand trap",

"One Wood...off the tee",

"Putter...only on the green",

"Five Iron...middle distance"};

writeRecords(golfClubs);

}

public void writeRecords(String[] sData) {

byte[] record;

try {

// Only add the records once

if (rs.getNumRecords() > 0) {

return;

}

for (int i = 0; i 

record = sData[i].getBytes();

rs.addRecord(record, 0, record.length);

}

} catch (Exception e) {

db(e.toString());

}

}

private void searchRecordStore() {

try {

// Record store is not empty

if (rs.getNumRecords() > 0) {

// Setup the search filter with the user requested text

SearchFilter search = new SearchFilter(tfFind.getString());

RecordEnumeration re = rs.enumerateRecords(search, null, false);

// A match was found using the filter

if (re.numRecords() > 0) // Show match in the stringItem on the form

{

siMatch.setText(new String(re.nextRecord()));

}

re.destroy(); // Free enumerator

}

} catch (Exception e) {

db(e.toString());

}

}

public void commandAction(Command c, Displayable s) {

if (c == cmFind) {

searchRecordStore();

} else if (c == cmExit) {

destroyApp(false);

notifyDestroyed();

}

}

private void db(String str) {

System.err.println("Msg: " + str);

}

}

class SearchFilter implements RecordFilter {

private String searchText = null;

public SearchFilter(String searchText) {

// This is the text to search for

this.searchText = searchText.toLowerCase();

}

public boolean matches(byte[] candidate) {

String str = new String(candidate).toLowerCase();

// Look for a match

if (searchText != null && str.indexOf(searchText) != -1) {

return true;

} else {

return false;

}

}

}

Q4. Write a J2me application using Media API to demonstrate playing a sound clip of .midi format.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.media.*;

import javax.microedition.media.control.*;

import javax.microedition.io.*;

import java.io.*;

public class Midlet extends MIDlet implements CommandListener {

private Display display; // Reference to Display object

private Form fmMain; // The main form

private Command Exit;

private Command PlayMidi;

public Midlet() {

display = Display.getDisplay(this);

PlayMidi = new Command("PlayMidi", Command.OK, 1);

Exit = new Command("Exit", Command.EXIT, 1);

fmMain = new Form("Media Player");

fmMain.addCommand(Exit);

fmMain.addCommand(PlayMidi);

fmMain.setCommandListener(this);

}

public void startApp() {

display.setCurrent(fmMain);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

public void commandAction(Command c, Displayable s) {

if (c == PlayMidi) {

try {

int id;

InputStream istream = getClass().getResourceAsStream("intro.midi");

Player midiplayer = Manager.createPlayer(istream, "audio/midi");

midiplayer.start();

} catch (Exception e) {

System.err.println("Msg: " + e.toString());

}

} else if (c == Exit) {

destroyApp(false);

notifyDestroyed();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值