Advertisement
Guest User

LAB5

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