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

Thread: Addition

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Addition

    A company provides 4 different services whose costs are as follows:
    Service 1: $100
    Service 2: $200
    Service 3: $300
    Service 4: $400
    Write a Java application that reads the service(s) provided to a particular customer then calculates and display the total charge of all services provided. You should be able to use some technique we learned in class to determine when program should stop looping and then display the final results. Perhaps end the program when “0” is entered. Note: The GUI is not required.

    Here is what I have so far:

    import java.util.Scanner;
    public class Service2 {
    public static void main(String agrs []){
    Scanner input = new Scanner(System.in);

    int service1 = 100;
    int service2 = 200;
    int service3 = 300;
    int service4 = 400;
    System.out.println("Enter service number, (1-4), (the program exits if the input is 0): ");
    int data = input.nextInt();

    // keep reading data until the input is 0

    int sum = 0;
    while (data != 0){
    sum +=data;

    System.out.println("Enter service number, (1-4), (the program exits if the input is 0): ");
    data = input.nextInt();

    }
    System.out.println("The sum is " + sum);

    } // end main
    } // end


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    Quote Originally Posted by ruffu054 View Post
    Here is what I have so far:
    And???

    Do you have a question? Do have errors? Do you want a pat on the back. Or do you just want someone to do your work for you?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    I dont have any errors however, I dont the output that I'm looking for. I need to somehow multiply or add the services to get the output of: 1 + 3 = 300.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    How about an if statement?
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Addition

    need to somehow multiply or add the services to get the output of: 1 + 3 = 300.
    Can you explain your arithmetic?
    What is the 1 and the 3 and the 300?
    Normally 1 + 3 = 4?

    Have you looked at using an array to hold the charges for each of the services?
    The elements would be the charge amount, the index would be the service id number.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    I'm sorry I'm so new at this. What should the if statement say????

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    Quote Originally Posted by Norm View Post
    the index would be the service id number minus 1.
    To avoid an empty array element.
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    In the top of the question it states the different services that are provided. Service 1 = 100....Service 4 = 400.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    My current program reads 1 + 3 =4. I need it to say 1 + 3= 400

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    Use an if statement on the user input to decide what value to add. Then again if service 1 costs 100, service 2 costs 200 etc then you could just multiply input by 100 and then add it.
    Improving the world one idiot at a time!

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    Again Im new the if statement should be in between the while statement?

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    Forget the if statement, forget the array. Just multiply by 100.
    loop if input is not 0
        get user input
        multiply by 100
        add to sum
    }
    display sum
    Improving the world one idiot at a time!

  13. #13
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    Help me out Junky?? how should I write this.

  14. #14
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    Thanks your awesome!!!

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    Ummmm its still not working?

  16. #16
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    Again Im very new...... the if statement should be in ()?

  17. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition

    You don't need an if statement. Just follow the pseudocode posted above.
    Improving the world one idiot at a time!

  18. #18
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Addition

    I got Junky; a little different but you really really really help me a lot..

Similar Threads

  1. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 09:52 AM
  2. text value addition problem
    By kundan_101 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: January 8th, 2011, 11:46 AM
  3. Vector Addition with small numbers giving NaN
    By Zula in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2010, 09:29 PM
  4. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 12:55 PM