javainheritanceabstract-classdisplay

How can I display the output of the parameters I have set from a child class?


I need help accessing the variables I have inputted using the child class. I made an object for the child class in my main class, however, I do not know how will I have access to the inputs I have entered and display them at the end of the code.

public abstract class player {
    public abstract String name(String name);
    public abstract void race(int race);
    public abstract int[] getStatArray();
}

import java.util.Scanner;
public class childplayer extends player {
    Scanner sc = new Scanner(System.in);
    
    public String name(String name) {
        System.out.print("Enter your name: ");
        name = sc.nextLine();
        return name;
    }
    
    public void race(int race) {
        System.out.println("Race options: ");
        System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
        System.out.print("Enter character's race: ");
        race = sc.nextInt();
        if((race>5)||(race<1)) {
            while ((race>5)||(race<1)) {
                System.out.print("Enter character's race: ");
                race = sc.nextInt();
            }
        }System.out.println(" ");
    }
    
    public int[] getStatArray(){
        int [] stat = new int [3];
        int x = 0, y = 0, pts = 0;
        System.out.println("Enter character's stats.");
        while(y<3) {
            System.out.print("Enter value: ");
            x = sc.nextInt();
            y++;
            pts = pts + x;
        }
        int i = 0;
        if(pts>10) {
            System.out.println("Invalid input. Try again.");
            while(i<3) {
                System.out.print("Enter value: ");
                x = sc.nextInt();
                i++;
                pts = pts + x;
            }
        }else {
            stat[i] = x;
            i++;
        }
        
        return stat;
    }

}

Solution

  • If you want to keep the values to use later then you need to store them. The best way to do this is simply with a class variable like so:

    public class Childplayer extends Player {
    
        //Create class variables that store the values
        String name = "";
        int race = -1;
        int[] stat = new int [3];
    

    Then we just modify your methods to use these variables, for example:

    public String name(String name) {
    
        //Process the name
        if (someCondition) {
            name = name +" Smith"
        }
    
        //Saved the passed in variable to the class variable `this.name`
        this.name = name;
    
        return this.name;
    }
    

    And another example:

    public void race(int race) {
        //Saved the passed in variable to the class variable `this.race`
        this.race = race:
    }
    

    Then to get the information later we simply use:

    // Earlier in the code
    Childplayer playerA = new Childplayer();
    
    // Some code here
    //...
    
    // Later in the code we can get the values
    String name = playerA.name;
    int storedRaceFromEarlier = playerA.race;
    

    I strongly recommend making use of a constructor method to populate the class data. I have simplified the code and value checking for the sake of this example:

    //Note that Java naming conventions require that classes should always have a capital letter at the start, I have fixed this in my example
    public abstract class Player {
        //Example getter abstract methods that must be implemented
        public abstract String getName();
        public abstract int getRace();
        public abstract int[] getStatArray();
    }
    
    // Note that Java naming conventions require that classes should always have a capital letter at the start, I have fixed this in my example
    public class Childplayer extends Player {
        Scanner sc = new Scanner(System.in);
    
        //Create class variables that store the values
        String name = "";
        int race = -1;
        int[] stat = new int [3];
    
        // Constructor method with exactly the same name as the class "Childplayer"
        // This method should be responsible for creating the object and populating data
        Childplayer() {
            //Set name
            System.out.print("Enter your name: ");
            name(sc.nextLine());
            System.out.print("Name set as " + name + "\r\n");
            
            // Set race
            System.out.println("Race options: ");
            System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
            int result = -1;
    
            while ((result > 5) || (result < 1)) {
                System.out.print("Enter character's race: ");
                result = sc.nextInt();
            }
    
            // Set the race with the abstract method
            race(result);
            System.out.print("Race set as " + race + "\r\n");
            
            System.out.println("Enter character's stats.");
            int i = 0;
    
            while (i < 3) {
                System.out.print("Enter stat value: ");
                //Save the stat to the class variable
                stat[i] = sc.nextInt();
                i++;
            }
        }
    
        // Abstract methods implemented to return the correct values
        public String getName() {
            return name;
        }
    
        public int getRace() {
            return race;
        }
    
        public int[] getStatArray() {
            return stat;
        }
    }