Java; how to call strings from other class?

Joined
Nov 6, 2007
Messages
880
Reaction score
11
So my friend gave me access to one of java course guides. I got to the last project which is writing a farm simulator. Where Every change of year a percentage of rabbits die, the year changes, wolves die, etc.

I've gotten the script to read from a properties file, to get the settings.

I'm having problems with figuring out how to change the settings and then get called by the main class.

Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.text.NumberFormat; 


class game {
    public static void main(String[] args) throws IOException {
        
        environment environment = new environment();
        rabbits rabbits = new rabbits();
        FileInputStream in = new FileInputStream("config.properties");
        Properties props = new Properties();
        props.load(in);
        //Opens properties file and reads from it 
        
        int session = 0;
        int death = 0;
        String byears = props.getProperty("years");
        int iyears = Integer.valueOf(String.valueOf(byears));
        String bnow = props.getProperty("now");    
        int inow = Integer.valueOf(String.valueOf(bnow));        
        String bagew = props.getProperty("agew");
        String bnor = props.getProperty("nor");
        int inor = Integer.valueOf(String.valueOf(bnor));        




        String bager = props.getProperty("ager");
        // Reads from the properties files and sets them as strings
        // b is for begging 
        
        System.out.println("At the start of the simulation:\n There are " + bnow + " wolves, and their average age is " + bagew);
        System.out.println(" There are " + bnor + " rabbits, and their average age is " + bager);
        
            while(session ==0) {
                float cyear = (int)(iyears);
                float cnor = (int)(inor);
                environment.changeYear(1, cyear);
                rabbits.rabbitsAlive(5, cnor);
                return environment.nor;


                
                session = 1;
            }
            
            while(session == 1){
                float cyear = (int)(iyears);
                environment.changeYear(2, cyear);
                session = 2;
            }
                
                
    }
}


class environment {
    String years;
    String now;
    String nor;
    String agew;
    String ager;
    
    public void changeYear(double yeari, double yearc) throws IOException { 
        double ayear = yeari + yearc;
        String cyear = Double.toString(ayear);
        System.out.println(cyear);
    }
}


class rabbits {
    public void rabbitsAlive(double percentage, double lives) throws IOException {
        double deaths = (lives*percentage/ 100);
        double srabbits = lives - deaths;
        environment environment = new environment();    


        environment.nor = Double.toString(srabbits);
        
    }
}
 
Last edited:
Back