Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.text.ChoiceFormat;
- import java.text.MessageFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import java.util.stream.Collectors;
- /*
- ============================================================*
- */
- class Quick {
- List<String> list;
- Quick(String strings[]) {
- list = new ArrayList<>(Arrays.asList(strings));
- }
- public String[] returnAsArray() {
- System.out.println("List" + list);
- String result[] = new String[list.size()];
- result = list.toArray(result);
- return result;
- }
- }
- /*
- ============================================================*
- */
- class Test {
- /* All of these are public by default */
- boolean aBoolean;
- int anInt;
- float aFloat;
- double aDouble;
- long aLong;
- String aString;
- Object anObject;
- ArrayList<String> strings;
- Integer anIntegerArray[];
- public Test(int anInt) {
- this.anInt = anInt;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Test test = (Test) o;
- if (aBoolean != test.aBoolean) return false;
- if (anInt != test.anInt) return false;
- if (Float.compare(test.aFloat, aFloat) != 0) return false;
- if (Double.compare(test.aDouble, aDouble) != 0) return false;
- if (aLong != test.aLong) return false;
- if (aString != null ? !aString.equals(test.aString) : test.aString != null) return false;
- if (anObject != null ? !anObject.equals(test.anObject) : test.anObject != null) return false;
- if (strings != null ? !strings.equals(test.strings) : test.strings != null) return false;
- // Probably incorrect - comparing Object[] arrays with Arrays.equals
- return Arrays.equals(anIntegerArray, test.anIntegerArray);
- }
- @Override
- public int hashCode() {
- int result;
- long temp;
- result = (aBoolean ? 1 : 0);
- result = 31 * result + anInt;
- result = 31 * result + (aFloat != +0.0f ? Float.floatToIntBits(aFloat) : 0);
- temp = Double.doubleToLongBits(aDouble);
- result = 31 * result + (int) (temp ^ (temp >>> 32));
- result = 31 * result + (int) (aLong ^ (aLong >>> 32));
- result = 31 * result + (aString != null ? aString.hashCode() : 0);
- result = 31 * result + (anObject != null ? anObject.hashCode() : 0);
- result = 31 * result + (strings != null ? strings.hashCode() : 0);
- result = 31 * result + Arrays.hashCode(anIntegerArray);
- return result;
- }
- }
- /*
- ============================================================*
- */
- /* Practice decorator with generics */
- interface Shape {
- void draw();
- default String getType() {
- return getClass().getName();
- }
- }
- class Rectangle implements Shape {
- @Override
- public void draw() {
- System.out.println("Shape: Rectangle");
- }
- }
- class Circle implements Shape {
- @Override
- public void draw() {
- System.out.println("Shape: Circle");
- }
- }
- abstract class ShapeDecorator implements Shape {
- protected Shape decoratedShape;
- public ShapeDecorator(Shape decoratedShape) {
- this.decoratedShape = decoratedShape;
- }
- @Override
- public void draw() {
- decoratedShape.draw();
- }
- }
- class RedShapeDecorator extends ShapeDecorator {
- public RedShapeDecorator(Shape decoratedShape) {
- super(decoratedShape);
- }
- @Override
- public void draw() {
- super.draw();
- System.out.println("Border Color: Red");
- }
- }
- /*
- ============================================================*
- */
- interface Item {
- String add();
- }
- class ItemDecorator implements Item {
- protected Item item;
- public ItemDecorator(Item item) {
- this.item = item;
- }
- @Override
- public String add() {
- return this.item.add();
- }
- }
- class BaseItem implements Item {
- @Override
- public String add() {
- return String.format(" %s", getClass().getName());
- }
- }
- class Coffee extends ItemDecorator {
- public Coffee(Item item) {
- super(item);
- }
- @Override
- public String add() {
- StringBuffer sb = new StringBuffer();
- sb.append(super.add());
- sb.append(String.format(" %s", getClass().getName()));
- return sb.toString();
- }
- }
- class Milk extends ItemDecorator {
- public Milk(Item item) {
- super(item);
- }
- @Override
- public String add() {
- StringBuffer sb = new StringBuffer();
- sb.append(super.add());
- sb.append(String.format(" %s", getClass().getName()));
- return sb.toString();
- }
- }
- class Cream extends ItemDecorator {
- public Cream(Item item) {
- super(item);
- }
- @Override
- public String add() {
- StringBuffer sb = new StringBuffer();
- sb.append(super.add());
- sb.append(String.format(" %s", getClass().getName()));
- return sb.toString();
- }
- }
- public class FileTests {
- static public void avoidElement(Object array[], int index) {
- int size = array.length;
- if (0 <= index && index <= size) {
- int elementsToMove = size - 1 - index;
- if (elementsToMove > 0) {
- /* This will copy the sub-array over the index element */
- System.arraycopy(array, index + 1, array, index, elementsToMove);
- }
- array[--size] = null;
- }
- return;
- }
- public static void main(String args[]) throws IOException, ParseException {
- String array[] = {"jas", "sum", "filip"};
- Quick quick = new Quick(array);
- String result[] = quick.returnAsArray();
- for (int i = 0; i < result.length; i++) {
- System.out.println("Result" + result[i]);
- }
- /*LocalDateTime localDateTime = LocalDateTime.now();*/
- /*String date = "12:23:34:456";
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
- Date thisDate = null;
- try{
- thisDate = simpleDateFormat.parse(date);
- } catch (ParseException e){
- e.printStackTrace();
- }
- System.out.println(thisDate);
- */
- /*
- Item item = new Coffee(new Coffee(new Cream(new Milk(new BaseItem()))));
- System.out.println(item.add());
- */
- /*
- Shape shape = new Circle();
- System.out.println(shape.getType());
- shape = new Rectangle();
- System.out.println(shape.getType());
- System.out.println(System.lineSeparator());
- shape = new RedShapeDecorator(new Circle());
- System.out.println(shape.getType());
- shape.draw();*/
- /*
- *//* Stream Practice *//*
- Integer array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- *//* Print all even numbers *//*
- Arrays.stream(array).filter(number -> number % 2 == 0).forEach(System.out::print);
- */
- /*Integer array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- System.out.println(Arrays.toString(array));
- avoidElement(array, 10);
- System.out.println(Arrays.toString(array));*/
- /*Test test = new Test(12);
- System.out.print(test.anInt);*/
- /*File fileName = new File("archive.txt");
- try(PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))){
- for (int i = 0; i < 10; i++) {
- pw.println(i);
- }
- pw.close();
- }
- try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
- String line;
- StringBuffer sb = new StringBuffer();
- while ((line = br.readLine()) != null){
- sb.append(line);
- sb.append("\n");
- }
- System.out.print(sb.toString());
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment