15 Replies - 761 Views - Last Post: 20 November 2011 - 10:15 AM
#1
Do i need to use a constructor?
Posted 08 November 2011 - 06:54 PM
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?
Replies To: Do i need to use a constructor?
#2
Re: Do i need to use a constructor?
Posted 08 November 2011 - 07:19 PM
So your 4 subclasses will need a constructor with the same signature
#3
Re: Do i need to use a constructor?
Posted 09 November 2011 - 01:02 AM
#4
Re: Do i need to use a constructor?
Posted 09 November 2011 - 01:04 AM
Quote
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
Have a look here: http://download.orac...nstructors.html
This post has been edited by Veitch: 09 November 2011 - 01:10 AM
#5
Re: Do i need to use a constructor?
Posted 09 November 2011 - 04:58 AM
Veitch, on 09 November 2011 - 04:04 AM, said:
Have you read the requirement ?
An abstract class for the values
A subclass for every operation
nexus490, on 09 November 2011 - 04:02 AM, said:
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);
#6
Re: Do i need to use a constructor?
Posted 09 November 2011 - 05:39 AM
Quote
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
#7
Re: Do i need to use a constructor?
Posted 09 November 2011 - 07:26 AM
#8
Re: Do i need to use a constructor?
Posted 12 November 2011 - 03:48 AM
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?
#9
Re: Do i need to use a constructor?
Posted 12 November 2011 - 01:44 PM
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();
}
}
#10
Re: Do i need to use a constructor?
Posted 13 November 2011 - 11:57 AM
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.
#11
Re: Do i need to use a constructor?
Posted 13 November 2011 - 12:21 PM
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();
}
}
#12
Re: Do i need to use a constructor?
Posted 16 November 2011 - 01:32 PM
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.
#13
Re: Do i need to use a constructor?
Posted 16 November 2011 - 03:05 PM
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
#14
Re: Do i need to use a constructor?
Posted 16 November 2011 - 03:08 PM
#15
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();
|
|

New Topic/Question
Reply



MultiQuote




|