TransWikia.com

Why are my separate random objects being assigned the same values?

Stack Overflow Asked on November 20, 2021

sorry for the long post. I suspect my issue is fairly easy to solve but (I think) the code I must post for it to make sense is quite long in length, so I apologize. If there was a way for me to shorten this post while still having it make sense that I’m not aware of, I’d appreciate the feedback.

I am working on a Java program that simulates a fictional 4-way intersection with stop signs (First St, which runs north/south and Main St, which runs east/west). I want it to create 4 objects of the Car class, randomize their 3 attributes (1. does it exist? 2. what direction is it traveling? 3. what order did it arrive at the intersection?) using Math.random(), and print them.

I do exactly this – instantiate 4 cars (car1, car2, car3, car4), randomize their attributes using the randomizeAll method, and then print their attributes using the printState method.. I expect it to run like this:

Car 1: This car does not exist.

Car 2: This car exists! It is traveling southbound on 1st St. It arrived first.

Car 3: This car exists! It is traveling westbound on Main St. It arrived second.

Car 4: This car does not exist.

Instead, it runs like some variation of this:

Car 1: This car exists! It is traveling westbound on Main St. It arrived second.

Car 2: This car exists! It is traveling westbound on Main St. It arrived second.

Car 3: This car exists! It is traveling westbound on Main St. It arrived second.

Car 4: This car exists! It is traveling westbound on Main St. It arrived second.

All four of the cars have the same exact attributes, (although they change each time I re-run the program). I can’t figure out what is causing this. There are only 2 things to look at, my main method in the CarDemo class and the Car class itself. My code is as follows:

CarDemo class, which contains the main method:

    public class CarDemo {

      public static void main(String[] args) {
       
        Car car1 = new Car();
        Car car2 = new Car();
        Car car3 = new Car();
        Car car4 = new Car();
        
        car1.randomizeAll();
        car2.randomizeAll();
        car3.randomizeAll();
        car4.randomizeAll();
        
        System.out.println("Car 1:");
        car1.printState();
        
        System.out.println("Car 2:");
        car2.printState();
        
        System.out.println("Car 3:");
        car3.printState();
        
        System.out.println("Car 4:");
        car4.printState();
        
    }
    
}

The Car class, which contains the randomizeAll and printState methods:

public class Car {
    
    private static int existence; 
    private static final int EXISTENCEMAX = 1;
    private static final int EXISTENCEMIN = 0;

    private static int direction;
    private static final int DIRECTIONMAX = 4;
    private static final int DIRECTIONMIN = 1;
    
    private static int order;
    private static final int ORDERMAX = 4;
    private static final int ORDERMIN = 1;

    
    public void randomizeAll() {
        existence = (int)(Math.random() * (EXISTENCEMAX - EXISTENCEMIN + 1) + EXISTENCEMIN);
        direction = (int)(Math.random() * (DIRECTIONMAX - DIRECTIONMIN + 1) + DIRECTIONMIN);
        order = (int)(Math.random() * (ORDERMAX - ORDERMIN + 1) + ORDERMIN);
    
}
        
    public void printState(){

    boolean exist;
    exist = existence == 1;
    if (exist) {
        System.out.println("This car exists!");
        switch(direction){
            case 1:
                System.out.println("It is traveling northbound on 1st St.");
                break;
            case 2:
                System.out.println("It is traveling southbound on 1st St.");
                break;
            case 3:
                System.out.println("It is traveling eastbound on Main St.");
                break;
            case 4:
                System.out.println("It is traveling westbound on Main St.");
                break;
        }
        switch(order){
            case 1:
                System.out.println("It arrived first.");
                break;
            case 2:
                System.out.println("It arrived second.");
                break;
            case 3:
                System.out.println("It has arrived third.");
                break;
            case 4:
                System.out.println("It arrived fourth.");
                break;
        }           
    } else {
        System.out.println("This car does not exist.");
    }  
    
  }    
    
}

Is anyone able to tell me why each car object shares the same values? I’ve been staring at this and researching online for several hours and nothing pops out at me. Any input is greatly appreciated. I apologize if my code is messy or redundant in some areas, I am in my very first semester of programming classes so I’m the epitome of a programming noob. Thanks a lot in advance.

One Answer

My attributes in the "car" class were declared as static, which means they are shared between all instances / objects of a class. Removing the static keyword fixed the issue.

Answered by Vinny on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP