Do i need to use a constructor?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 761 Views - Last Post: 20 November 2011 - 10:15 AM Rate Topic: -----

#1 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Do i need to use a constructor?

Posted 08 November 2011 - 06:54 PM

Hi all,

I've been set a coursework task, which is to create a simple calculator. The basic structure i have to stick to is as follows:

* An abstract class which has to represent any arithmetic expression (2 double arguments only)

* 4 subclasses which inherit from my abstract class, one for each operation add,sub,mult,div

* two methods in my abstract class so far, evaluate and display

My calculator would have to be able to accept an expression like this (3.4*2.1)+(6.0) for example

but as i'm only using two arguments i'm guessing i need to make (3.4*2.1) a single object for addition

is a constructer the right way to do it?

Is This A Good Question/Topic? 0
  • +

Replies To: Do i need to use a constructor?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 08 November 2011 - 07:19 PM

Your abstract class will need a constructor to store the two values to add/subtract/multiply/divide in instance variables

So your 4 subclasses will need a constructor with the same signature
Was This Post Helpful? 1
  • +
  • -

#3 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 09 November 2011 - 01:02 AM

How is a constructor used to store values? I was under the impression a constructor was used to create objects.
Was This Post Helpful? 0
  • +
  • -

#4 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Do i need to use a constructor?

Posted 09 November 2011 - 01:04 AM

Quote

but as i'm only using two arguments i'm guessing i need to make (3.4*2.1) a single object for addition

It is still possible to calculate (3.4*2.1)+(6.0) if you only have two numbers as arguments for every operation in the constructor. The call might be like this:
double result = new Addition(new Multiplication(3.4, 2.1).eval(), 6.0).eval();


However you could also create a contructor that allows arithmetic expression objects as arguments. But then you need a class that provides constants too.

Edit:

Quote

How is a constructor used to store values? I was under the impression a constructor was used to create objects.

Have a look here: http://download.orac...nstructors.html

This post has been edited by Veitch: 09 November 2011 - 01:10 AM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 09 November 2011 - 04:58 AM

View PostVeitch, on 09 November 2011 - 04:04 AM, said:

However you could also create a contructor that allows arithmetic expression objects as arguments. But then you need a class that provides constants too.

Have you read the requirement ?
An abstract class for the values
A subclass for every operation

View Postnexus490, on 09 November 2011 - 04:02 AM, said:

How is a constructor used to store values? I was under the impression a constructor was used to create objects.

and objects are built of instance variables :)

class Dog {
   String name;
   int age;

   // constructor
   Dog(String name, int age) {
      this.name = name;
      this.age = age;
   }
}



// this statement construct a new Dog object whose name is "Fido" and age 5
Dog fido = new Dog("Fido", 5);
Was This Post Helpful? 1
  • +
  • -

#6 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Do i need to use a constructor?

Posted 09 November 2011 - 05:39 AM

Quote

Have you read the requirement ?
An abstract class for the values
A subclass for every operation

Here is a sketch about what I meant:
public abstract class ArithmeticExpression {
	private final double value1;
	private final double value2;

	public ArithmeticExpression (double value1, double value2) {
		this.value1 = value1;
		this.value2 = value2;
	}
	
	public abstract double evaluate();

	public double getValue1() {
		return value1;
	}

	public double getValue2() {
		return value2;
	}
}

public class Addition extends ArithmeticExpression {
	
	public Addition(ArithmeticExpression exp1, ArithmeticExpression exp2) {
		super(exp1.evaluate(), exp2.evaluate());
	}

	@Override
	public double evaluate() {
		return getValue1() + getValue2();
	}
	
}

Is that a contradiction to the requirement?
The only thing that is different from the requirement imho is that you need a class Constant (which is by definition an arithmetic expression too). But that is the reason I mentioned this and I think the first approach (using double values as constructor arguments in the child classes) is easier.
Edit: Sorry I don't know how to prevent that <b></b> are created within my code

This post has been edited by Veitch: 09 November 2011 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

#7 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 09 November 2011 - 07:26 AM

Thanks for the help so far guys, I'll give a try using your suggestions and let you know how i get on.
Was This Post Helpful? 0
  • +
  • -

#8 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 12 November 2011 - 03:48 AM

Hey again,

Sorry it's taken me so long to get back to you; Lot's of coursework going on!

I've tried using code similar to what Veitch as follows:

public abstract class ArithmeticExpression
{
    double num1;
    double num2;
    
    public ArithmeticExpression(double GenNum1, double GenNum2)
    {
        GenNum1 = num1;
        GenNum2 = num2;
    }
    
    public abstract double Evaluate();
    
    public double getnum1() {
	        return num1;
                
	    }
    
    public double getnum2() {
	        return num2;
	    }
    
    public void Display()
    {
     
    }
}


 class Addition extends ArithmeticExpression
{
    public Addition(ArithmeticExpression AddExpPrt1, ArithmeticExpression AddExpPrt2)
    {super(AddExpPrt1.Evaluate(), AddExpPrt2.Evaluate());}


        @Override
    public double Evaluate()
    {
        return getnum1() + getnum2();
        
    }
    }


So in my addition class I have specific constructor which will use the two doubles passed into it when an object is created. The super being there is used to call the Evaluate method from the parent class ArithmeticExpression which is in turn given its body in the addition class.

Am I right in thinking before I can write the code for my Display method I would need to create an Addition object and I would do it like this:
Addition test = new Addition(5.0,2.0);
I can't find anywhere in the code it allows me to create that object. It wont go in main because the main is static and I can't do it inside the Addition class either. Am I understanding this code correctly?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 12 November 2011 - 01:44 PM

You make it complicated for nothing

public abstract class ArithmeticExpression
{
	double num1;
	double num2;
	double result;

	public ArithmeticExpression(double GenNum1, double GenNum2)
	{
		num1 = GenNum1;
		num2 = GenNum2;
		result = evaluate();    
	}

	public abstract double evaluate();

	public double getnum1() {
		return num1;
	}
	public double getnum2() {
		return num2;
	}
	public void Display() {
		System.out.println("The result is: " + result);

	}

	public static void main(String[] args) {
		Addition x = new Addition(10.0, 42.0);
		x.Display();
	}
}


class Addition extends ArithmeticExpression
{
	public Addition(double num1, double num2) {
		super(num1, num2);
	}
	@Override
	public double evaluate()
	{
		return getnum1() + getnum2();

	}
}



Was This Post Helpful? 1
  • +
  • -

#10 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 13 November 2011 - 11:57 AM

Thanks pbl,


My question is; how do you get it to compile like that? having the main inside the abstract class is causing a problem for me, as does having it outside.

In the end i'm intending to write a class which i will use to test the functionality and a another which will randomize it.

But for now i just want to get it working as a test and the main doesn't seem to want to go anywhere.

Firstly, i get an error saying there is an Illegal static declaration in inner class
it says static can only be used in static declarations.

The other error is on the line creating the new Addition object it says that the non-static variable can't be referenced from within a tatic context.

When i force it to compile it get the following:
java.lang.NoSuchMethodError: main
Exception in thread "main" Java Result: 1

I'm careful about making things static as i'm not allowed to use any static methods.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 13 November 2011 - 12:21 PM

ArithmeticExpression.java
public abstract class ArithmeticExp<b></b>ression
{
	double num1;
	double num2;
	double result;

	public ArithmeticExp<b></b>ression(double GenNum1, double GenNum2)
	{
		num1 = GenNum1;
		num2 = GenNum2;
		result = evaluate();    
	}

	public abstract double evaluate();

	public double getnum1() {
		return num1;
	}
	public double getnum2() {
		return num2;
	}
	public void Display() {
		System.out.println("The result is: " + result);

	}
}


Tester.java
class Tester {
	public static void main(String[] args) {
		Addition x = new Addition(10.0, 42.0);
		x.Display();
	}
}


Addition.java
class Addition extends ArithmeticExp<b></b>ression
{
	public Addition(double num1, double num2) {
		super(num1, num2);
	}
	@Override
	public double evaluate()
	{
		return getnum1() + getnum2();

	}
}




Was This Post Helpful? 1
  • +
  • -

#12 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 16 November 2011 - 01:32 PM

Hey guys,

This should hopefully be the last question i have for you. (Deadline is looming). I've shown what i have managed so far to my tutor and it's ok a few changes have had to be made but i have all the basic functionality. My last problem is still this business of composite expressions and displaying them. The way it must be implemented is as follows:

* I have a structure which is pretty much the same as the one pbl recommended in my addition subclass

* Also a similar structure in my ArithmeticExpression class to the one suggested.

The way i have to do it is by having a second constructor in my ArithmeticExpression class which creates Arithmetic Expression objects from two inputed objects created from doubles by another constructor, say the addition one for example.

Then i would have to define methods for evaluating and printing out the expressions and answer in the correct format: such as (3.2*4.1)+(9.7-6.5) = 16.32

I know it's probably not the best way to go about this but it's what i've been forced into doing.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 16 November 2011 - 03:05 PM

Nothing stops you from having a ArithmeticExpression construction that receives 2 ArithmeticExpression object as parameter

    ArithmethicExpression(ArithmeticExpression ae1, ArithmeticExpression ae2) {
         num1 = ae1.result;
         num2 = ae2.result;
         result = evaluate();
    }

// addition, subtraction, multiplication,... will have the same thing
    Addition(ArithmeticExpression ae1, ArithmeticExpression ae2) {
         super(ae1, ae2);
    }



So in your running code you can do

     Addition a = new Addition(3, 4);
     Subtraction s = new Subtraction(10, 3);

     Addition totalAdd = new Addition(a, s);
     Subtraction totalSub = new Subtraction(a, s);


totalAdd.value should be (3+4) + (10 - 3)
totalSub.value should be (3+4) - (10 - 3)

Happy coding
Was This Post Helpful? 1
  • +
  • -

#14 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-November 11

Re: Do i need to use a constructor?

Posted 16 November 2011 - 03:08 PM

What about displaying? How would that be done?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Do i need to use a constructor?

Posted 16 November 2011 - 05:57 PM

???
Your ArithmeticExpression class has a Display() method
It can be called on any ArithmeticExpression object

Addition a = new Addition(3, 4);
Subtraction s = new Subtraction(10, 3);

Addition totalAdd = new Addition(a, s);
Subtraction totalSub = new Subtraction(a, s);

a.Display();
s.Display();
totalAdd.Display();
totalSub.Display();


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2