Advertisement
lashrone1

Rez

Apr 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. static Scanner scan = new Scanner(System.in);
  6.  
  7. static public void test1() {
  8. Vector v = new Vector();
  9. for (int i = 0; i < 5; i++) {
  10. v.arr[i] = ((int) (Math.random() * 12) - 15);
  11. }
  12. v.size = 5;
  13. v.print();
  14. System.out.println("Введите позицию эл значение которого хотите узнать:");
  15. int N = scan.nextInt();
  16. System.out.println(v.get(N));
  17. System.out.println("Введите позицию эл значение которого хотите заменить:");
  18. int P = scan.nextInt();
  19. double V = scan.nextInt();
  20. v.set(V, P);
  21. v.print();
  22. System.out.println("Введите позицию эл и значение которое хотите установить:");
  23. int Po = scan.nextInt();
  24. double Va = scan.nextInt();
  25. v.insert(Va, Po);
  26. v.print();
  27. v.erase(3);
  28. v.print();
  29. v.clear();
  30. v.print();
  31. }
  32.  
  33.  
  34. static public void test2() {
  35. Vector v = new Vector(5);
  36. v.print();
  37. System.out.println("Введите позицию эл значение которого хотите узнать:");
  38. int N = scan.nextInt();
  39. System.out.println(v.get(N));
  40. System.out.println("Введите позицию эл значение которого хотите заменить:");
  41. int P = scan.nextInt();
  42. double V = scan.nextInt();
  43. v.set(V, P);
  44. v.print();
  45. System.out.println("Введите позицию эл и значение которое хотите установить:");
  46. int Po = scan.nextInt();
  47. double Va = scan.nextInt();
  48. v.insert(Va, Po);
  49. v.print();
  50. v.erase(1);
  51. v.print();
  52. v.clear();
  53. v.print();
  54.  
  55. }
  56.  
  57.  
  58. static public void test3() {
  59. double[] arr = new double[10];
  60. for (int i = 0; i < 5; i++) {
  61. arr[i] = ((int) (Math.random() * 12) - 15);
  62. }
  63. Vector ve = new Vector(arr);
  64. ve.print();
  65. System.out.println("Введите позицию эл значение которого хотите узнать:");
  66. int N = scan.nextInt();
  67. System.out.println(ve.get(N));
  68. System.out.println("Введите позицию эл значение которого хотите заменить:");
  69. int P = scan.nextInt();
  70. double V = scan.nextInt();
  71. ve.set(V, P);
  72. ve.print();
  73. System.out.println("Введите позицию эл и значение которое хотите установить:");
  74. int Po = scan.nextInt();
  75. double Va = scan.nextInt();
  76. ve.insert(Va, Po);
  77. ve.print();
  78. ve.erase(1);
  79. ve.print();
  80. ve.clear();
  81. ve.print();
  82. }
  83.  
  84. public static void main(String[] args) {
  85. System.out.println("Выберите конструктор: \n" +
  86. "1.По умолчанию\n" +
  87. "2.С заданным размером\n" +
  88. "3.С созданным массивом");
  89. boolean i=true;
  90. while(i) {
  91. int a = scan.nextInt();
  92. switch (a) {
  93. case 1: test1();break;
  94. case 2: test2();break;
  95. case 3: test3();break;
  96. case 4: i=false;break;
  97. }
  98. }
  99. }
  100. }
  101.  
  102.  
  103. public class Vector {
  104.  
  105. double[] arr = new double[10];
  106. int size=0;
  107.  
  108. Vector(){}
  109.  
  110. Vector(int capacity){
  111. this.size=capacity;
  112. for (int i=0;i<capacity;i++){
  113. this.arr[i]=((int)(Math.random() * 12) - 15);
  114. }
  115. }
  116.  
  117. Vector(double[] arr){
  118. copy(arr);
  119. }
  120.  
  121. int getSize(){
  122. return this.size;
  123. }
  124.  
  125. double get(int pos){
  126. if((pos>this.arr.length)||(pos<0)){
  127. return 404;
  128. }
  129. else{
  130. return this.arr[pos-1];
  131. }
  132. }
  133.  
  134. void set(double val, int pos){
  135. this.arr[pos-1]=val;
  136. }
  137.  
  138. void insert(double val, int pos) {
  139. if (this.size == this.arr.length) {
  140. double[] mas = new double[arr.length * 2];
  141. for (int i = 0; i < arr.length; i++)
  142. mas[i] = this.arr[i];
  143. arr=mas;
  144. System.out.println("Массив увеличен!");
  145. }
  146. double tmp=this.arr[pos-1];
  147. for(int i=this.arr.length-1;i>0;i--){
  148. this.arr[i]=this.arr[i-1];
  149. }
  150. this.arr[pos-1]=tmp;
  151. this.arr[pos]=val;
  152. size++;
  153. }
  154.  
  155. void erase(int pos){
  156. double tmp=this.arr[pos-1];
  157. for(int i=0;i<this.arr.length-1;i++){
  158. this.arr[i]=this.arr[i+1];
  159. }
  160. this.arr[pos-1]=tmp;
  161. size--;
  162. }
  163.  
  164. void clear(){
  165. for(int i=0;i<this.arr.length;i++){
  166. this.arr[i]=0;
  167. }
  168. }
  169.  
  170. void print() {
  171. if (getSize()== 0) {
  172. System.out.println("Empty");
  173. } else {
  174. for (int i = 0; i < this.arr.length; i++) {
  175. System.out.print(arr[i] + " ");
  176. }
  177. System.out.println();
  178. }
  179. }
  180. double[] copy(double[] arr){
  181. double[] mas=new double[10];
  182. for(int i=0; i<10;i++){
  183. mas[i]=arr[i];
  184. }
  185. return mas;
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement