#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    6

    Need Help in Design


    Hi,
    i am new to Java
    i need to make a project in java this semester.
    What i am trying to do is RF Calculator.
    The user inserts component to the RF chain and the program
    prints all the calculations at the end.
    Moderator forbids me to use Matlab.

    I have a class component
    Code:
    abstract class Component{
    	private componentType m_type;
    	
    	public Component(componentType type)
    	{
    		m_type = type;
    	}
    }
    and 2 subclasses
    Code:
    class AmplifierComponent extends Component{
    	public AmplifierComponent()
    	{
    		super(componentType.Amp);
    		System.out.print(this.m_type);
    	}
    }
    
    class AttenuatorComponent extends Component{
    	public AttenuatorComponent()
    	{
    		super(componentType.Att);
    		System.out.print(this.m_type);
    	}
    }
    my problems are:
    1.i can't instantiate any kind of component because m_type isn't visible(what that means?)
    2.i need to make an array of all the components the user has insert to the chain. i can't manage to create an array of Component class.

    can someone help me with the design?
    or with some workarounds?

    Thanks in advance
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    private instance - not visible


    In your abstract class Component, you have declared the m_type variable as a private variable. Hence, it is only accessible within the Component class itself, not even the implementing classes of Component (a.k.a subclasses) can access it.

    To solve this problem, simply change the access modifier of m_type from private to protected instead.


    As for your second question, I am not sure how you wrote your codes to declare/create an array of Component, but there should not have any problems doing that:

    Java Code:
     
       private Component[] arrayOfComponents = new Component[10];


    Unless you know in advance exactly how many Component instances you are going to have to put into the collection, using a fixed-sized data structure like an array is not a good idea. If you do not know how many Component instances your program would have at run time, it is better to declare the variable to hold collection of Component instances as an ArrayList instance.
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    6
    Thanks tvc3mye
    ArrayList is equivalent to Map from c++?
  6. #4
  7. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Map vs List


    No. Map and its variations accept 2 parameters: an object acting as the 'key' and an object acting as the item referenced by the 'key'. List and its variations, however, are like expandable arrays. They do not require a reference to their values like maps do, but merely use position indices to reference an item they held at a particular position, just like an array does.
    Last edited by tvc3mye; December 22nd, 2012 at 02:33 AM. Reason: grammar error :p
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo