0

I'm fairly new to Java and I'm struggling to understand a few concepts. What I am trying to achieve is constructing a Magazine in Magazine class, which requires an array of objects from the Supplement class in its constructor. I've created a method inside Supplement which fills up an array of objects of supplements, and I'm trying to call this method inside Magazine to fill up another supplement object I have created in there. Then, I am trying to pass that array of objects to the Magazine constructor so that one magazine can have this array of 4 supplements. Make sense? The problem I'm facing firstly, is that my method fillArray is not recognized in Magazine from class Supplement. All my classes are under the same source package, so what's the problem here? Some feedback on where I may be going wrong with my logic some guidance in the right direction would be helpful as I have to apply this logic for some other associated classes as well and I'm not quite sure how to go about it.

Another question I have, is that upon doing some research I see that some people use public static void main(String args[]) in their classes for some operations. What is the purpose of this, as I thought it was only used inside the client program, and if it is used in classes what would be the use of the client program?

Magazine.java:

public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private Supplement[] supplement;

    public static void main(String args[]){

        Supplement[] supplements = new Supplement[3];
        supplements.fillArray();

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
    };

    public void SetMagazineName(String magazinename1){    
        magazinename = magazinename1;
    };

    public void SetWeeklyCost(int WeeklyCost1){
        WeeklyCost = WeeklyCost1;    
    };

    public String GetMagazineName(){        
        return magazinename;
    };

    public int GetWeeklyCost() {        
        return WeeklyCost;
    };

    public void SetMagazine(String magazinename1, int WeeklyCost1, Supplement[] supplements1){
            magazinename = magazinename1;
            WeeklyCost = WeeklyCost1;
            supplement = supplements1;
    };

    public Magazine(String magazinename1, int WeeklyCost1, Supplement[] supplements1){
        SetMagazine(magazinename1,WeeklyCost1,supplements1);
    };
}

Supplement.java:

public class Supplement {
    private String supplementname;
    private int WeeklySupCost;

    Supplement[] supplements = new Supplement[3];

    public void fillArray(){ 
        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);
     }

    public void SetSupplementName(String supplementname1){ 
        supplementname = supplementname1;
    };

    public void WeeklySupCost(int WeeklySupCost1){
        WeeklySupCost = WeeklySupCost1;
    };

    public String GetSupplementName(){
        return supplementname;
    };

    public int GetWeeklyCost(){        
        return WeeklySupCost;
    };

    public void SetSupplement(String supplementname1, int WeeklySupCost1){
        supplementname = supplementname1; 
    };

    public Supplement(String supplementname1, int WeeklySupCost1){
        SetSupplement(supplementname1, WeeklySupCost1);
    }
}
name not found
  • 622
  • 4
  • 13
thedafferg
  • 99
  • 7
  • Note: you should follow the Java Naming Conventions: variabele names and method names should be written in camelCase. – MC Emperor Apr 22 '20 at 08:13

2 Answers2

0

Change fillArray as below,

 public Supplement[] fillArray(){
        Supplement[] supplements = new Supplement[3];

        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);
        return supplements;
     }

And then create Supplement object and call fillArray ,

        public static void main(String args[]){

            Supplement supplement = new Supplement();
            Supplement[] supplements = supplement.fillArray();

            Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);

        };

About the main method, check here

Vikas
  • 6,868
  • 4
  • 27
  • 41
0

Your problem is that you are attempting to call the Supplement instance method fillArray() on a Supplement[], not a Supplement.

You need to call the fillArray() method on each element of the array (rather than on the array).

Change:

supplements.fillArray(); 

to:

for (Supplement supplement : supplements) {
      supplement.fillArray();
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • That appears to get rid of the errors. Can you explain to me what the statement Supplement supplement : supplements really means, and once I've created the Magazine object using this array of supplements, how would I use it in the main program? – thedafferg Apr 22 '20 at 06:40
  • The syntax `for (Supplement supplement : supplements)` is a [*foreach loop*](https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html), which iterates over `supplements`, assigning each element to `supplement` in turn. If you have a Magazine with Supplements, try `for (Supplement supplement : magazine.getSupplements()) { /* do something with supplement */ }` – Bohemian Apr 23 '20 at 23:42