Quick actions

cmd+k|ctrl+k

Navigation

Languages

Test 2 Java

Snippet info

Language

Java

Visibility

public

Author

manxglot

Created

2020-02-14T16:10:47Z

Updated

2020-02-14T16:17:50Z

import java.util.List;
import java.util.ArrayList;

public class Main {


    public static void main(String[] args) throws Exception {
    
        Deque queue = new Deque();

        int random = (int)(Math.random() * 100);

        queue.addLast(10);
        queue.addLast(20);
        queue.addLast(random);
        queue.addLast(40);
        queue.addLast(50);

        for(int i = 0; i < 2; i++) {

            assertTrue(get(queue, 0) == 10);
            assertTrue(get(queue, 1) == 20);
            assertTrue(get(queue, 2) == random);
            assertTrue(get(queue, 4) == 50);
            assertTrue(get(queue, 3) == 40);

            try {
				get(queue, 5);
				assertTrue(false);
			} catch(Exception e) {
				assertTrue(true);
			}
		}
	}

    public static void assertTrue(boolean v) {
        if(!v) {
            Thread.dumpStack();
            System.exit(0);
        }
    }


    public static int get(Deque queue, int index) throws Exception 
    {     
       	// 1) Only fill in your code in this method
		// 2) Do not modify anything else
		// 3) Use of 'new' keyword is not allowed
		// 4) Do not use reflection
		// 5) Do not use string concatenation
		// 6) If your code cannot compile or fails the test case in 'main()', you will NOT receive a response from us
        return queue.getList().get(index);      
    }
}

class Deque {
    private List<Integer> items;

    public Deque() {
        items = new ArrayList<Integer>();
    }

    public void addFirst(int item) {
        items.add(0, item);
    }

    public void addLast(int item) {
        items.add(item);
    }

    public int removeFirst() {
        if(isEmpty()) throw new RuntimeException();
        return items.remove(0);
    }

    public int removeLast() {
        if(isEmpty()) throw new RuntimeException();
        return items.remove(items.size() - 1);
    }

    public boolean isEmpty() {
        return items.size() == 0;
    }

    public List<Integer> getList()
    {
        return items;       
    }
}
INFO