0

So for my course programming with java I want to exercise a bit. I made a project about planets. I got 4 planets with each of them contain information. I made a "constructor" class Planet:

public class Planet {

    String name;
    int number;
    String color;
    int width;


    public Planet(String name){
        this.name = name;
    }

    public void Number(int Number){
        number =  Number;
    }

    public void Color(String Color){
        color = Color;
    }

    public void Width(int Width){
        width = Width;
    }

    public void printPlanet(){
        System.out.println("Name:"+ name );
        System.out.println("Number:" + number );
        System.out.println("Color:" + color );
        System.out.println("Width:" + width);
    }
}

and other classes (total = 4) of this type that represent the planets:

public class Earth {

    public static void main(String args[]){
        Planet earth = new Planet("Earth");

        earth.Number(2);
        earth.Color("Blue");
        earth.Width(47000);

    }
}

But now i want to create a simple file with simple code that outputs all the information of all the planets together. I know I can put all the codes from the planets files in one but it's too much and I want a simple file that contains one or two methods/constructors that outputs all the information. Thanks

4
  • You can't "create" information and have less constructors than there are planets to talk about. You can, however, create some static instances of Planet inside of Planet (yes you can do that) and just have all the existing planets saved there. (i.e. static Planet Mercury,static Planet Venus, etc). Then just access those. Commented Nov 3, 2015 at 10:28
  • You can creat a static Arraylist member in the class that the constructor will use to store the each created instance. Then you can have a static method that will itterate the Arraylist and call the printPlanet for each record. Commented Nov 3, 2015 at 10:32
  • Ok I get it but what statement should be under those 'static' instances in order to use them in the the new file? Commented Nov 3, 2015 at 10:33
  • 1
    Cross-posted: programmers.stackexchange.com/questions/301570/… Commented Nov 3, 2015 at 14:13

2 Answers 2

2

Do the changes to the Planet class as suggested

public class Planet {

    private String name;
    private int number;
    private String color;
    private int width;

    public Planet(String name, int number, String color, int width) {
        this.name = name;
        this.number = number;
        this.color = color;
        this.width = width;
    }

    public void Number(int Number) {
        number = Number;
    }

    public void Color(String Color) {
        color = Color;
    }

    public void Width(int Width) {
        width = Width;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Planet [name=" + name + ", number=" + number + ", color="
                + color + ", width=" + width + "]";
    }

}

And for the initialization part

public static void main(String[] args) {
    Planet earth = new Planet("Earth", 2, "Blue", 47000);
    Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
    Planet mars = new Planet("Mars", 5, "Blue", 47000);
    Planet other = new Planet("Other", 1, "Blue", 47000);
    System.out.println(earth);
    System.out.println(pluto);
    System.out.println(mars);
    System.out.println(other);
}
Sign up to request clarification or add additional context in comments.

5 Comments

mmm, pluto is not a planet anymore ;(
Sad:(, but u can choose other one.
Yeah it's quite nice but is there a way i could do it like this: one file(Planet) that contains the variables and the print-method, 5 files(ex. earth) that contain the information about the variables and executes the print-method and one files that ONLY executes the print-method with the information of all the planets?
Well, if you suggest to change the Planet class, then please also fix the names of the methods and their parameters (see naming conventions). And you don't need to call super(). Btw: the mars isn't "blue" :P.
Yes you can have one class that would contain list of planets and other main class which would load this list of planets and print it one by one
1

Well, first you need to initiate the instances of the planets. In your code it goes something like this (with use of the above given and update class):

public class TestPlanets {
    public static void main (String [] args) {

        Planet earth = new Planet("Earth", 2, "Blue", 47000);
        Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
        Planet mars = new Planet("Mars", 5, "Blue", 47000);
        Planet venus = new Planet("Venus", 1, "Blue", 47000);

        // make an list of the planets: (called planets)
        ArrayList<Planet> planets = new ArrayList<Planet>();
        planets.add(earth);
        planets.add(pluto);
        planets.add(mars);
        planets.add(venus);

        // and now magic:
        for (int i = 0; i < planets.size(); i++) {
            System.out.println(planets.get(i).toString());
        }
    }
}

Now I hope you get how this works ;) Java docs and tutorials are awesome!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.