get set methods How to return a total? plz help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: get set methods How to return a total? plz help

  1. #1
    Join Date
    Feb 2006
    Posts
    9

    get set methods How to return a total? plz help

    I want to send length and width to my constructor which uses set methods to set the values. I want to be able to print out the total from my getTotal method. I dont know how to? here is the code. It only returns a Zero 0

    public class Contact{
    private int length;
    private int width;
    private int total;

    public Contact(int l, int w){
    setLength(l);
    setWidth(w);
    }
    public void setLength(int s1){
    length=s1;
    }
    public void setWidth(int s2){
    length=s2;
    }
    public int getTotal(){
    return length*width;
    }
    }
    //
    //
    public class testing{
    public static void main(String[] args){


    Contact n=new Contact(3,4);

    System.out.println(n.getTotal());

    }
    }

  2. #2
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    your sending the lenght and width to your constructor and them from there your sending the values to methods, why not just remove but the methods and let the constructor deal with the process.

  3. #3
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    by the way:
    public void setWidth(int s2){
    length=s2;
    }
    that's a bug. it should be
    public void setWidth(int s2){
    width=s2;
    }

  4. #4
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    and the
    private int total;
    is never used, so you should just remove it.

  5. #5
    Join Date
    Feb 2006
    Posts
    9
    Thanks i made some changes to it and its working now.

Similar Threads

  1. SQL DMO Restores in VB?
    By ObiWan in forum VB Classic
    Replies: 3
    Last Post: 05-23-2006, 11:35 AM
  2. need to return total record count, but here is the trick...
    By barbarosa80503 in forum VB Classic
    Replies: 2
    Last Post: 10-28-2005, 04:33 PM
  3. Why is this in the console?
    By Dark Rain in forum Java
    Replies: 8
    Last Post: 09-30-2005, 07:42 PM
  4. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  5. Return Top / Bottom Of Table?
    By T. Bradley Dean in forum Database
    Replies: 2
    Last Post: 02-02-2001, 06:59 PM

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