return to tranceaddict TranceAddict Forums Archive > Other > Technology & Gadget Zone

 
need coding help
View this Thread in Original format
stan229
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 (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
paranoik0
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. :wtf:

Also, shouldn't this part:

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


actually be this:

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


:conf:

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.
stan229
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.(PositionPair.java:41) at homework6.PositionPair.main(PositionPair.java:56)
paranoik0
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.(PositionPair.java:41) at homework6.PositionPair.main(PositionPair.java:56)


NullPointerException in Java? :wtf: Maybe one of your for cicles isn't ending for some reason.
stan229
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
stan229
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
Omega_M
we don't do other people's homeworks.
ô§§|E
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
ô§§|E
wow u must of needed that badly :rolleyes:
stan229
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.
CLICK TO RETURN TO TOP OF PAGE
 
Privacy Statement