Search code examples
javavariablesmethodsprogram-entry-point

How to use method variables in main?


For my school I need to create a method which moves a bug in any direction. I have the following code:

package Test;

//imports
import java.util.Scanner;
import java.util.Random;

public class test {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug
        int loop = 1;
        int i = 0;
        do {
            BugObj[i] = new ABug();  //creating instance
            System.out.println("Please enter the name of the bug:");   
            BugObj[i].name = reader.next(); 
            System.out.println("Please enter the species of the bug:");   
            BugObj[i].species = reader.next(); 
            System.out.println("Please enter the horizontal position of the bug:");   
            BugObj[i].horpos = reader.nextInt();
            System.out.println("Please enter the vertical postion of the bug:");   
            BugObj[i].vertpos = reader.nextInt();   

            System.out.println("_______________ Bug " +(+i+1) + " _______________\n" );
            System.out.println("Name: " + BugObj[i].name);           //Printing bug information out
            System.out.println("Species: " + BugObj[i].species);
            System.out.println("Horizontal Position: " + BugObj[i].horpos);
            System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n");
            move();

            i++;
            System.out.println("Would you like to enter another bug? \n 0-No,  1-Yes\n");
            loop = reader.nextInt();
        } while(loop == 1);
    }

    public static void move() {
        Scanner reader = new Scanner(System.in);
        System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
        if (reader.nextInt() == 0) {
            System.exit(0);
        }
        int r = (int) (Math.random() * (2- -2)) + -2;
        System.out.println(r);
    }
}

class ABug {                 //ABug class
    int horpos, vertpos, energy, id;
    char symbol;
    String species, name;
}

Basically all I need to do is use the values of the bugs position with the random number generated in the method. I am really new to java and am unsure how to do it or even if its possible.


Solution

  • Since objects are passed by reference in java, you can just pass your ABug object to the move function and change the horpos, vertpos attributes. so

    move(BugObj[i]);
    

    and

    public static void move(ABug bug){
            Scanner reader = new Scanner(System.in);
            System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
            if (reader.nextInt() == 0)
            {
                System.exit(0);
            }
    
            int r = (int) (Math.random() * (2- -2)) + -2;
    
            int originalHorpos = bug.horpos
    
            int originalVertpos = bug.vertpos
    
            // Now just change the attributes however you see fit. i am just adding r
            bug.horpos = originalHorpos + r;
    
            bug.vertpos = originalVertpos + r
    
            /*by the way, we dont need to use variables for the original values. something like this would also work 
    
               bug.horpos += r; 
               bug.vertpos += r;
    
     i just want to explain that in java when you pass objects, they are passed by reference and hence you have access to all of its members.
    
    */
            System.out.println(r);
    
    
        }
    

    also, you dont need to declare the Scanner object again inside the move function. you can pass that to the move function as well and then read as many times as you like.