Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question How to count the number of an element using an argument?

    Good afternoon!

    I'm learning how to run collection and Exception at my university.

    I have a code I have to fill in in order to count the number of vehicules using a fuel gived as an argument:

    Here is the code:

    Java Code:
    import java.util.*;
    
    class Vehicule{
    	int no;
    	int puissance;
    	String fuel;
    	static List LV = new LinkedList();
    	Moteur motorisation;
    	static int = 10;
    	Vehicule (String carb, int p){
    		no=NoC++;
    		LV.add(this);
    		if(carb=="Diesel"){
    			fuel= carb;
    			puissance = p;
    			this.motorisation = new Diesel();
    			motorisation = motorisation.this;
    		}
                    if(carb == "Essence"){
    			fuel= carb;
    			puissance = p;
    			this.motorisation = new Essence();
    			motorisation = motorisation.this;
                    }
    	}
    }
    
    static int NbModeles(String carburant){
    	int n = 0;
    	
    	FILL IN
    
    }
    
    class Moteur implements Comparable{
    	String carburant;
    	int no;
    	int puissance;
    	Moteur(){ListeMoteurs.add(this);}
    	static set ListeMoteurs = new TreeSet();
    	static String MotorisationDefaut(Vehicule v){
    		return "Diesel";
    	}
    	Set puissancesDispo(Moteur m)
    	{
    		Set s = new TreeSet(); return s;
    	}
    }
    I tried:

    Java Code:
    static int NbModeles(String fuel){
    	int n = 0;
    	
    	retunrn n = frequency(LV, fuel);
    
    }
    But the code given in the answer was

    Java Code:
    static int NbModeles(String fuel){
    	int n = 0;
    	Iterator it = Moteur.ListeMoteurs.iterator();
    	While(it.hasNext()) if (((Moteur)it.next()).carburant.equals(carburant))n++;
    	return n;
    }
    I think I understand that I should have used
    Java Code:
    Moteur.ListeMoteurs
    but I'm not quite sure why...
    I thin I didn't understood what we were talking about here... So should I have guessed we where talking about
    Java Code:
    Moteur.ListeMoteurs
    ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to count the number of an element using an argument?

    Do you have a specific java programming question?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. count every 45 records and prfix the count number
    By dkr786 in forum New To Java
    Replies: 8
    Last Post: 02-15-2013, 07:31 PM
  2. Count number of rows
    By Kenan_89 in forum Advanced Java
    Replies: 11
    Last Post: 01-26-2012, 12:52 PM
  3. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  4. day number count
    By droidus in forum New To Java
    Replies: 14
    Last Post: 03-23-2011, 10:15 PM
  5. Replies: 4
    Last Post: 10-31-2010, 05:19 PM

Tags for this Thread

Posting Permissions

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