Lab1

Lab1 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서(3-4장정도 – 장수제한없음)를 넣고 Lab1_학번_이름.zip 압축한 후 e-learning(http://lms.dankook.ac.kr/index.jsp)으로 제출
java1-lab1

// https://www.ptable.com/?lang=ko
// number(int) symbol(String) name(String) weight(double)
// 1 H 수소 1.008
// 2 He 헬륨 4.0026
// 3 Li 리튬 6.94
// 4 Be 베릴륨 9.0122
// 5 B 붕소 10.81
// 6 C 탄소 12.011
// 7 N 질소 14.007
// 8 O 산소 15.999
// 9 F 플루오린 18.998
// 10 Ne 네온 20.180

Lab1 – Basics (method, command line arguments, Scanner)

1 – method
// PeriodicElement [number=” + number + “, symbol=” + symbol + “, name=” + name + “, weight=” + weight + “]” 을 출력하는 메소드 작성

2 – command line arguments

3 – Scanner 클래스를 이용하여 사용자에게 입력받아서 PeriodicElement 정보를 출력한다.

4 – 본인이 원하는 코드를 추가작성한다

1,2,3,4에 해당하는 부분을 /* 주석문 */으로 표시해준다.

Int String Conversion


//http://javadevnotes.com/java-integer-to-string-examples
// int => String type conversion
int i = -10; // -10
String s1 = Integer.toString(i); // "-10"
String s2 = String.valueOf(i); // "-10"
String s3 = new Integer(i).toString(); // "-10"
String s4 = String.format("%d", i); // "-10"
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);

//http://javadevnotes.com/java-string-to-integer-examples
// String => int type conversion
String x = "-123"; // "-123"
int y = Integer.parseInt(x); // -123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int z = Integer.valueOf(x); // -123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int w = new Integer(x).intValue(); // 123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error) 
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
System.out.println("w = " + w);

Numeric Data Type Conversion


byte bVal = 127;
int iVal = 100;
System.out.println(bVal+iVal); // 127 + 100 = 227
System.out.println(10/4); // int/ int 2.5 => 2
System.out.println(10.0/4); // double / int => double divided by double => 2.5
System.out.println((char)0x12340041);
System.out.println((byte)(bVal+iVal)); // 127 + 100 => (byte)227 => 1110 0011 => 1 + 2 + 32 + 64 – 128 => -29
System.out.println((int)2.9 + 1.8); // 2 + 1.8 => 2.0 + 1.8 => 3.8
System.out.println((int)(2.9 + 1.8)); // (int)4.1 => 4
System.out.println((int)2.9 + (int)1.8); // 2 + 1 => 3

Implicit/Explicit Type Conversion

//Implicit type conversion vs Explicit type conversion
int intVal = 32555;
byte byteVal = 25;
long longVal = intVal; // implicit type conversion
System.out.println(longVal); // 32555
intVal = (int) longVal; // explicit type conversion
System.out.println(intVal); // 32555
longVal = byteVal; // implicit type conversion
System.out.println(longVal); // 25
byteVal = (byte) longVal; // explicit type conversion
System.out.println(byteVal); // 25

Java’s byte (-128 to 127)

Java’s byte is a signed 8-bit numeric type whose range is -128 to 127 (JLS 4.2.1). 233 is outside of this range; the same bit pattern represents -23 instead.

11101001 = 1 + 8 + 32 + 64 + 128 = 233 (int)
           1 + 8 + 32 + 64 - 128 = -23 (byte)

A byte plus a byte is an int in Java.

 

byte a = 1;
byte b = 2; 
byte c = a + b; // compile error (byte + byte = int)
byte c = (byte)(a + b); // corrected

Data Type

Primitive Type vs Reference Type

8 Primitive Data Type
https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types

boolean A binary value of either true or false
byte 8 bit signed value, values from -128 to 127
short 16 bit signed value, values from -32.768 to 32.767
char 16 bit Unicode character
int 32 bit signed value, values from -2.147.483.648 to 2.147.483.647
long 64 bit signed value, values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
float 32 bit floating point value
double 64 bit floating point value

Object Types

Data type Description
Boolean A binary value of either true or false
Byte 8 bit signed value, values from -128 to 127
Short 16 bit signed value, values from -32.768 to 32.767
Character 16 bit Unicode character
Integer 32 bit signed value, values from -2.147.483.648 to 2.147.483.647
Long 64 bit signed value, values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
Float 32 bit floating point value
Double 64 bit floating point value
String N byte Unicode string of textual data. Immutable

Reference Types

class A class is also a data type – a non primitive reference data type
interface An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods.
array An array’s type is written as type[] , where type is the data type of the contained elements; arrays are objects, are dynamically created, and may be assigned to variables of type Object
enum An enum type is a special data type that enables for a variable to be a set of predefined constants.