problem with constructor
Hi guys
when i created this constructor, it keep throwing back this error whcih i don't understand.
public interface MyNumber {
// data defination
private int wholenumber = 0;
private float fraction = 0;
// constructor
public MyNumber(int whole, float fract) {
wholenumber = whole;
fraction = fract;
}
may i know what is wrong with my constructor code?
it keep saying <identifier>> expected
> Hi guys
> when i created this constructor, it keep throwing
> back this error whcih i don't understand.
>
> public interface MyNumber {
> // data defination
> private int wholenumber = 0;
> private float fraction = 0;
>
> // constructor
>public MyNumber(int whole, float fract) {
> wholenumber = whole;
>fraction = fract;
> }
>
> may i know what is wrong with my constructor code?
> it keep saying <identifier>> expected
public interface MyNumber {
It's public class MyNumber
constructor in an interface?
hi guys
an interface class cannot contains any constructor?
i have problem with this question that i need to do
can anyone assist me with part b onwards?
We are familiar with the mathematical concept of number. It represents a numerical value. We are able to add a number to another number and it produces a new number. This number can be a whole number or a fraction.
a) Define an ADT for MyNumber. Suppose there are three operations for this MyNumber: add, subtract, multiply. (3 marks)
b) Write the Java interface class for MyNumber. (3 marks)
c) Write a Java class, MyInteger, that implements the MyNumber interface. A default constructor and a specific constructor are to be implemented. The specific constructor will accept a int value as its arguments. Include in the implementation a toString method. (6 marks)
d) Write a Java class, MyRational, that implements MyNumber interface. A rational number has a nominator and a denominator that are integer type. An example of a rational number is 3/5 where 3 is the nominator and 5 is the denominator. A default constructor and a specific constructor are to be implemented. The specific constructor will accept two int values as its arugments. Include in the implementation a toString method that returns the rational number as a string eg ?/5?
(10 marks)
e) Write a driver class, MyNumberApp, to test your Java classes. (3 marks)
part b of the question has ask for an interface class
any problem with my code?
public class MyNumber {
// data defination
private int wholenumber = 0;
private float fraction = 0;
// constructor
public MyNumber(int whole, float fract) {
wholenumber = whole;
fraction = fract;
}
// methods or operations
public MyNumber add(MyNumber c, MyNumber d){
int r = wholenumber + c.getWholeNumber();
float i = fraction + c.getFraction();
MyNumber newC = new MyNumber(r,i);
//MyNumber newD = new MyNumber(i);
return newC;
//return newD;
}
public MyNumber subtract(MyNumber c, MyNumber d){
int r = wholenumber - c.getWholeNumber();
float i = fraction - c.getFraction();
MyNumber newC = new MyNumber(r,i);
//MyNumber newD = new MyNumber(i);
return newC;
//return newD;
}
public MyNumber multiply(MyNumber c, MyNumber d){
int r = wholenumber * c.getWholeNumber();
float i = fraction * c.getFraction();
MyNumber newC = new MyNumber(r,i);
//MyNumber newD = new MyNumber(i);
return newC;
//return newD;
}
public int getWholeNumber(){
return wholenumber;
}
public float getFraction(){
return fraction;
}
}
public interface MyNumber {
//add only method declarations here...
public int add(int a, int b);
public int subtract(int a, int b); //any more operations you need...
}
public class MyInteger implements MyNumber {
//add implementations of the methods here...
public int add(int a, int b) {
return a+b;
}
public int subtract(int a , int b) {
return a-b;
}
}
> public interface MyNumber {
> //add only method declarations here...
> public int add(int a, int b);
> public int subtract(int a, int b); //any more
> operations you need...
> }
>
> public class MyInteger implements MyNumber {
> //add implementations of the methods here...
>
> public int add(int a, int b) {
> return a+b;
> }
>
> public int subtract(int a , int b) {
> return a-b;
> }
> }
If you hand in the above design you'll get 0 points for your effort. -:)
hi guys
as for the class MyInteger, constructors are needed as mentioned.
A default constructor and a specific constructor are to be implemented.
how to i add in a defualts constructors and a specific constructors to the code
public class MyInteger implements MyNumber {
private
public int add(int a, int b) {
return a+b;
}
public int subtract(int a , int b) {
return a-b;
}
public int multiply(int a , int b) {
return a*b;
}
public float add1(float a , float b) {
return a+b;
}
public float subtract1(float a , float b) {
return a-b;
}
public float multiply1(float a , float b) {
return a*b;
}
}
> hi guys
>
> as for the class MyInteger, constructors are needed
> as mentioned.
You're on the wrong track (as I indicated in my previous reply). What's the use of defining a new abstract data type MyNumber without any operations involving MyNumber objects? Why should it add ints?
sorryi don't quite get it.if possible maybe u can show me an example to get me started.i am totally lostthanks
> sorry
> i don't quite get it.
> if possible maybe u can show me an example to get me
> started.
> i am totally lost
> thanks
Why don't you start with the design part, namely question a).
MyNumber is supposed to be able to handle both integral numerics and fractional numerics.
To me it looks like it's best to treat an integral number as a special case of a fractional number, that is with denominator 1.
Now say you add two MyNumber numbers. What happens if you add a fractional number to an integral number? The most natural maybe is to add the integral part of the fractional number, that is the number you get from integral division with the nominator and the denominator.
To be able to implement the above, MyNumber must expose both a nominator and a denominator, and in addition the numerical operations. So it would look like this,
public interface MyNumber {
int nom();
int den();
void add(MyNumber number);
// other operations
}
An implementation of MyInteger would look like this,
public class MyInteger implements MyNumber {
private int n;
public MyInteger() { // default constructor
this.n = 0;
}
public MyInteger(int n) { // constructor
this.n = n;
}
public int nom() { // nominator
return this.n;
}
public int den() { // denominator
return 1;
}
public void add(MyNumber number) { // add integral part of number
this.n += number.nom()/number.den();
}
public String toString() { // overides toString of Object
return "" + this.n;
}
}
> > i am totally lost
The design challange in this task was to make the MyNumber type general enougth to "cover" the expected subtypes, namely MyInteger and MyFractional. This was accomplished by treating MyInteger as a special case of MyFractional really.
The above is according to a very important design principle called the the Liskov Substitution Principle (LSP). If you follow LSP (the supertype "covers" for all subtypes) then code written using supertype variables (MyNumber) will work with objects of all subtypes (MyInteger and MyFractional).
If you don't follow the LSP you end up having to check what subtype objects are using instanceof and your program becomes less general and harder to change and maintain. But that's maybe not your most pressing concern right now -:)
> > i am totally lost
Another interesting question is whether nom() and den() really should be publicly exposed in the MyNumber interface. If they're considered an implementation detail they shouldn't and there are different ways to accomplish this. One is to make MyNumber an abstract class instead and declaring nom() and den() protected.