9 Replies - 1667 Views - Last Post: 19 February 2009 - 10:39 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Java generics

Posted 19 February 2009 - 07:17 PM

A topic i have never been too fond of, generics. I have started a class, egnore the pathetic extending of a Collection, its by order of the tutor and my complaints have been put forward.
import java.util.*;

public class Aircraft extends ArrayList
{
	
		public Aircraft(Passenger element)
		{
			super.add(element);
		}
		
		public void add(Passenger element) 
		{
			try
			{
				if (size()<250)
				{
					super.add(element);
				}
			}
			
			catch(Exception e) 
			{
				System.out.println(e); 
				System.out.println("Passenger number reached");
			}
		}
		
		
}


At the moment i am not even overridding the add method of ArrayList as i am passing a Passenger rather than an Object. So i want to make this generic instead. I am trying somthing along these lines
public void add(Object<Passenger> element) 
		{
			try
			{
				if (size()<250)
				{
					super.add(element);
				}
			}


And its not having any of it, and to be honest, i dont even know what i am attempting here! How would i make this class generic to the Passenger type?
cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Java generics

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java generics

Posted 19 February 2009 - 07:51 PM

That's slightly oxymoronic. You want to make it generic specific to the Passenger class? Generics are used when you won't know what object will be used at run time.

A generic function:

public T add (T t, S s)
{
	return (t+s);
}


Was This Post Helpful? 1
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Java generics

Posted 19 February 2009 - 08:33 PM

So you would have something like this:
package DICForum;

import java.util.ArrayList;



public class Aircraft extends ArrayList<Passenger> {
	
	public Aircraft(int seats) {
		super(seats);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Aircraft air = new Aircraft(23);
		Passenger joe = new Passenger();
		air.add(joe);

	}
}


Note that the add() method has a signature of:

boolean add(Passenger e);
or
boolean add(int index, Passenger e);

This is given to you for free when you extend ArrayList<Passenger> because ArrayList<E> has the generic functions:

boolean add(E e);
boolean add(int index, E e);


Of course if you insisted you could do something like:

package DICForum;

import java.util.ArrayList;



public class Aircraft<E extends Passenger> extends ArrayList {
	
	public Aircraft(int seats) {
		super(seats);
	}
	
	public boolean add(E e) {
		return super.add(e);
	};
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Aircraft air = new Aircraft(23);
		Passenger joe = new Passenger();
		air.add(joe);
	}
}
-- But other programmers would point and laugh at you.... so it is probably better if you don't (and don't let your tutor talk you into it).

By the way... you can often take a look at the source of the class you are extending to ensure you are not recreating functionality (like ensuring that the capacity is not exceeded).

I have the source attached within my IDE do that I can quickly look up any of the JRE classes... You learn a great deal that way.

This post has been edited by NickDMax: 19 February 2009 - 08:29 PM

Was This Post Helpful? 1
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java generics

Posted 19 February 2009 - 09:13 PM

The only reason i was insisting on overridding the add method was because i need to put limitations on when items can and cannot be added (dependant on passenger number and weight). Although terrible and it promotes bad java coding, this was the specification of my Aircraft class

Quote

The Aircraft
This must be an object which contains the passengers. You must use a Java Collection object (not an array) to hold the Passengers and this must limit the number of passengers either when the maximum seat number is reached (250 for this demo) or if the maximum weight is reached (21300). An elegant way to do this would be to subclass the Collection class you choose and add extra functionality to it (this will also get you better marks) but you could also just build in the functionality to your Aircraft class. The aircraft should calculate the fare for each passenger as they are added and keep track of the revenue being generated (total fares of all the passengers).


Why on earth you would want to extend a collection is beyond me when there are so many better choices e.g. composition. But for now i suppose i just have to suck it up and do it until my complaints are heard, lol.

BTW, where did you get the source code for the ArrayList class? I was looking everywhere for the JFrame source so i could make a triangle gui, without having to use to horrible looking new transparancy class, but i couldnt find the source anywhere.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java generics

Posted 19 February 2009 - 09:19 PM

Probably the Java API?
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java generics

Posted 19 February 2009 - 09:25 PM

The api only seems to provide documentation of the methods which are in a class, it dont specifically go behind the scenes and show the source code for these methods.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java generics

Posted 19 February 2009 - 09:26 PM

Oh, I misunderstood. I wonder if one could dig through the .jars in the C:/...java/jdk directory


edit: Nick didn't post any behind the scenes code. He just used what the API provided.

This post has been edited by KYA: 19 February 2009 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java generics

Posted 19 February 2009 - 09:37 PM

It is behind the scene code, for instance, if you click on the link, you can see all the methods which would be in the api for an ArrayList, and then it provides the code
  163	   public void trimToSize() {
  164		   modCount++;
  165		   int oldCapacity = elementData.length;
  166		   if (size < oldCapacity) {
  167			   elementData = Arrays.copyOf(elementData, size);
  168		   }
  169	   }


So thats the code behind the trimToSize method in ArrayList. I went to the root of Nick's link, and you can get them all from here. I am going through the JFrame code now. This is going to provide too much fun now!! Time too see how java truely works!
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java generics

Posted 19 February 2009 - 09:39 PM

Neat. I was looking at his re imagining of add(), but it is still not "source" since he just called super. Great link though.

This post has been edited by KYA: 19 February 2009 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Java generics

Posted 19 February 2009 - 10:39 PM

You can download the source for the Sun JDK from the Java.Sun.com You can also find most of it in the OpenJDK website (see the links for source)

You are right that you will have to set the hard limit yourself. The array list will grow to fit the data. I was not really thinking clearly -- Airplanes don't magically grow new seats (despite what JetBlue would like to think). Where as the ArrayList will create new space. So you have to set a limit. You should probably make that a property though rather than a hard-coded value. -- Generally I try to stay away from hard coded values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1