第一问
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了铁岭免费建站欢迎大家使用!
Hello,This is a fruit!
Hello,The is an apple!
因为子类构造方法中
Apple() {
System.out.println("Hello,The is an apple!");
}
在没有特定声明时等价于
Apple() {
super();//这里将执行父类的构造方法
System.out.println("Hello,The is an apple!");
}
第二问题
执行结果是
T0
gender is wrong
T3
T4
try{
System.out.println("T0");// 1这里始终会执行,一进到try就执行
if(!flag1||!flag2)
throw new GenderException();// 2、这里判断得到,当两个都为false时抛出异常,但没有打印信息
System.out.println("T1");
}catch(GenderException e){
System.out.println("gender is wrong");//3,步骤2抛出的异常在这里被截取,所以打印出来
}catch(Exception){
System.out.println("T2");
}finally{
System.out.println("T3");//4,try catch下 finally始终会执行,也打印
}
System.out.println("T4");// 5、try catch 外,继续执行
}
}
模糊C均值聚类算法,可将输入的数据集data聚为指定的cluster_n类
【函数描述】
语法格式
[center, U, obj_fcn] = FCM(data, cluster_n, options)
用法:
1. [center,U,obj_fcn] = FCM(Data,N_cluster,options);
2. [center,U,obj_fcn] = FCM(Data,N_cluster);
输入变量
data ---- n*m矩阵,表示n个样本,每个样本具有m维特征值
cluster_n ---- 标量,表示聚合中心数目,即类别数
options ---- 4*1列向量,其中
options(1): 隶属度矩阵U的指数,1(缺省值: 2.0)
options(2): 最大迭代次数(缺省值: 100)
options(3): 隶属度最小变化量,迭代终止条件(缺省值: 1e-5)
options(4): 每次迭代是否输出信息标志(缺省值: 0)
输出变量
center ---- 聚类中心
U ---- 隶属度矩阵
obj_fcn ---- 目标函数值
以下代码调试通过:
1234567class LuciaClass: # 定义类 def luciaprint(self, text): # 类里面的方法 print('\n', text) # 方法就是输出 text x = LuciaClass() # 方法的实例 xx.luciaprint('today is a bad day ~~~') # 实例调用类方法
运行效果:
Java中如果除运算符“/”,在不加任何限制的情况下,两个整数相除,得到的是整数,小数点后的被舍弃。但是有些场景下我们需要拿到除得的小数,还要指定位数的小数。这时候有以下处理方法:
1.使用DecimalFormat来限定得到的小数位数
int pcm = 98;
int fcm = 11;
DecimalFormat df = new DecimalFormat("0.00");
double tmpVal = Double.parseDouble(df.format((double) pcm/(pcm+fcm)));
//get value 0.89
注意,它默认返回的是String,如果需要double/float要做一下转换。
2.直接使用Decimal运算
@Test
public void testDecimalOper(){
int pcm = 94;
int fcm = 11;
BigDecimal pcmbd = new BigDecimal(pcm);
BigDecimal fcmbd = new BigDecimal(fcm);
BigDecimal rate = new BigDecimal(0.00);
rate = pcmbd.divide(pcmbd.add(fcmbd), 2, RoundingMode.HALF_UP);
System.out.println(rate);//0.90
}
float/double在工程运算中使用的比较多,在商业计算中使用Decimal类型的比较多。(注:
在《Effective Java》这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal,另外,我们如果需要精确计算,要用String来够造BigDecimal。在《Effective Java》一书中的例子是用String来够造BigDecimal的。(注意:divide方法中推荐使用枚举RoundingMode.HALF_UP)
)
两种方式都可以。推荐使用第二种方式来处理精度和round mode的设置。
附BigDecimal rouding mode:
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal } 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
public final static int ROUND_UNNECESSARY = 7;
模糊c均值聚类
函数: fcm
格式: [center,U,obj_fcn] = fcm(data,cluster_n)
举例如下所示:
data = rand(100, 2);
[center,U,obj_fcn] = fcm(data, 2);
plot(data(:,1), data(:,2),'o');
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2, :) == maxU);
line(data(index1,1), data(index1, 2), 'linestyle', 'none', 'marker', '*', 'color', 'g');
line(data(index2,1), data(index2, 2), 'linestyle', 'none', 'marker', '*', 'color', 'r');