Advertisement
Guest User

Rozwiazanie lab03_1

a guest
Nov 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package mobileapplication1;
  7.  
  8. import java.util.Random;
  9. import javax.microedition.lcdui.Command;
  10. import javax.microedition.lcdui.CommandListener;
  11. import javax.microedition.lcdui.Display;
  12. import javax.microedition.lcdui.Displayable;
  13. import javax.microedition.lcdui.Form;
  14. import javax.microedition.lcdui.StringItem;
  15. import javax.microedition.lcdui.TextField;
  16. import javax.microedition.midlet.*;
  17.  
  18. public class Midlet extends MIDlet implements CommandListener {
  19.  
  20. private final Form win;
  21. private final String[] eng;
  22. private final String[] pl;
  23. private final StringItem points;
  24. private final StringItem word;
  25. private final StringItem excercise;
  26. private final TextField answer;
  27. private final StringItem check;
  28. private final Command cmdCheck;
  29. private final Command cmdAgain;
  30. private final Command cmdEnd;
  31. static int word_num = 0;
  32. static int points_num = 0;
  33. static int index = 0;
  34.  
  35. public Midlet() {
  36. win = new Form("Test z angielskiego");
  37. eng = new String[5];
  38. pl = new String[5];
  39. points = new StringItem("Punkty:", "");
  40. word = new StringItem("Wyraz:", "");
  41. excercise = new StringItem("Podaj angielskie tłumaczenie słowa:", "");
  42. answer = new TextField("Odpowiedź:", "", 30, TextField.ANY);
  43. check = new StringItem("Odpowiedź jest:", "");
  44. win.append(points);
  45. win.append(word);
  46. win.append(excercise);
  47. win.append(answer);
  48. win.append(check);
  49. cmdCheck = new Command("Sprawdź", Command.ITEM, 0);
  50. cmdAgain = new Command("Jeszcze raz", Command.ITEM, 0);
  51. cmdEnd = new Command("Koniec", Command.ITEM, 0);
  52. win.addCommand(cmdCheck);
  53. win.addCommand(cmdAgain);
  54. win.addCommand(cmdEnd);
  55. win.setCommandListener(this);
  56. }
  57.  
  58. public void startApp() {
  59. Display display = Display.getDisplay(this);
  60. display.setCurrent(win);
  61. getData();
  62. }
  63.  
  64. public void pauseApp() {
  65. }
  66.  
  67. public void destroyApp(boolean unconditional) {
  68. }
  69.  
  70. public void getData() {
  71. eng[0] = "bike";
  72. eng[1] = "car";
  73. eng[2] = "paper";
  74. eng[3] = "blackboard";
  75. eng[4] = "keyboard";
  76. pl[0] = "rower";
  77. pl[1] = "samochod";
  78. pl[2] = "papier";
  79. pl[3] = "tablica";
  80. pl[4] = "klawiatura";
  81. word.setText(0 + "");
  82. points.setText(0 + "");
  83. random();
  84. word_num = 0;
  85. points_num = 0;
  86. }
  87.  
  88. public void random() {
  89. Random generator = new Random();
  90. int r = generator.nextInt(eng.length);
  91. String tekst = pl[r];
  92. excercise.setText(tekst);
  93. index = r;
  94. }
  95.  
  96. public void commandAction(Command c, Displayable d) {
  97. if (c == cmdCheck) {
  98. String userAnswer = answer.getString();
  99.  
  100. word_num += 1;
  101. word.setText(word_num + "");
  102. if (userAnswer.equalsIgnoreCase(eng[index])) {
  103. points_num += 1;
  104. check.setText("Poprawna" + "");
  105. points.setText(points_num + "");
  106. } else {
  107. check.setText("Niepoprawna" + "");
  108. }
  109. answer.setString("");
  110. random();
  111.  
  112. }
  113. if (c == cmdAgain) {
  114. getData();
  115. }
  116. if (c == cmdEnd) {
  117. notifyDestroyed();
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement