• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in RHE page 78(new edition)

 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
i have a doubt on page 78 RHE (new edition chapter3(Modifiers).the programme can be compiled but giving a run time error as "NoSuchMethodError" can some one help me please?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since many may not have the new edition of RHE, how about posting the code here??
Ajith
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code is----
class complex{
private double real,imaginary;
public complex(double r,double imaginary);{
real=r,imaginary=i;
}
public complex add(complex c){
return new complex(real + c.real,imaginary + c.imaginary);
}
}
class client{
public static void main(String args[]){
complex c1=new complex(1,2);
complex c2=new complex(3,4);
complex c3=c1.add(c2);
}
}
the error is "NoSuchMethodError" in "main".(at run time )
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you named the source file?? Since the main method is declared in the "client" class, you should name the source file as client.java
Notwithstanding various typos you have in your code, it runs just fine when saved in client.java.
Here's the error-free source code -

Ajith
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There were some typos in your code. But altogether it works fine.
I have changed your code slightly.

HTH
Lakshmi
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry...
i did store it in wrong file
thanks
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had success saving in file complex.java and compiling "javac complex.java", but need to run with "java client" and not "java complex" as shown below (compiling Ajith's code):
>javac complex.java
>java complex
Exception in thread "main" java.lang.NoSuchMethodError: main
>java client
4.0 +i 6.0
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic