Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, July 30, 2009

Multiplicaton Table using Singleton Pattern

Hi friends,

Today I would like to present you a small JAVA Program to Display a Multiplication Table using Singleton Pattern in between two ranges.

Logic: Accept the table which you want to display, then get the upper and lower bounds to display and display the table.

Code:
package chandra.multiplicationTable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Multiply {
private static Multiply chandraSample = null;
static int flag = 1;
static BufferedReader bufferedReader;

public static Multiply getInstance() throws IOException {
if (chandraSample == null) {
flag = 1;
chandraSample = new Multiply();
flag = 0;
}
else {
System.out.println("Error");
return chandraSample;
}
return chandraSample;
}

public Multiply() throws IOException {
System.out.println("Multiplication Table");
System.out.println("Enter the number for which you want to display");
try {
bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
String st1 = bufferedReader.readLine();
double mulValue = Double.parseDouble(st1);

System.out.println("Enter the first range value:");
String st2 = bufferedReader.readLine();
double firstValue = Double.parseDouble(st2);

System.out.println("Enter the last range value:");
String st3 = bufferedReader.readLine();
double lastValue = Double.parseDouble(st3);
compute(mulValue, firstValue, lastValue);
}

catch (Exception e) {
System.out.println("Enter only numbers");
Multiply.getInstance();
}
}

public static void main(String[] args) throws IOException {
Multiply multiply = Multiply.getInstance();
}

public static int compute(double mvalue, double fvalue, double lvalue)
throws IOException {
double i;
double disp;
if ((fvalue < lvalue) || (fvalue == lvalue)) {
for (i = fvalue; i <= lvalue; i++) {

System.out.print(mvalue + " * " + (i) + " = ");
disp = i * mvalue;

System.out.println(disp);
}
System.out.println("Do u Want to Continue: yes/no");
String yn1 = bufferedReader.readLine();
if (yn1.equalsIgnoreCase("yes") || yn1.equalsIgnoreCase("y")) {
Multiply.getInstance();
}

else if (yn1.equalsIgnoreCase("no") || yn1.equalsIgnoreCase("n")) {
System.exit(0);
}

else {

System.out.println("Totally irrelevant input! System will exit!");
System.exit(0);
}
}

else {
System.out.println("Enter correct data!");
Multiply.getInstance();
}
return 1;
}
}

Output:

Multiplication Table
Enter the number for which you want to display
2
Enter the first range value:
3
Enter the last range value:
12
2.0 * 3.0 = 6.0
2.0 * 4.0 = 8.0
2.0 * 5.0 = 10.0
2.0 * 6.0 = 12.0
2.0 * 7.0 = 14.0
2.0 * 8.0 = 16.0
2.0 * 9.0 = 18.0
2.0 * 10.0 = 20.0
2.0 * 11.0 = 22.0
2.0 * 12.0 = 24.0
Do u Want to Continue: yes/no
yes
Multiplication Table
Enter the number for which you want to display
3
Enter the first range value:
0
Enter the last range value:
10
3.0 * 0.0 = 0.0
3.0 * 1.0 = 3.0
3.0 * 2.0 = 6.0
3.0 * 3.0 = 9.0
3.0 * 4.0 = 12.0
3.0 * 5.0 = 15.0
3.0 * 6.0 = 18.0
3.0 * 7.0 = 21.0
3.0 * 8.0 = 24.0
3.0 * 9.0 = 27.0
3.0 * 10.0 = 30.0
Do u Want to Continue: yes/no
no

Dear friends,
Kindly have a look at the program and report bugs. It will be helpful to readers as well as me. Thanks for your co-operation and support!

1 comment:

Chandrasekar said...

I had a better look at the singleton pattern and I hope to post a shorter code soon. :)