Question

abstract class Exp { abstract void print(); abstract int eval(); } class ConstExp extends Exp {...

abstract class Exp {
    abstract void print(); 
    abstract int eval(); 
}
class ConstExp extends Exp {
    private int value;
    ConstExp(int v) {
        value = v;
    }
    void print() {
        System.out.print(value);
    }
    int eval() {
        return value;
    }
}
class BinaryExp extends Exp {
    private Exp arg1;
    private Exp arg2;
    private char op;
    BinaryExp(char op, Exp arg1, Exp arg2) {
        this.op = op;
        this.arg1 = arg1;
        this.arg2 = arg2;
    }
    void print() {
        System.out.print("(");
        arg1.print();
        System.out.print(" " + op + " ");
        arg2.print();
        System.out.print(")");
    }
    int eval() {
        int a = arg1.eval();
        int b = arg2.eval();
        switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
        }
        return 0;
    }
}
class Test {
    public static void main(String args[]) {
        Exp exp = 
        new BinaryExp('+', 
            new BinaryExp('*', new ConstExp(1), new ConstExp(2)),
            new BinaryExp('/', 
                new ConstExp(3),          
                new BinaryExp('-', new ConstExp(4), new ConstExp(3))));
        exp.print();
        System.out.print(" = ");
        System.out.println(exp.eval());
    }
}
from code above naswer this questions:

Practice questions (0)
Add a method to do differentiation.
      (f (x) + g (x)) '= f' (x) + g '(x)
      (f (x) * g (x)) '= f (x) * g' (x) + f '(x) * g (x)
       x '= 1
       c '= 0

       Exp diff (VarExp x) {....}

Practice questions (1)
Add, subtract, multiply and divide into one in a formula program
Define the summarized class BinaryExp.

(Hint) Define a field char op that contains an operator.

Practice questions (2)
Define a method that returns the number of constants contained in the formula.

Practice questions (3)
  Extend the print method of the formula program
Make it output considering the strength of the operator.
For example, ((1 + (2 * 3)) + (4 + 5)) is
  Output like 1 + 2 * 3 + (4 + 5).

(Hint) Use instanceof or give operators precedence
Add field

Practice questions (4)
Define a class that represents a Java logical expression
However, a logical expression consisting of only &&, ||,! Is sufficient.
Also, as a method, print that outputs a logical expression and Define an "eval" method to calculate the value of the formula.
The value of the logical expression is given by the parameter of the constructor.

 
please ex number 2,3,4 and explain thanks!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

For Question 2 :

abstract class Exp {

abstract void print();

abstract int eval();

protected static int constCount = 0;

public int getConstCount() {

return constCount;

}

}

class ConstExp extends Exp {

private int value;

ConstExp(int v) {

constCount++;

value = v;

}

void print() {

System.out.print(value);

}

int eval() {

return value;

}

}

class BinaryExp extends Exp {

private Exp arg1;

private Exp arg2;

private char op;

BinaryExp(char op, Exp arg1, Exp arg2) {

this.op = op;

this.arg1 = arg1;

this.arg2 = arg2;

}

void print() {

System.out.print("(");

arg1.print();

System.out.print(" " + op + " ");

arg2.print();

System.out.print(")");

}

int eval() {

int a = arg1.eval();

int b = arg2.eval();

switch (op) {

case '+':

return a + b;

case '-':

return a - b;

case '*':

return a * b;

case '/':

return a / b;

}

return 0;

}

}

class Test {

public static void main(String args[]) {

Exp exp = new BinaryExp('+', new BinaryExp('*', new ConstExp(1), new ConstExp(2)),

new BinaryExp('/', new ConstExp(3), new BinaryExp('-', new ConstExp(4), new ConstExp(3))));

exp.print();

System.out.print(" = ");

System.out.println(exp.eval());

System.out.println("Const. Count = " + exp.getConstCount());

}

}

For Question 4:

class LogicalExp {

private boolean arg1;

private boolean arg2;

private String op;

public LogicalExp(String op, boolean arg1, boolean arg2) {

super();

this.op = op;

this.arg1 = arg1;

this.arg2 = arg2;

}

public LogicalExp(String op, boolean arg1) {

super();

this.op = op;

this.arg1 = arg1;

}

void print() {

System.out.print("(");

if (op != "!") {

System.out.print(arg1);

}

System.out.print(" " + op + " ");

System.out.print(arg2);

System.out.print(")");

}

boolean eval() {

switch (op) {

case "&&":

return arg1 && arg2;

case "||":

return arg1 || arg2;

case "!":

return !arg2;

}

return false;

}

public static void main(String[] args) {

LogicalExp obj1 = new LogicalExp("||", true, false);

LogicalExp obj2 = new LogicalExp("&&", true, false);

LogicalExp obj3 = new LogicalExp("!", true, false);

obj1.print();

System.out.print(" = ");

System.out.println(obj1.eval());

obj2.print();

System.out.print(" = ");

System.out.println(obj2.eval());

obj3.print();

System.out.print(" = ");

System.out.println(obj3.eval());

}

}

Question 1 : is already solved in the given problem(if you need anything else please let me know and provide some more information, comment here, I'll try my best to help you.)

Question 0 : I didn't understand as there are some constant X and C which are not properly defined. Please provide some example.

Add a comment
Know the answer?
Add Answer to:
abstract class Exp { abstract void print(); abstract int eval(); } class ConstExp extends Exp {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...

    public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } } public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len...

  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer)...

    C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary: public: Employee; // default constructor: age=0, id=0, and salary=0 void setAge(int x); // let age = x void setId(int x); // let id = x void setSalary(float x); // salary = x int getAge(); // return age int getId; // return id float...

  • Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression...

    Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...

  • Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....

    Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide. Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create. Your class must include the following. Implementations for all of the abstract methods defined in abstract class AmusementRide. At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide. Override the inherited repair() method following the...

  • Question 28 public class Main public static void main(String args[]) { Iparentp=new Child (1.2); p.printo: interface...

    Question 28 public class Main public static void main(String args[]) { Iparentp=new Child (1.2); p.printo: interface Iparent public int x = 1; public void printo: class Child implements Iparent private int y public Child(int a, int b) y b: public void printot System.out.print("Child"-yl: Child 2 public int x = 1; public void print(); class Child implements Iparent! private int y public Child(int a, in lb) y=b; public vold print! System.out.pnt("Child"+y): Child 2 1 None of the above • Previous

  • Java Programming: Math Quiz

    Starting codes:MathQuiz.javaimport java.util.Scanner;public class MathQuiz {    private final int NUMBER_OF_QUESTIONS = 10;    private final MathQuestion[] questions = new MathQuestion[NUMBER_OF_QUESTIONS];    private final int[] userAnswers = new int[NUMBER_OF_QUESTIONS];    public static void main(String[] args) {        MathQuiz app = new MathQuiz();        System.out.println(app.welcomeAndInstructions());        app.createQuiz();        app.administerQuiz();        app.gradeQuiz();    }    private String welcomeAndInstructions() {        return "Welcome to Math Quiz!\n" +         ...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT