d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Generics Problem
Prev12
Add Reply New Topic New Poll
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 09:36pm
Dude Puzzle<T> can be anything, Clock extends Puzzle<Integers> that means all data types of T get replaced with Integers.

Clock extends Puzzle<Integer>
Water extends Puzzle<ArrayList<Integer>>

Get it now?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 09:54pm
Quote (NamelessPrince @ Apr 11 2013 11:36pm)
Dude Puzzle<T> can be anything, Clock extends Puzzle<Integers> that means all data types of T get replaced with Integers.

Clock extends Puzzle<Integer>
Water extends Puzzle<ArrayList<Integer>>

Get it now?


you misunderstood. i am well aware of what java generics are. i do not understand why your goals require generics. your code is full of contradictions and holes.

For example, your Puzzle class is using getStart generically but getStart isn't even defined so i know Puzzle isn't even compiling.

Then your Clock defined getStart but it is not generic.

Then your Solver class calls getStart from Puzzle (which doesn't exist) and gets back a T return value. but you dont actually use this value. ergo you do not need this based on your code so far.

Puzzle has a solve() method, but it doesn't return anything. what do you expect it to do?

if Puzzle has a solve() method, why is Solver calling getStart() instead of solve()?

So for now, i need you to ignore Clock and just show me the completed (pseudo)code for Puzzle and Solver so i can understand why you need T.

nobody can help you until you give sample code explaining what you're trying to accomplish, and that's done via Puzzle and Solver classes.
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 09:57pm
No I want getStart() To be called from Clock,

I am telling you all my functions in Puzzle need to call to CLOCK and return Data!

getStart() returns data from Clock and since Puzzle<T> is already cast as a Puzzle<Integer> it returns to Integer start = getStart();

I cannot declare abstract getStart() in Puzzle though.

Not showing psedocode because theres nothing to show!

I just wanna know how I can make the Friken Puzzle communicate with Clock, THAT IS ALL!

Don't worry about generics, just tell me how I can get puzzle to use the functions located in Clock

This post was edited by NamelessPrince on Apr 11 2013 09:57pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 10:01pm
Quote (NamelessPrince @ Apr 11 2013 11:57pm)
No I want getStart() To be called from Clock,

I am telling you all my functions in Puzzle need to call to CLOCK and return Data!

getStart() returns data from Clock and since Puzzle<T> is already cast as a Puzzle<Integer> it returns to Integer start = getStart();

I cannot declare abstract getStart() in Puzzle though.

Not showing psedocode because theres nothing to show!

I just wanna know how I can make the Friken Puzzle communicate with Clock, THAT IS ALL!

Don't worry about generics, just tell me how I can get puzzle to use the functions located in Clock


lets pretend for a moment that you have some magical function that gets you this info. show me the rest. this is the critical part.

eg:

Code
public abstract class Puzzle<T> {

public Puzzle(){}

public void Solve( ){
   T start = myClockClass.getStart(); // lets assume this magically works
   // now what goes here???
   // everything inside this method is crucial for me to determine how to help you
   // how exactly are you using the 'start' variable?
}

}



This post was edited by carteblanche on Apr 11 2013 10:08pm
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 10:07pm
Don't worry about it it's written in C++ I don't even care, if I can get 1 piece of data from the Clock class than I can do the rest myself...Seriously it's not hard I just need this one part solved.

I just NEED Puzzle to use functions Located in Clock, It might be I need to change Clock from extending to something else, That is it, Don't worry about the rest of the code, seriously, I just need to know how to communicate to Clock and I can do everything else.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 10:08pm
based on what you've said, i'm confident that your design is flawed. if you want to keep going with this idea, you can do this

Code
public void solve(){
  if (this instanceof Clock){
      int start = ((Clock)this).getStart();
      // do other stuff specific to clock
   } else if (this instanceof Jigsaw){
       int numberOfPieces = ((Jigsaw)this).getNumberOfPieces();
       // do other stuff specific to jigsaw
   }
}

but that's incorrect design

or if you specifically need to know the type but not use it, use this.getClass() and optionally .toString()

This post was edited by carteblanche on Apr 11 2013 10:11pm
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 10:11pm
// Name: solve
//
// Description: solves the puzzle in the fewest steps
// by adding the neighbors to the map with the current value
// it previously was calcualted from. At the end it adds the values
// to the list to be printed.
void solve(){
int test = 0;
//Solution is currently true
bool solution = true;

//Create Empty Queue
deque<T> myQueue;

//Set Current position to start
T current = getStart();

//Create Empty Vector of no neighbors
vector<T> neighbor;

//Create a Map for each
map<T,T> visited;

//Get Start Positon + add to Queue
myQueue.push_back(getStart());

//Add start to Map ( as key + value )
visited.insert( pair<T,T> (getStart(),getStart()) );

//While Goal is not found at current value
while( !(isGoal(current)) && solution ){

//get neighbors
neighbor = getNeighbors(current);

//While neighbor is not empty
while( !neighbor.empty() ){
//Get Current Neightbor
T n = neighbor.at(neighbor.size() - 1);

//Put in map current node and position called from
if( visited.insert(pair<T,T> (n,current)).second ){
//Put number on queue
myQueue.push_back(n);}

//Pop off key
neighbor.pop_back();

}

//if Queue is empty, no solution
if (isGoal(current)) {
solution = true;}
else if( myQueue.empty()){
solution = false;}
else{
//current is not front element
current = myQueue.front();

//take off front element from queue
myQueue.pop_front();}
}


if( !solution){
cout << "NO Solution is found" << endl;
}
else{
//Create iterator to traverse through the map
typename map<T, T>::iterator it;

//Get first instance of Stop key in map
it = visited.find(current);

//While there still exists a key
while( visited.count((*it).first ) > 0 ) {

//Push key value onto stack
_solution.push_front((*it).first);

//Save key Value
T key = (*it).first;

//Search for new key value
it = visited.find((*it).second);

visited.erase(key);
}
}

}

// Name: (constructor)
//
// Description: Creates a Clock with Given Information
Clock::Clock(int hours, int start, int stop):
Puzzle<int>(),
_hours(hours),
_start(start),
_stop(stop){
}

// Name: (deconstructor)
//
// Description: Destroys the bound memory on stack/heap
Clock::~Clock(){
//std::cout << "DESTROY CLOCK" << std::endl;
}

// Name: getStart
//
// Description: returns clock's starting value
int Clock::getStart(){
return _start;
}

// Name: isGoal
//
// Description: Checks if given argument is equal to
// stop value and return boolean value.
bool Clock::isGoal(int& v){
int goal = 0;
//Is destination = end goal
if( v == _stop){
goal = 1;}
//return (1 = true, 0 = false)
return goal;
}

// Name: getNeighbors
//
// Description: Calculates the given neighbors of the value
// provided, stores those values in the neighbor vector and
// returns the vector.
std::vector<int> Clock::getNeighbors(int& v){
//Check for Special Cases 12 + 1
int n1;
int n2;

//If Calculated to neighbors is 1 O'clock
if( v == 1){
n1 = _hours;
n2 = 2;}
//Calcualte neighbors if 12 O'Clock
else if ( v == _hours ){
n1 = _hours - 1;
n2 = 1;}
//Calculate Normally
else{
n1 = v - 1;
n2 = v + 1;}

//Create + add neighbors to vector
std::vector<int> neighbor;
neighbor.push_back(n1);
neighbor.push_back(n2);

return neighbor;
}

// Name: display
//
// Description: displays the tui interpretation of the answer.
void Clock::display(){
//Display List
int count = 0;

for (list<int>::iterator it=_solution.begin();
it != _solution.end(); it++){
cout << "Step " << count << " : " << *it << endl;
count++;}

cout << "# of Steps : " << _solution.size() << endl;
}

See this is the C++ code, all Puzzle needs to do is Call methods from Clock, THATS IT! It's not flawed it works perfectly fine in C++ I just can't get the abstract class to work write as a template

Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Apr 17 2013 05:27am
What you are doing with generics in Java is just straight wrong. Java generics don't work like that and your design is fucked, regardless of whatever that terrible C++ code does. Define an interface, have clock implement it and have the solver call the methods you defined on the interface. You should probably first figure out what generics are for and how they work before you start using them because you obviously have no clue.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll