Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Interface in java - an error in an example program

  1. #1
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Interface in java - an error in an example program

    Please help me to clear this error:
    E:\java\somas\chapter9\workedprog>javac Prob91.java
    Prob91.java:11: error: Hysin is not abstract and does not override abstract method Hypfun(double) in Hyper class Hysin implements Hyper {
    ^
    1 error
    i am using jdk1.7.

    Java Code:
    /* Problem 9.1 , Page : 156 - 158.
        Write an interface called Hyperbolic with a method to find either Cosh or Sinh.
    	Implement this interface to calculate Sinh(x) and Cosh(x) given by:
    	Sinh(x) = x + ( 10 sumE n=1 ( (x^(2n + 1))/ (2n + 1)!
    	Cosh(x) = 1 + ( 10 sumE n=1 ( (x^(2n))/ (2n)!
     */
     interface Hyper	{
    	final int n = 10;
    	double Hypfun(double x);
     }
     class Hysin implements Hyper	{
    	double sinh;
    	long factn;
    	int i;
    	long Factfun(int nmax)	{
    		long fact = 1;
    		for(i=2; i <= nmax; i++)	{
    			fact *= i;
    			return fact;
    		}
    	}
    	public double Hypnfun(double x)	{
    		sinh = x;
    		for(int k = 1; k <= n; k++)	{
    			factn = Factfun(2 * k + 1);
    			sinh = sinh + Math.pow(x,( 2 * k + 1)) / factn;
    		}
    		return sinh;
    	}
     }
     class Hycos implements Hyper	{
    	double cosh;
    	long factn;
    	int i;
    	long Factfun(int max)	{
    		long fact = 1;
    		for(i =2; i <= max ; i++)
    			fact *= 1;
    		return fact;
    	}
    	public double Hypfun(double x)	{
    		cosh = 1;
    		for(int k = 1; k <= x; k++)	{
    			factn = Factfun(2 * k);
    			cosh = cosh + Math.pow(x, (2 * k)) / factn;
    		}
    		return cosh;
    	}
     }
     class Prob91	{
    	public static void main(String args[])	{
    		double x, sinh, cosh, jsh, jch;
    		Hyper hcos;
    		Hyper hsin;
    		int pn = 50;
    		for(int i = 0; i < pn ; i++)
    			System.out.println("-");
    		System.out.println("\n");
    		System.out.println("x \t sinh \t cosh \t");
    		for(int i = 0; i < pn ; i++)
    			System.out.println("-");
    		System.out.println("\n");
    			for(x = 0; x <= 5; )	{
    				hsin = new Hysin();
    				hcos = new Hycos();
    				sinh = hsin.Hypfun(x);
    				cosh = hcos.Hypfun(x);
    				// Reduce the fractional digits for display
    				double sx = (int)(sinh * 1000);
    				double cx = (int)(cosh * 1000);
    				sinh = sx / 1000;
    				cosh = cx / 1000;
    				System.out.println(x + "\t" + sinh + "\t" + cosh);
    				x += 0.5;
    			}
    			for(int i = 0; i < pn; i++)
    				System.out.print("-");
    			System.out.println("\n");
    		
    			
    	}
     }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Did you read the error message properly?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Check the class access modifier where you want to implement your interface.

  4. #4
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: Interface in java - an error in an example program

    Quote Originally Posted by Eranga View Post
    Did you read the error message properly?
    ya , i pasted the error message as it was shown in the console.

  5. #5
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: Interface in java - an error in an example program

    The message shows error on line number 11 where i think all are correct . i will check those modifiers...

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Interface in java - an error in an example program

    Your code is difficult to follow because you have not adopted standard coding conventions. Method (and variable) names should start with a lowercase letter.

    db
    Eranga likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    In the interface:
    double Hypfun(double x);

    In the class implementing the interface:
    public double Hypnfun(double x)

    They're not the same...

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    The name is 'Hypfun', not 'Hypnfun' in yout Hysin class ...

    kind regards,

    Jos

    edit: *darn* too slow again ... ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Only by an hour.
    :)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Quote Originally Posted by Tolls View Post
    Only by an hour.
    :)
    Oh well, it's improving ;-) (I should refresh the page more and not wait before I want to reply)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    I do that.
    Open a load of thread pages and then do some work occasionally dipping in...only to find someone has answered in the intervening time.

    The other classic is to completely miss the fact that the thread has gone onto a second (or more) page...

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Quote Originally Posted by Tolls View Post
    I do that.
    Open a load of thread pages and then do some work occasionally dipping in...only to find someone has answered in the intervening time.

    The other classic is to completely miss the fact that the thread has gone onto a second (or more) page...
    Well, you still do it faster than I do it ;-) I do too many things at the same time and I get distracted easily ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    "ooh look. Shiny things!"
    :)

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interface in java - an error in an example program

    Quote Originally Posted by Tolls View Post
    "ooh look. Shiny things!"
    :)
    We want to hear typing sounds mr. Tolls! Stop chasing that poor little butterfly and get off that desk and sit down!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: Interface in java - an error in an example program

    Quote Originally Posted by DarrylBurke View Post
    Your code is difficult to follow because you have not adopted standard coding conventions. Method (and variable) names should start with a lowercase letter.

    db
    i will try to code according to the conventions. Thanks..

Similar Threads

  1. Program for interface
    By NetJava in forum New To Java
    Replies: 5
    Last Post: 11-20-2011, 07:12 PM
  2. class interface or enum expected Imports java.io.*; error
    By prathamkool in forum New To Java
    Replies: 2
    Last Post: 10-02-2011, 04:53 PM
  3. Java program error
    By Taszk in forum New To Java
    Replies: 16
    Last Post: 06-06-2011, 05:59 AM
  4. error in java program..
    By mudit222 in forum New To Java
    Replies: 2
    Last Post: 12-17-2010, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •