0

I am creating an object of a class from 2 separate classes and both objects are returning different values for the same method. I suspect it may be an issue with the while loop but here are the classes. The main class works, the setup class is the class that is being turned into and object and the game loop class has the object that doesn't return the right values. it returns the values defined at the beginning of setup and not the modified versions.

import java.util.Scanner;

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop();

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}

import java.util.Scanner;

public class setup {
    static Scanner input = new Scanner(System.in);

    String goverment;
    int happyness;
    double money;
    int population = 1000000;


    public setup()
    {
    }

    public void statsSetup()
    {
        System.out.println("Choose a goverment: 1. democracy 2. monarchy 3. dictatorship");
        goverment = input.nextLine();

        if (goverment.equals("1"))
        {
            happyness = 75;
            money = 250000.0;

        }
        else if (goverment.equals("2"))
        {
            happyness = 50;
            money = 500000.0;
        }
        else if (goverment.equals("3"))
        {
            happyness = 25;
            money = 750000.0;
        }
        else
        {
            System.out.println("ENTER A VALID VALUE");
        }
    }
    public int getHappyness()
    {
        return happyness;
    }
    public double getMoney()
    {
        return money;
    }
    public int getPopulation()
    {
        return population;
    }
}

import java.util.Scanner;

public class gameLoop 
{
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop()
    {
    }

    setup setupGov = new setup();

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}
4
  • 2
    James, I am finding your question very unclear. What input are you providing, what result are you expecting, and what result are you actually getting? And what do you mean by "turning a class into an object"? Do you mean instantiating it? Commented Nov 12, 2016 at 22:00
  • The class at the bottom has an empty main method.... Nothing will run. What are you trying to do there? Commented Nov 12, 2016 at 22:01
  • @cricket_007 I believe he's running the class at the top. Commented Nov 12, 2016 at 22:04
  • I believe the main class is from the top, but @James Clarke could you check which class have you got in your run configuration? Commented Nov 12, 2016 at 22:05

2 Answers 2

2

You are creating two different instances of class setup. One is created directly in main function and other is created in gameLoop object. They do not share their attributes so methods may return different value. Every time you use 'new' operator, a new instance of class is created with it's own attributes (only static member are shared since static member belongs to class instead of instances). If you want to have same instances you could write:

 public class gameLoop 
 {
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop(setup setupGov) 
    {
       this.setupGov = setupGov;
    }

    setup setupGov;

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}

And in main:

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop(setupGov);

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}

Now both of objects setupGov will be the same instance.

Please note: It is good practice to have class name written with capitalized first letter eg. GameLoop instead of gameLoop

Sign up to request clarification or add additional context in comments.

Comments

0

I don't really understand what you're trying to do or what the question is, but in your main class you have an object with the same exact name of the class.

gameLoop gameLoop = new gameLoop();

I don't know if that's the exact cause of your problem, but I'm almost sure that that isn't supposed to be like that.

1 Comment

Actually, that's legal, although not the best thing to do. It won't be the cause of his problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.