Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7539543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:24:38+00:00 2026-05-30T07:24:38+00:00

I am doing some exercises from the book Thinking In Java . I have

  • 0

I am doing some exercises from the book Thinking In Java. I have some problems with one exercise. It says:

A constructor is a kind of factory method. Modify RegisteredFactories.java so that instead of using an explicit factory, the class object is stored in the List, and newInstance( ) is used to create each object.

How can I achieve that? Below is code for factory interface and RegisteredFactories class. Thank you

package typeinfo.factory;    
public interface Factory<T> { T create(); } ///:~
package cont;

import typeinfo.factory.*;
import java.util.*;


class Part {

  public String toString() {
    return getClass().getSimpleName();
  }

  static List<Factory<? extends Part>> partFactories =
    new ArrayList<Factory<? extends Part>>();     

  static {
    // Collections.addAll() gives an "unchecked generic
    // array creation ... for varargs parameter" warning.
    partFactories.add(new FuelFilter.Factory());
    partFactories.add(new AirFilter.Factory());
    partFactories.add(new CabinAirFilter.Factory());
    partFactories.add(new OilFilter.Factory());
    partFactories.add(new FanBelt.Factory());
    partFactories.add(new PowerSteeringBelt.Factory());
    partFactories.add(new GeneratorBelt.Factory());
  }

  private static Random rand = new Random(47);
  public static Part createRandom() {
    int n = rand.nextInt(partFactories.size());
    return partFactories.get(n).create();
  }
}

class Filter extends Part {}

class FuelFilter extends Filter {
  // Create a Class Factory for each specific type:
  public static class Factory
  implements typeinfo.factory.Factory<FuelFilter> {
    public FuelFilter create() { return new FuelFilter(); }
  }
}

class AirFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<AirFilter> {
    public AirFilter create() { return new AirFilter(); }
  }
} 

class CabinAirFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<CabinAirFilter> {
    public CabinAirFilter create() {
      return new CabinAirFilter();
    }
  }
}

class OilFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<OilFilter> {
    public OilFilter create() { return new OilFilter(); }
  }
} 

class Belt extends Part {}

class FanBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<FanBelt> {
    public FanBelt create() { return new FanBelt(); }
  }
}

class GeneratorBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<GeneratorBelt> {
    public GeneratorBelt create() {
      return new GeneratorBelt();
    }
  }
} 

class PowerSteeringBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<PowerSteeringBelt> {
    public PowerSteeringBelt create() {
      return new PowerSteeringBelt();
    }
  }
} 

public class RegisteredFactories {
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      System.out.println(Part.createRandom());
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:24:39+00:00Added an answer on May 30, 2026 at 7:24 am

    here:

    import java.util.*;
    
    class Part {
    
      public String toString() {
    
        return getClass().getSimpleName();
    
      }
    
      static List<Class> partFactories = new ArrayList<Class>();
    
      static {
    
        // Collections.addAll() gives an "unchecked generic
    
        // array creation ... for varargs parameter" warning.
    
        partFactories.add(FuelFilter.class);
    
        partFactories.add(AirFilter.class);
    
        partFactories.add(CabinAirFilter.class);
    
        partFactories.add(OilFilter.class);
    
        partFactories.add(FanBelt.class);
    
        partFactories.add(PowerSteeringBelt.class);
    
        partFactories.add(GeneratorBelt.class);
      }
    
    
      private static Random rand = new Random(47);
    
      public static Part createRandom() throws Exception {
    
        int n = rand.nextInt(partFactories.size());
    
        return (Part)(partFactories.get(n).newInstance());
    
      }
    
    }
    
    
    
    class Filter extends Part {}
    
    class FuelFilter extends Filter {
    }
    class AirFilter extends Filter {
    }
    class CabinAirFilter extends Filter {
    }
    class OilFilter extends Filter {
    }
    class Belt extends Part {}
    class FanBelt extends Belt {
    }
    class GeneratorBelt extends Belt {
    }
    class PowerSteeringBelt extends Belt {
    }
    
    
    
    public class aa{
    
      public static void main(String[] args) {
    
        for(int i = 0; i < 10; i++)
    
          try {System.out.println(Part.createRandom()); }catch (Exception e) {}
    
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some exercises from a book and I'm wondering if you could
i'm doing some exercises in my Java book. I'm very new to programming. Therefore,
I'm doing the first chapter exercises on my Java book and I have been
im doing some practice sql problems one of the questions is: Prepare a shipping
I'm doing exercise #9 from http://openbookproject.net/thinkcs/python/english2e/ch09.html and have ran into something that doesn't make
And I'm doing some exercises about switch. I just did it from console application
I just recently made the move to Objective-C. I am doing some exercises from
I have been doing some experimenting with iOS drawing. To do a practical exercise
Today I was doing the thread ring exercise from the Programming Erlang book and
I'm doing some exercises and I came across a problem. I have got DLL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.