static/ dynamic help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: static/ dynamic help

  1. #1
    Join Date
    Sep 2005
    Posts
    15

    static/ dynamic help

    Code:
    public class getHypo
    {
    	
    	private double a;
    	private double b;
    	private double c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
    	
    	
    	double getC()
    	{
    		return c;
    	}
    	
    	
    }
    
    
    
    
    
    
    public class pythagorian
    {
    	
    	public static void main(String[] args)
    	{
    		double a, b, c;
    		ConsoleIO console = new ConsoleIO();
    		getHypo **** = new getHypo();
    		
    		System.out.print("a = ");
    		a = console.readDouble();
    		System.out.print("\nb = ");
    		b = console.readDouble();
    		
    		System.out.print("\nc = ");
    		c = ****.getC();
    		System.out.println(c);	
    	}	
    }
    when i execute it, and input a = 3 b = 4 c should = 5, but it equals 0.0......what do i do/ what did i do wrong?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Check my comments

    With less code than this getHypo is reduced to a mere wrapper
    Code:
    public class getHypo {
      /*
      private double a;
      private double b; 
       the c you declare here is local to the getHypo class, the a and
       b values are also local (and 0).
      private double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
      */
    
      double getC(double a, double b) {
        return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
      }
    
    }
    
    public class pythagorian {
    
      public static void main(String[] args) {
        double a, b, c;
        ConsoleIO console = new ConsoleIO();
        getHypo **** = new getHypo();
    
        System.out.print("a = ");
        a = console.readDouble();
        System.out.print("\nb = ");
        b = console.readDouble();
    
        System.out.print("\nc = ");
        c = ****.getC();
        System.out.println(c);
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Sep 2005
    Posts
    15
    thanks! it works now. any idea what the problem was before?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Check my comments....
    eschew obfuscation

Similar Threads

  1. Trouble buffering transparent gif
    By axlan in forum Java
    Replies: 2
    Last Post: 06-22-2005, 08:21 AM
  2. Static variables and inheritance
    By grmAbay in forum .NET
    Replies: 2
    Last Post: 08-27-2003, 11:00 PM
  3. Top fixed screen
    By Cheng in forum Web
    Replies: 1
    Last Post: 11-06-2001, 09:43 AM
  4. Replies: 13
    Last Post: 10-03-2001, 04:05 PM
  5. Static Vs Dynamic Page - URGENT
    By Anand in forum Enterprise
    Replies: 1
    Last Post: 08-13-2000, 09:10 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center