On 2014-05-17 12:52, Albert van der Horst wrote:
> Now translate E=mc^2 into Java.

I suspect it would be something like

public class Einstein  {
  private double mass=0, energy=0;
  public class Relativity implements IEquation {
    Relativity(double mass) {
      set_mass(mass);
    }
    public double getEnergy() {return energy;}
    public double setEnergy(double newEnergy) {
      energy = newEnergy;
      mass = newEnergy / (units.SPEED_OF_LIGHT * units.SPEED_OF_LIGHT);
    }
    public double get_mass() {return mass;}
    public double setMass(double newMass) {
      mass = newMass;
      energy = newMass * (units.SPEED_OF_LIGHT * units.SPEED_OF_LIGHT);
    }
  }
  
  public static void main(String[] args) {
    Relativity relativity = new Relativity(
      Integer.parseInt(args[1])
      );
    System.out.println(relativity.getEnergy())
  }
}


(untested, as it has been a long time since I've touched any Java code)

-tkc



Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to