Advertisement
Haifisch7734

Lab8Java

Apr 23rd, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. /*
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class Edytor {
  6. String[] content;
  7.  
  8. public Edytor() {
  9. content = new String[1];
  10. }
  11.  
  12. public void run() {
  13. int m;
  14. Scanner read = new Scanner(System.in);
  15. while (true) {
  16. System.out.println("1. Dodaj linie.");
  17. System.out.println("2. Usuń linie.");
  18. System.out.println("3. Dodaj tekst na końcu wybranej linii.");
  19. System.out.println("4. Pokaż wszystko.");
  20. System.out.println("0. Wyjdź.");
  21. m = read.nextInt();
  22.  
  23. if (m > 0 && m < 4) {
  24. System.out.println("Podaj numer linii:");
  25. int number = read.nextInt();
  26. switch (m) {
  27. case 1: {
  28. Scanner newLine = new Scanner(System.in);
  29. System.out.println("Podaj tekst:");
  30. String line = newLine.nextLine();
  31. checkLength(number);
  32. content[number - 1] = line;
  33. break;
  34. }
  35. case 2: {
  36. if (number == content.length)
  37. content = (String[]) Arrays.copyOf(content, number - 1);
  38. if (number < content.length)
  39. content[number] = "";
  40. break;
  41. }
  42. case 3: {
  43. if (content.length < number)
  44. break;
  45. Scanner newLine = new Scanner(System.in);
  46. System.out.println("Podaj tekst:");
  47. String line = newLine.nextLine();
  48. checkLength(number);
  49. content[number - 1] = content[number - 1].concat(line);
  50. break;
  51. }
  52.  
  53. }
  54. } else if (m == 4) {
  55. for (int i = 0; i < content.length; i++)
  56. if (content[i] == null)
  57. System.out.println("");
  58. else
  59. System.out.println(content[i]);
  60. }
  61.  
  62. else if (m == 0) {
  63. read.close();
  64. break;
  65. }
  66. }
  67. }
  68.  
  69. public void checkLength(int number) {
  70. if (number > content.length)
  71. content = (String[]) Arrays.copyOf(content, number);
  72. }
  73.  
  74. public static void main(String[] args) {
  75. Edytor editor = new Edytor();
  76. editor.run();
  77. }
  78. }
  79. */
  80.  
  81. /*
  82. package zad2;
  83.  
  84. import java.util.Arrays;
  85.  
  86. public class Stos {
  87.  
  88. int[] stack = null;
  89.  
  90. public void push(int newInt){
  91. if(stack == null)
  92. stack = new int[1];
  93. else
  94. stack = Arrays.copyOf(stack, stack.length + 1);
  95. stack[stack.length-1] = newInt;
  96. }
  97.  
  98. public int pop(){
  99. int ret = stack[stack.length-1];
  100. stack = Arrays.copyOf(stack, stack.length - 1);
  101. return ret;
  102. }
  103.  
  104. public void showStack(){
  105. for(int i=stack.length-1;i >= 0; i--)
  106. System.out.println(stack[i]);
  107. System.out.println();
  108. }
  109.  
  110. public static void main(String[] args){
  111. Stos stack = new Stos();
  112. stack.push(2);
  113. stack.push(5);
  114. stack.push(3);
  115. stack.showStack();
  116. stack.pop();
  117. stack.push(12);
  118. stack.showStack();
  119.  
  120. }
  121. }
  122. */
  123.  
  124. /*
  125. import java.util.Arrays;
  126.  
  127. class Grupa {
  128. String nazwa;
  129. Student[] studenci;
  130.  
  131. public Grupa(String nazwa){
  132. this.nazwa = nazwa;
  133. studenci = null;
  134. }
  135.  
  136. public void dodajStudenta(Student student) {
  137. if (studenci == null)
  138. studenci = new Student[1];
  139. else
  140. studenci = (Student[]) Arrays.copyOf(studenci, studenci.length + 1);
  141. studenci[studenci.length - 1] = student;
  142. Arrays.sort(studenci);
  143. }
  144.  
  145. public void usunStudenta(int ind){
  146. studenci[ind] = studenci[studenci.length-1];
  147. studenci = (Student[]) Arrays.copyOf(studenci, studenci.length - 1);
  148. Arrays.sort(studenci);
  149. }
  150.  
  151. public void sklad(){
  152. System.out.println(this.nazwa);
  153. for(int i = 0;i < studenci.length;i++){
  154. System.out.println(studenci[i]);
  155. }
  156. }
  157. }
  158.  
  159. @SuppressWarnings("rawtypes")
  160. class Student implements Comparable {
  161. String imie;
  162. String nazwisko;
  163. Grupa grupa;
  164.  
  165. public Student(String imie, String nazwisko, Grupa grupa) {
  166. this.imie = imie;
  167. this.nazwisko = nazwisko;
  168. this.grupa = grupa;
  169. }
  170. @Override
  171. public String toString(){
  172. return imie + " " + nazwisko;
  173.  
  174. }
  175.  
  176. @Override
  177. public int compareTo(Object o) {
  178. Student p = (Student) o;
  179. int porownaneNazwiska = nazwisko.compareTo(p.nazwisko);
  180.  
  181. if (porownaneNazwiska == 0) {
  182. return imie.compareTo(p.imie);
  183. } else {
  184. return porownaneNazwiska;
  185. }
  186. }
  187. }
  188.  
  189. public class Dziekanat {
  190. public static void main(String[] args){
  191. Grupa g1 = new Grupa("2ID12");
  192. g1.dodajStudenta(new Student("Jan","Kowalski",g1));
  193. g1.dodajStudenta(new Student("Andrzej","Kowalski",g1));
  194. g1.dodajStudenta(new Student("Jan","Adamczyk",g1));
  195. g1.dodajStudenta(new Student("Jan","Ziemczyk",g1));
  196. g1.sklad();
  197. int x = Arrays.binarySearch(g1.studenci, new Student("Jan","Kowalski",g1));
  198. System.out.println(x);
  199. g1.usunStudenta(x);
  200. g1.sklad();
  201. }
  202.  
  203. }
  204. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement