Past exam problems on Java methods

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1632 Views - Last Post: 13 April 2014 - 12:48 AM Rate Topic: -----

#1 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Past exam problems on Java methods

Posted 11 April 2014 - 11:21 PM

Hello everyone, I am new to java and new to this forum. However I have some questions regarding my past exam questions relating to methods that im stuck on. So here is a method i wrote for the first part for initializing an array:


The question was, "write a method which initializes a 2D target array (assuming all other classes have been imported and given), and all targets should be stationary."

and this was my method i wrote:

 public void initializeTargetArray(Target[][] targets) {
        int row = targets.length();
        int col = targets[0].length;
        
        for(int i = 0; i < row; i++)
               for (int j = 0; j < col; j++)
               targets[i][j] = new Target(Target.STATIONARY, false);
         }


Now my question how do I initialize an array similar to the method above for a different question?

Quote

Part b
Write a method which initializes an array (You can assume that the 2D array itself (but not the Target
objects inside) is already initialized), where the game level stage is now set:
Game level 1 - all targets are stationary,
Game level 2 - For even numbered rows, targets at even numbered columns are stationary, targets at odd
numbered columns are movable. For odd numbered rows, targets at odd numbered columns are
stationary, targets at even numbered columns are movable.
Game Level 3 - All targets are movable.

 public void initializeTargetArray(Target[][] targets, int gameLevel) { 
//*** Finish this method. ***// 

Attached File(s)


This post has been edited by jon.kiparsky: 11 April 2014 - 11:27 PM
Reason for edit:: Keep text in-line, it's easier for everyone.


Is This A Good Question/Topic? 0
  • +

Replies To: Past exam problems on Java methods

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Past exam problems on Java methods

Posted 11 April 2014 - 11:30 PM

You should probably think about this in terms of simple decision branching - if this, then that, and so forth. There are three possible states to consider, so you have three possible branches to implement, or four if you (wisely) consider the possibility of user error.

There may be more effective ways to solve the problem - in fact, since the introduction of Java 8, I can think of one for sure - but this is the place to start thinking about it.
Was This Post Helpful? 0
  • +
  • -

#3 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 02:13 AM

Could you please enlighten me? because I really dont get how to write this particular method.
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 10:36 PM

What was said is correct. You need a control statement, which controls what part of code is execute and which is not based on some information.

The first thing you must is resolve the different options use if else statements.
http://docs.oracle.c...ndbolts/if.html
Was This Post Helpful? 1
  • +
  • -

#5 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 10:42 PM

actually the first part of the problem was the method i originally wrote and was indeed correct. Now I get you should use the if and if - else to control the different levels, but how do you make it movable? and how would i set the odd and even? Could you give me some ideas?

Thank You
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Past exam problems on Java methods

Posted 12 April 2014 - 10:46 PM

To test whether a number n is even, check to see whether it is evenly divisible by two - that is, if n%2 ==0. It looks like there's a defined constant for Target.STATIONARY, so I'd bet there's one for Target.MOVABLE as well.
Was This Post Helpful? 0
  • +
  • -

#7 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 10:51 PM

could you go something like

int row = targets.length();
int col = targets[0].length;
int gameLevel;

if (gameLevel == 1)
{for(int i = 0; i < row; i++)
   for (int j = 0; j < col; j++)
   targets[i][j] = new Target(Target.STATIONARY, false);}
if else (gameLevel == 2)
{....}
if else (gameLevel == 3)
{...}



but i dont get how to make it movable, and set even and odd, i mean even i think i can go something like if i = 0, then i + 2 would be even etc... but like how to determine if it will be movable or not I am truly confused.
Was This Post Helpful? 0
  • +
  • -

#8 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 10:58 PM

Read what jon.kiparskywrote, and you made the same mistake you don't do .length() for arrays only do .length

Just in case : http://en.wikipedia....odulo_operation
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:01 PM

That looks like a good frame for this logic. Now you have the hard part.

Quote

but i dont get how to make it movable, and set even and odd, i mean even i think i can go something like if i = 0, then i + 2 would be even etc... but like how to determine if it will be movable or not


You're not asking if it's movable - you're telling it. The same way you told it to be STATIONARY in your first example. On the other hand, you're not setting even and odd. If the row number is even, you do it one way, and if the row number is odd, you do it the other way. You'll have to check even and odd for each column as well.


Quote

I am truly confused.


I'm going to gently suggest that you're flailing now. To stop flailing, start designing. You have your top-level logic: if it's level one, do level one stuff. If it's level two, do level two stuff. If it's level three, do level three stuff.
Level three looks a lot like level one to me, maybe you should try writing that next.

For level two, you're going to have to do more decision branching. There are some clever ways to do this that don't require a huge amount of code. For now, don't bother trying to find those. Just do something that you think you can understand.

If you're still confused, try to pose a question that someone can answer, or try to pose your confusion in a useful way. Why are you confused? Do you have a hypothesis that seems to be contradicted by the facts you're observing? Do you have some doubts about the logic? What is confusing you? Asking better questions will help you think through your problems, which is the real point of this assignment.
Was This Post Helpful? 2
  • +
  • -

#10 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:40 PM

jon.kiparsky do you mind taking a look at the code that i think i completed? I have sent you a PM.

Thank you
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:50 PM

Might as well post it here. I don't know that I'm so infallible that I might not miss something, and if what I have to say is worth anything, we might as well let everyone see it. And anyway, it's 3 AM here. I might not be up that much longer.

I can tell you that there are a few mistakes in what you sent me, but it's much better to see you making mistakes than sitting around not writing code because you might make a mistake. Mistakes, you can fix.
Was This Post Helpful? 0
  • +
  • -

#12 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:52 PM

Could you please let me know what these mistake are? that would be greatly helpful.
Was This Post Helpful? 0
  • +
  • -

#13 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:56 PM

the mistakes are always wrong
lol
Was This Post Helpful? 0
  • +
  • -

#14 1ArchMage1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 14

Re: Past exam problems on Java methods

Posted 12 April 2014 - 11:57 PM

I have actually edited it again in the PM, maybe you could take a look at it again once you are up. Greatly appreciated.

Thank you

View Postinfernorthor, on 12 April 2014 - 11:56 PM, said:

the mistakes are always wrong
lol


what is this supposed to mean?

View Postinfernorthor, on 12 April 2014 - 11:56 PM, said:

the mistakes are always wrong
lol


what is this supposed to mean?
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Past exam problems on Java methods

Posted 13 April 2014 - 12:18 AM

ooookay.

Here's what you sent me:

public void initializeTargetArray(Target[][] targets, int gameLevel) {
int row = targets.length;
int col = targets[0].length;

if (gameLevel == 1)
{for(int i = 0; i < row; i++)
   for (int j = 0; j < col; j++)
   targets[i][j] = new Target(Target.STATIONARY, false);}
else if (gameLevel == 2)
{ int i;
if (i%2 == 0) {
 for (int i = 0; i < row; i = i + 2)
  for (int j = 0; j < col; j = j + 1)
  targets[i][j] = new Target(Target.STATIONARY, false);
  for (int j = 1; j < col; j = j + 2)
  targets[i][j] = new Target (Target.MOVABLE, false);
}
else { 
for (int i = 1; i < row; i = i + 2)
  for (int j = 0; j < col ; j = j + 2 )
  targets[i][j] = new Target(Target.MOVABLE, false);
  for (int j = 1; j < col; j = j + 2)
  targets[i][j] = new Target(Target.STATIONARY, false); 
}
}
else if (gameLeve == 3) {
for(int i = 0; i < row; i++)
   for (int j = 0; j < col; j++)
   targets[i][j] = new Target(Target.MOVABLE, false);
}
}



The biggest problem is the formatting is all off. This is important. Badly formatted code is prime bug habitat - bugs can find lots of places to hide in there. If you format your code in a reasonable fashion, errors will be easier to see. So that's the first thing to fix.
Second mistake: there is an obvious compiler error. This of course is an error in its own right, but it's also a sign that you haven't even compiled this code and tested it yourself. Why not? ("What's the error?", you ask. The compiler will tell you)

The logic in the level two section is clearly an attempt, which is good, but it's got a lot of flaws in it, and it makes an unreasonable assumption about the structure of the array (you assume that it has an even number of rows)

Try writing out the logic for level again, following the assignment carefully. It's pretty clear about what you have to do. If you edit the code into running state, you can of course test it and see what's going wrong, and try to reason about why it's failing, that often helps. But really: follow the assignment as it's written.

for each row
  if the row is even, do one thing
  else, do another thing


The "one thing" and the "another thing" are specified in the assignment.

This post has been edited by jon.kiparsky: 13 April 2014 - 12:19 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2