Filip_Markoski

[NP] First Midterm Practice

Nov 17th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. import java.io.*;
  2. import java.text.ChoiceFormat;
  3. import java.text.MessageFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.time.LocalDateTime;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12.  
  13.  
  14. /*
  15. ============================================================*
  16. */
  17.  
  18. class Quick {
  19.     List<String> list;
  20.  
  21.     Quick(String strings[]) {
  22.         list = new ArrayList<>(Arrays.asList(strings));
  23.     }
  24.  
  25.     public String[] returnAsArray() {
  26.         System.out.println("List" + list);
  27.         String result[] = new String[list.size()];
  28.         result = list.toArray(result);
  29.         return result;
  30.     }
  31. }
  32.  
  33. /*
  34. ============================================================*
  35. */
  36.  
  37. class Test {
  38.     /* All of these are public by default */
  39.     boolean aBoolean;
  40.     int anInt;
  41.     float aFloat;
  42.     double aDouble;
  43.     long aLong;
  44.     String aString;
  45.     Object anObject;
  46.     ArrayList<String> strings;
  47.     Integer anIntegerArray[];
  48.  
  49.     public Test(int anInt) {
  50.         this.anInt = anInt;
  51.     }
  52.  
  53.     @Override
  54.     public boolean equals(Object o) {
  55.         if (this == o) return true;
  56.         if (o == null || getClass() != o.getClass()) return false;
  57.  
  58.         Test test = (Test) o;
  59.  
  60.         if (aBoolean != test.aBoolean) return false;
  61.         if (anInt != test.anInt) return false;
  62.         if (Float.compare(test.aFloat, aFloat) != 0) return false;
  63.         if (Double.compare(test.aDouble, aDouble) != 0) return false;
  64.         if (aLong != test.aLong) return false;
  65.         if (aString != null ? !aString.equals(test.aString) : test.aString != null) return false;
  66.         if (anObject != null ? !anObject.equals(test.anObject) : test.anObject != null) return false;
  67.         if (strings != null ? !strings.equals(test.strings) : test.strings != null) return false;
  68.         // Probably incorrect - comparing Object[] arrays with Arrays.equals
  69.         return Arrays.equals(anIntegerArray, test.anIntegerArray);
  70.     }
  71.  
  72.     @Override
  73.     public int hashCode() {
  74.         int result;
  75.         long temp;
  76.         result = (aBoolean ? 1 : 0);
  77.         result = 31 * result + anInt;
  78.         result = 31 * result + (aFloat != +0.0f ? Float.floatToIntBits(aFloat) : 0);
  79.         temp = Double.doubleToLongBits(aDouble);
  80.         result = 31 * result + (int) (temp ^ (temp >>> 32));
  81.         result = 31 * result + (int) (aLong ^ (aLong >>> 32));
  82.         result = 31 * result + (aString != null ? aString.hashCode() : 0);
  83.         result = 31 * result + (anObject != null ? anObject.hashCode() : 0);
  84.         result = 31 * result + (strings != null ? strings.hashCode() : 0);
  85.         result = 31 * result + Arrays.hashCode(anIntegerArray);
  86.         return result;
  87.     }
  88. }
  89.  
  90. /*
  91. ============================================================*
  92. */
  93.  
  94. /* Practice decorator with generics */
  95. interface Shape {
  96.     void draw();
  97.  
  98.     default String getType() {
  99.         return getClass().getName();
  100.     }
  101. }
  102.  
  103. class Rectangle implements Shape {
  104.  
  105.     @Override
  106.     public void draw() {
  107.         System.out.println("Shape: Rectangle");
  108.     }
  109. }
  110.  
  111. class Circle implements Shape {
  112.     @Override
  113.     public void draw() {
  114.         System.out.println("Shape: Circle");
  115.     }
  116. }
  117.  
  118. abstract class ShapeDecorator implements Shape {
  119.     protected Shape decoratedShape;
  120.  
  121.     public ShapeDecorator(Shape decoratedShape) {
  122.         this.decoratedShape = decoratedShape;
  123.     }
  124.  
  125.     @Override
  126.     public void draw() {
  127.         decoratedShape.draw();
  128.     }
  129. }
  130.  
  131. class RedShapeDecorator extends ShapeDecorator {
  132.  
  133.     public RedShapeDecorator(Shape decoratedShape) {
  134.         super(decoratedShape);
  135.     }
  136.  
  137.     @Override
  138.     public void draw() {
  139.         super.draw();
  140.         System.out.println("Border Color: Red");
  141.     }
  142. }
  143.  
  144. /*
  145. ============================================================*
  146. */
  147.  
  148. interface Item {
  149.     String add();
  150. }
  151.  
  152. class ItemDecorator implements Item {
  153.     protected Item item;
  154.  
  155.     public ItemDecorator(Item item) {
  156.         this.item = item;
  157.     }
  158.  
  159.     @Override
  160.     public String add() {
  161.         return this.item.add();
  162.     }
  163. }
  164.  
  165. class BaseItem implements Item {
  166.  
  167.     @Override
  168.     public String add() {
  169.         return String.format(" %s", getClass().getName());
  170.     }
  171. }
  172.  
  173. class Coffee extends ItemDecorator {
  174.     public Coffee(Item item) {
  175.         super(item);
  176.     }
  177.  
  178.     @Override
  179.     public String add() {
  180.         StringBuffer sb = new StringBuffer();
  181.         sb.append(super.add());
  182.         sb.append(String.format(" %s", getClass().getName()));
  183.         return sb.toString();
  184.     }
  185. }
  186.  
  187. class Milk extends ItemDecorator {
  188.     public Milk(Item item) {
  189.         super(item);
  190.     }
  191.  
  192.     @Override
  193.     public String add() {
  194.         StringBuffer sb = new StringBuffer();
  195.         sb.append(super.add());
  196.         sb.append(String.format(" %s", getClass().getName()));
  197.         return sb.toString();
  198.     }
  199. }
  200.  
  201. class Cream extends ItemDecorator {
  202.     public Cream(Item item) {
  203.         super(item);
  204.     }
  205.  
  206.     @Override
  207.     public String add() {
  208.         StringBuffer sb = new StringBuffer();
  209.         sb.append(super.add());
  210.         sb.append(String.format(" %s", getClass().getName()));
  211.         return sb.toString();
  212.     }
  213. }
  214.  
  215. public class FileTests {
  216.  
  217.     static public void avoidElement(Object array[], int index) {
  218.         int size = array.length;
  219.         if (0 <= index && index <= size) {
  220.             int elementsToMove = size - 1 - index;
  221.             if (elementsToMove > 0) {
  222.             /* This will copy the sub-array over the index element */
  223.                 System.arraycopy(array, index + 1, array, index, elementsToMove);
  224.             }
  225.             array[--size] = null;
  226.         }
  227.         return;
  228.     }
  229.  
  230.     public static void main(String args[]) throws IOException, ParseException {
  231.  
  232.         String array[] = {"jas", "sum", "filip"};
  233.         Quick quick = new Quick(array);
  234.         String result[] = quick.returnAsArray();
  235.         for (int i = 0; i < result.length; i++) {
  236.             System.out.println("Result" + result[i]);
  237.         }
  238.  
  239.         /*LocalDateTime localDateTime = LocalDateTime.now();*/
  240.  
  241.         /*String date = "12:23:34:456";
  242.  
  243.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
  244.         Date thisDate = null;
  245.         try{
  246.             thisDate = simpleDateFormat.parse(date);
  247.         } catch (ParseException e){
  248.             e.printStackTrace();
  249.         }
  250.  
  251.         System.out.println(thisDate);
  252.         */
  253.  
  254.         /*
  255.         Item item = new Coffee(new Coffee(new Cream(new Milk(new BaseItem()))));
  256.         System.out.println(item.add());
  257.         */
  258.  
  259.         /*
  260.         Shape shape = new Circle();
  261.         System.out.println(shape.getType());
  262.         shape = new Rectangle();
  263.         System.out.println(shape.getType());
  264.  
  265.         System.out.println(System.lineSeparator());
  266.  
  267.         shape = new RedShapeDecorator(new Circle());
  268.         System.out.println(shape.getType());
  269.         shape.draw();*/
  270.  
  271.         /*
  272.         *//* Stream Practice *//*
  273.         Integer array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  274.  
  275.         *//* Print all even numbers *//*
  276.         Arrays.stream(array).filter(number -> number % 2 == 0).forEach(System.out::print);
  277.         */
  278.         /*Integer array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  279.         System.out.println(Arrays.toString(array));
  280.         avoidElement(array, 10);
  281.         System.out.println(Arrays.toString(array));*/
  282.  
  283.         /*Test test = new Test(12);
  284.         System.out.print(test.anInt);*/
  285.  
  286.         /*File fileName = new File("archive.txt");
  287.         try(PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))){
  288.             for (int i = 0; i < 10; i++) {
  289.                 pw.println(i);
  290.             }
  291.             pw.close();
  292.         }
  293.  
  294.         try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
  295.             String line;
  296.             StringBuffer sb = new StringBuffer();
  297.             while ((line = br.readLine()) != null){
  298.                 sb.append(line);
  299.                 sb.append("\n");
  300.             }
  301.             System.out.print(sb.toString());
  302.         }*/
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment