TranceAddict Forums

TranceAddict Forums (www.tranceaddict.com/forums)
- Technology & Gadget Zone
-- need coding help


Posted by stan229 on Oct-09-2006 21:26:

need coding help

any java coders? or c++ ?

this should be easy for you but im getting stumped.

(a) Create a Java class PositionPair, with instance fields private int firstOccurrence = -1 and private int secondOccurrence = -1. Use the Eclipse tool Source->Generate Getters and Setters to write the get and set methods for these fields.

(b) In your main class, write a method that contains an array PositionPair[] positions = new PositionPair[10]. Loop through all of digitArray. The first time you see the digit d in digitArray, store the index in positions[d].firstOccurrence and the second time, store the index in positions[d].secondOccurrence. When you are done, find the minimum value of all the firstOccurrences, that are positive, where the corresponding secondOccurrence is not -1. Return that minimum firstOccurrence as the return value of the method.

I cant get it working..
so far my code is

code:
package homework6; public class PositionPair { private int firstOccurence = -1; private int secondOccurence = -1; public int getFirstOccurence() { return firstOccurence; } public void setFirstOccurence(int firstOccurence) { this.firstOccurence = firstOccurence; } public int getSecondOccurence() { return secondOccurence; } public void setSecondOccurence(int secondOccurence) { this.secondOccurence = secondOccurence; } public static int Fuck(int[] digitArray) { PositionPair[] positions = new PositionPair[10]; int i = 0; int z = 0; for (i = 0; i < digitArray.length; i++) { for (z = i + 1; z < digitArray.length; z++) { if (digitArray[z] == digitArray[i]) { positions[i].firstOccurence = i; positions[i].secondOccurence = z; } } } //return something }


Thanks guys, really appreciate it


Posted by noikeee on Oct-10-2006 01:15:

Re: need coding help

I found something odd:

quote:
Originally posted by stan229
(b) In your main class, write a method that contains an array PositionPair[] positions = new PositionPair[10].


You were told to do this in your main class, not in the PositionPair class as it is. It seems odd to create an array of PositionPairs in a method part of a single PositionPair object.

Also, shouldn't this part:

code:
if (digitArray[z] == digitArray[i])


actually be this:

code:
if (digitArray[z] == i)




I think that's the problem with filling out the "positions" array, after it has been filled it should be easy to return the wanted value.

Try drawing a bunch of arrays in a sheet of paper and filling them out by yourself as you simulate the program running in your head. I ocasionally do that and it helps. Or you could just debug.


Posted by stan229 on Oct-10-2006 02:13:

then i woudlnt be able to access firstOccurence cuz its private
unless theres another way?

also changin the thing to digitArray[z] == i doesn change anything, and im using that just to test if the value is repeated

the error i get is:

code:
Exception in thread "main" java.lang.NullPointerException at homework6.PositionPair.Fuck(PositionPair.java:41) at homework6.PositionPair.main(PositionPair.java:56)


Posted by noikeee on Oct-10-2006 02:37:

quote:
Originally posted by stan229
then i woudlnt be able to access firstOccurence cuz its private
unless theres another way?


That's why they tell you to generate the getFirstOccurence(), getSecondOccurence(), etc methods - you can use them to access the fields indirectly.

quote:
also changin the thing to digitArray[z] == i doesn change anything, and im using that just to test if the value is repeated

the error i get is:

code:
Exception in thread "main" java.lang.NullPointerException at homework6.PositionPair.Fuck(PositionPair.java:41) at homework6.PositionPair.main(PositionPair.java:56)


NullPointerException in Java? Maybe one of your for cicles isn't ending for some reason.


Posted by stan229 on Oct-10-2006 03:33:

yea so i modified the code.. had to create a ne class
i used your advice on the method so i have

code:
int[] digitArray = { 0, 1, 1, 3, 4, 5, 5, 7, 8, 9 }; PositionPair[] positions = new PositionPair[10]; for (int i = 0; i < digitArray.length; i++) { for (int z = i + 1; z < digitArray.length; z++) { if (digitArray[z] == digitArray[i]) { positions[i].setFirstOccurence(i); positions[i].setSecondOccurence(z); System.out.println(i); System.out.println(z); } } }


the prof juts sent us an email with the advice, but im ont sure if it even relates to me yet
quote:


To deal with the problem indicated below, we either have to initialize the array OR
check for null before accessing a particular location. I will expalin the former
solution in the assignment, right now.

Problem:
When checking to see if firstOccurrence equals -1 i
write "if(positions[num].getFirstOccurrence()==-1)" and when i run the
program it gives me a null pointer exception.


im confused lol

still getting a null pointer exception on the line that uses the setFirstOccurence method


Posted by stan229 on Oct-10-2006 04:22:

tried a simple test and still got a nullPointerException..hmm

code:
positions[0].setFirstOccurence(5); System.out.println(positions[0].getFirstOccurence());


the first line caused the exception.. wtf is going on lol


Posted by Omega_M on Oct-10-2006 15:36:

we don't do other people's homeworks.


Posted by ���|E on Oct-15-2006 12:10:

Firstly, where's your default constructor for the PositionPair Class? cause at the moment you cant even create one of these objects right? So make one of those.

code:
public PostionPair() { firstOccurence = -1; secondOccurence = -1; }


Ur missing one lil concept here i think, the line ....
code:
PositionPair[] positions = new PositionPair[10];

Doesn't magically give you instantiations of 10 PositionPair objects, you still need to do this via ...

code:
for(int a = 0; a < 10; a++) { positions[a] = new PositionPair(); //You'll need a default constructor for this! }


so now ur code looks like ....

code:
int[] digitArray = { 0, 1, 1, 3, 4, 5, 5, 7, 8, 9 }; PositionPair[] positions = new PositionPair[10]; //ADDED CODE for(int a = 0; a < 10; a++) { positions[a] = new PositionPair(); //You'll need a default constructor for this! } for (int i = 0; i < digitArray.length; i++) { for (int z = i + 1; z < digitArray.length; z++) { if (digitArray[z] == digitArray[i]) { positions[i].setFirstOccurence(i); positions[i].setSecondOccurence(z); System.out.println(i); System.out.println(z); } } }


Should do the trick by initialising the objects in the array first, let me know how that works for you


Posted by ���|E on Oct-17-2006 11:56:

wow u must of needed that badly


Posted by stan229 on Oct-17-2006 12:21:

thanks for everyones help i appreciate it.. lol i did
heres the final working code:

code:
// Sorry that I put everything in the test class. //I realized to change it in the last minute and don't have time to, code will still work though. package homework6; public class PositionTest { /** * Find the minimum index for firstOccurence * * @param positions * @return index */ public static int findMinimum(PositionPair[] positions) { if (positions.length == 0) return -1; int smallestVal = -1; for (int i = 0; i < positions.length; i++) { if (positions[i].getSecondOccurence() != -1 && (positions[i].getFirstOccurence() < smallestVal || smallestVal == -1)) smallestVal = positions[i].getFirstOccurence(); } return smallestVal; } public static void main(String[] args) { int[] digitArray = { 0, 1, 2, 1, 2, 5, 5, 7, 8, 9 }; PositionPair[] positions = new PositionPair[10]; for (int i = 0; i < positions.length; i++) positions[i] = new PositionPair(); // Loop to test for firstOccurence and secondOccurence for (int i = 0; i < digitArray.length; i++) { if (positions[digitArray[i]].getFirstOccurence() == -1) { positions[digitArray[i]].setFirstOccurence(i); } else if (positions[digitArray[i]].getSecondOccurence() == -1) { positions[digitArray[i]].setSecondOccurence(i); } } // Prints out array for (int i = 0; i < positions.length; i++) { System.out.println(positions[i].getFirstOccurence() + " " + positions[i].getSecondOccurence()); } int n = findMinimum(positions); System.out.println(n); } }


code:
package homework6; public class PositionPair { private int firstOccurence = -1; private int secondOccurence = -1; public int getFirstOccurence() { return firstOccurence; } public void setFirstOccurence(int firstOccurence) { this.firstOccurence = firstOccurence; } public int getSecondOccurence() { return secondOccurence; } public void setSecondOccurence(int secondOccurence) { this.secondOccurence = secondOccurence; } }


Once again, thank you guys. I was up till very late in the morning working on this that day.



Powered by: vBulletin
Copyright © 2000-2021, Jelsoft Enterprises Ltd.