📄 integerrange.java
字号:
/*====================================================================*\IntegerRange.javaInteger range class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage util;//----------------------------------------------------------------------// INTEGER RANGE CLASSpublic class IntegerRange implements Cloneable{////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public IntegerRange( ) { } //------------------------------------------------------------------ public IntegerRange( int lowerLimit, int upperLimit ) { this.lowerLimit = lowerLimit; this.upperLimit = upperLimit; } //------------------------------------------------------------------ public IntegerRange( IntegerRange range ) { lowerLimit = range.lowerLimit; upperLimit = range.upperLimit; } //------------------------------------------------------------------ public IntegerRange( String str ) throws IllegalArgumentException, NumberFormatException { String[] strs = str.split( " *, *", -1 ); if ( strs.length != 2 ) throw new IllegalArgumentException( ); lowerLimit = Integer.parseInt( strs[0] ); upperLimit = Integer.parseInt( strs[1] ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Object clone( ) { Object range = null; try { range = super.clone( ); } catch ( CloneNotSupportedException e ) { e.printStackTrace( ); } return range; } //------------------------------------------------------------------ public boolean equals( Object obj ) { if ( obj instanceof IntegerRange ) { IntegerRange range = (IntegerRange)obj; return ( (lowerLimit == range.lowerLimit) && (upperLimit == range.upperLimit) ); } return false; } //------------------------------------------------------------------ public int hashCode( ) { int sum = lowerLimit + upperLimit; return ( sum * (sum + 1) / 2 + lowerLimit ); } //------------------------------------------------------------------ public String toString( ) { return new String( lowerLimit + ", " + upperLimit ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public int getInterval( ) { return ( upperLimit - lowerLimit + 1 ); } //------------------------------------------------------------------ public int getValue( double fraction ) { return ( lowerLimit + (int)Math.round( (double)(upperLimit - lowerLimit) * fraction ) ); } //------------------------------------------------------------------ public boolean contains( int value ) { return ( (value >= lowerLimit) && (value <= upperLimit) ); } //------------------------------------------------------------------ public int nearestValueWithin( int value ) { return ( Math.min( Math.max( lowerLimit, value ), upperLimit ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// public int lowerLimit; public int upperLimit;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -