01 Object 클래스
º 2차원 평면 상의 점을 나타내는 Point 클래스를 정의해 보자
<1-1> Point 클래스
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
<1-2> PointEx 클래스
public class PointEx {
public static void main(String[] args) {
Point p1 = new Point(1, 2);
System.out.println(p1);
System.out.println(p1.toString());
System.out.println(p1.getClass());
System.out.println(p1.getClass().getName());
System.out.println(p1.hashCode());
}
}
<2-1> Demo 클래스
public class Demo {
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);
Point p3 = p1;
if(p1 == p2) System.out.println("(1)");
if(p1 == p3) System.out.println("(2)");
if(p1.equals(p2)) System.out.println("(3)");
if(p1.equals(p3)) System.out.println("(4)");
}
}
실행 결과
(2)
(4)
<3-1> Point 클래스
º equals()의 재정의(overriding)
public class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
thix.y = y;
}
public String toString() {
return "p(" + x + "," + y + ")";
}
public boolean equals(Point p) {
if((x/(double)y) == (p.x/(double)p.y))
return true;
else return false;
}
}
<3-2> Demo 클래스
public class Demo {
public static void main(String[] args) {
Point p1 = new Point(2,3);
Point p2 = new Point(4,6);
System.out.println(p1);
System.out.println(p2);
System.out.println(p1.equals(p2));
}
}
º toString() 메소드
<4-1> Rectangle 클래스
public class Rectangle {
int width, height;
String style;
public Rectangle(int w,int h, String s) {
width = w;
height = h;
style = s;
}
}
<4-2> RectangleEx 클래스
public class RectangleEx {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(10,10,"정사각형");
Rectangle r2 = new Rectangle(10,20,"직사각형");
System.out.println(r1);
System.out.println(r1.toString());
System.out.println(r2);
System.out.println(r2.toString());
}
}
º toString() 메소드 재정의
<4-3> Rectangle 클래스
public class Rectangle {
int width, height;
String style;
public Rectangle(int w, int h, String s) {
width = w;
height = h;
style = s;
}
public String toString() {
return "(" + style + ")" + "가로= " + width + ", 세로= " + height;
}
}
<4-4> RectangleEx 클래스
public class RectangleEx {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(10,10,"정사각형");
Rectangle r2 = new Rectangle(10,20,"직사각형");
System.out.println(r1);
System.out.println(r2);
}
}
02 Wrapper 클래스
º Integer 클래스의 활용
<5-1> Demo1 클래스
public class Demo1 {
public static void main(String[] args) {
int i = 13;
Integer ii = Integer.valueOf(i);
System.out.println(i);
System.out.println(ii.floatVlue()); //실수값 구하기
System.out.println((float)i); //윗 줄과 같은 역할
}
}
<5-2> Demo2 클래스
public class Demo2 {
public static void main(String[] args) {
String s1 = "3";
String s2 = "5";
int n1, n2;
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s2);
System.out.println(n1+n2);
}
}
<5-3>
º n에 저장되어 있는 숫자의 16진수 표현과 2진수 표현을 화면에 출력
public class Demo3 {
public static void main(String[] args) {
int n = 123;
System.out.println(Integer.toHexString(n));
System.out.println(Integer.toBinaryString(n));
}
}
<6-1> Demo4 클래스
º 숫자 찾기, 숫자로 변환 후 사용
c[] 배열에서 숫자만 골라 출력하고, s[]배열에서는 양수만 골라 출력
public class Demo4 {
public static void main(String[] args) {
char c[] = {'4','h','1','8','a','x','7'};
String s[] = {"4","-2","1","8","-10","-5","7"};
for(int i=0; i<c.length; i++) {
if(Character.isDigit(c[i]))
System.out.println(c[i]);
}
System.out.println();
for(int i=0; i<s.length; i++) {
if(Integer.parseInt(s[i]) > 0)
System.out.println(s[i]);
}
}
}
<7-1> Demo5 클래스
º 복잡한 boxing, unboxing 부분 제거
public class Demo5 {
public static void main(string[] args) {
int a[] = {1,2,3,4,5};
Integer A[] = {6,7,8};
int sum = 0;
for(int i=0; i<a.length; i++)
sum += a[i];
for(int i=0; i<A.length; i++)
sum += A[i];
System.out.println(sum);
}
}
03 String 클래스
º split(), replace()에 대한 공부
<7-2> Demo6 클래스
public class Demo6 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = s1.replace("He", "ABC");
System.out.println(s2);
String s3 = "cherry apple banana";
String fruits[] = s3.split(" ");
for(int i=0; i<fruits.length; i++)
System.out.print(fruits[i] + " ");
}
}
04 String 클래스의 메소드 사용
º String 클래스의 메소드인 concat(두 문자열 연결), trim(문자열마다 양 옆의 공백 제거), replace(문자열 대치),
split(문자열 분리), substring(문자열의 일부를 반환), charAt(문자열 속의 문자), toLowerCase(모두 소문자 변환),
toUpperCase(모두 대문자 변환)기능들 활용
<8> Demo7 클래스
public class Demo7 {
public static void main(String[] args) {
String a = new String(" Hello, ");
String b = new String("World! ");
//두 문자열 연결
a = a.concat(b);
System.out.println("문자열 연결");
System.out.println(a);
System.out.println();
//문자열 마다 양 옆의 공백 제거
a = a.trim();
System.out.println("문자열 양 옆의 공백 제거");
System.out.println(a);
System.out.println();
//문자열 대치
a = a.replace("o","###");
System.out.println("문자열에서 o를 ###로 바꿈");
System.out.println(a);
System.out.println();
//문자열 분리
String s[] = a.split(",");
for (int i=0; i<s.length; i++)
System.out.println("분리된" + i + "번 문자열: " + s[i]);
System.out.println();
//서브 스트링a[3]~
a = a.substring(3);
System.out.println("문자열의 일부 a[3]~끝");
System.out.println(a);
System.out.println();
//문자열 속의 문자
char c = a.charAt(2);
System.out.println("문자열 속의 문자 a[2]");
System.out.println(c);
System.out.println();
//모두 소문자로 변환
a = a.toLowerCase();
System.out.println("소문자로 변환");
System.out.println();
//모두 대문자로 변환
a = a.toUpperCase();
System.out.println("대문자로 변환");
System.out.println(a);
System.out.println();
}
}
<9> Demo8 클래스
º 정수 배열 A[ ]에 숫자들이 저장되어 있다. 이 숫자들을 오름차순으로 정렬한 후, 그 값을 화면에 출력하라.
public class Demo8 {
public static void main(String[] args) {
int A[] = {5, 1, 4, 9, 2, 8, 6, 3, 7};
for(int i=0; i<A.length-1; i++) {
for(int j=0; j<A.length-i-1; j++) {
if(A[j]>A[j+1]) {
int temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
for(int i=0; i<A.length; i++) {
System.out.print(A[i]+" ");
}
}
}
<10> Demo9 클래스
public class Demo9 {
public static void main(String[] args) {
String a = "banana,applempineapplemgrape,mango,guava,kiwi,cherry";
String b[];
b = a.split(",");
for(int i=0; i<b.length; i++)
System.out.println("분리된" + i + "번 문자열: " + b[i]);
System.out.println();
}
}
04 compareTo() 활용
<11> Demo10 클래스
º 과일 이름 오름차순 정렬
public class Demo10 {
public static void main(String[] args) {
String a = "banana,apple,pineapple,grape,mango,guava,kiwi,cherry";
String b[];
b = a.split(",");
for(int i=0; i<b.length-1;i++) {
for(int j=0; j<b.length-i-1; j++) {
if(b[j].compareTo(b[j+1])==1) {
String temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
}
}
for(int i=0; i<b.length; i++) {
System.out.print(b[i]+" ");
}
}
}
'JAVA > 자바 실습' 카테고리의 다른 글
Java | Lab10 (0) | 2021.06.09 |
---|---|
Java | Lab9 (0) | 2021.06.08 |
Java | Lab8 (0) | 2021.06.08 |
Java | Lab6 (0) | 2021.04.16 |
Java | Lab5 (0) | 2021.04.15 |