public class Planet {
	private String planetName;
	private int travelDay;

	public Planet(){
		planetName = "Earth";
		travelDay = 365;
	}
	
	public Planet(String planetName, int travelDay){
		this.planetName = planetName;
		this.travelDay = travelDay;
	}
	
	public void setPlanetName(String planetName){
		this.planetName = planetName;
	}
	public String getPlanetName(){
		return planetName;
	}
	
	public void setTravelDay(int travelDay){
		this.travelDay = travelDay;
	}
	public int getTravelDay(){
		return travelDay;
	}
	
    public String printPlanet(){
    	return "\nPlanet Name: " + getPlanetName() + "\nTravel Day: " + getTravelDay();
    }
    
    public int calculateAge(int age){
    	return (age * 365) / travelDay;
    }
}

class testPlanet2{
	public static void main(String[] args){
		
		Planet[] arrayPlanet = new Planet[4];
		
		Scanner scan = new Scanner(System.in);
		
		System.out.print("Enter Yuor Age on Earth: ");
		int age = scan.nextInt();
		
		System.out.print("---------------------");
		for(int i = 0; i < arrayPlanet.length; i++){
			if(i == 0){
				Planet p2 = new Planet("Venus", 255);
				arrayPlanet[i] = p2.calculateAge(age);
				System.out.println(p2.printPlanet());
				System.out.println("Age on " + p2.getPlanetName() + " :" + arrayPlanet[i]);
			}
			if(i == 1){
				Planet p3 = new Planet("Mercury", 88);
				System.out.println(p3.printPlanet());
				System.out.println("Age on " + p3.getPlanetName() + " :" + p3.calculateAge(age));
			}
			if(i == 2){
				Planet p4 = new Planet("Jupiter", 4380);
				System.out.println(p4.printPlanet());
				System.out.println("Age on " + p4.getPlanetName() + " :" + p4.calculateAge(age));
			}
			if(i == 3){
				Planet p5 = new Planet("Saturn", 10767);
				System.out.println(p5.printPlanet());
				System.out.println("Age on " + p5.getPlanetName() + " :" + p5.calculateAge(age));
			}
		}
	}
}

in the testPlanet2 class, if i declare an array for Planet class and assign the value with the planet age and also use for loop to do it as long as display too. but i get an error while i assigning the age value into my array. help dude. :)
and i was just tried on the first looping. :)

Planet[] arrayPlanet = new Planet[4];

creates an array with slots for four Planets, but all those slots are originally empty (null). You need to initialise them with actual instances of Planet, eg

arrayPlanet[0] = new Planet("Venus", 255);
arrayPlanet[1] = new Planet("Mercury", ... etc

Now you can get rid of your p2, p3 etc variables and use the array instead, and get rid of all the if tests inside the loop. eg

System.out.println(arrayPlanet[i].calculateAge(age));

awh, i got the solution already. thanks for helping dude. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.