IBM Interview Question
Software Engineer / DevelopersTeam: ISL
Country: India
Interview Type: In-Person
A Planet Class can have properties of Name,Radius, Distance from the SUN in AU. The behaviour can be getters.
The Sun class be defined as a Singleton.
The SolarSystem has composition of Planets and the Sun.
Modeling a solar system in Java, besides being fun, is a good exercise to illustrate and practice thinking in terms of some basic concepts in Object Oriented Design (OOD), especially inheritance and polymorphism.
public abstract class GravityObject {
double xPosition;
double yPosition;
double degreeInOrbit;
double distanceFromParent;
GravityObject() {
this.distance = 0;
}
GravityObject(double distance) {
this.distance = distance;
}
}
import java.util.ArrayList;
public abstract class OrbitalSystem extends GravityObject {
private ArrayList children = new ArrayList(); // Objects within the system. They will orbit the parent.
public void add(GravityObject child) { children.add(child); }
public void tick() {
for (int x = 0; x < children.size(); x++) {
GravityObject current = children.get(x);
current.degree += 1
current.xPosition = this.xPosition + Math.cos(degree/180 * Math.PI)* current.distance;
current.yPosition = this.yPosition - Math.sin(degree/180 * Math.PI) * current.distance;
}
}
}
public class Star extends OrbitalSystem {
};
public class Planet extends GravityObject {
};
public static int main(String[] args) {
Star s = new Star(); // Create a new star.
s.add(new Planet(20)); // Add a planet to the star's orbital system which orbits at a distance of 20 units.
s.add(new Planet(66)); // Add another planet to the star's orbital system which orbits at a distance of 66 units.
while (true) {
s.tick();
}
}
Can refer the below link for details:
ehow.com/how_5579338_build-own-solar-system-java.html
Take solar panels that generate electricity enough for the requirement.
Place them such that they are at perpendicular to east-west line. In case of large number of panels they could be placed in a circle with a tower in the center which reflects light on the panels. this ensures that no matter which direction the sunlight comes from we get energy.
Connect rest of the circuit to a battery or the device. Add switches, transformers, cables and wah la!!
The Solar System includes the star, planets, natural satellites and artificial satellites. Use the Composite design pattern.
- Anonymous October 02, 2012Our galaxy (Milky Way) object that consists of other objects such as our solar system which further contains objects such as the Earth and the Sun. An easy way to describe the Earth to an alien residing in some other galaxy would be:
Milky way - Solar System - Earth
Thus, the Milky Way is a parent object to the Solar System which is itself a parent to the Earth and other planets.