Javaプログラミング思想第四版第三章個人練習
2407 ワード
, ( , ……)
第三章
練習9(1)floatとdouble指数記数法で表す最大と最小の数字をそれぞれ表示する
public class MaxMinFloatDouble {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
float fmax = Float.MAX_VALUE;
float fmin = Float.MIN_VALUE;
double dmax = Double.MAX_VALUE;
double dmin = Double.MIN_VALUE;
System.out.println(fmax);
System.out.println(fmin);
System.out.println(dmax);
System.out.println(dmin);
/*result:
3.4028235E38
1.4E-45
1.7976931348623157E308
4.9E-324*/
}
}
練習(10)は2つの定数値を有するプログラムを作成し、1つは交互のバイナリビット1と0を有し、そのうち最低有効ビットは0であり、もう1つは交互のバイナリビット1と0を有するが、その最低有効ビットは1である(ヒント:16進数定数を用いて最も簡単な方法を表す).2つの値を取り、ビットオペレータで可能な限り結合して演算し、Integerを使用します.toBinaryString()表示
public static void main(String[] args) {
int i1 = 0xaaaaaaaa;
int i2 = 0x55555555;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
/**
* result
* 10101010101010101010101010101010
1010101010101010101010101010101
*/
}
練習14(3)は,2つの文字列パラメータを受信する方法を記述し,この2つの文字列を種々のブール値の比較関係で比較し,結果を印刷する.==と!と同時にequals()でテストします.main()では、このメソッドをいくつかの異なる文字列オブジェクトで呼び出します.
/**
* @param args
*/
public static void main(String[] args) {
String str1="hello";
String str2="word";
String str3=new String("hello");
testStr(str1, str2);
System.out.println("--------------------------");
testStr(str1, str3);
}
public static void testStr(String s1,String s2){
//boolean b1=s1>s2;
//boolean b2=s1<s2;
boolean b3=s1==s2;
System.out.println(s1+"=="+s2+":\t"+b3);
boolean b4=s1.equals(s2);
System.out.println(s1+".equals("+s2+"):\t"+b4);
boolean b5=s1!=s2;
System.out.println(s1+"!="+s2+":\t"+b5);
/**result
*
* hello==word: false
hello.equals(word): false
hello!=word: true
--------------------------
hello==hello: false
hello.equals(hello): true
hello!=hello: true
*/
}