Han

Han Ferik

Home | Projects | AP CSA

Project Planetary

Tools: VSCode | Source Code: GitHub

The Planetary program is designed to manage planetary information and calculate the travel time between two planets based on their distances from the Sun. The user first inputs details for each planet, including the planet's name, its distance from the Sun, diameter, and the number of moons orbiting it. These details are stored in separate lists to easily track each planet’s attributes. The program then allows the user to calculate travel time between two planets by comparing their distances from the Sun.

The program starts by asking the user how many planets they wish to enter. For each planet, it prompts the user to provide the planet's name, its distance from the Sun, its diameter, and the number of moons. These inputs are stored in four different lists, with each list dedicated to a specific attribute (names, distances, diameters, and number of moons). After collecting this data, the program offers the user the option to calculate travel time between two planets. If the user agrees, they are prompted to enter the index numbers of the two planets they wish to compare. The travel time is calculated using the absolute difference between the distances of the two planets, divided by 50,000 to simulate the time it would take to travel between them.

In addition to calculating travel time, the program provides a feature to display the details of all the planets entered. The PlanetaryInfo method iterates through each planet, printing its name, distance from the Sun, diameter, and number of moons. This allows the user to review the full set of planetary information they’ve entered in an organized manner.

Working on this project involved learning how to collect and store data dynamically using ArrayList. The lists allow for flexible storage of multiple planets and make it easy to retrieve specific information when needed. I also practiced using arithmetic functions like Math.abs() to calculate absolute differences and handling user input through Scanner. A critical point in the project was correctly comparing user input strings with the .equals() method, ensuring the program responds correctly when asked whether to calculate travel time. Structuring the program into distinct methods, such as PlanetaryInfo for displaying information and TravelTime for performing calculations, made the code modular and easier to maintain.

In conclusion, this project helped me improve my skills in managing user input, working with lists, performing calculations, and structuring Java programs. By separating the logic into different methods, I learned how to keep the code more organized and modular, allowing for better readability and functionality.


Planetary Information Simulation

Planetary Information Simulation in Java


		import java.util.Scanner;
		
		public class PlanetaryInfo {
		    private String planetName;
		    private double distanceFromSun; // in million kilometers
		    private double travelTime; // in years
		
		    public void setPlanetInfo(String name, double distance, double time) {
		        this.planetName = name;
		        this.distanceFromSun = distance;
		        this.travelTime = time;
		    }
		
		    public void displayPlanetInfo() {
		        System.out.println("Planet Name: " + planetName);
		        System.out.println("Distance from Sun: " + distanceFromSun + " million km");
		        System.out.println("Travel Time: " + travelTime + " years");
		    }
		
		    public static void main(String[] args) {
		        Scanner scanner = new Scanner(System.in);
		        int numberOfPlanets;
		
		        System.out.print("Enter the number of planets: ");
		        numberOfPlanets = scanner.nextInt();
		
		        PlanetaryInfo[] planets = new PlanetaryInfo[numberOfPlanets];
		
		        for (int i = 0; i < numberOfPlanets; i++) {
		            planets[i] = new PlanetaryInfo();
		            System.out.println("Enter details for Planet " + (i + 1) + ":");
		            System.out.print("Planet Name: ");
		            String name = scanner.next();
		            System.out.print("Distance from Sun (million km): ");
		            double distance = scanner.nextDouble();
		            System.out.print("Travel Time (years): ");
		            double time = scanner.nextDouble();
		
		            planets[i].setPlanetInfo(name, distance, time);
		        }
		
		        System.out.println("Planet Information:");
		        for (PlanetaryInfo planet : planets) {
		            planet.displayPlanetInfo();
		            System.out.println();
		        }
		
		        scanner.close();
		    }
		}
		    

Find me on the interwebs!

Github LinkedIn Instagram Facebook