Java basics Part 2 – DecimalFormat and Conditional sentences

This article is based on DecimalFormat and Conditional sentences when using Java-language as a programing language. Conditional sentences are great way to make your programing to next level. This is still basics.

Once again, time for little Theory

First let’s talk about DecimalFormat, what it is and what it does? Well with this specific word you are able to edit the way the actual number are printed for user. This can be very handy especially when dealing with decimal and big numbers. Usually it is used to show only enough numbers after comma-mark (numbers that come after “,”-mark) .
To use DecimalFormat you type: DecimalFormat a = new DecimalFormat(“0.00”); . If you type this, you must be specific that you refer to right format, example below. 

double hnumber = 17.56031265656656544812385874
DecimalFormat a = new DecimalFormat("0.00");

System.out.print("The answer is " + a.format(hnumber) );

The outprint is: 17,56 . If you would like to give some kind of else outcome, you need to work with numbers in the new format. Like to give four digits after the comma you could say (“0.0000”) . Then if you would like to give some space use comma-mark to get it for you like (“#,0.0000”) . The outcome of this is 1 7,5603 .

Then to the second topic: Conditional sentences. These are also known as a recursion/repeat sentences. Why to use them? Well there are some cases where you need to ask something again from user or you need user to stop the process. There are three ways to great repeats, or should I prefer word “loops” and they are

  • while
  • do …. while
  • for

While repeats the sentence until the certain condition is complete, like certain ending digit or you can only give three different numbers. Do … While repeats the given sentences until the while term is complete (What is the cause is up to coder) . Last but not least there is For and this is the type you could use in banking business a lot. You give all the essential information before it and the you make a sentence where you typically give starting value to variable i and always when the term is concluded the raises by one number. You also set boundary for it so the i wont go off the charts.

These are a little pain to complete master even thought they are not hard to understand but to get the loop work correctly, that is the catch. For example try to make program that tells and shows you how much you have to pay for loan that is 400 € and you are going to pay it back in four months. Each time you shorten the loan, bank charges 7€ fee for using the public service because the you shorten the loan at bank, not in Web Bank where it is free. Then that program also shows you the number of the shortens you have made, how much you shorten and how much you still have to pay. The fee obviously isn’t shown in the last print.

I created some while ago solution to this kind of programing problem and below is the solution for that problem.

Some examples

Here is couple example for conditional/repeat/loop sentences. I also type to the comment part what does this program do.

package basic;

import java.text.DecimalFormat;
import java.util.Scanner;

 /*
 * This program is use to calculate the amount of postage fee when you
 * order heavy stuff and they are waiting in local post office. Note that the
 * end of loop isn't so professional, but it works out as well.
 *
 */

public class PostageFee {

 public static void main (String[] args) {

 Scanner input = new Scanner(System.in);
 double weight = 0;
 double total = 0;
 final double ACLASS = 8.10;
 final double BCLASS = 14.00;
 final double CCLASS = 19.00;

 //The Loop
 while( weight != -1 ) {
 System.out.print("Give the weight of the product (-1 ends process): ");
 weight = input.nextDouble();
 input.nextLine();

 total = total + weight; //means each time new weight given add it to the total weight

 }
 //End of loop
 total = total + 1; // +1 because the -1 end's the loop

 DecimalFormat f = new DecimalFormat("0.00");

 System.out.println("Weight is " + f.format(total) + " kg.");

 if (total < 20.00) {
 System.out.println("Postage fee is " + f.format(ACLASS) + " €.");
 } else if (total >= 20.00 && total < 100.00){
 System.out.println("Postage fee is " + f.format(BCLASS) + " €.");
 } else {
 System.out.println("Postage fee is " + f.format(CCLASS) + " €.");
 }

 input.close();
 }

}

And here is the LoanShorter example.

package basic;

import java.text.DecimalFormat;
import java.util.Scanner;

 /*
 * As I told this loan calculation in my wordpress blog, here is a solution
 * for it. It took more than try and err to get it work the way I talked and
 * some support was given.
 * 
 *
 */

public class LoanShorter {

 public static void main(String[] args) {

 Scanner input= new Scanner(System.in);

 System.out.print("Give the amount of loan: ");
 double loan = input.nextDouble();
 input.nextLine();
 System.out.print("Give the amount of months you are going to need pay it back: ");
 double month = input.nextDouble();
 input.nextLine();
 double summ;
 double shortening;
 double loanleft; 

 summ = month * 7 + loan;
 shortening = summ / month;
 loanleft = loan / month;

 DecimalFormat f = new DecimalFormat("0.00");

 for (int i = 1; i < month + 1; i++) {

 System.out.println( i + ". shortening " + f.format(shortening) + " €, loan left " + f.format((loan - (loanleft * i))) + " €" );

 }

 input.close();
 }

}

– Tuomas Törmä

Sources

  •  Studies in Haaga-Helia Autumn 2013 course Introduction to Programing Teacher Lahtinen Seija
  • Try and err

Leave a comment