The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.7.
Question: (Find the number of years) Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.7 - Find The Number Of Years<br /> *<br /> */ <br /><br />import javax.swing.JOptionPane;<br /><br />public class FindNumberOfYears {<br /> public static void main (String [] args) {<br /> System.out.println("This Program Displays The Number Of Years And Days "<br /> + "In The Value Of Minutes Entered.n");<br /> String number = JOptionPane.showInputDialog(null, "Enter the number of minutes:", <br /> "Input Dialog", JOptionPane.QUESTION_MESSAGE);<br /> int minutes = Integer.parseInt(number);<br /> int hours = minutes / 60;<br /> int days = hours / 24;<br /> int years = days / 365;<br /> int remainingDays = days % 365;<br /> <br /> String message = minutes + " minutes is approximately " + years + <br /> " years and " + remainingDays + " days.n";<br /><br /> JOptionPane.showMessageDialog(null, message, "Answer", JOptionPane.INFORMATION_MESSAGE);<br /> }<br />}<br /> |
The above program was done using Java’s GUI. The same code without using GUI is written below:
1 2 |
import java.util.Scanner;<br /><br />public class FindNumberOfYears {<br /> public static void main (String [] args) {<br /> Scanner input = new Scanner(System.in);<br /> System.out.println("This Program Displays The Number Of Years And Days "<br /> + "In The Value Of Minutes Entered.n");<br /> System.out.println("Enter the number of minutes: );<br /> int minutes = input.nextInt();<br /> int hours = minutes / 60;<br /> int days = hours / 24;<br /> int years = days / 365;<br /> int remainingDays = days % 365;<br /> <br /> System.out.printf (minutes + " minutes is approximately " + years + <br /> " years and " + remainingDays + " days.n");<br /> }<br />}<br /> |
Click here to see other solutions to Introduction to Java Programming.