Advertisement
Guest User

5ta

a guest
Jan 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.13 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. class ComplexNumber<T extends Number,U extends Number> implements Comparable<ComplexNumber<T,U>>{
  6. private T real;
  7. private U imaginary;
  8. public ComplexNumber(T _real, U _imaginary){
  9. real = _real;
  10. imaginary = _imaginary;
  11. }
  12. public T getReal(){
  13. return real;
  14. }
  15. public U getImaginary(){
  16. return imaginary;
  17. }
  18. public double modul(){
  19. return Math.sqrt(Math.pow(real.doubleValue(),2)+Math.pow(imaginary.doubleValue(),2));
  20. }
  21. public String toString(){
  22. if(imaginary.doubleValue() >= 0) return String.format("%.2f+%.2fi", real.doubleValue(), imaginary.doubleValue());
  23. return String.format("%.2f%.2fi", real.doubleValue(), imaginary.doubleValue());
  24. }
  25. public int compareTo(ComplexNumber<T,U> o){
  26. if(this.modul() == o.modul()) return 0;
  27. if(this.modul() > o.modul()) return 1;
  28. return -1;
  29. }
  30. }
  31.  
  32. public class ComplexNumberTest {
  33.  
  34. public static void main(String[] args) {
  35. Scanner jin = new Scanner(System.in);
  36. int k = jin.nextInt();
  37. if ( k == 0 ) { //test simple functions int
  38. int r = jin.nextInt();int i = jin.nextInt();
  39. ComplexNumber<Integer, Integer> c = new ComplexNumber<Integer, Integer>(r, i);
  40. System.out.println(c);
  41. System.out.println(c.getReal());
  42. System.out.println(c.getImaginary());
  43. System.out.println(c.modul());
  44. }
  45. if ( k == 1 ) { //test simple functions float
  46. float r = jin.nextFloat();
  47. float i = jin.nextFloat();
  48. ComplexNumber<Float, Float> c = new ComplexNumber<Float, Float>(r, i);
  49. System.out.println(c);
  50. System.out.println(c.getReal());
  51. System.out.println(c.getImaginary());
  52. System.out.println(c.modul());
  53. }
  54. if ( k == 2 ) { //compareTo int
  55. LinkedList<ComplexNumber<Integer,Integer>> complex = new LinkedList<ComplexNumber<Integer,Integer>>();
  56. while ( jin.hasNextInt() ) {
  57. int r = jin.nextInt(); int i = jin.nextInt();
  58. complex.add(new ComplexNumber<Integer, Integer>(r, i));
  59. }
  60. System.out.println(complex);
  61. Collections.sort(complex);
  62. System.out.println(complex);
  63. }
  64. if ( k == 3 ) { //compareTo double
  65. LinkedList<ComplexNumber<Double,Double>> complex = new LinkedList<ComplexNumber<Double,Double>>();
  66. while ( jin.hasNextDouble() ) {
  67. double r = jin.nextDouble(); double i = jin.nextDouble();
  68. complex.add(new ComplexNumber<Double, Double>(r, i));
  69. }
  70. System.out.println(complex);
  71. Collections.sort(complex);
  72. System.out.println(complex);
  73. }
  74. if ( k == 4 ) { //compareTo mixed
  75. LinkedList<ComplexNumber<Double,Integer>> complex = new LinkedList<ComplexNumber<Double,Integer>>();
  76. while ( jin.hasNextDouble() ) {
  77. double r = jin.nextDouble(); int i = jin.nextInt();
  78. complex.add(new ComplexNumber<Double, Integer>(r, i));
  79. }
  80. System.out.println(complex);
  81. Collections.sort(complex);
  82. System.out.println(complex);
  83. }
  84. }
  85. }
  86.  
  87. import java.time.Instant;
  88. import java.time.LocalDateTime;
  89. import java.time.ZoneId;
  90. import java.util.ArrayList;
  91. import java.util.Collections;
  92. import java.util.List;
  93. import java.util.Scanner;
  94. import java.util.stream.Collectors;
  95. import java.util.Comparator;
  96.  
  97. class Timestamp <T> implements Comparable<Timestamp<?>>{
  98. final private LocalDateTime time;
  99. final private T element;
  100. public Timestamp(LocalDateTime _time, T _element){
  101. time = _time;
  102. element = _element;
  103. }
  104. public LocalDateTime getTime(){
  105. return time;
  106. }
  107. public T getElement(){
  108. return element;
  109. }
  110. public int compareTo(Timestamp<?> t){
  111. if(time.isBefore(t.time)) return -1;
  112. if(time.isAfter(t.time)) return 1;
  113. return 0;
  114. }
  115. public boolean equals(Object o){
  116. if(this == o) return true;
  117. if(o == null) return false;
  118. if(this.getClass() != o.getClass()) return false;
  119. Timestamp ts = (Timestamp)o;
  120. if(!ts.time.isEqual(time)) return false;
  121. return true;
  122. }
  123. public String toString(){
  124. return time.toString()+" "+element.toString();
  125. }
  126.  
  127. }
  128. class Scheduler<T> {
  129. List<Timestamp<T>> stamps;
  130. public Scheduler(){
  131. stamps = new ArrayList<Timestamp<T>>();
  132. }
  133. public void add(Timestamp<T> newStamp){
  134. stamps.add(newStamp);
  135. }
  136. public boolean remove(Timestamp<T> delete){
  137. for(int i = 0; i <stamps.size(); i ++){
  138. if(delete.equals(stamps.get(i))){
  139. stamps.remove(i);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. public Timestamp<T> last(){
  146. LocalDateTime now = LocalDateTime.now();
  147. Timestamp<T> ret = null;
  148. for (int i = 0; i < stamps.size(); i++){
  149. if(stamps.get(i).getTime().isBefore(now)){
  150. if(ret == null) ret = stamps.get(i);
  151. else if(stamps.get(i).getTime().isAfter(ret.getTime()))
  152. ret = stamps.get(i);
  153. }
  154. }
  155. return ret;
  156. }
  157. public Timestamp<T> next(){
  158. LocalDateTime now = LocalDateTime.now();
  159. Timestamp<T> ret = null;
  160. for( int i = 0;i < stamps.size(); i++){
  161. if(stamps.get(i).getTime().isAfter(now)){
  162. if(ret == null) ret = stamps.get(i);
  163. else if ( stamps.get(i).getTime().isBefore(ret.getTime()))
  164. ret = stamps.get(i);
  165. }
  166. }
  167. return ret;
  168. }
  169. public List<Timestamp<T>> getAll(LocalDateTime begin, LocalDateTime end){
  170. List<Timestamp<T>> ret = new ArrayList<Timestamp<T>>();
  171. for(int i = 0; i < stamps.size(); i++){
  172. if(stamps.get(i).getTime().isAfter(begin)&&stamps.get(i).getTime().isBefore(end)){
  173. ret.add(stamps.get(i));
  174. }
  175. }
  176. return ret;
  177. }
  178.  
  179. }
  180.  
  181. public class SchedulerTest {
  182.  
  183. static final LocalDateTime TIME = LocalDateTime.of(2016, 10, 25, 10, 15);
  184.  
  185. public static void main(String[] args) {
  186. Scanner jin = new Scanner(System.in);
  187. int k = jin.nextInt();
  188. if (k == 0) { //test Timestamp with String
  189. Timestamp<String> t = new Timestamp<>(TIME, jin.next());
  190. System.out.println(t);
  191. System.out.println(t.getTime());
  192. System.out.println(t.getElement());
  193. }
  194. if (k == 1) { //test Timestamp with ints
  195. Timestamp<Integer> t1 = new Timestamp<>(TIME, jin.nextInt());
  196. System.out.println(t1);
  197. System.out.println(t1.getTime());
  198. System.out.println(t1.getElement());
  199. Timestamp<Integer> t2 = new Timestamp<>(TIME.plusDays(10), jin.nextInt());
  200. System.out.println(t2);
  201. System.out.println(t2.getTime());
  202. System.out.println(t2.getElement());
  203. System.out.println(t1.compareTo(t2));
  204. System.out.println(t2.compareTo(t1));
  205. System.out.println(t1.equals(t2));
  206. System.out.println(t2.equals(t1));
  207. }
  208. if (k == 2) {//test Timestamp with String, complex
  209. Timestamp<String> t1 = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.next());
  210. System.out.println(t1);
  211. System.out.println(t1.getTime());
  212. System.out.println(t1.getElement());
  213. Timestamp<String> t2 = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.next());
  214. System.out.println(t2);
  215. System.out.println(t2.getTime());
  216. System.out.println(t2.getElement());
  217. System.out.println(t1.compareTo(t2));
  218. System.out.println(t2.compareTo(t1));
  219. System.out.println(t1.equals(t2));
  220. System.out.println(t2.equals(t1));
  221. }
  222. if (k == 3) { //test Scheduler with String
  223. Scheduler<String> scheduler = new Scheduler<>();
  224. LocalDateTime now = LocalDateTime.now();
  225. scheduler.add(new Timestamp<>(now.minusHours(2), jin.next()));
  226. scheduler.add(new Timestamp<>(now.minusHours(1), jin.next()));
  227. scheduler.add(new Timestamp<>(now.minusHours(4), jin.next()));
  228. scheduler.add(new Timestamp<>(now.plusHours(2), jin.next()));
  229. scheduler.add(new Timestamp<>(now.plusHours(4), jin.next()));
  230. scheduler.add(new Timestamp<>(now.plusHours(1), jin.next()));
  231. scheduler.add(new Timestamp<>(now.plusHours(5), jin.next()));
  232. System.out.println(scheduler.next().getElement());
  233. System.out.println(scheduler.last().getElement());
  234. List<Timestamp<String>> result = scheduler.getAll(now.minusHours(3), now.plusHours(4).plusMinutes(15));
  235. String out = result.stream()
  236. .sorted()
  237. .map(Timestamp::getElement)
  238. .collect(Collectors.joining(", "));
  239. System.out.println(out);
  240. }
  241. if (k == 4) {//test Scheduler with ints complex
  242. Scheduler<Integer> scheduler = new Scheduler<>();
  243. int counter = 0;
  244. ArrayList<Timestamp<Integer>> forRemoval = new ArrayList<>();
  245. while (jin.hasNextLong()) {
  246. Timestamp<Integer> ti = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.nextInt());
  247. if ((counter & 7) == 0) {
  248. forRemoval.add(ti);
  249. }
  250. scheduler.add(ti);
  251. ++counter;
  252. }
  253. jin.next();
  254.  
  255. while (jin.hasNextLong()) {
  256. LocalDateTime left = ofEpochMS(jin.nextLong());
  257. LocalDateTime right = ofEpochMS(jin.nextLong());
  258. List<Timestamp<Integer>> res = scheduler.getAll(left, right);
  259. Collections.sort(res);
  260. System.out.println(left + " <: " + print(res) + " >: " + right);
  261. }
  262. System.out.println("test");
  263. List<Timestamp<Integer>> res = scheduler.getAll(ofEpochMS(0), ofEpochMS(Long.MAX_VALUE));
  264. Collections.sort(res);
  265. System.out.println(print(res));
  266. forRemoval.forEach(scheduler::remove);
  267. res = scheduler.getAll(ofEpochMS(0), ofEpochMS(Long.MAX_VALUE));
  268. Collections.sort(res);
  269. System.out.println(print(res));
  270. }
  271. }
  272.  
  273. private static LocalDateTime ofEpochMS(long ms) {
  274. return LocalDateTime.ofInstant(Instant.ofEpochMilli(ms), ZoneId.systemDefault());
  275. }
  276.  
  277. private static <T> String print(List<Timestamp<T>> res) {
  278. if (res == null || res.size() == 0) return "NONE";
  279. return res.stream()
  280. .map(each -> each.getElement().toString())
  281. .collect(Collectors.joining(", "));
  282. }
  283.  
  284. }
  285.  
  286. import java.util.LinkedList;
  287. import java.util.Scanner;
  288.  
  289. class EmptyQueueException extends Exception{}
  290.  
  291. class Node<T>{
  292. T element;
  293. Node<T> succ;
  294. public Node(T _element, Node<T> _succ){
  295. element = _element;
  296. succ = _succ;
  297. }
  298. public T getElement(){
  299. return element;
  300. }
  301. public Node<T> getNext(){
  302. return succ;
  303. }
  304. public void setNext(Node<T> next){
  305. succ = next;
  306. }
  307. }
  308. class Queue<T>{
  309. Node<T> first;
  310. public Queue(){
  311. first = null;
  312. }
  313. public boolean isEmpty(){
  314. if (first == null)
  315. return true;
  316. return false;
  317. }
  318. public void enqueue(T element){
  319. Node<T> ins = new Node<T>(element, first);
  320. first = ins;
  321. }
  322.  
  323. public T dequeue() throws EmptyQueueException{
  324. if(first == null) throw new EmptyQueueException();
  325. if(first.getNext() == null){
  326. T ret = first.getElement();
  327. first = null;
  328. return ret;
  329. }
  330. Node<T> curr = first;
  331. T ret = null;
  332. while(curr.getNext().getNext()!=null){
  333. curr = curr.getNext();
  334. }
  335. ret = curr.getNext().getElement();
  336. curr.setNext(null);
  337. return ret;
  338. }
  339.  
  340. public T peek() throws EmptyQueueException{
  341. if(first == null) throw new EmptyQueueException();
  342. Node<T> curr = first;
  343. T ret = null;
  344. while(curr.getNext()!=null){
  345. curr = curr.getNext();
  346. }
  347. ret = curr.getElement();
  348. return ret;
  349. }
  350. public T inspect() throws EmptyQueueException{
  351. if(first == null) throw new EmptyQueueException();
  352. return first.getElement();
  353. }
  354. public int count(){
  355. if(first == null) return 0;
  356. Node<T> curr = first;
  357. int counter = 1;
  358. while(curr.getNext()!=null){
  359. counter++;
  360. curr = curr.getNext();
  361. }
  362. return counter;
  363. }
  364. }
  365.  
  366. public class QueueTest {
  367.  
  368.  
  369. public static void main(String[] args) throws EmptyQueueException {
  370. Scanner jin = new Scanner(System.in);
  371. int k = jin.nextInt();
  372. if ( k == 0 ) { //Simple test case with one int element
  373. int t = jin.nextInt();
  374. Queue<Integer> queue = new Queue<Integer>();
  375. System.out.println("Queue empty? - "+queue.isEmpty());
  376. System.out.println("Queue count? - "+queue.count());
  377. System.out.println("Queue enqueue "+t);
  378. queue.enqueue(t);
  379. System.out.println("Queue empty? - "+queue.isEmpty());
  380. System.out.println("Queue count? - "+queue.count());
  381. System.out.println("Queue dequeue? - "+queue.dequeue());
  382. System.out.println("Queue empty? - "+queue.isEmpty());
  383. System.out.println("Queue count? - "+queue.count());
  384. }
  385. if ( k == 1 ) { //a more complex test with strings
  386. Queue<String> queue = new Queue<String>();
  387. int counter = 0;
  388. while ( jin.hasNextInt() ) {
  389. String t = jin.next();
  390. queue.enqueue(t);
  391. ++counter;
  392. }
  393. for ( int i = 0 ; i < counter ; ++i ) {
  394. System.out.println(queue.dequeue());
  395. }
  396. queue.enqueue(jin.next());
  397. System.out.println("Queue inspect? - "+queue.inspect());
  398. System.out.println("Queue peek? - "+queue.peek());
  399. queue.enqueue(queue.dequeue());
  400. queue.enqueue(jin.next());
  401. System.out.println("Queue inspect? - "+queue.inspect());
  402. System.out.println("Queue peek? - "+queue.peek());
  403. }
  404. if ( k == 2 ) {
  405. Queue<String> queue = new Queue<String>();
  406. String next = "";
  407. int counter = 0;
  408. while ( true ) {
  409. next = jin.next();
  410. if ( next.equals("stop") ) break;
  411. queue.enqueue(next);
  412. ++counter;
  413. }
  414. while ( !queue.isEmpty() ) {
  415. if ( queue.count()<counter) System.out.print(" ");
  416. System.out.print(queue.dequeue());
  417. }
  418. }
  419. if ( k == 3 ) { //random testing
  420. Queue<Double> queue = new Queue<Double>();
  421. LinkedList<Double> java_queue = new LinkedList<Double>();
  422. boolean flag = true;
  423. int n = jin.nextInt();
  424. for ( int i = 0 ; i < n ; ++i ) {
  425. double q = Math.random();
  426. if ( q < 0.5 ) {
  427. double t = Math.random();
  428. queue.enqueue(t);
  429. java_queue.addFirst(t);
  430. }
  431. if ( q < 0.8&&q >= 0.5 ) {
  432. if ( ! java_queue.isEmpty() ) {
  433. double t1 = java_queue.removeLast();
  434. double t2 = queue.dequeue();
  435. flag &= t1==t2;
  436. }
  437. else {
  438. flag &= java_queue.isEmpty()==queue.isEmpty();
  439. }
  440. }
  441. if ( q < 0.9 && q >= 0.8 ) {
  442. if ( ! java_queue.isEmpty() ) {
  443. double t1 = java_queue.peekLast();
  444. double t2 = queue.peek();
  445. flag &= t1==t2;
  446. }
  447. else {
  448. flag &= java_queue.isEmpty()==queue.isEmpty();
  449. }
  450. }
  451. if ( q < 1 && q >= 0.9 ) {
  452. if ( ! java_queue.isEmpty() ) {
  453. double t1 = java_queue.peekFirst();
  454. double t2 = queue.inspect();
  455. flag &= t1==t2;
  456. }
  457. else {
  458. flag &= java_queue.isEmpty()==queue.isEmpty();
  459. }
  460. }
  461. flag &= java_queue.size()==queue.count();
  462. }
  463. System.out.println("Compared to the control queue the results were the same? - "+flag);
  464. }
  465. if ( k == 4 ) { //performance testing
  466. Queue<Double> queue = new Queue<Double>();
  467. int n = jin.nextInt();
  468. for ( int i = 0 ; i < n ; ++i ) {
  469. if ( Math.random() < 0.5 ) {
  470. queue.enqueue(Math.random());
  471. }
  472. else {
  473. if ( ! queue.isEmpty() ) {
  474. queue.dequeue();
  475. }
  476. }
  477. }
  478. System.out.println("You implementation finished in less then 3 seconds, well done!");
  479. }
  480. if ( k == 5 ) { //Exceptions testing
  481. Queue<String> queue = new Queue<String>();
  482. try {
  483. queue.dequeue();
  484. }
  485. catch ( Exception e ) {
  486. System.out.println(e.getClass().getSimpleName());
  487. }
  488. try {
  489. queue.peek();
  490. }
  491. catch ( Exception e ) {
  492. System.out.println(e.getClass().getSimpleName());
  493. }
  494. try {
  495. queue.inspect();
  496. }
  497. catch ( Exception e ) {
  498. System.out.println(e.getClass().getSimpleName());
  499. }
  500. }
  501. }
  502.  
  503. }
  504.  
  505. import java.util.Scanner;
  506. import java.util.LinkedList;
  507.  
  508. class ResizableArray<T>{
  509. private T[] elements;
  510. private int numberElements;
  511. public ResizableArray(){
  512. elements = (T[]) new Object[10];
  513. numberElements = 0;
  514. }
  515. public void addElement(T element){
  516. if(numberElements == elements.length){
  517. T [] temp = (T[]) new Object [elements.length+10];
  518. for(int i = 0; i < numberElements; i ++){
  519. temp[i] = elements[i];
  520. }
  521. elements = temp;
  522. }
  523. elements[numberElements] = element;
  524. numberElements++;
  525. }
  526. public boolean removeElement(T element){
  527. for(int i = 0; i < numberElements; i++){
  528. if ( elements[i].equals(element)){
  529. for(int j = i; j <numberElements-1;j++)
  530. elements[j] = elements[j+1];
  531. numberElements--;
  532. return true;
  533. }
  534. }
  535. return false;
  536. }
  537. public boolean contains(T element){
  538. for(int i = 0; i < numberElements; i++){
  539. if ( elements[i].equals(element)){
  540. return true;
  541. }
  542. }
  543. return false;
  544. }
  545. public int count(){
  546. return numberElements;
  547. }
  548. public Object[] toArray(){
  549. T [] ret = (T[]) new Object [numberElements];
  550. for(int i = 0;i < numberElements; i++){
  551. ret[i]=elements[i];
  552. }
  553. return ret;
  554. }
  555. public boolean isEmpty(){
  556. if(numberElements == 0) return true;
  557. return false;
  558. }
  559. public T elementAt(int index){
  560. if(index < 0 || index >= numberElements) throw new ArrayIndexOutOfBoundsException();
  561. return elements[index];
  562. }
  563. public static <T> void copyAll(ResizableArray<? super T> dest, ResizableArray<? extends T> src){
  564. int c = src.count();
  565. for (int i = 0; i < c; i++)
  566. dest.addElement(src.elementAt(i));
  567. }
  568. }
  569.  
  570. class IntegerArray extends ResizableArray<Integer>{
  571. public double sum(){
  572. int ret = 0;
  573. for(int i = 0; i < this.count(); i++){
  574. ret += this.elementAt(i);
  575. }
  576. return ret;
  577. }
  578. public double mean(){
  579. return this.sum()/this.count();
  580. }
  581. public int countNonZero(){
  582. int counter = 0;
  583. for(int i = 0; i < this.count(); i++){
  584. if(this.elementAt(i) != 0)
  585. counter++;
  586. }
  587. return counter;
  588. }
  589. public IntegerArray distinct(){
  590. IntegerArray ret = new IntegerArray();
  591. for(int i = 0; i < this.count(); i++){
  592. if(!ret.contains(this.elementAt(i)))
  593. ret.addElement(this.elementAt(i));
  594. }
  595. return ret;
  596. }
  597. public IntegerArray increment(int offset){
  598. IntegerArray ret = new IntegerArray();
  599. for(int i = 0; i < this.count(); i++){
  600. ret.addElement(this.elementAt(i)+offset);
  601. }
  602. return ret;
  603. }
  604. }
  605.  
  606. public class ResizableArrayTest {
  607.  
  608. public static void main(String[] args) {
  609. Scanner jin = new Scanner(System.in);
  610. int test = jin.nextInt();
  611. if ( test == 0 ) { //test ResizableArray on ints
  612. ResizableArray<Integer> a = new ResizableArray<Integer>();
  613. System.out.println(a.count());
  614. int first = jin.nextInt();
  615. a.addElement(first);
  616. System.out.println(a.count());
  617. int last = first;
  618. while ( jin.hasNextInt() ) {
  619. last = jin.nextInt();
  620. a.addElement(last);
  621. }
  622. System.out.println(a.count());
  623. System.out.println(a.contains(first));
  624. System.out.println(a.contains(last));
  625. System.out.println(a.removeElement(first));
  626. System.out.println(a.contains(first));
  627. System.out.println(a.count());
  628. }
  629. if ( test == 1 ) { //test ResizableArray on strings
  630. ResizableArray<String> a = new ResizableArray<String>();
  631. System.out.println(a.count());
  632. String first = jin.next();
  633. a.addElement(first);
  634. System.out.println(a.count());
  635. String last = first;
  636. for ( int i = 0 ; i < 4 ; ++i ) {
  637. last = jin.next();
  638. a.addElement(last);
  639. }
  640. System.out.println(a.count());
  641. System.out.println(a.contains(first));
  642. System.out.println(a.contains(last));
  643. System.out.println(a.removeElement(first));
  644. System.out.println(a.contains(first));
  645. System.out.println(a.count());
  646. ResizableArray<String> b = new ResizableArray<String>();
  647. ResizableArray.copyAll(b, a);
  648. System.out.println(b.count());
  649. System.out.println(a.count());
  650. System.out.println(a.contains(first));
  651. System.out.println(a.contains(last));
  652. System.out.println(b.contains(first));
  653. System.out.println(b.contains(last));
  654. ResizableArray.copyAll(b, a);
  655. System.out.println(b.count());
  656. System.out.println(a.count());
  657. System.out.println(a.contains(first));
  658. System.out.println(a.contains(last));
  659. System.out.println(b.contains(first));
  660. System.out.println(b.contains(last));
  661. System.out.println(b.removeElement(first));
  662. System.out.println(b.contains(first));
  663. System.out.println(b.removeElement(first));
  664. System.out.println(b.contains(first));
  665.  
  666. System.out.println(a.removeElement(first));
  667. ResizableArray.copyAll(b, a);
  668. System.out.println(b.count());
  669. System.out.println(a.count());
  670. System.out.println(a.contains(first));
  671. System.out.println(a.contains(last));
  672. System.out.println(b.contains(first));
  673. System.out.println(b.contains(last));
  674. }
  675. if ( test == 2 ) { //test IntegerArray
  676. IntegerArray a = new IntegerArray();
  677. System.out.println(a.isEmpty());
  678. while ( jin.hasNextInt() ) {
  679. a.addElement(jin.nextInt());
  680. }
  681. jin.next();
  682. System.out.println(a.sum());
  683. System.out.println(a.mean());
  684. System.out.println(a.countNonZero());
  685. System.out.println(a.count());
  686. IntegerArray b = a.distinct();
  687. System.out.println(b.sum());
  688. IntegerArray c = a.increment(5);
  689. System.out.println(c.sum());
  690. if ( a.sum() > 100 )
  691. ResizableArray.copyAll(a, a);
  692. else
  693. ResizableArray.copyAll(a, b);
  694. System.out.println(a.sum());
  695. System.out.println(a.removeElement(jin.nextInt()));
  696. System.out.println(a.sum());
  697. System.out.println(a.removeElement(jin.nextInt()));
  698. System.out.println(a.sum());
  699. System.out.println(a.removeElement(jin.nextInt()));
  700. System.out.println(a.sum());
  701. System.out.println(a.contains(jin.nextInt()));
  702. System.out.println(a.contains(jin.nextInt()));
  703. }
  704. if ( test == 3 ) { //test insanely large arrays
  705. LinkedList<ResizableArray<Integer>> resizable_arrays = new LinkedList<ResizableArray<Integer>>();
  706. for ( int w = 0 ; w < 500 ; ++w ) {
  707. ResizableArray<Integer> a = new ResizableArray<Integer>();
  708. int k = 2000;
  709. int t = 1000;
  710. for ( int i = 0 ; i < k ; ++i ) {
  711. a.addElement(i);
  712. }
  713.  
  714. a.removeElement(0);
  715. for ( int i = 0 ; i < t ; ++i ) {
  716. a.removeElement(k-i-1);
  717. }
  718. resizable_arrays.add(a);
  719. }
  720. System.out.println("You implementation finished in less then 3 seconds, well done!");
  721. }
  722. }
  723.  
  724. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement