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 8 of 8

Thread: help writing this program...

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question help writing this program...

    Hello everyone! So, I am currently in an online Java Programming course and one of the projects is to make this program. I am totally stumped. We are supposed to use Netbeans to do it. Can anyone help writing this program?


    1. Name, Age and Annual Income

    Write a program that declares the following:

    * a String variable named name

    * an int variable named age

    * a double variable named annualPay

    Store your age, name, and desired annual income as literals in these variables. The program should display these values on the screen in a manner similar to the following: My name is Joe Mahoney, my age is 26 and I hope to earn $1,000,000.00 per year.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: help writing this program...

    What have you tried? Be sure to wrap any posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help writing this program...

    public class DeclareVariables {

    public static void main(String[] args) {
    String name;
    int age;
    double annualPay;

    name = "Joe Mahoney";
    age = 26;
    annualPay = 100000;// try changing these

    toString(name, age, annualPay);
    }//end main()

    public static void toString(String n, int a, double p) {
    DecimalFormat df = new DecimalFormat("$000,000");
    System.out.println("My name is "+n+", my age is "+a
    +" and I hope to earn "+df.format(p)+" per year.");
    }//end toString()
    }//end class


    how do you wrap posted code in code tags?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: help writing this program...

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    BTW The method: toString() is defined in many places to return a String describing the contents of an object. It would be better to use a different name, something that describes what the method does.

    Did you have any questions about your code?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,287
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: help writing this program...

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    How to post your code using code or highlight tags is explained near the top of the above link.

  6. #6
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    80
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: help writing this program...

    Quote Originally Posted by Norm View Post
    BTW The method: toString() is defined in many places to return a String describing the contents of an object. It would be better to use a different name, something that describes what the method does.

    Did you have any questions about your code?
    Actually, overriding toString() in this manner is perfectly reasonable, and is quite a good habit to get into. The notion is to provider a nice human readable textual representation of the class for debugging purposes, and sometimes the superclass implementation is rather cryptic. Even when used against a simple class printing only primitives and objects with reasonable toString() implementations, classes will change. Having a nice toString() to make human sense of, say, a complicated Map you add at some point is very useful, and also makes you think about being a "client" consumer of your own class right in the class itself.

    My nit would be with the //end blahBlah nonsense, which only makes a little sense when you absolutely have to have a long control structure. A method should generally never be so long that you, or your editor, lose context.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: help writing this program...

    overriding toString() in this manner is perfectly reasonable,
    The toString() in the posted code does not override the default toString() method. It is very different: static, void and has parameters.

    I agree that the correct toString() method is useful. My objection was the reusing of the name of a common, useful method in a different way.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    80
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: help writing this program...

    Quote Originally Posted by Norm View Post
    The toString() in the posted code does not override the default toString() method. It is very different: static, void and has parameters.
    Ah, yes. I missed that in the unformatted code. I often start a class like this with a main() and some class toString() debugging, so I made the poor assumption that is what was being done. You are, of course, correct.

    To the OP, in this case the method should be renamed to something useful; or if the class is intended to be used by "client" code, move the properties into the class itself and create methods to get/set them so a main() and/or toString() can be defined for testing and debugging.

Similar Threads

  1. need help with writing this program
    By nickans in forum Java Theory & Questions
    Replies: 12
    Last Post: May 6th, 2014, 08:33 AM
  2. Writing an encryption program, and keep getting this exception.
    By Jdafforn1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2013, 07:33 AM
  3. Need help writing a Java program
    By goose05 in forum Loops & Control Statements
    Replies: 9
    Last Post: April 3rd, 2012, 08:00 AM
  4. [SOLVED] Help needed writing a program using JOptionPane
    By s1mmi in forum Object Oriented Programming
    Replies: 3
    Last Post: January 30th, 2012, 05:39 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 04:14 AM

Tags for this Thread