Report3
講義資料を参考に、その範囲のJavaの基礎的な文法について教科書と対応させながら解説し、考察せよ。
Report3_1
public class report3_1 {
public static void main(String[] args) {
System.out.println(" char型の出力");
charver();
System.out.println(" boolean型の出力");
booleanver();
System.out.println(" byte型の出力");
bytever();
System.out.println(" short型の出力");
shortver();
System.out.println(" int型の出力");
intver();
System.out.println(" long型の出力");
longver();
System.out.println(" float型の出力");
floatver();
System.out.println(" double型の出力");
doublever();
}
/*charの出力*/
public static void charver(){
char c = 'A';
System.out.println("c = " + c);
c = 'あ';
System.out.println("c = " + c);
c = '唖';
System.out.println("c = " + c);
}
/*booleanの出力*/
public static void booleanver() {
boolean b = (100%3 == 2);
System.out.println("b = " + b);
b = (300%3 == 0);
System.out.println("b = " + b);
}
/*byteの出力*/
public static void bytever(){
byte by = 127;
System.out.println("by = " + by);
by = -128;
System.out.println("by = " + by);
}
/*shortの出力*/
public static void shortver(){
short sh = 32767;
System.out.println("sh = " + sh);
sh = -32768;
System.out.println("sh = " + sh);
}
/*intの出力*/
public static void intver() {
int i = 2147483647;
System.out.println("i = " + i);
i = -2147483648;
System.out.println("i = " + i);
}
/*longの出力*/
public static void longver() {
long l = (long)Math.pow(2, 63) - 1;
System.out.println("l = " + l);
l = (long)Math.pow(-2, 63);
System.out.println("l = " + l);
}
/*floatの出力*/
public static void floatver() {
float f = (float)Math.pow(2, -31);
System.out.println("f = " + f);
f = (float)Math.pow(-2, -31);
System.out.println("f = " + f);
}
/*doubleの出力*/
public static void doublever(){
double d = Math.pow(2, -63);
System.out.println("d = " + d);
d = Math.pow(-2, -63);
System.out.println("d = " + d);
}
}
実行結果
char型の出力
c = A
c = あ
c = 唖
boolean型の出力
b = false
b = true
byte型の出力
by = 127
by = -128
short型の出力
sh = 32767
sh = -32768
int型の出力
i = 2147483647
i = -2147483648
long型の出力
l = 9223372036854775806
l = -9223372036854775808
float型の出力
f = 4.656613E-10
f = -4.656613E-10
double型の出力
d = 1.0842021724855044E-19
d = -1.0842021724855044E-19
Report3_2
public class report3_2 {
static int a = 3, b = 10, d;
static float f = 2.4f, e;
static char c = 5;
public static void wa() {
d = a+b;System.out.println("a + b = " + d);
e = a+f;System.out.println("a + f = " + e);
d = a+c;System.out.println("a + c = " + d);
e = a+f+c;System.out.println("a + f + c = " + e);
}
public static void sa() {
d = a-b;System.out.println("a - b = " + d);
e = a-f;System.out.println("a - f = " + e);
d = a-c;System.out.println("a - c = " + d);
e = a-f-c;System.out.println("a - f - c = " + e);
}
public static void seki() {
d = a*b;System.out.println("a * b = " + d);
e = a*f;System.out.println("a * f = " + e);
d = a*c;System.out.println("a * c = " + d);
e = a*c*f;System.out.println("a * c * f = " + e);
}
public static void syou() {
d = a/b;System.out.println("a / b = " + d);
e = a/f;System.out.println("a / f = " + e);
d = a/c;System.out.println("a / c = " + d);
e = a/f/c;System.out.println("a / f / c = " + e);
}
public static void main(String[] args) {
System.out.println("四則演算");
System.out.println("加算");
wa();
System.out.println("減算");
sa();
System.out.println("乗算");
seki();
System.out.println("除算");
syou();
}
}
実行結果
四則演算
加算
a + b = 13
a + f = 5.4
a + c = 8
a + f + c = 10.4
減算
a - b = -7
a - f = 0.5999999
a - c = -2
a - f - c = -4.4
乗算
a * b = 30
a * f = 7.2000003
a * c = 15
a * c * f = 36.0
除算
a / b = 0
a / f = 1.25
a / c = 0
a / f / c = 0.25
Report3_3
import java.util.Random;
public class report3_3 {
static int i[] = {1,3,5,7,9};
public static void forver() {
int n;
for(n=0; n<=4; n++) {
System.out.println("i[" + n + "] = " + i[n]);
}
}
public static void ifver() {
int m = 3, t;
Random r = new Random();
while(--m>=0){
t = r.nextInt()%7;
if(t <= 4 & t >= 0) {
System.out.println("i[" + t + "] = " + i[t]);
} else {
System.out.println("i[" + t + "] is nothing.");
}
}
}
public static void main(String[] args) {
System.out.println("for文の例");
forver();
System.out.println("if,while文の例");
ifver();
}
}
実行結果
for文の例
i[0] = 1
i[1] = 3
i[2] = 5
i[3] = 7
i[4] = 9
if,while文の例
i[2] = 5
i[6] is nothing.
i[4] = 9
考察
書式
コメントについて
・『/*』と『*/』の間の文字はすべてコメントになります。これは範囲を指定出来ます。
・『//』の後に続く文字はコメントになります。ただし、これは1行だけで行末まではコメントになります。
画面に文字列を表示するのについて
・『print』は文字列を表示して、改行をしません。
・『println』は文字列を表示して、自動改行します。
文字列連結演算子について
演算子 | 意味 | 使用例 | 結果 |
+ | 文字列の連結
| "1年"+365+"日" | 1年365日 |
算術演算子について
算術演算子 | 意味 | 使用例 | 結果 |
+ | 加算 | 2+3 | 5 |
− | 減算 | 5-3 | 3 |
* | 乗算 | 4*3 | 12 |
/ | 除算 | 8/2 | 4 |
% | 商余算 | 8%3 | 2 |
四則演算子の実行順序について
優先順位 | 使用例 | 結果 |
1.かっこ(二重以上は内側から) | (2+3)*4
| 20 |
2.『*』および『/』 | 3*4/2 | 6 |
3.『+』および『-』 | 4*(2+3)/2-5 | 5 |
関係演算子および等値演算子について
演算子 | 意味 | 例 | 例の意味 |
> | 大きい | if(a > 100) | もしaが100より大なら |
>= | 大きいか等しい(以上) | if(a >= 100) | もしaが100以上なら |
< | 小さい | if(a < 100) | もしaが100より小なら |
<= | 小さいか等しい(以下) | if(a <= 100) | もしaが100以下なら |
== | 等しい | if(a == 100) | もしaが100なら |
!= | 等しくない | if(a != 100) | もしaが100でないならなら |
・if文には必要不可欠!!
論理演算子について
優先順位 | 演算子 | 名称 | 意味 | 例 | 例の意味 |
1 | ! | 否定 | でない,not | if(!(a==1 && b==1)) | aが1かつbが1、ではない |
2 | && | 論理積 | かつ,and | if(a>10 && a<50) | aが10を超えかつ50未満 |
3 | || | 論理和 | または,or | if(a==10 || a==50) | aが10または50 |
インクリメント・デクリメント演算にについて
| | | 式中での評価、更新順序 |
インクリメント | y = y +1; |
y++; | y を評価してから式を演算し、y + 1 に更新 |
++y; | y + 1 に更新してから式の評価、演算 |
デクリメント | y = y -1; | y--; |
評価してから更新 |
--y; |
更新してから評価 |
基本データ型について
基本データ型
型名 | ビット数(バイト数) | 表現できる範囲 |
分類 |
byte | 8
(1) | (-27, 27-1) = (-128, 127) |
符号付整数型 |
short | 16
(2) | (-215,
215-1) |
int | 32
(4) | (-231,
231-1) |
long | 64
(8) | (-263,
263-1) |
char | 16
(2) | Unicode の一文字 |
文字(整数型) |
float | 32
(4) | IEEE 754 規格の 32 ビット浮動小数点数 |
浮動小数点数型 |
double | 64
(8) | IEEE
754 規格の
64 ビット浮
動小数点数
|
boolean | ?
(?) | true /false |
論理値型 |
- 符号付整数型
整数型にはいくつか種類があるが、保持できる数字の範囲が異なる。
long 型の整数の場合、代入数字に L を付けたほうがよい。数字は実は
int 型と同じ 32 ビットしか保持できませんので、 long 型に代入する
ときには、明示的に示す必要がある。
long dbla=12345678901L;
- 文字型
文字型 char は UNICODE (UTF-16) の一文字をあらわす。 UNICODE とは
文字セットの一つで、文字セットとは、二進数のコンピュータで文字を
識別する為の番号と文字の組のことであり、文字をビットパターンに符
号化したものの組のこと。 Java では Unicode という文字セットに符号
化する文字符号化方法を標準で利用している。
char 型のリテラルはシングル・クオティション (') で括ります。代入
する文字は、普通にキーボードで文字を入力するか、 Unicode のマップ
番号を直に記述します。
char lett='あ';
char c2='\u0041';
文字型データは、符号なしの整数型として扱われます。
- 浮動小数点数型
浮動小数の場合は double 型と解釈されるので、 float 型変数に小数を
代入する際には F を付けて明示する。
float f1=3.1415F;
- 論理値型
boolean 型の変数には、 true か false 以外の値は入りません。例えば、
式 a lt; b は、a が b よりも小さいならば true が返り、そうで
なければ false が返ります。この結果を代入できる変数の型は、
boolean 型になります。
反省・感想
今回の課題はいろいろ用事などもあり、時間をかける時間がなかったので友達
のレポートにかなり頼ってしまいました。次からはきちんと自分でできるように
しないと後々困りそうだな〜と思っています。次の課題はちゃんと自分で時間を
かけて教科書やネットをみて勉強しながらやります。
参考文献
・独習Java ジョゼフ・オニール著
・しっかり学ぶ Java標準テキスト 安藤明之・米岡学著 技術評論社
・http://bw-www.ie.u-ryukyu.ac.jp/~j04058/Report_3.htm