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

Thread: Can you show me better a faster ways to accomplish these tasks.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Phoenix, AZ
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you show me better a faster ways to accomplish these tasks.

    This is a long post. I don't blame you if you don't read it. I'm very new to Java programming. I took an intro to Java class to learn more. My teacher gives us assignments and I'm able to accomplish them, however I just wish he would take more time to look at our code. I want to know better approaches that will help me solve problems more efficiently and with fewer code. My friend told to re-write some of the java built in functions so I can better understand how the language works. I took his challenge and decided to write a program that rounds decimal numbers. Can you show me faster ways to write this program? Its very long and probably shouldn't be.

    the program let's the user continually enter decimal numbers to round if they don't chose to exit

    I just put in the code that actually does the rounding

    In short, it takes the double, iterates an int to the first whole number that greater then the double, subtracts it by one, iterates that int by .10 and then compares that iteration to 5, if it's less then 5 it rounds down, if it's greater then 5 it rounds up. Then uses another loop to iterate an integer to equal the double value and then prints that iteration as the final integer.

    and the code does work

    int i = 0;
    while(i <= userDouble + 1)// Loop finds the first whole number that's greater then the double
    {
    if(i > userDouble){
    double tens = i - 1;// takes the greater int and subtracts one to get the closet whole the less then the double.
    double round = tens;
    int iTwo = 1;
    while(tens <= userDouble)// iterates the int directly below the double until it equals the double
    {
    tens = tens + .10;
    iTwo++; // keeps track of home many times it added . 10 to double tens
    if(tens > userDouble){
    if(iTwo < 5){ // when iTw0 is greater then initial double compare the iteration to 5.
    int w = 1;
    while(w <= round)// enters new loop and iterates an integer to equal the the double tens
    {
    if(w == round){ // checks when int w equals double round and then prints int w.
    int rounded = w;
    System.out.print(rounded);
    }
    w++;
    }
    }else{ // if iTwo is greater then 5 enter this loop
    int w = 1;
    while(w <= round) // iterate int w to eqaul double round
    {
    if(w == round){
    int rounded = w + 1; // print w + 1 to round up
    System.out.print(rounded);
    }
    w++;
    }
    }
    }
    }

    i++;
    }
    i++;
    }


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

    Default Re: Can you show me better a faster ways to accomplish these tasks.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. common tasks in java
    By gorgoro in forum Java Theory & Questions
    Replies: 9
    Last Post: October 4th, 2012, 06:26 AM
  2. android button tasks
    By bdtuhin007 in forum Android Development
    Replies: 0
    Last Post: January 30th, 2012, 09:39 PM
  3. [SOLVED] Is there any way I can access heap memory faster?
    By FunkyTecknician in forum Java Theory & Questions
    Replies: 8
    Last Post: July 30th, 2011, 12:51 AM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 03:43 PM

Tags for this Thread