Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: my code displaying some minor wrong o/p ....plz suggest me an answer !

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my code displaying some minor wrong o/p ....plz suggest me an answer !

    its a simple program thats return the weight of the dog bt whenever any one entered a negative weigh it ll also display that as a positive weight
    for eg if enter -50 it ll show you +50 or 50 ....but it is just repeating the 1st entered weight i.e 45
    so if u entered 45 once and -80 on second time the o/p should be 45 & 80 not 45 and 45
    plz tell me whats wrong in the code so that i can learn
    thnx in advance


    public class Dog {
    private int weight;
    public int getweight() {
    	return weight;
    }
     
    public void setweight(int newweight){
    if (newweight > 0){
    	weight = newweight;
    }
    }
    }
     
    public class TestDog {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    Dog d = new Dog();
    //System.out.print("the weight of the dog is " + d.getWeight());
    d.setweight(45);
     
    System.out.print(" the weight of the dog is  "  + d.getweight());
    String newLine = System.getProperty("line.separator");
    System.out.println("" + newLine + "");
    d.setweight(-25);
     
     
    System.out.print("the weight of the dog is  " + d.getweight());
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: my code displaying some minor wrong o/p ....plz suggest me an answer !

    Please see the link in my signature on asking smart questions, especially the part on not using abbreviations and SMS speak. I can barely understand you, and I'm a native English speaker.

    Have you run through your code with a debugger to figure out what's going on? Boil your problem down to an example that's a few lines long (an SSCCE), and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: my code displaying some minor wrong o/p ....plz suggest me an answer !

    public void setweight(int newweight){
    if (newweight > 0){
    	weight = newweight;
    }
    }

    If newweight is greater than zero, you update the weight of the dog. Cool!
    However, what if the weight is equal to or less than zero? You haven't told the method what to do with those values.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my code displaying some minor wrong o/p ....plz suggest me an answer !

    ok ! now i understand this ..thnx

Similar Threads

  1. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM
  2. [SOLVED] Please help with this... I think I have a minor coding error
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:02 AM
  3. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  4. JList with Vector for auto-suggest
    By nik_meback in forum AWT / Java Swing
    Replies: 0
    Last Post: December 31st, 2010, 03:56 AM
  5. Suggest Me What to Do
    By Tapan Barik in forum Member Introductions
    Replies: 0
    Last Post: September 29th, 2010, 12:08 AM