Help me with this code plz
publicclass ImaginaryNumber
{
privateint a;
privateint b;
public ImaginaryNumber(int num1,int num2){
a = num1;
b = num2;
firstnumber = Integer.toString(a);
secondnumber = Integer.toString(b);
String s =new String();
s = firstnumber +"+" + secondnumber"i";
}
}
it says ';' expected, can someone tell me what's wrong, thx
which line is giving you this error?
secondnumber"i";Notice something missing? Also what are firstnumber and secondnumber?
s = firstnumber + "+" + secondnumber"i";change it to s = firstnumber + "+" + secondnumber + "i";
Once again fastmike to the rescue, saving students from thinking!
ok thanks, i guess i missed that + sign
anyways, i'm stuck on something else now
public class ImaginaryNumber
{
private static int a;
private static int b;
String s = new String();
public ImaginaryNumber(int num1, int num2){
a = num1;
b = num2;
s = a + "+" + b + "i";
}
public ImaginaryNumber add(ImaginaryNumber num1, ImaginaryNumber num2){
a = a + num1;
b = b + num2;
}
[b]public ImaginaryNumber add(ImaginaryNumber num){
a = a + num;
}
public ImaginaryNumber subtract(ImaginaryNumber num1, ImaginaryNumber num2){
a = a - num1;
b = b - num2;
}
public ImaginaryNumber subtract(ImaginaryNumber num){
a = a - num;[/b]
}
public int getRealPart(){
return a;
}
public int getImaginaryPart(){
return b;
}
}
what i'm trying to do is create an overloaded method (bold) where it changes the state of an object. so if the original was something like 5 + 6i, and i called the overloaded method with (4, 5), teh value would become 9 + 11i.
any ideas?
a + num1;This makes no sense, a is an int and num1 is an ImaginaryNumber. Can you add a dog to a cat?
> Once again fastmike to the rescue, saving students> from thinking!lol.i was waiting for it i am just trying to increase my skills flounder the more program i write the more i will learn. much to accomplish.
ok i am gone have to study for my test Later guys
yea, it said "operator + cannot be applied..." any suggestions on how i can make "a" an ImaginaryNumber? thx
NO!That is not the solution. Try flipping it around. How can you make num1 an int (or more importantly how can you get some int value out of num1)?
> > Once again fastmike to the rescue, saving students
> > from thinking!
>
> lol.i was waiting for it i am just trying to increase
> my skills flounder the more program i write the more
> i will learn. much to accomplish.
That's well and good Supermike. You can practice and learn all you like but once you have a solution keep it to yourself. Don't post it on the net for people to copy and hand in as their own work. This is called plagiarism and you can be expelled for it.
first of all every method is suppose to have a return . you dont have nothing.for eg
public int getMax(int num, int num2)
{
int max=0;;
if num>num2
max = num1;
else
max = num2;
return max;
}
just a hint.
Message was edited by:
fastmike
> NO!
>
> That is not the solution. Try flipping it around. How
> can you make num1 an int (or more importantly how can
> you get some int value out of num1)?
hmm i'm really stumped on this one. i was thinking of doing something with toString but i'm not sure.
also, i changed my code to this:
public class ImaginaryNumber
{
private static int a;
private static int b;
public ImaginaryNumber(int num1, int num2){
a = num1;
b = num2;
public String toString(){
String s = new String();
s = a + "+" + b + "i";
return s;
}
public static ImaginaryNumber add(ImaginaryNumber num1, ImaginaryNumber num2){
a = a + num1;
b = b + num2;
}
public static ImaginaryNumber add(ImaginaryNumber num){
a = a + num;
}
public static ImaginaryNumber subtract(ImaginaryNumber num1, ImaginaryNumber num2){
a = a - num1;
b = b - num2;
}
public static ImaginaryNumber subtract(ImaginaryNumber num){
a = a - num;
}
public int getRealPart(){
return a;
}
public int getImaginaryPart(){
return b;
}
}
> > > Once again fastmike to the rescue, saving
> students
> > > from thinking!
> >
> > lol.i was waiting for it i am just trying to
> increase
> > my skills flounder the more program i write the
> more
> > i will learn. much to accomplish.
>
> That's well and good Supermike. You can practice and
> learn all you like but once you have a solution keep
> it to yourself. Don't post it on the net for people
> to copy and hand in as their own work. This is called
> plagiarism and you can be expelled for it.
Aigh Aigh Captain :) me gone. appologies.
i was thinking it over, and i came up with this
does this make sense/work?
public ImaginaryNumber add(ImaginaryNumber num1, ImaginaryNumber num2){
a = a + num1.getRealPart();
b = b + num2.getImaginaryPart();
}
Message was edited by:
obeserabbit
> i was thinking it over, and i came up with this
> does this make sense/work?
>
> public ImaginaryNumber add(ImaginaryNumber
> num1, ImaginaryNumber num2){
>
> a = a + num1.getRealPart();
> b = b + num2.getImaginaryPart();
> }
>
>
> Message was edited by:
> obeserabbit
Lol sorry dude flounder have already given you alot of hints. can't you see ? i can provide you the solution but can't do it :(. i posted a method also for you did you see my getMax method? the data type? method name? parameter type and name? and return statement?
i assume NO
ok thx, i think i see what youre getting at
i tried doing something like this
return ImaginaryNumber(a, b);
but it couldnt find ImaginaryNumber(int, int), any ideas?
EDIT:
i switched it to this:
ImaginaryNumber x = new ImaginaryNumber(a, b);
return x;
does that make sense?
Message was edited by:
obeserabbit
> ok thx, i think i see what youre getting at
>
> i tried doing something like this
>
> return ImaginaryNumber(a, b);
>
> but it couldnt find ImaginaryNumber(int, int), any
> ideas?
>
>
> EDIT:
> i switched it to this:
> ImaginaryNumber x = new ImaginaryNumber(a, b);
> return x;
>
> does that make sense?
NO
If you are trying to return the Object "ImaginaryNumber", then somewhere in the code you have to have For example:
public ImaginaryNumber someMethodName(ImaginaryNumber in) {
ImaginaryNumber myVarName = new ImaginaryNumber(Whatever_the_ args_are);
myVarName = ln;
//Work out your code all in here. (Whatever needs to be done)
//**
//**
return ln;
}
Notice how it returned the variable "ln" ? That is because we declared and initialized "ln" is an object of the ImaginaryNumber class.
So now our method is returning an ImaginaryNumber object.
Message was edited by:
lethalwire