943,735 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 908
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 27th, 2009
0

Determining output (test yourself!)

Expand Post »
Hey guys I just ran into some pretty interesting java problems on a different website and thought some of you might get a kick out of this. Determining on how well this goes I may post some more. So here comes problem 1.

Determine the following output with the use of data.txt

Java Syntax (Toggle Plain Text)
  1. import java.io.File;
  2. import java.util.Scanner;
  3. import java.io.IOException;
  4.  
  5. class RecArray{
  6.  
  7. private final static int NUM = 6;
  8. private int [][] myGrid = new int[NUM][NUM];
  9.  
  10. public RecArray(){
  11. load();
  12. }
  13.  
  14. public void load(){
  15. int row, col;
  16. String fileName = "data2.txt";
  17. try{
  18. Scanner inFile = new Scanner(new File(fileName));
  19. for (row = 0; row < NUM; row++)
  20. for (col = 0; col < NUM; col++)
  21. myGrid[row][col] = inFile.nextInt();
  22. }catch(IOException i){
  23. System.out.println("Error: " + i.getMessage());
  24. }
  25. }
  26.  
  27. public int calculate(){
  28. return calculate(0);
  29. }
  30.  
  31. public int calculate(int count){
  32. if (count >= myGrid.length){
  33. return 0;
  34. }else{
  35. int total = 0;
  36. for (int i = 0; i <= count; i++){
  37. total += myGrid[count][i];
  38. total += myGrid[i][count];
  39.  
  40. }
  41. total -= myGrid[count][count];
  42. System.out.println("count is: " + count + " and this total is: "
  43. + total);
  44. return total + calculate(count + 1);
  45. }
  46. }
  47.  
  48.  
  49.  
  50.  
  51. public void display(){
  52. int row, col;
  53. for (row = 0; row < NUM; row++){
  54. for (col =0; col < NUM; col++){
  55. System.out.print(myGrid[row][col] + " ");
  56. }
  57. System.out.println();
  58. }
  59. System.out.println();
  60. }
  61.  
  62. }
  63.  
  64. public static void main(String[] args) {
  65. RecArray app = new RecArray();
  66. int answer = app.calculate();
  67. System.out.println("The final answer is: " + answer);
  68. }

data.txt:

5 6 -4 10 -6 7
2 3 6 3 -8 2
4 -9 8 4 9 -3
7 9 2 -3 5 -7
5 -9 -4 7 6 -5
-1 3 8 -4 0 8



Okay guys get at it! Hopefully this will spark a little competitive spirit within the forum...

~IMtheBESTatJAVA
Last edited by IMtheBESTatJAVA; Feb 27th, 2009 at 9:08 am.
Similar Threads
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Feb 27th, 2009
1

Re: Determining output (test yourself!)

So answering to this thread would be just showcasing my Java skills as opposed to helping anyone ????

or

Is this a way of disguising your homework ??

or

Is this an assignment which you have copied from the NET and trying to get us to explain the code to ???

Well in the first case I do not have anytime for it and in the second case show us how much effort you put in tracing the execution path and for the third case forget about getting any help.
Last edited by stephen84s; Feb 27th, 2009 at 10:15 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Feb 27th, 2009
0

Re: Determining output (test yourself!)

Well assuming that this is not homework, I think that it is a good idea.

I know that most of us, including stephen84s, can do this in 30 seconds, but others might learn from it and newbies may test their skill.

Of course the OP could also add the link from which he took the code
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Feb 27th, 2009
2

Re: Determining output (test yourself!)

Homework by may search, from Palo Verde High School (home page) and teacher name is Ms. Chang( class site)
Last edited by peter_budo; Feb 27th, 2009 at 10:56 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Feb 27th, 2009
0

Re: Determining output (test yourself!)

This was the worst attempt at disguising homework I've ever seen. Lol.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Feb 27th, 2009
0

Re: Determining output (test yourself!)

This was the worst attempt at disguising homework I've ever seen. Lol.
I always like to give the benefit of the doubt. The OP might not go to that school, and accidentally found about that site, searched it a little and found that assignment and decided to post it.

Unlikely but possible
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Feb 27th, 2009
0

Re: Determining output (test yourself!)

Click to Expand / Collapse  Quote originally posted by javaAddict ...
I always like to give the benefit of the doubt. The OP might not go to that school, and accidentally found about that site, searched it a little and found that assignment and decided to post it.

Unlikely but possible
I too was secretly wishing that this wasn't some attempt at disguising homework, but the fact that the O.P. after reading the first two responses from me and you didn't dare post again (even though I saw him as one the viewers of the thread at the bottom), gives a clue as to what he was trying to do.

Note:-
  • I still hope that I am wrong.
Last edited by stephen84s; Feb 27th, 2009 at 11:21 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Feb 28th, 2009
0

Re: Determining output (test yourself!)

I am the best at java... Interesting statement. Does ego help at programming? Apparently, it surely does not help at getting a homework done
Reputation Points: 15
Solved Threads: 0
Newbie Poster
Taniotoshi is offline Offline
18 posts
since Feb 2009
Mar 1st, 2009
-1

Re: Determining output (test yourself!)

The name is a joke not a conceited egotistical statement of any sorts. And I'm not taking any courses right now I saved all of these worksheets from an AP Computer Science online course I found a way to access and figured some newer programmers could use problems from them to better themselves. If this was any attempt to disguise my homework and get you guys to do it, wouldn't I have included number 2? Do you guys honestly think I'd just post all the problems right in a row? You can view this post however you want I was just trying to get the community of newer programmers more involved in learning the language other than asking for help. Good job on looking up the worksheet though I figured someone would have done that.

Anyways this didn't turn out quite how I liked it as people would rather spend more time flaming and making accusations based on assumption than actually trying to LEARN. You don't even have to post your answers guys, I just thought this might assess some members' knowledge or maybe certain veterans' retainment. Oh and as to why I didn't repost, I've been too busy with work to check how this turned out. At any rate, I can guarantee that I won't be doing many more of these due to the amount of hostility. You guys obviously have some sort of obsessive compulsive disorders to go this far out of your way to denounce my attempt to actually help people. If anyone wants to know about this problem pm me because posters will just tear us apart.

-IMtheBESTatJAVA (Don't take that literally now you over-analyzing flamers!)
Last edited by IMtheBESTatJAVA; Mar 1st, 2009 at 2:28 pm.
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Mar 1st, 2009
1

Re: Determining output (test yourself!)

Before anybody else step into this I will said don't do it.
IMtheBESTatJAVA blaming this on the community will not work. You are fully aware of the rule We only give homework help to those who show effort. The above "pretty interesting java problems" does not include any details of what you may think is the out come of this code execution. Given your previous history (yes people keep an eye on other people posting history) and no reply to obvious accusation even though you read our replies you could step in and state your intentions, but you did not do so.

Next time do us and your self favour, provide some possible problem solution and not just copy and paste whatever from wherever assignment.

Now we can continue discussion on top if you want to provide some comments on code, otherwise we may close this post down. Your call...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: java problem
Next Thread in Java Forum Timeline: Java and HTML development enviornment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC