Other Planets
<< State Applet | OtherProjectsTrailIndex | Random Number Generator >>
AgeApplet.java
import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.Date; import java.util.GregorianCalendar; /** * Class AgeApplet - Finds age in days * and converts age to age on different planets * * @author Chris Thiel * @version 1.0 */ public class AgeApplet extends Applet { static final int FIRST_YEAR=1930; static final int LAST_YEAR=2010; private GregorianCalendar now, bd; private Label today,daysAge; private Choice month, day, year; private PlanetChoice planet; private Label age; public void init() { now=new GregorianCalendar(); now.setTime(new Date()); today=new Label("Today is "+showDate(now)); Panel p=new Panel(new GridLayout(0,1)); p.add(today); p.add(new Label("Enter your birth date:")); bd=new GregorianCalendar(); bd.setTime(new Date()); daysAge=new Label("You are "+daysBetween(now.getTime(),bd.getTime())+" days old"); month=new Choice(); month.addItem("Jan"); month.addItem("Feb"); month.addItem("Mar"); month.addItem("Apr"); month.addItem("May"); month.addItem("Jun"); month.addItem("Jul"); month.addItem("Aug"); month.addItem("Sep"); month.addItem("Oct"); month.addItem("Nov"); month.addItem("Dec"); month.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { repaint(); } }); Panel d = new Panel(new GridLayout(1,0)); d.add(month); day=new Choice(); for (int i=1;i<32;i++) day.addItem(" "+i); day.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { repaint(); } }); d.add(day); year = new Choice(); for (int i=FIRST_YEAR;i<=LAST_YEAR;i++) year.addItem(" "+i); year.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { repaint(); } }); d.add(year); p.add(d); year.select(LAST_YEAR-FIRST_YEAR); p.add(daysAge); planet = new PlanetChoice(); planet.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { repaint(); } }); double x=daysBetween(now.getTime(),bd.getTime()); age=new Label("that's "+planet.getAge(x)+" years on "); p.add(age); p.add(planet); add(p); } public void paint(Graphics g) { int m=month.getSelectedIndex(); int d=day.getSelectedIndex()+1; int y=year.getSelectedIndex()+FIRST_YEAR; bd=new GregorianCalendar(y,m,d); double x=daysBetween(now.getTime(),bd.getTime()); daysAge.setText("You are "+x+" days old"); age.setText("that's "+planet.getAge(x)+" years on "); } public static String showDate(GregorianCalendar d) { return (d.get(d.MONTH)+1)+"/"+d.get(d.DAY_OF_MONTH)+"/"+d.get(d.YEAR); } private static long daysBetween(Date time1, Date time2) { final long ONE_HOUR = 60 * 60 * 1000L; return (time1.getTime() - time2.getTime() + ONE_HOUR) / (ONE_HOUR * 24); } }
PlanetChoice.java
import java.awt.Choice; import java.util.ArrayList; public class PlanetChoice extends Choice { private ArrayList<Double> values; public PlanetChoice() { super(); values = new ArrayList<Double>(); addItem("Mercury",87.97); addItem("Venus",224.7); addItem("Earth",365.24); addItem("Mars",686.93); addItem("Ceres",1680.5); //4.6 years addItem("Jupiter",4330.6); addItem("Saturn",10755.7); addItem("Uranus",30687.2); addItem("Neptune",60190.0); addItem("Pluto",90553.0); addItem("Varuna",103440.6); addItem("Haumea",103468.0);//283.28 years addItem("Quaoar",105196.3); addItem("Makemake",113183.0);//309.88 years addItem("Eris(Xena)",203600.0);//557 years addItem("Sedna",4404480.0); } public void addItem(String name, double daysPerYear) { super.addItem(name); values.add(daysPerYear); } public double getAge(double days) { return days/values.get(this.getSelectedIndex()); } }
Now lets change it up
See if you can adapt what is going on Planet Choice
to have weights, rather than the time (in days) of a year, and also alter AgeApplet
so it shows your weight on a selected planet.
Object | Weight of 1 lb | No. of Earth days in 1 orbit of sun |
---|---|---|
Mercury | 0.38 | 87.97 |
Venus | 0.9 | 224.7 |
Earth | 1 | 365.24 |
Moon | 0.17 | 365.24 |
Mars | 0.38 | 686.93 |
Jupiter | 2.4 | 4330.6 |
Saturn | 0.93 | 10755.7 |
Uranus | 0.79 | 30687.2 |
Neptune | 1.12 | 60190.0 |
Pluto | 0.04 | 90553.0 |