Wednesday, 27 July 2016

Chapter 5 Exercise 5, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

5.5 (Conversion from kilograms to pounds and pounds to kilograms) Write a program that displays the following two tables side by side:

Kilograms Pounds | Pounds Kilograms
1 2.2 | 20 9.09
3 6.6 | 25 11.36
...
197 433.4 | 510 231.82
199 437.8 | 515 234.09

public class ProgrammingEx5_5 {
 public static void main(String[] args) {
  final int END = 199;
  final int POUND_START = 20;
 
  // printing table header
  System.out.printf("%-9s%15s","Kilograms","Pounds");
  System.out.print("\t|\t");
  System.out.printf("%-9s%15s\n","Pounds","Kilograms");
 
  for (int j=0, i = 1; i <= END; i+=2, j+=5) {
   System.out.printf("%-9d%15.1f", i, i * 2.2);
   System.out.print("\t|\t");
   System.out.printf("%-9d%15.1f\n",POUND_START+j,(POUND_START+j)/2.2);
 
  }
 }
}

No comments :

Post a Comment