0

I have to make a program which uses a different class for each part. For the first class: Quest1, I calculated the rate of growth in a population. Second class: Quest2, i have to store the growth into an array, theres where im having a problem, how would I go about doing that?

public class Quest1 {

public double pop, rate;
public int day;

public void setPop(double population)
{
    pop = population;
}
public double getPOP()
{
    return pop;
}
public void setRate(double rates)
{
    rate = rates;
}
public double getRate()
{
    return rate;
}
public void setDay(int days)
{
    day = days;
}
public double getDays()
{
    return day;
}

public double getNew(double pop, int day, double rate)
{
    double popul, population = 0;
    for (double i = 0; i < day; i++)
    {
        popul = pop + (pop * rate/100);
        population = day*popul;
    }
    return population;
}

}

public class Main {


public static void main(String[] args) throws IOException{
    Scanner kd = new Scanner(System.in);
    Quest1 a = new Quest1();
    Quest2 b = new Quest2();

    double tempPop, tempRate; int tempDay;


    System.out.println("Enter Population: ");
    tempPop = kd.nextDouble();
    a.setPop(tempPop);

    System.out.println("Enter Days: ");
    tempDay = kd.nextInt();
    a.setDay(tempDay);


    System.out.println("Enter Rate: ");
    tempRate = kd.nextDouble();
    a.setRate(tempRate);


    b.storeArray();
}



public class Quest2 {


Quest1 a = new Quest1();

public void storeArray(){
    double scores [] = new double[(int) a.getDays()];
    for(int i = 0; i < a.getDays(); i++)
    {
        scores[i] = a.getNew(a.getPOP(), i+1, a.getRate()); 
        System.out.println(scores[i]);
    }
    return;
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
roro
  • 713
  • 2
  • 6
  • 19
  • how do i successfully store from class quest1 to class quest2 in an array. – roro May 21 '13 at 18:00
  • Why do you eave need two Classes? Just make the function in Q1 which returns a double[]. And when you need it in Q2 just call this function. – Kuchi May 21 '13 at 18:02
  • the output from the getNew method, how do i store those values in an array in class Quest2. @0A0D – roro May 21 '13 at 18:03
  • thats obvious @Kuchi but its in the question to make another class. If that was the case, i would not be asking the question. – roro May 21 '13 at 18:04

2 Answers2

1

I think you need to pass the quest1 object to quest2 class.

b.storeArray(a);

You are creating a new object of Quest1 in quest2.so that will not calculate the growth. Modify in quest2 class.

public void storeArray(Quest1 a){..}

Hope you are asking this.

sp_user123
  • 512
  • 3
  • 6
  • 28
0

If you want to pass an array from one class to another you can just use the below if its a servlet.

request.setAttribute("name", arrayListName);
rd = request.getRequestDispatcher(classname);
rd.forward(request,response);

then pick it up on the other class with

ArrayList namelist=(ArrayList)request.getAttribute("name");

Is that what you are after?

Otherwise there is another similar question below.

Passing an array from class A to class B

Community
  • 1
  • 1
Rhys
  • 2,807
  • 8
  • 46
  • 68