Monday, 30 October 2017

Introduction to Java program



Problem 1.12
                   Assume a runner runs 24 miles in 1 hour, 40 minutes,
and 35 seconds. Write a program that displays the average speed in kilometers per
hour.

Description
 Note that 1 mile is 1.6 kilometers.

Solution
              public class Message {

    public static void main(String[] args) {
         
        System.out.println("Total KM/h = "+(24*1.6));
        } }

Output:



Introduction to Java program



Problem 1.11
                     The U.S. Census Bureau projects population based on the
following assumptions:
One birth every 7 seconds
One death every 13 seconds
One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume the
current population is 312,032,486 and one year has 365 days.

Description
 In Java, if two integers perform division, the result is an integer. The fractional part is truncated. For   example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with
the fractional part, one of the values involved in the division must be a number with a
decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5

Solution:
                  public class Message {

    public static void main(String[] args) {
         
        System.out.println("Total born = "+(5*365*24*60*60)/7);
        System.out.println("Total died = "+(5*365*24*60*60)/13);
        System.out.println("Total immigrant  = "+(5*365*24*60*60)/45);
         
    }
   
}

Output:
                     

                        

Introduction to Java program



Problem 1.10
Assume a runner runs 14 kilometers in 45 minutes and 30seconds. Write a program that displays the average speed in miles per hour. (Note that 1 mile is 1.6 kilometers.)
       
Descripition:
                        By using formula   S=V * T
Solution:
          public class Message {

    public static void main(String[] args) {
          float inKM=14f,mile;
          mile=inKM/1.6f;
         
          System.out.println("  "+(45.3f/60)*mile);
    }
   
}

Output
               
                        


Introduction to Java program


Problem 1.9:
                   Write a program that displays the area and perimeter of a rectangle with the width of 4.5 and height of 7.9 using the following
Description
formula:
area = width * height
Solution:
          public class Message {

    public static void main(String[] args) {
          float heigth=7.9f,width=4.5f;
          System.out.println("The area of rectangle is  "+(heigth*width));
    } }


Output: