fork download
  1. class AddDistance {
  2. int feet;
  3. int inches;
  4.  
  5. AddDistance(int feet, int inches) {
  6. this.feet = feet;
  7. this.inches = inches;
  8. }
  9.  
  10. void addDistance(AddDistance d) {
  11. this.feet += d.feet;
  12. this.inches += d.inches;
  13.  
  14. if (this.inches >= 12) {
  15. this.feet += this.inches / 12;
  16. this.inches %= 12;
  17. }
  18. }
  19.  
  20. void display() {
  21. System.out.println("Total Distance: " + feet + " feet " + inches + " inches");
  22. }
  23.  
  24. public static void main(String[] args) {
  25. AddDistance d1 = new AddDistance(5, 9);
  26. AddDistance d2 = new AddDistance(3, 11);
  27.  
  28. d1.addDistance(d2);
  29. d1.display();
  30. }
  31. }
Success #stdin #stdout 0.16s 57868KB
stdin
Standard input is empty
stdout
Total Distance: 9 feet 8 inches