-
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());
}
}
-
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.
-
by the way:
public void setWidth(int s2){
length=s2;
}
that's a bug. it should be
public void setWidth(int s2){
width=s2;
}
-
and the
private int total;
is never used, so you should just remove it.
-
Thanks i made some changes to it and its working now.
Similar Threads
-
By ObiWan in forum VB Classic
Replies: 3
Last Post: 05-23-2006, 10:35 AM
-
By barbarosa80503 in forum VB Classic
Replies: 2
Last Post: 10-28-2005, 03:33 PM
-
By Dark Rain in forum Java
Replies: 8
Last Post: 09-30-2005, 06:42 PM
-
By mdengler in forum ASP.NET
Replies: 0
Last Post: 11-26-2002, 02:32 PM
-
By T. Bradley Dean in forum Database
Replies: 2
Last Post: 02-02-2001, 05: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks