TranceAddict Forums (www.tranceaddict.com/forums)
- Technology & Gadget Zone
-- need coding help
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 }
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]. |
code:
if (digitArray[z] == digitArray[i])
code:
if (digitArray[z] == i)
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)
| quote: |
| Originally posted by stan229 then i woudlnt be able to access firstOccurence cuz its private unless theres another way? |
| 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: |
Maybe one of your for cicles isn't ending for some reason.
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); } } }
| 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. |
tried a simple test and still got a nullPointerException..hmm
code:
positions[0].setFirstOccurence(5); System.out.println(positions[0].getFirstOccurence());
we don't do other people's homeworks.
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; }
code:
PositionPair[] positions = new PositionPair[10];
code:
for(int a = 0; a < 10; a++) { positions[a] = new PositionPair(); //You'll need a default constructor for this! }
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); } } }
wow u must of needed that badly 
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; } }
Powered by: vBulletin
Copyright © 2000-2021, Jelsoft Enterprises Ltd.