Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote two programs and can\'t get either either to run. Please help. Part A. W

ID: 3541679 • Letter: I

Question

I wrote two programs and can't get either either to run. Please help.


Part A.


Write a test program that stores 5 million integers in a linked list and test the time to traverse the list using an iterator vs. using the get(index) method.


My Program:


import java.util.*;

public class IteratorsOnLinkedLists
{
public static void main(String[] args)
{
Random rnd = new Random();
LinkedList<Integer> list = new LinkedList<Integer>();
for(int i = 0; i < 5000000; i++)
list.add(rnd.nextInt());
System.out.printf("List contains %d values ", list.size());
Integer value = new Integer(0);
long start = System.currentTimeMillis();
for(int i = 0; i < 5000000; i++)
value = list.get(i);
long stop = System.currentTimeMillis();
System.out.printf("Elapsed time is %.3f seconds ", (stop-start)/1000);
ListIterator<Integer> listIterator = list.listIterator();
start = System.currentTimeMillis();
while(listIterator.hasNext())
value = listIterator.next();
stop = System.currentTimeMillis();
System.out.printf("Elapsed time is %.3f seconds ", (stop-start)/1000);

}
}


Part B:


A Java program contains various pairs of grouping symbols such as

Paratheses:( and )

Braces: { and }

Brackets:[ and ]

Single Quotes: ' and '

Double Quotes: " and "

< and >

Note that grouping symbols cannot overlap. For example (a{b)} is illegal.

Write  a program to check whether a Java source-code file has correct pairs of grouping symbols. Pass the source-code file name as a command-line argument.


My Program:


import java.util.*;
import java.io.*;

public class SymbolMatch
{
   public static void main(String[] args) throws Exception
   {
      Scanner input = new Scanner(new File(args [0]));
      Stack<Character> stack = new Stack<Character>();
      int count = 0;
      while(input.hasNext())
      {
         String line = input.nextLine();
         count++;
         System.out.println(count+" -> "+line);
         for(int i = 0; i < line.length(); i++)
         {
            char c = line.charAt(i);
            if(c == '"')
            {
               i += 1;
               while(line.charAt(i) != '"')
                  i += 1;
               continue;
            }
            if(c == ''')
            {
               i += 1;
               continue;
            }
            switch(c)
            {
               case '{':
                  stack.push(c);
                  System.out.printf("Character { pushed in line %d ", count);
                  break;
               case '(':
                  stack.push(c);
                  System.out.printf("Character ( pushed in line %d ", count);
                  break;
               case '[':
                  stack.push(c);
                  System.out.printf("Character [ pushed in line %d ", count);
                  break;
                case '<':
                  stack.push(c);
                  System.out.printf("Character < pushed in line %d ", count);
                  break;
               case '}':
                  if(stack.peek() == '{')
                     stack.pop();
                  else
                  {
                     System.out.println("Improper nesting detected");
                     System.exit(1);
                  }
                  break;
               case ')':
                  if(stack.peek() == '(')
                     stack.pop();
                  else
                  {
                     System.out.println("Improper nesting detected");
                     System.exit(1);
                  }
               case ']':
                  if(stack.peek() == '[')
                     stack.pop();
                  else
                  {
                     System.out.println("Improper nesting detected");
                     System.exit(1);
                  }
                  case '"':
                  if(stack.peek() == '"')
                     stack.pop();
                  else
                  {
                     System.out.println("Improper nesting detected");
                     System.exit(1);
                  }
                  case '>':
                  if(stack.peek() == '<')
                     stack.pop();
                  else
                  {
                     System.out.println("Improper nesting detected");
                     System.exit(1);
                  }

            }
         }
      }
   }
}

Explanation / Answer

http://www.chegg.com/homework-help/questions-and-answers/use-iterators-linked-list-write-test-program-stores-5-million-integers-linked-list-test-ti-q3677976