d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Generics Problem
12Next
Add Reply New Topic New Poll
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 06:09pm
I am creating a generic puzzle Solver and am having trouble with one thing. Getting my solver completed.


I have an abstract class Puzzle<T>

Clock extends Puzzle<Integer>

Water extends Puzzle <ArrayList<Integer>>

public Solver(Puzzle puzzle)
{
this.puzzle = puzzle;
}

public void Solve( )
{

ArrayList queue = new ArrayList();

boolean solution = false;

T current = puzzle.getStart();
}

right now I just want to be able to return a TYPE back and store it in Solver, anyone have any ideas?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 08:28pm
what do you mean you want to be able to return the type? you want to know what class it has? you can use someobject.getClass() for that, but i don't think that's really what you want.
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 08:51pm
import java.util.ArrayList;

public class Clock extends Puzzle<Integer>
{
private int _start;
private int _hours;
private int _goal;


public Clock(int hours, int start, int goal )
{
_start = start;
_hours = hours;
_goal = goal;
}

public int getStart(){
return _start;}

public boolean isGoal(int node){
return (node == _hours);
}

/*public ArrayList<Integer> getNeighbors(int hour){
ArrayList<Integer> neighbors = new ArrayList<Integer>();

//If Hours is 1, Move to Capacity Hour hour
if( hour == 1){
neighbors.add(getCapacity());
neighbors.add(hour+1);
}
else if (hour == getCapacity()){
neighbors.add(hour-1);
neighbors.add(1);
}
else{
neighbors.add(hour-1);
neighbors.add(hour+1);
}

return neighbors;
}*/
}

import java.util.ArrayList;

public abstract class Puzzle<T> {

public Puzzle(){}

public void Solve( ){
T start = getStart();
}

}

This help?

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 09:01pm
i think your design is just flawed.

First, ignore Clock altogether. you have to finish your Puzzle class first. two most obvious questions:
1) why is Puzzle generic if none of the methods are generic?
2) assuming getStart() is supposed to return T, why aren't you defining it in Puzzle to return T?

does getStart() have any meaning inside Puzzle, or is it only valid for Clock? i assume getStart() should always return an int. so either define it that way in Puzzle, or have Clock override Solve and use getStart internally. if getStart() should return a generic T, then you should not need to know what class it is. at best, you can declare T to extend a class/interface to have specific methods.

This post was edited by carteblanche on Apr 11 2013 09:08pm
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 09:14pm
The whole point is that I need to make multiple puzzle types, Clock, Water, Pogo, Etc, and the Solver method calls methods from Clock or Water, etc to create the solution.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 09:16pm
Quote (NamelessPrince @ Apr 11 2013 11:14pm)
The whole point is that I need to make multiple puzzle types, Clock, Water, Pogo, Etc, and the Solver method calls methods from Clock or Water, etc to create the solution.


explain this part a little better.

So far i'm not seeing anything that requires generics. just have Puzzle with an abstract solve() and have each subclass override solve() to do the solution.

if there is a lot of duplicate code, create a second wrapper method

eg:

abstract void solveImpl();

void solve(){
// start up, prompt user, etc
solveImpl();
// show message, clear the screen, etc
}

where each subclass overrides solveImpl()


Either way, ignore Clock for now and just write Puzzle completely. the whole point of a super class or generics is that you don't need to know the subclasses ahead of time. the logic should just work regardless

This post was edited by carteblanche on Apr 11 2013 09:20pm
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 09:22pm
public static void main(String args[]){

Puzzle c = new Clock(12,3,5);
c.solve();

}

this is basically what I wanna do, I want Puzzle to call methods from CLock.

Or if we could do Clock c = new Clock(12,3,5);
c.solve();

This post was edited by NamelessPrince on Apr 11 2013 09:22pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 09:24pm
Quote (NamelessPrince @ Apr 11 2013 11:22pm)
public static void main(String args[]){

Puzzle c = new Clock(12,3,5);
c.solve();

}

this is basically what I wanna do, I want Puzzle to call methods from CLock.

Or if we could do Clock c = new Clock(12,3,5);
c.solve();


Ok then. do what i suggested. get rid of generics and make solve() an abstract method. if Puzzle doesn't have any logic by itself, i recommend changing Puzzle to an interface IPuzzle instead of an abstract class.
Member
Posts: 4,609
Joined: Mar 9 2008
Gold: 2,937.00
Apr 11 2013 09:27pm
Quote (carteblanche @ Apr 11 2013 11:24pm)
Ok then. do what i suggested. get rid of generics and make solve() an abstract method. if Puzzle doesn't have any logic by itself, i recommend changing Puzzle to an interface IPuzzle instead of an abstract class.


My Solve() method needs to be able to solve all puzzles through generic means, Which I have it working, I did this project in c++ I just can't seem to get it to work the same with java.

I want to create a Clock that implements Puzzle, and Puzzle has the solve method that can Call onto Clock for Data. Thats all it needs to do, I want:

Clock c = new Clock(12,3,5);
c.solve();

and the is where the solve() method is located of generic types and can call data from the Clock class.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 11 2013 09:32pm
i do not know why you keep saying "generic" since i dont see anything generic about it. please explain what is generic.

write what you're trying to accomplish in pseudocode if you have to. explain why you need to know the types, as per your original question.

until you give me more information, my post #6 has two options you can do depending on whether you have shared logic inside puzzle.

keep in mind that Puzzle should NOT be dependent on what the subclass is. i should be able to create a brand new subclass that adheres to your interface without changing Puzzle and it should just work.

This post was edited by carteblanche on Apr 11 2013 09:33pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll