JAVA Compiliation

Ads

 
 

Share on Google+Share on Google+

Chelsea
JAVA Compiliation
0 Answer(s)      5 years and 6 months ago
Posted in : Java Beginners

How do I code and compile in the areas that say "IMPLEMENT ME"?

import java.util.Scanner;

import java.util.Stack;

class EvalException extends Exception{ public EvalException(String msg) { super(msg); }

interface Expr {

   public int eval() throws EvalException;

}

class Atom implements Expr {

  String value;

  public Atom(String value) { this.value = value; }

  public String toString() { return value; }

  public int eval() throws EvalException {

  /* IMPLEMENT ME
   * We'll assume Atoms represent integers when we eval them, so try to
   * parse the value as an int.  If this can't be done, we'll want to throw
   * an EvalException with the proper error message.
   */
  }

}

class List<E> implements Expr {

  private Node<E> head;

  public List(E... elements) {
     head = null;

     for(int i = elements.length - 1; i >= 0; i--) {
        head = new Node<E>(elements[i], head);
     }
  }

  public void append(E element) {
     if (head == null) {
        head = new Node<E>(element, null);
     }
     else {
        Node<E> last = head;
        while (last.link != null) last = last.link;
        last.link = new Node<E>(element, null);
     }
  }

  public String toString() {
     String s = "( ";
     Node<E> elements = head;
     while (elements != null) {
        s += elements.data + " ";
        elements = elements.link;
     }
     return s + ")";
  }

  public void evalAndPrintEach() {
     Node<Expr> current = (Node<Expr>) head;
     while (current != null) {
        try {
           System.out.println( ((Expr) current.data).eval() );
        }
        catch (EvalException e) {
           System.out.println(e);
        }
        current = current.link;
     }
  }

  private Node<Integer> evalArgs(Node<Expr> xs) throws EvalException {
     if (xs == null) return null;
        return new Node<Integer>(xs.data.eval(), evalArgs(xs.link));
  }

  public int eval() throws EvalException {
     Node<Expr> elts = (Node<Expr>) head;
     if (elts == null) throw new EvalException("Empty list.");
        String op = ((Atom) elts.data).value;

     /* IMPLEMENT ME
      * To evaluate a (non-empty) List<Expr>, we look at the first element
      * (which we assume is an Atom), evaluate each of the remaining elements
      * (using evalArgs), then apply the appropriate operator to the results.
      * See the examples in the handout.
      *
      * Attempting to evaluate an empty list results in an error.  If op isn't
      * one of "+", "-", "*", "/", then this too is an error.
      */
  }

  private int add(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Find the sum of xs using a right fold.
      */
  }

  private int mul(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Find the product of xs using a left fold.
      */
  }

  private int sub(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Calculate the result by using a tail-recursive helper method.
      */
  }

  private int div(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Calculate the result iteratively.  If xs doesn't have at least 2
      * elements, throw an Exception.
      */
  }

  private class Node<E> {
     E data;
     Node<E> link;

     public Node (E data, Node<E> link) {
        this.data = data;
        this.link = link;
     }
  }

}

class UnclosedListException extends Exception { }

class Lisp {

  public static List<Expr> parse(String[] tokens) throws Exception {
     Stack<List<Expr>> stack = new Stack<List<Expr>>();

     stack.push(new List<Expr>());

     for (String token : tokens) {
        if (token.equals("(")) {
           stack.push(new List<Expr>());
        }

        else if (token.equals(")")) {
           List<Expr> toAppend = stack.pop();
           stack.peek().append(toAppend);
        }

        else {
           stack.peek().append(new Atom(token));
        }
     }

     if (stack.size() > 1)
        throw new UnclosedListException();

     return stack.pop();
  }

  public static void main(String[] args) throws Exception {
     Scanner in = new Scanner(System.in);

     while(true) {
        System.out.print("lisp> ");
        String line = in.nextLine();
        try {
           parse(line.split("\\s+")).evalAndPrintEach();
        }
        catch (Exception e) {
           System.out.println("Parsing error.");
        }
     }
  }

}

How do I code and compile in the areas that say "IMPLEMENT ME"?

Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
JAVA Compiliation
JAVA Compiliation  How do I code and compile in the areas that say "IMPLEMENT ME"? import java.util.Scanner; import java.util.Stack; class EvalException extends Exception{ public EvalException(String msg) { super(msg
Advertisements
java
java  diff bt core java and java
java
java  what is java
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
Java
Java   Whether Java is Programming Language or it is SOftware
Tutorials   
Java Spring Hibernate Struts Training What type of programming language is Java? How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017

Ads

 
Advertisement null

Ads