Create Java Class Object (Nobody)

Asked 2 years ago, Updated 2 years ago, 87 views

I want to make data of several people into objects and store them in the similar way below, but I don't know how to do it. I was just trying to trick you. As expected, there's an error. Please let me know.

```The programming language you use is Java public class Background {

    private String name;
    private long number;
    private String birthday;

    public Background(String name, long number, String birthday) {
        this.name = name;
        this.number = number;
        this.birthday = birthday;
    }

    public static void main(String[] args) {
            String name = ["Steve", "Dave", "Janny"]
            int number = [1, 2, 3]
            String birthday = ["2000", "2005", "2015" ]

            int i = 0;
    Background  array[i] = (Background) new Background(name, number, birthday);
    System.out.println(array[i].name);
    System.out.println(array[i].number);
    System.out.println(array[i].birthday);

}

java entity

2022-09-22 18:38

2 Answers

class BackGround {

String name;
int number;
String birthday;

public BackGround(String name, int i, String birthday) {
    this.name = name;
    this.number = i;
    this.birthday = birthday;    
}// Background constructor

public String toString(){
    return "("+name+","+number+","+birthday+")";
}// ToString method

}// Background Class

public class Ex01{

public static void main(String[] args) {

    BackGround[] arr = { new BackGround("Steve",1,"2000")
                        ,new BackGround("Dave",2,"2005")
                        ,new BackGround("Janny",3,"2015")
                        };

    for(int i=0; i<arr.length; i++){
        System.out.println(arr[i]);
    }//--for statement        
}//-- }//-- main

} // Ex01 Class


2022-09-22 18:38

public class Background {

public static void main(String[] args) {
    String[] name = {"Steve", "Dave", "Janny"};
    int[] number = {1, 2, 3};
    String[] birthday = {"2000", "2005", "2015" };

    String[] a = new String[3]; // name + number + birthday Create an array of length 3 to hold information

    for(int i=0;i<3;i++){
       a[i] = "["+name[i]+","+number[i]+","+birthday[i]+"]";
    }// Include [name, number, birthday] information in arrays a[0] to a[2] 

    for(int i=0;i<3;i++){
    System.out.println(a[i]);
    } // a[0] to a[2] output

}//main end

}//class end


2022-09-22 18:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.