• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm stuck in one of my programs for tomorrow's exam  RSS feed

 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers!
I'm a Java newbie, and even though I can do basic Java programs with ease, some programs make me feel I've hit a roadblock.
Tomorrow, that is 27th October in the Indian calendar, I have my Java final Lab Examination. I need an explanation on some programs that I may feel tough.
So, here's my first program which I've felt difficult. Please help me in understanding it, most importantly the algorithm of the program in all the loops, both Do-While and the For loops inside the program.

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In your program there are 2 kinds of loops: do-while and for.

The essence of do-while loops is that it will always execute at least once, continue until the while condition is false. Do-while loops usually used when you don't know how many iterations.

For loops is used when you do know how many iterations. As you iterate you can use the index of the loop element (eg array, list, map, set, etc) to do what you like.

 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I do know the basic functions (definitions) of the loops. What I meant in my question was that I need to know how the loops in this program work depending on the input we give during program execution.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if you have learned collections, but there is a simple method in Collections class to do sorting: Collections.sort(list)

Since you are working with arrays, the Arrays class has a method to convert an array to a list: Arrays.asList(array)

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhilesh Puranik wrote:Thanks for the reply. I do know the basic functions (definitions) of the loops. What I meant in my question was that I need to know how the loops in this program work depending on the input we give during program execution.


Well the goal of the program is sorting. So does the for loop able to sort the input array correctly?
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll surely try that out once.
Thanks again.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post. Always use them: doesn't it look better
What do the catch blocks do? Are you sure they will maintain the correctness of the programming? If I enter 123.45 instead of an integer, what will happen you your array? Will it finish up with 5 entries in, or will there be an extraneous 0?
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the code tags. Yes, they do look better! I was ignorant and couldn't notice how I could add code tags. I'm new to the whole concept of forums.
The first catch block, that is the NumberFormatException catches any number that is not int. So 123.45 or any other number of a different data type (even characters) will result in an exception and all the five inputs inserted will go waste. The program will stop execution.
Regarding the second catch block, the ArrayIndexOutOfBoundsException, it catches the exception when we put more than 6 entries. This program is coded to take in 6 entries, five for whole numbers, and the sixth entry, the 0, for the program to exit. Else it will repeat because of the While-Do looping construct of the program.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhilesh Puranik wrote:Thank you for the code tags. . . .
You're welcome
Nikhilesh Puranik wrote: . . .
The first catch block, that is the NumberFormatException catches any number that is not int. So 123.45 or any other number of a different data type (even characters) will result in an exception and all the five inputs inserted will go waste. The program will stop execution. . . .
In which case, apart from showing that you know how to write try‑catch, what is the point of catching such an Exception?
Nikhilesh Puranik wrote: . . . Regarding the second catch block, the ArrayIndexOutOfBoundsException, it catches the exception when we put more than 6 entries. This program is coded to take in 6 entries, five for whole numbers, and the sixth entry, the 0, for the program to exit. Else it will repeat because of the While-Do looping construct of the program.
What is wrong with programming the loop to run 5×?
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhilesh Puranik wrote:Thanks for the reply. I do know the basic functions (definitions) of the loops. What I meant in my question was that I need to know how the loops in this program work depending on the input we give during program execution.

Hi Nikhilesh,
You can try adding some logging (System.out.println) inside the loops and check how the loops are working, what algorithm is it using to sort the numbers.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Nikhilesh Puranik wrote:Thank you for the code tags. . . .
You're welcome
Nikhilesh Puranik wrote: . . .
The first catch block, that is the NumberFormatException catches any number that is not int. So 123.45 or any other number of a different data type (even characters) will result in an exception and all the five inputs inserted will go waste. The program will stop execution. . . .
In which case, apart from showing that you know how to write try‑catch, what is the point of catching such an Exception?
Nikhilesh Puranik wrote: . . . Regarding the second catch block, the ArrayIndexOutOfBoundsException, it catches the exception when we put more than 6 entries. This program is coded to take in 6 entries, five for whole numbers, and the sixth entry, the 0, for the program to exit. Else it will repeat because of the While-Do looping construct of the program.
What is wrong with programming the loop to run 5×?


Oh, the try and catch blocks were used in this program as per the requirement of my Java Lab in college. Also, our exam invigilator will ask us to enter other values apart from integers, and also enter only integers more than the array size to check out how we've coded the try-catch blocks.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pallavi Sadit wrote:
Nikhilesh Puranik wrote:Thanks for the reply. I do know the basic functions (definitions) of the loops. What I meant in my question was that I need to know how the loops in this program work depending on the input we give during program execution.

Hi Nikhilesh,
You can try adding some logging (System.out.println) inside the loops and check how the loops are working, what algorithm is it using to sort the numbers.


Yeah. I'll definitely do that. But unforch, my lab exam has ended. It was held today.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the try is required by the exam, then it is required. Hope it went well
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It did go well today, and luckily, I did not get this program, as I had never completely understood the program. I was told to write and execute two programs, Operations on two character strings, and method overloading to calculate different areas of triangles.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done anyway.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well done anyway.


Well, thank you
And since I'm satisfied now, I shall mark this thread as resolved, soon.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!