«« ( Date ) »» // «« ( Thread ) »» // si3ts - 2011

SI3TS DZ2 - Marko Vukobrat, 2007/0478

by IvanG
sreda, 05. januar 2011 - 19:26.

http://www.javadb.com/how-to-find-and-calculate-prime-numbers
<http://www.javadb.com/how-to-find-and-calculate-prime-numbers>
/**
*
* @author www.javadb.com
*/
import java.util.Scanner;

public class Main {

private final int UPPER_LIMIT = 10000;

public void calculatePrimeNumbers() {

int i = 0;
int primeNumberCounter = 0;
Scanner in = new Scanner(System.in);

// Reads a integer from the console
// and stores into age variable
int limit=in.nextInt();
in.close();

while (++i <= limit) {

int i1 = (int) Math.ceil(Math.sqrt(i));

boolean isPrimeNumber = false;

while (i1 > 1) {

if ((i != i1) && (i % i1 == 0)) {
isPrimeNumber = false;
break;
} else if (!isPrimeNumber) {
isPrimeNumber = true;
}

--i1;
}

if (isPrimeNumber) {
System.out.println(i);
++primeNumberCounter;
}
}

System.out.println("Nr of prime numbers found: " +
primeNumberCounter);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().calculatePrimeNumbers();
}
}

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

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

/**
 *
 * @author www.javadb.com
 */
public class Main {

    public void start() throws IOException {

        boolean inputOk = false;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        //1 ounce = 0.0283495231 kilograms
        //1 pound = 0.45359237 kilograms

        double pound = 0;
        while (!inputOk) {
            System.out.println("Enter number in pound:");
            try {
                pound = Double.parseDouble(reader.readLine().trim());
                inputOk = true;
                if (pound < 0 || pound > Double.MAX_VALUE)
                {
                	
                	System.out.println("Number is not valid, try again.");
                	inputOk = false;
                }
                
            } catch (NumberFormatException e) {
                System.out.println("Invalid number, try again.");
            }
        }
        System.out.println(pound + " pound is equal to " + getPoundToKg(pound) + " kgs & " + getPoundToGrams(pound) + " grams");

        inputOk = false;
        double ounce = 0;
        while (!inputOk) {
            System.out.println("Enter number in ounce:");
            try {
                ounce = Double.parseDouble(reader.readLine().trim());
                inputOk = true;
                if (ounce < 0 || ounce > Double.MAX_VALUE)
                {
                	
                	System.out.println("Number is not valid, try again.");
                	inputOk = false;
                }
                
            } catch (NumberFormatException e) {
                System.out.println("Invalid number, try again.");
            }
        }
        System.out.println(ounce + " ounce is equal to " + getOunceToKg(ounce) + " kgs & " + getOunceToGrams(ounce) + " grams");
    }
    
    private int getPoundToKg(double pound) {
      
        double kg = pound * 0.45359237;
        return (int)Math.floor(kg);
    }

    private double getPoundToGrams(double pound) {
      
        double kg = pound * 0.45359237;
        return (kg - getPoundToKg(pound)) * 1000;
    }

    private int getOunceToKg(double ounce) {
      
        double kg = ounce * 0.0283495231;
        return (int)Math.floor(kg);
    }

    private double getOunceToGrams(double ounce) {
      
        double kg = ounce * 0.0283495231;
        return (kg - getOunceToKg(ounce)) * 1000;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {

            new Main().start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}