SlideShare a Scribd company logo
Easy practice with
currentTimeMillis method and
more examples
Java programming
Nooria Esmaelzade
2016
What do you know about (GMT)??
GMT is World Time and
the basis of every
world time zone
Program1
Write a program that displays current time in GMT in the format
hour:minute:second
To create the program we use the currentTimeMillis method in the System class
public class ShowCurrentTime {
public static void main(String[] args) {
// Obtain the total milliseconds since midnight, Jan 1, 1970
long totalMilliseconds = System.currentTimeMillis();
// Obtain the total seconds since midnight, Jan 1, 1970
long totalSeconds = totalMilliseconds / 1000;
// Compute the current second in the minute in the hour
long currentSecond = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
long currentMinute = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
long currentHour = (int)(totalHours % 24);
// Display results
System.out.println("Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + " GMT");
}
}
Out
put
Current time is 2:13:6 GMT
Program 2
Keeping Two Digits After Decimal Points
public class show2Digit {
public static void main(String[ ]args) {
double purchaseAmount = 3.14;
double tax = purchaseAmount * 10.3;
System.out.println((int)(tax * 100)/100.0);
}
}
Output 32.34
Computing Loan Payments
The program asks the user to enter:
- Loan amount
- Annually interest rate
- Number of years
Then the program should compute:
- The monthly interest rate
- Monthly payment
- The total payment
12
)1(
11 



arsnumberOfYe
erestRatemonthlyInt
erestRatemonthlyIntloanAmount
import java.util.Scanner;
public class ComputeLoan {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter yearly interest rate
System.out.print("Enter yearly interest rate, for example 8.25: ");
double annualInterestRate = input.nextDouble();
// Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
// Enter number of years
System.out.print(
"Enter number of years as an integer, for example 5: ");
int numberOfYears = input.nextInt();
Program 3
Computing Loan Payments
Program 3 continuous
Computing Loan Payments
// Enter loan amount
System.out.print("Enter loan amount, for example 120000.95: ");
double loanAmount = input.nextDouble();
// Calculate payment
double monthlyPayment = loanAmount * monthlyInterestRate / (1
- 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
// Display results
System.out.println("The monthly payment is " +
(int)(monthlyPayment * 100) / 100.0);
System.out.println("The total payment is " +
(int)(totalPayment * 100) / 100.0);
}
}
Enter yearly interest rate, for example 8.25: 7
Enter number of years as an integer, for example 5: 3
Enter loan amount, for example 120000.95: 100000
The monthly payment is 3087.7
The total payment is 111157.54
Out put
Math.pow method
The java.lang.Math.pow(double a, double b) returns the value of the
first argument raised to the power of the second argument

More Related Content

Similar to Easy practice with current timemillis method

C++ project
C++ projectC++ project
C++ project
Sonu S S
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
KimVeeL
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdf
jaipur2
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdf
ankit11134
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
Quratulain Naqvi
 
Project 2Project 2.pdfIntroduction to Programming EECS 1.docx
Project 2Project 2.pdfIntroduction to Programming  EECS 1.docxProject 2Project 2.pdfIntroduction to Programming  EECS 1.docx
Project 2Project 2.pdfIntroduction to Programming EECS 1.docx
wkyra78
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
AASTHA76
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
geeksrik
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
Serhii Kartashov
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
annapurnnatextailes
 
Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
aaseletronics2013
 
Proposed pricing model for cloud computing
Proposed pricing model for cloud computingProposed pricing model for cloud computing
Proposed pricing model for cloud computing
Adeel Javaid
 
C#.net
C#.netC#.net
C#.net
vnboghani
 

Similar to Easy practice with current timemillis method (13)

C++ project
C++ projectC++ project
C++ project
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdf
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdf
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Project 2Project 2.pdfIntroduction to Programming EECS 1.docx
Project 2Project 2.pdfIntroduction to Programming  EECS 1.docxProject 2Project 2.pdfIntroduction to Programming  EECS 1.docx
Project 2Project 2.pdfIntroduction to Programming EECS 1.docx
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 
Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
 
Proposed pricing model for cloud computing
Proposed pricing model for cloud computingProposed pricing model for cloud computing
Proposed pricing model for cloud computing
 
C#.net
C#.netC#.net
C#.net
 

More from Nooria Esmaelzade

Walt Disney Animators
Walt Disney Animators Walt Disney Animators
Walt Disney Animators
Nooria Esmaelzade
 
Communication barriers
Communication barriersCommunication barriers
Communication barriers
Nooria Esmaelzade
 
one smile can be the key to open everyone s heart
one smile can be the key to open everyone s heartone smile can be the key to open everyone s heart
one smile can be the key to open everyone s heart
Nooria Esmaelzade
 
Why we need to control scope
Why we need to control scope Why we need to control scope
Why we need to control scope
Nooria Esmaelzade
 
Easy practice with if statement and boolean type
Easy practice with if statement and boolean typeEasy practice with if statement and boolean type
Easy practice with if statement and boolean type
Nooria Esmaelzade
 
Easy practice with java variables
Easy practice with java variables Easy practice with java variables
Easy practice with java variables
Nooria Esmaelzade
 
Easy java key definition & practice
Easy java key definition & practiceEasy java key definition & practice
Easy java key definition & practice
Nooria Esmaelzade
 
Easy java installation & practice
Easy java installation & practiceEasy java installation & practice
Easy java installation & practice
Nooria Esmaelzade
 

More from Nooria Esmaelzade (8)

Walt Disney Animators
Walt Disney Animators Walt Disney Animators
Walt Disney Animators
 
Communication barriers
Communication barriersCommunication barriers
Communication barriers
 
one smile can be the key to open everyone s heart
one smile can be the key to open everyone s heartone smile can be the key to open everyone s heart
one smile can be the key to open everyone s heart
 
Why we need to control scope
Why we need to control scope Why we need to control scope
Why we need to control scope
 
Easy practice with if statement and boolean type
Easy practice with if statement and boolean typeEasy practice with if statement and boolean type
Easy practice with if statement and boolean type
 
Easy practice with java variables
Easy practice with java variables Easy practice with java variables
Easy practice with java variables
 
Easy java key definition & practice
Easy java key definition & practiceEasy java key definition & practice
Easy java key definition & practice
 
Easy java installation & practice
Easy java installation & practiceEasy java installation & practice
Easy java installation & practice
 

Easy practice with current timemillis method

  • 1. Easy practice with currentTimeMillis method and more examples Java programming Nooria Esmaelzade 2016
  • 2. What do you know about (GMT)?? GMT is World Time and the basis of every world time zone
  • 3. Program1 Write a program that displays current time in GMT in the format hour:minute:second To create the program we use the currentTimeMillis method in the System class
  • 4. public class ShowCurrentTime { public static void main(String[] args) { // Obtain the total milliseconds since midnight, Jan 1, 1970 long totalMilliseconds = System.currentTimeMillis(); // Obtain the total seconds since midnight, Jan 1, 1970 long totalSeconds = totalMilliseconds / 1000; // Compute the current second in the minute in the hour long currentSecond = (int)(totalSeconds % 60); // Obtain the total minutes long totalMinutes = totalSeconds / 60; // Compute the current minute in the hour long currentMinute = (int)(totalMinutes % 60); // Obtain the total hours long totalHours = totalMinutes / 60; // Compute the current hour long currentHour = (int)(totalHours % 24); // Display results System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT"); } } Out put Current time is 2:13:6 GMT
  • 5. Program 2 Keeping Two Digits After Decimal Points public class show2Digit { public static void main(String[ ]args) { double purchaseAmount = 3.14; double tax = purchaseAmount * 10.3; System.out.println((int)(tax * 100)/100.0); } } Output 32.34
  • 6. Computing Loan Payments The program asks the user to enter: - Loan amount - Annually interest rate - Number of years Then the program should compute: - The monthly interest rate - Monthly payment - The total payment 12 )1( 11     arsnumberOfYe erestRatemonthlyInt erestRatemonthlyIntloanAmount
  • 7. import java.util.Scanner; public class ComputeLoan { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Enter yearly interest rate System.out.print("Enter yearly interest rate, for example 8.25: "); double annualInterestRate = input.nextDouble(); // Obtain monthly interest rate double monthlyInterestRate = annualInterestRate / 1200; // Enter number of years System.out.print( "Enter number of years as an integer, for example 5: "); int numberOfYears = input.nextInt(); Program 3 Computing Loan Payments
  • 8. Program 3 continuous Computing Loan Payments // Enter loan amount System.out.print("Enter loan amount, for example 120000.95: "); double loanAmount = input.nextDouble(); // Calculate payment double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); double totalPayment = monthlyPayment * numberOfYears * 12; // Display results System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) / 100.0); System.out.println("The total payment is " + (int)(totalPayment * 100) / 100.0); } } Enter yearly interest rate, for example 8.25: 7 Enter number of years as an integer, for example 5: 3 Enter loan amount, for example 120000.95: 100000 The monthly payment is 3087.7 The total payment is 111157.54 Out put
  • 9. Math.pow method The java.lang.Math.pow(double a, double b) returns the value of the first argument raised to the power of the second argument