⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 positiveinteger.java

📁 卡内基梅陇大学的网上教程 ssd3第一单元的源码
💻 JAVA
字号:
package unitOne;

import java.io.*;

/**
 * Wraps a positive integer value.
 *
 * @author  iCarnegie
 * @version 1.0.0
 */
public class PositiveInteger  {

    private static BufferedReader  stdIn =
        new BufferedReader(new  InputStreamReader(System.in));

    private static PrintWriter  stdErr =
        new PrintWriter(System.err, true);

    private static PrintWriter  stdOut =
        new PrintWriter(System.err, true);

    private int value;

    /**
     * Tests method <code>readInteger</code>
     *
     * @param args  not used
     * @throws IOException if error reading from standard input.
     */
    public static void  main(String[] args) throws IOException  {

        PositiveInteger object;

        do  {
            try  {
                stdErr.print("Enter an integer> ");
                stdErr.flush();

                int value = Integer.parseInt(stdIn.readLine());
                object = new  PositiveInteger(value);

                break;

            } catch (NumberFormatException  nfe)  {
                stdErr.println("Invalid number format");
            } catch (OutOfRangeException  ore)  {
                stdErr.println(ore.getMessage());
            }
        }  while (true);

        stdOut.println("The value is: " + object.getValue());
    }

    /**
     * Constructs a <code>PositiveInteger</code> object with the
     * value specified by the argument.
     *
     * @param initialValue  an integer. The value should be positive.
     * @throws OutOfRangeException  if the value is negative.
     */
    private PositiveInteger(int initialValue) throws OutOfRangeException {

        if (initialValue < 0) {

            throw new OutOfRangeException("Number not positive");

        } else {
            this.value = initialValue;
        }
    }

    /**
     * Returns the positive number
     *
     * @return the value
     */
    public int getValue()  {

        return this.value;
    }

    /**
     * Modify the positive number.
     *
     * @param newValue  the new integer. The value should be positive.
     * @throws OutOfRangeException  if the new value is negative.
     */
    private void setValue (int newValue) throws OutOfRangeException {

        if (newValue < 0) {

            throw new OutOfRangeException("Number not positive");

        } else {
            this.value = newValue;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -