I find it difficult in this java programming language that's why I need your help to defend this code in my class.

I got this code from internet with comments and explanation. but there are some codes and explanation that I don't understand.

/* Read me:
 Note: As I have said this program has already comments.
   	If you see this "==" sign in a comment like this "//==", 
   	it means that is my question.
*------------------------------------------------------------------------------/  
 		 


/*   
   This module contains the recursive descent  
   parser that does not use variables.  
*/  
   
// Exception class for parser errors.  
class ParserException extends Exception {  
  String errStr; // describes the error 
	
 
  public ParserException(String str) { 
    errStr = str; 
  }   
  
  public String toString() {  
    return errStr; 
  }  
}  
  
class Parser {  //==what is the concept of parser?
  // These are the token types. 
  final int NONE = 0; 		//== why NONE is initially zero
  final int DELIMITER = 1; 	//== why DELIMITER is initially to 1	
  final int VARIABLE = 2; 	//== what is purpose of these token type
  final int NUMBER = 3; 
 
  // These are the types of syntax errors. 
  final int SYNTAX = 0; 	
  final int UNBALPARENS = 1;
  final int NOEXP = 2; 		
  final int DIVBYZERO = 3; 
 
  // This token indicates end-of-expression. 
  final String EOE = "\0"; //== purpose of this and why "\0"?
  						   //== Can you explain what is end-of-expression.
 
 	//== What is exp, expIndx, token and toktype and its purpose
  private String exp;   // refers to expression string  
  private int expIdx;   // current index into the expression  
  private String token; // holds current token  
  private int tokType;  // holds token's type  
  
  // Parser entry point.  
  public double evaluate(String expstr) throws ParserException 
  {  
    double result;  
    exp = expstr;  
    expIdx = 0;   
   
   
    getToken(); 
    if(token.equals(EOE)) 
     handleErr(NOEXP); // no expression present  
 
    // Parse and evaluate the expression. 
    result = evalExp2();  //== What is parse? and why result is equals to evalExp2
  
    if(!token.equals(EOE)) // last token must be EOE  //== Meaning of this 
      handleErr(SYNTAX);  //== Why we throw to SYNTAX
  
    return result;  //== Why we return to result
  }  
    
  // Add or subtract two terms.  
  private double evalExp2() throws ParserException 
  {  
    char op;  	
    double result; 		//== why we have so many result declaration
    double partialResult; //== purpose of this, Why we have this?
 
    result = evalExp3();  //== purpose of evalExp3
 
 
 //== Meaning of chartAt(0)
    while((op = token.charAt(0)) == '+' || op == '-') {  
      getToken();  
      partialResult = evalExp3();  //==why do we need to declare partialResult is equals to evalExp3? 
      switch(op) {  //== meaning of switch adn why we switch op?
        case '-':  
          result = result - partialResult;  
          break;  
        case '+':  
          result = result + partialResult;  
          break;  
      }  
    }  
    return result; 
  }  
    
  // Multiply or divide two factors.  
  private double evalExp3() throws ParserException 
  {  
    char op;  
    double result; 
    double partialResult;  
    
    result = evalExp4();
 
    while((op = token.charAt(0)) == '*' ||  
           op == '/' || op == '%') {  
      getToken();  
      partialResult = evalExp4();  
      switch(op) {  
        case '*':  
          result = result * partialResult;  
          break;  
        case '/':  
          if(partialResult == 0.0)  
            handleErr(DIVBYZERO);  
          result = result / partialResult;  
          break;  
        case '%':  
          if(partialResult == 0.0)  
            handleErr(DIVBYZERO);  
          result = result % partialResult;  
          break;  
      }  
    }  
    return result; 
  }  
    
  // Process an exponent.  
  private double evalExp4() throws ParserException 
  {  
    double result; 
    double partialResult; 
    double ex;  
    int t;  
    
    result = evalExp5();  
 
    if(token.equals("^")) {  //== Pls elaborate this exponent
      getToken();  
      partialResult = evalExp4();  
      ex = result;  //== What we have ex
      if(partialResult == 0.0) {  
        result = 1.0;  
      } else  
        for(t=(int)partialResult-1; t > 0; t--)  //== especially this one
        //== why t-1 and t--
          result = result * ex;  
    }  
    return result; 
  }  
    
  // Evaluate a unary + or -.  
  private double evalExp5() throws ParserException 
  {  
    double result; 
    String  op;  
 
    op = "";  
    if((tokType == DELIMITER) &&  
        token.equals("+") || token.equals("-")) {  
      op = token;  
      getToken();  
 
    }  
    result = evalExp6();  
 
    if(op.equals("-")) result = -result; 
 
    return result;  
  }  
    
  // Process a parenthesized expression.  
  private double evalExp6() throws ParserException 
  {  
    double result; 
 
    if(token.equals("(")) {  
      getToken();  
      result = evalExp2();  
      if(!token.equals(")"))  
        handleErr(UNBALPARENS);  
      getToken();  
    }  
    else result = atom();  
 
    return result; 
  }  
    
  // Get the value of a number.  
  private double atom() throws ParserException  
  {  
    double result; 
 
    switch(tokType) {  
      case NUMBER:  
        try {  // What is try command
          result = Double.parseDouble(token);  
        } catch (NumberFormatException exc) {  // What happen in this line
          handleErr(SYNTAX);  
        }  
        getToken();  
        break; 
      default:  
        handleErr(SYNTAX);  
        break;  
    }  
    return result; 
  }  
    
  // Handle an error.  
  private void handleErr(int error) throws ParserException 
  {  
    String[] err = {"Syntax Error","Unbalanced Parenthesesa","No Expression Present","Division by Zero"  
    };  
  
    throw new ParserException(err[error]);  
  }  
    
  // Obtain the next token.  
  private void getToken()  
  {  
    tokType = NONE;  
    token = "";  
     
    // Check for end of expression.  
    if(expIdx == exp.length()) { 
      token = EOE; 
      return; 
    } 
    
    // Skip over white space. 
    while(expIdx < exp.length() &&  
      Character.isWhitespace(exp.charAt(expIdx))) ++expIdx;  
  
    // Trailing whitespace ends expression. 
    if(expIdx == exp.length()) { 
      token = EOE; 
      return; 
    } 
  
    if(isDelim(exp.charAt(expIdx))) { // is operator  
      token += exp.charAt(expIdx);  
      expIdx++;  
      tokType = DELIMITER;  
    }  
    else if(Character.isLetter(exp.charAt(expIdx))) { // is variable  
      while(!isDelim(exp.charAt(expIdx))) {  
        token += exp.charAt(expIdx);  
        expIdx++;  
        if(expIdx >= exp.length()) break;  
      }  
      tokType = VARIABLE;  
    }  
    else if(Character.isDigit(exp.charAt(expIdx))) { // is number  
      while(!isDelim(exp.charAt(expIdx))) {  
        token += exp.charAt(expIdx);  
        expIdx++;  
        if(expIdx >= exp.length()) break;  //== why we use break
      }  
      tokType = NUMBER;  
    }  
    else { // unknown character terminates expression 
      token = EOE; 
      return; 
    } 
  }  
    
  // Return true if c is a delimiter.  
  private boolean isDelim(char c)  //== why we have char c
  {  
    if((" +-/*%^=()".indexOf(c) != -1))  //== Meaning of this line, why != -1? 
      return true;  //== Why return to true
    return false;  
  }  
    
} 


/*== If you think I missed something in my questions that so important please let me know what it is and explain what you mean.
	 
	 
	 Thanks for reading....
*/

Here's the link of explanation that I don't fully understand.

http://www.usaupload.net/d/yfxpm4fwp3x

---------------------------------------------------------------------------------------------------------

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class cal {

JLabel expression1 = new JLabel("Expression:");
JLabel result1 = new JLabel("Result: ");


JTextField expression_textf1 = new JTextField(22);
JTextField result_textf1 = new JTextField(10);


JButton clear1 = new JButton(" Clear ");





JButton calculate1 = new JButton(" Calculate");
JButton cancel1 = new JButton(" Cancel");

Parser p = new Parser();

public JPanel gui_2() {

JPanel northPanel1 = new JPanel();
JPanel northPanel2 = new JPanel();

northPanel1.setLayout(new GridLayout(2, 0));

northPanel1.add(expression1);
northPanel1.add(expression_textf1);
northPanel1.add(result1);
northPanel1.add(result_textf1);

northPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
northPanel2.add(northPanel1);

return northPanel2;

}

public JPanel gui_3() {


JPanel southPanel1 = new JPanel();
JPanel southPanel2 = new JPanel();

southPanel1.setLayout(new GridLayout(2, 2));


southPanel2.add(calculate1);
southPanel2.add(clear1);
southPanel2.add(cancel1);

result_textf1.setEditable(false); // Result field should not be editable

calculate1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		String expr = expression_textf1.getText().trim();
		try {
			result_textf1.setText(String.valueOf(p.evaluate(expr)));	
		}
		catch(ParserException exc) {
			result_textf1.setText(String.valueOf(exc));
		}
	}
});

clear1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		expression_textf1.setText("");
		result_textf1.setText("");
		expression_textf1.requestFocus();
	}
});

cancel1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		System.exit(0);
	}
});

southPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
southPanel2.add(southPanel1);

return southPanel2;

}


public JFrame buildNewFrame() {

try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());


}catch(Exception e){}

cal re = new cal();

JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel finalPanel = new JPanel();

northPanel = re.gui_2 ();
southPanel = re.gui_3 ();


finalPanel.setLayout( new BorderLayout());
finalPanel.add(northPanel, BorderLayout.NORTH);
finalPanel.add(southPanel, BorderLayout.SOUTH);


JFrame frame = new JFrame("Calculator");

frame.setSize(900, 900);

frame.getContentPane().add(finalPanel);

frame.addWindowListener( new WindowAdapter()





{

public void windowClosing( WindowEvent e) {

System.exit(0);
}
});


return frame;


}

}

And lastly the runner.

import javax.swing.JFrame;

public class runner {

public static void main(String args[]){

JFrame lor = new JFrame();
cal rece = new cal();
lor = rece.buildNewFrame();

lor.pack();
lor.setVisible(true);

}

}
jwenting commented: plagiarising sleezeball homework kiddo -4
Alex Edwards commented: Horrible -1

Recommended Answers

All 4 Replies

It's your homework. If you think you can get away with passing off someone elses work as your own you're a sleezball who's attempting plagiarism and noone should help you.
In fact you deserve to be thrown out of school and banned from ever enlisting in any other school for any course whatsoever.

By the way, I don't agree with the way the ParserException is written. This is not the right way to extend the Exception class

By the way, I don't agree with the way the ParserException is written. This is not the right way to extend the Exception class

To tell you the truth, after your post I decided to give a quick look at the code, and you know what, I don't agree with the way anything is written there.

To tell you the truth, after your post I decided to give a quick look at the code, and you know what, I don't agree with the way anything is written there.

It looks like, that kid should be more careful when copying others people code. If you don't understand it, you can't tell if it is correct

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.