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

Thread: What Is The Purpose Of This Piece Of Code

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What Is The Purpose Of This Piece Of Code

    This is a exercise to practice desk checking so dont say to run it.
    I cant follow through the loops
    can someone please help me



    import javax.swing.JOptionPane;
    public class Main {
    public static void main(String[] args) {
    int values[] = new int[5];
    int z;
    for (int i = 0; i<5; i++){
    values[i]=Integer.parseInt(JOptionPane.showInputDialog("Ent er Int
    value"));
    }
    for (int p = 0; p<4; p++){
    for (int q=(p+1); q<5; q++) {
    if (values[q]<values[p]){
    z=values[q];
    values[q]=values[p];
    values[p]=z;
    }
    }
    }
    for (int i = 0; i<5; i++){
    System.out.println(values[i]);
    }
    }
    }


    Presuming that you have identified the purpose of the above code, and how its algorithm operates, write a brief specification of the method.
    Now assume that you are an independent tester of software, and have only been given the specification of that method, and that you have to “fully” test it as an independent method.
    Could you think of a set of test cases (i.e. more than just one) which you would need to develop to
    do this? Key to developing a set of test cases is to think of any “special cases” of the input data which might not be “picked up” due to “slight” errors in the development of the algorithm used by the programmer.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Get out a piece of paper and write down all the variables and their values, Step through each step slowly and write down the values accordingly. That's the best way to do a desk check.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    i cant seem to follow the loop through would you be able to help me

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    take each piece one at a time.

    Ill help you get started


    int values[] = new int[5];
    		int z;
    		for (int i = 0; i<5; i++){
    			values[i]=Integer.parseInt(JOptionPane.showInputDialog("Ent er Int value"));
    		}

    This allows the user to enter in 5 integers into an array.


    for (int p = 0; p<4; p++){   // this loop goes 4 times
    			for (int q=(p+1); q<5; q++) {   //this loop goes 4 times
    				if (values[q]<values[p]){     
    					z=values[q];             //temp variable used for swapping. HINT: swapping involves sorting.
    					values[q]=values[p];
    					values[p]=z;
    				}
    			}
    		}

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Can you post here what you have written down for the values of the variables when the loops start?
    What are the variables's values first thing inside the outer loop
    and first thing inside of the inner loop?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    i knew that the first part asks for them to enter 5 numbers and puts them into the arrays its the next part that i cant follow as i dont really know where to start

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Do what Norm suggested, write down the values each time through the loop and post what you think they are here.

  8. #8
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    i only have the values when it asks the user to enter values and puts them in the array as i cant seem to follow the loops. the values i have there are 7, 4, 10, 1 ,19

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Do you know how for loops work?

    Look into nested loops . The inner code of the outter loop will go (inner loop * outter loop) times.

  10. #10
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    where would i start. with the if statement then work out to the inner for then the outer?

  11. #11
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    https://www.youtube.com/watch?v=ZcTgmKtvSQ4

    This will help explain nested loops better.

    You're going to want to start with the first loop, then the second loop, then the if statement.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    @amb101293: 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.

  13. #13
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    in the if statement where does the values[q] and values [p] come from

  14. #14
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    they are the values that you put in. q and p are just the indexes. So if you have an array that you put 5 integers in say they were

    values = 2, 90, 6, 100, 8

    and value of q is 0, Then values[q] is the same as saying values[0], which in turn is 2.

  15. #15
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    is it right then that the first time that the loops go through that it is as follows

    for(int p=0; p<4; p++){
       for(int q=0+1(1); q<5; q++){
          if(values[q] < values[p])
             //If statement wont run because values[q] !< values[p]
    Last edited by amb101293; October 5th, 2014 at 11:40 AM. Reason: forgot [/highlight]

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    The notes you are writing should show the values of these 4 variables just before the if statement:
    p
    q
    values[p]
    values[q]

    Then you will know whether the if is true or not.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    the code that i posted is the only code i have for this question

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Yes, that is the code we are talking about. You need to use paper and pencil to write down the values of variables as the code executes. Just before the if statement what are those 4 values?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    p - 0
    q - 1
    values[p] - 7
    values[q] - 12

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    The notes need to show the full contents of the values array so the code that follows can use those values.

    Now continue with the rest of the statements in the code: "execute" each statement and write down the new values of the variables if they are changed in a statement.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    would the final answer after the loops have ran all 4 times that the array is as follows if the 5 values entered is : 7, 12, 2, 10, 21

    would the answer be

    7, 2, 10, 12, 21

  22. #22
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    Not quite, you're getting closer though.

  23. #23
    Junior Member
    Join Date
    Oct 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What Is The Purpose Of This Piece Of Code

    This is what i got well at the end of the for, for And if loop is that correct before I go on to do the last bit

Similar Threads

  1. Why doesn't JTextArea execute in this piece of code?
    By coltson in forum AWT / Java Swing
    Replies: 2
    Last Post: June 15th, 2013, 03:00 AM
  2. Converting a piece of C# code to Java (unsigned longs, etc)
    By nahkiss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2013, 12:44 PM
  3. How do I represent any character in a piece of code?
    By 93tomh in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2012, 05:28 AM
  4. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  5. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM

Tags for this Thread