Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Generics and GeometricObject

    When I compile, I'm getting an error that says the Geometric class cannot be found. I've imported all of the right packages, and as far as I know, the syntax where I'm creating an ArrayList with Generics is correct. Can you spot where my problem is?

    Just for kicks, I replaced "GeometricObject" with "String" and it compiled perfectly. My TA also cannot find the problem.

    Code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    
    public class TowersOfHanoi
    {
    	public static void main(String[] args) throws Exception
    	{
    		ArrayList<GeometricObject> vList = new ArrayList <GeometricObject>();
            }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Is GeometricObject in the same package? If not you need to ensure its either been imported or provide it with a resolvable packed within the generic call.

    Are you compiling the TowersOfHanoi manually with javac or are you using an IDE? If using javac, I'd actually throw it into an IDE and verify it compiles. If so, you are likely looking at a classpath config, or a non-compiled GeometricObject (although the last time I tested with Javac it gladly compiled the required dependencies for me; I'm quite sure it didn't in the past o.O).

    Edit:
    Oh, and yes your call is fine (although it may be ArrayList<com.yourpackage.GeometricObject> instead, but that can always be imported)
    Regardless the idea is correct.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I realized that GeometricObject is not in the API, so now I'm creating my own GeometricObject class. I'm confused however about not being able to use the add method for vList. vList is an ArrayList variable with generic GeometricObject, right? So I assumed I could use the ArrayList methods on it. But after I've tried it, it seems that I need to make an add method in my GeometricObject class. Is this correct?

    Code:
    /*
    	Program by Tommy Tran
    	CS 2336.OU1 Prof Kirtzic
    	This program demonstrates recursive problem solving using the Towers of Hanoi example, uses File input/output,
    	arrayList, and graphs.  
    */
    
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    
    public class TowersOfHanoi
    {
    	static int counter = 1;
    	
    	public static void main(String[] args) throws Exception 
    	{
    		Scanner input = new Scanner(System.in);
    		//code for part 1
    		//get the number of discs from the user
    		/*
    		System.out.print("Enter the number of initial disks: ");
    		int n = input.nextInt();
    		
    		System.out.println("Start: " + n + " disks are on pole A.");
    		solve(n, 'A', 'B', 'C');
    		
    		System.out.println("Finish: " + n + " disks are on pole B.");
    		System.out.println();
    		*/
    			
    		//this is where the code for part 2 starts. create a scanner to read from the file
    		File file = new File("graph_input.dat");
    		Scanner inputFile = new Scanner(file);
    		
    		//create arraylists to hold the pieces of data
    		ArrayList<GeometricObject> vList = new ArrayList<GeometricObject>();
    		ArrayList<String> eList = new ArrayList<String>();
    		ArrayList<Circle> thirdList = new java.util.ArrayList<Circle>();
    
    		//move set V into an arraylist
    		String vertices = inputFile.nextLine();
    		StringTokenizer st = new StringTokenizer(vertices, "V={}, ");
    
    		//a temp string array to hold all of the integers in V from the input file
    		for(int i = 0; i <= 21; i++)
    		{
    			vList.add(st.nextToken());
    		}
    		
    		//move every third element into thirdList
    		//
    		
    		//move set E into eList
    		String edges = inputFile.nextLine();
    		StringTokenizer sE = new StringTokenizer(edges, "E={}");
    		while(sE.hasMoreTokens())
    		{
    			eList.add(sE.nextToken());
    		}
    		
    		//remove the indices that contain just commas
    		for(int k = 1; k < eList.size(); k++)
    		{
    			eList.remove(k);
    		}
    		
    		//now create and output to graph_output.txt an adjacency list, adjacency matrix, and incident matrix.
    		File outFile = new File("graph_output.txt");
    		PrintWriter output = new PrintWriter(outFile);
    		
    		//write out the adjacency list
    		output.println("Adjacency List");
    		
    		//write out the adjacency matrix
    		output.println("Adjacency Matrix");
    		
    		//write out the incidence matrix
    		output.println("Incidence Matrix");
    	}
    
    	
    	//the method that recursively solves the problem
    	public static void solve(int n, char fromPole, char toPole, char auxPole)
    	{		
    		//base case
    		if(n == 1)
    		{
    			System.out.println("Step " +  counter++ + ": Move disk " + n + " from " + fromPole + " to " + toPole);
    		}
    		
    		//recursively call itself
    		else
    		{
    			solve(n-1, fromPole, auxPole, toPole);
    			System.out.println("Step " + counter++ + ": Move disk " + n + " from " + fromPole + " to " + toPole);
    			solve(n-1, auxPole, toPole, fromPole);
    		}
    	}
    }
    
    class GeometricObject
    {
    	GeometricObject()
    	{
    	}
    	void add(
    }
    
    class Circle extends GeometricObject
    {
    	Circle()
    	{
    	}
    }

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This part's wrong:
    PHP Code:
    class GeometricObject
    {
        
    GeometricObject()
        {
        }
        
    void add(

    Its syntactically incomplete. Just remove the void add(.

    The generics is correct. The problem is you are pulling a token off of the st which is a tokenizer. That will be a string, so you cannot add it to the list. Not sure what you need to do with the token, but presumably you'll need to use it to build the GeometricObject itself.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I need to output an adjacency list to a file. I know how to write out to a file, but have no clue how to make an adjacency list. Would I use if statements? But what would I put in the if's parenthesis?
    Right now, this is what I have in mind:

    Code:
    for(int p = 1; p <= 8; p++)
    {
         output.print(p + "   ");
         for(int t = 1; t <= 8; t++)
         {
              if(//p is paired with t in set eList)
              {
                     output.print(t + "   ");
              }
         }
    }
    Here's the whole code. The piece above will go under the comment "write out the adjacency list"
    Code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    
    public class TowersOfHanoi
    {
    	static int counter = 1;
    	
    	public static void main(String[] args) throws Exception 
    	{
    		Scanner input = new Scanner(System.in);
    		//code for part 1
    		//get the number of discs from the user
    		/*System.out.print("Enter the number of initial disks: ");
    		int n = input.nextInt();
    		
    		System.out.println("Start: " + n + " disks are on pole A.");
    		solve(n, 'A', 'B', 'C');
    		
    		System.out.println("Finish: " + n + " disks are on pole B.");
    		System.out.println();
    		*/
    			
    		//this is where the code for part 2 starts. create a scanner to read from the file
    		File file = new File("graph_input.dat");
    		Scanner inputFile = new Scanner(file);
    		
    		//create arraylists to hold the pieces of data
    		ArrayList<GeometricObject> vList = new ArrayList<GeometricObject>();
    		ArrayList<String> eList = new ArrayList<String>();
    		ArrayList<Circle> thirdList = new java.util.ArrayList<Circle>();
    
    		//move set V into an arraylist
    		String vertices = inputFile.nextLine();
    		StringTokenizer st = new StringTokenizer(vertices, "V={}, ");
    
    		//a generic arraylist to hold all of the integers from the V set as GeometricObject objects
    		for(int i = 0; i <= 21; i++)
    		{
    			GeometricObject piece = new GeometricObject(st.nextToken());
    			vList.add(piece);
    			
    			//move the appropriate Vertices into the third arraylist of circles
    			if(i==0 || i%3 == 0)
    			{
    				Circle item = new Circle(piece.token);
    				thirdList.add(item);
    			}
    		}
    		
    		//move set E into eList
    		String edges = inputFile.nextLine();
    		StringTokenizer sE = new StringTokenizer(edges, "E={}");
    		while(sE.hasMoreTokens())
    		{
    			eList.add(sE.nextToken());
    		}
    		
    		//remove the indices that contain just commas
    		for(int k = 1; k < eList.size(); k++)
    		{
    			eList.remove(k);
    		}
    		
    		//now create and output to graph_output.txt an adjacency list, adjacency matrix, and incident matrix.
    		File outFile = new File("graph_output.txt");
    		PrintWriter output = new PrintWriter(outFile);
    		
    		//write out the adjacency list
    		output.println("Adjacency List");
    		for(int n = 1; n <= 8; n++)
    		{
    			output.print("   " + n);
    		}
    		1
    		output.println();
    		
    		//write out the adjacency matrix
    		output.println("Adjacency Matrix");
    		
    		//write out the incidence matrix
    		output.println("Incidence Matrix");
    		
    		output.close();
    	}
    
    	
    	//the method that recursively solves the problem
    	public static void solve(int n, char fromPole, char toPole, char auxPole)
    	{		
    		//base case
    		if(n == 1)
    		{
    			System.out.println("Step " +  counter++ + ": Move disk " + n + " from " + fromPole + " to " + toPole);
    		}
    		
    		//recursively call itself
    		else
    		{
    			solve(n-1, fromPole, auxPole, toPole);
    			System.out.println("Step " + counter++ + ": Move disk " + n + " from " + fromPole + " to " + toPole);
    			solve(n-1, auxPole, toPole, fromPole);
    		}
    	}
    }
    
    class GeometricObject 
    {
    	String token = "nothing";
    	
    	GeometricObject()
    	{
    	}
    	
    	GeometricObject(String nextToken)
    	{
    		token = nextToken;
    	}
    }
    
    class Circle extends GeometricObject
    {
    	Circle()
    	{
    	}
    	
    	Circle(String nextToken)
    	{
    		token = nextToken;
    	}
    	
    }
    edit: I think I'd use eList.contains(t); but this would check to see if t is anywhere in eList. I need to check where t AND p are in the same element.
    Last edited by trantommyd; 06-28-2012 at 06:57 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is now an entirely different beast; this doesn't relate to the original question. If you are writing adjacency lists, it may be required to use a graph to place them. If you are using adjacency, then you would write a Vertex class which includes an ArrayList<Vertex> within it, as well as the Graph containing an ArrayList<Vertex>.

    Data for it can be dealt with as you see fit. You can create a single entry of data where each line represent a relationship and the first item within it is the primary node, or you can write it in sets such as {a, c}, {c, f}, {b, a}, etc, or even write serialized data. I'd probably write the sets as {a, c, x} where x would represent a weight for use in a weighted graph. You can choose many formats, plain text, line by line, grouped like I have here, xml, whatever. All that matters is you can interpret it both ways.

    In any case if you are now looking into using something like a graph datastructure, you should write a graph. I would not try to simulate a graph without one as that's just a headache waiting to happen. Writing graphs for adjacency matrix, incidence matrix, adjacency lists, or incidence lists are not that hard to do, but you have to have knowledge in the structure in order to make use of it.

    Your small question of generic usage has now become a requirement for a graph datastructure.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •