package Demo5;

public class MathDemo {

	public static void main(String[] args) {
		
		double result1 = Math.ceil(3.1);
		double result2 = Math.ceil(3.0);
		double result3 = Math.ceil(3.9);
		System.out.println(result1);
		System.out.println(result2);
		System.out.println(result3);
		
		double result4 = Math.floor(3.1);
		double result5 = Math.floor(3.0);
		double result6 = Math.floor(3.9);
		System.out.println(result4);
		System.out.println(result5);
		System.out.println(result6);
		
		double result7 = Math.round(3.1);
		double result8 = Math.round(3.0);
		double result9 = Math.round(3.9);
		System.out.println(result7);
		System.out.println(result8);
		System.out.println(result9);
		
	}
}
package Demo4;

import java.util.Date;

public class DateDemo {

	public static void main(String[] args) {
		// 컴퓨터 현재 날짜 및 시간 정보를 갖고 있는 Date객체가 생성된다.
		Date now = new Date();
		java.sql.Date x = null;
		
		System.out.println(now);
		System.out.println(now.toString()); // 날짜와 시간정보를 문자열로 제공하도록 재정의
		
		long value = now.getTime(); // UNIX타임
		System.out.println(value);
		
		now.setTime(1234567890123L);
		System.out.println(now);
		
	}
}
package Demo4;

import java.util.Random;

public class RandomeDemo {
	public static void main(String[] args) {
	
		// 난수를 발생시키는 Random객체 생성
		Random random = new Random();
		
		// int범위의 최솟값과 최댓값 사이의 임의의 정수를 제공
		int num1 = random.nextInt();
		System.out.println(num1);
		
		// 0 <= 난수 < 100
		int num2 = random.nextInt(100);
		System.out.println(num2);

		int num3 = random.nextInt(6) + 1;
		System.out.println(num3);
	}
}
package Demo4;

import java.util.Arrays;

public class ArraysDemo {
	public static void main(String[] args) {
		
		int[] scores = {10, 30, 1, 7, 31, 46, 5};
		Arrays.sort(scores);
		
		System.out.println(Arrays.toString(scores));
		
		String[] names = {"홍길동", "이순신", "강감찬", "김유신", "유관순", "김구"};
		Arrays.parallelSort(names);
		System.out.println(Arrays.toString(names));
	}
}
package Demo4;

public class StringBuilderDemo {

	public static void main(String[] args) {
		
		String title = "이것이 자바다";
		String writer = "신용권";
		int price = 30000;
		
		String text = "제목 : " + title + ", 저자 : " + writer + ", 가격 : " + price;
		System.out.println(text);
		StringBuilder sb = new StringBuilder();
		
		sb.append("제목 : ");
		sb.append(title);
		sb.append(", 저자 : ");
		sb.append(writer);
		sb.append(", 가격 : ");
		sb.append(price);
		
		String text2 = sb.toString();
		System.out.println(text2);
	}
}
package Demo4;

import java.util.StringTokenizer;
public class StringTokenizerDemo {
	public static void main(String[] args) {
		StringTokenizer st = new StringTokenizer("김유신 강감찬 이순신 홍길동");
		
		int count = st.countTokens();
		System.out.println("토큰 개수 : " + count);
		
		boolean b1 = st.hasMoreTokens();
		System.out.println("토큰이 남아 있나? " + b1);

		String name1 = st.nextToken();
		System.out.println(name1);
		
		while (st.hasMoreElements()) {
			String name = st.nextToken();
			System.out.println(name);
			
		}
		
		StringTokenizer st2 = new StringTokenizer("100,20,50,28,59,9,17,83", ",");
		int total = 0;
		
		while (st2.hasMoreElements()) {
			int a = Integer.parseInt(st2.nextToken());
			System.out.println(a);
			total+=a;
		}
		
		System.out.println("합계 : " + total);
		
		String text = "홍길동,010-1111-1111,,중앙HTA";
		String[] values = text.split(",");
		StringTokenizer st3 = new StringTokenizer(text,",");
		
		System.out.println("배열의 길이 : " + values.length);
		System.out.println("토큰의 길이 : " + st3.countTokens());		
	}
}
package Demo4;

public class SystemDemo {
public static void main(String[] args) {
	
	long time = System.currentTimeMillis();
	System.out.println(time);
	
	String value1 = System.getProperty("java.version");
	String value2 = System.getProperty("os.name");
	String value3 = System.getProperty("user.name");
	String value4 = System.getProperty("user.home");
	System.out.println("자바버전 : " + value1);
	System.out.println("운영체제이름: " + value2);
	System.out.println("사용자 이름 : " + value3);
	System.out.println("사용자 홈 디렉토리 : " + value4);
	}
}
package Demo4;
import java.util.Properties;
import java.util.Set;

public class SystemDemo2 {
	public static void main(String[] args) {
		Properties prop = System.getProperties();
		// 시스템의 현재 시간 정보 조회하기
		long time = System.currentTimeMillis();
		System.out.println(time);
		
		// 시스템의 속성값 조회하기
		// String System.getProperty(String name)
		Set<Object> keys = prop.keySet();
		for (Object key : keys) {
			String value = prop.getProperty(key.toString());
			System.out.println("[" + key + "]" + value);
		}
		
		// 시스템의 환경변수값 조회
		// String System.getenv(String name)
		String env1 = System.getenv("JAVA_HOME");
		String env2 = System.getenv("Path");
		System.out.println("자바홈: " + env1);
		System.out.println("패스: " + env2);
		
	}
}
package Demo4;

import java.util.Arrays;

public class SystemDemo3 {
	public static void main(String[] args) {
		
		// 배열 복사
		// void System.arraycopy(Object src, int srcPost, Object dest, int destPos, int length)
		// src : 원본배열
		// srcPso : 원본배열의 복사 시작 위치
		// dest : 목적지 배열
		// destPos : 목적지 배열의 데이터 삽입 시작 위치
		// length : 복사할 배열의 아이템 개수
		String[] src = {"사과", "배", "바나나", "토마토", "레몬", "감", "포도", "오렌지", "멜론"};
		String[] dest1 = new String[5];
		String[] dest2 = new String[10];
		
		System.arraycopy(src, 3, dest1, 0, 5);
		System.arraycopy(src, 0, dest2, 4, 6);
		
		System.out.println(Arrays.deepToString(dest1));
		System.out.println(Arrays.deepToString(dest2));
		
	}
}
package Demo4;

public class WrapperDemo1 {

	public static void main(String[] args) {
		Integer num1 = new Integer(30);   // 박싱
		Integer num2 = new Integer("30");
		
		// 정수값 획득 : int intValue()
		int value1 = num1.intValue();    // 언박싱
		int value2 = num2.intValue();
		System.out.println(value1);
		System.out.println(value2);

		
		Double num3 = new Double(3.14);   // 박싱
		Double num4 = new Double("3.14");
		
		
		// 실수값 획득 : double doubleValue()
		double value3 = num3.doubleValue();    //언박싱
		double value4 = num4.doubleValue();
		System.out.println(value3);
		System.out.println(value4);
		
	}
}
package Demo4;

public class WrapperDemo2 {
	public static void main(String[] args) {
		
		// 오토박싱/언박싱
		Integer num1 = 10; 
		Double num2 = 3.14;
	}
}

'자바 > api' 카테고리의 다른 글

demo3  (0) 2019.06.10
demo2  (0) 2019.06.10
demo1  (0) 2019.06.10
package demo3;

public class StringDemo1 {
	public static void main(String[] args) {
		
		String str1 = "hello world";
		// String str1 = new String("hello world");
		
		// String toUpperCase() : 소문자를 대문자로 변환해서 반환한다.
		// String toLowerCase() : 대문자를 소문자로 변환해서 반환한다.

		String result1 = str1.toUpperCase();
		
		System.out.println("원본1 : " + str1);
		System.out.println("결과1 : " + result1);
		System.out.println("결과1 : " + str1.toUpperCase());
		
		String result2 = result1.toLowerCase();
		System.out.println("결과2 : " + result2);
		System.out.println(str1);

		str1 = str1 + "a";
		System.out.println(str1);

		
		// String replace(String old, String new) : 해당 글자 모두 바꾸기
		String str2 = "이것이 자바다. 자바 초급자를 위한 안내서";
		String result3 = str2.replace("자바", "파이썬");
		System.out.println("원본2 : " + str2);
		System.out.println("결과3 : " + result3);
		
		// int length() : 문자열의 길이를 반환한다.
		String str3 = "오늘 점심은 어떤거 먹지?";
		int len = str3.length();
		System.out.println("문자열의 길이 : " + len);
		
		
		
		String name1 = new String("이순신");
		String name2 = new String("이순신");
		String name3 = "이순신";
		String name4 = "이순신";
		
		System.out.println(name1 == name2);
		System.out.println(name1.equals(name2));
		System.out.println(name3 == name4);
		System.out.println(name3.equals(name4));		

		// 1. == 주소값 비교
		// 2. equals() 내용 비교
		// equals메소드는 String객체를 생성하는 방법에 상관업이 문자열의 내용이 동일하면
		// true값을 반환. 따라서 문자열의 비교는 반드시 equals()메소드를 사용해야 한다.
	
		
		// String substring(int beginIndex): 끝까지 잘라낸 새로운 문자열을 반환
		//String substring(int beginIndex, int endIndex): ~까지 잘라낸 새로운 문자열을 반환
		String str4 = "이것이 자바다";
		String result5 = str4.substring(1);
		System.out.println("결과5: " + result5);
		
		String result6 = str4.substring(2,6);
		System.out.println("결과6: " + result6);
		
		String str5 = "이것이 자바다";
		boolean result7 = str5.contains("자바");
		System.out.println("결과: "+ result7);
		
		// boolean startsWith(String str) : 지정된 문자열로 시작하는지 여부를 반환한다.
		// boolean endsWith(String str) : 지정된 문자열로 끝나는지 여부를 반환한다.
		String str6 = "http://www.daum.net";
		String str7 = "www.google.com";
		String str8 = "www.naver.com";
		System.out.println("결과8: " + str6.startsWith("http"));
		System.out.println("결과9: " + str6.startsWith("http"));
		System.out.println("결과10: " + str6.startsWith("http"));
		System.out.println("결과11: " + str6.startsWith("com"));
		System.out.println("결과12: " + str6.startsWith("com"));
		System.out.println("결과13: " + str6.startsWith("com"));
		
		// boolean empty(): 문자열이 비어있으면 true를 반환.
		
		String str12 = "     이것이 자바다        ";
		String result17 = str12.trim();
		String str11 = "";
		System.out.println("결과17: " + "[" + result17 + "]");
			
		String str13 = "이것이 자바다. 자바 업무용 추천 도서";
		int result18 = str13.indexOf("자바");
		int result19 = str13.indexOf("파이썬");
		System.out.println("결과18 : " + result18);
		System.out.println("결과19 : " + result19);

		String str14 = "이것이 자바다";
		char result20 = str14.charAt(1);
		System.out.println("결과20: " + result20);

		String str15 = " 이순신, 김유신, 강감찬, 홍길동, 유관순";
		String[] result21 = str15.split(",");
		System.out.println("결과21: " + result21[0]);
		System.out.println("결과21: " + result21[1]);
		System.out.println("결과21: " + result21[2]);
		System.out.println("결과21: " + result21[3]);
		System.out.println("결과21: " + result21[4]);	
	}
}
package demo3;

import java.util.Scanner;

public class StringDemo2 {
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	
	System.out.print("비밀번호를 입력하세요: ");
	String pwd1 = scanner.next();
	System.out.print("비밀번호를 다시 입력하세요: ");
	String pwd2 = scanner.next();
	
	System.out.println(pwd1 ==pwd2);
	System.out.println(pwd1.contentEquals(pwd2));
	
	// scanner.close();
	}
}
package demo3;

import java.util.Scanner;

public class StringDemo3 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("이메일을 입력하세요: ");
		String email = scanner.next();
		
		int endIndex = email.indexOf("@");
		String id = email.substring(0, endIndex);
		System.out.println("아이디: " + id);
		
		scanner.close();
		}
}
package demo3;

import java.util.Scanner;

public class StringDemo4 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("이름,국어,영어,수학 정보 입력> ");
		String text = sc.next();	// ex. 홍길동,60,80,50
		
		String[] values = text.split(",");
		System.out.println("이름: " + values[0]);
		System.out.println("국어: " + values[1]);
		System.out.println("영어: " + values[2]);
		System.out.println("수학: " + values[3]);
		
		sc.close();
	}
}
package demo3;

public class StringDemo5 {

	public static void main(String[] args) {
		
		String str = "이것이 자바다";
		System.out.println(str.length());
		
		System.out.println("이것이 자바다".length());
		
		String str2 = "       this is a java           ".trim().toUpperCase().substring(0, 5);
		System.out.println(str2);
	}
}

'자바 > api' 카테고리의 다른 글

demo4  (0) 2019.06.10
demo2  (0) 2019.06.10
demo1  (0) 2019.06.10
package demo2;

public class Contact {
	private int no;
	private String name;
	private String tel;
	private String email;

	public Contact() {}

	public Contact(int no, String name, String tel, String email) {
		super();
		this.no = no;
		this.name = name;
		this.tel = tel;
		this.email = email;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	// 오브젝트의 toString[] 메소드 재정의

	@Override
	public String toString() {
		return "Contact [no=" + no + ", name=" + name + ", tel=" + tel + ", email=" + email + "]";
	}
	
	public Contact copyContact() throws CloneNotSupportedException {
		Object obj = clone();
		Contact c = (Contact)obj;
		return c;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + no;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Contact other = (Contact) obj;
		if (no != other.no)
			return false;
		return true;
	}
}
package demo2;

public class ContactApp {

	public static void main(String[] args) {
		
		Contact c = new Contact(10, "김유신", "010-1111-1111", "kim@naver.com");
		Contact cc = new Contact(10, "김유신", "010-1111-1111", "kim@naver.com");
		
		//int hashcode() : 객체에 부여된 일련번호를 점수로 제공하는 메소드
		int hash = c.hashCode();
		int hash2 = cc.hashCode();
		System.out.println("해쉬코드 값 : " + hash);
		System.out.println("해쉬코드 값 : " + hash2);
		
		// String toString() : 객체의 정보를 문자열로 제공하는 메소드
		//					   설계도의 이름 + "@" + 해시코드값(16진수)
		//					   toString()메소드는 재정의해서 필드에 저장된 값들을 문자열로 이어서 제공하도록
		//					   재정의해서 사용한다.
		String info = c.toString();
		String info2 = cc.toString();
		System.out.println("객체의 정보 : " + info);
		System.out.println("객체의 정보 : " + info2);
		//System.out.println(c);
		//System.out.println(cc);
		//System.out.println(c.toString());
		//System.out.println(cc.toString());	
		
		// boolean equals(Object other) : - 전달받은 다른 객체와 이 객체가 동일한 객체인지 여부를 반환한다.
		boolean res1 = c.equals(cc);
		boolean res2 = cc.equals(c);
		System.out.println(res1);
		System.out.println(res2);
		// 같은 곳을 바라볼 때만(=동일한 객체일 때만) true
	}
}
package demo2;

public class ContactCloneApp {

	public static void main(String[] args) throws Exception {
		
		Contact src = new Contact(100, "홍길동", "010-1111-1111", "hong@gmail.com");
	
		Contact cloneContact = src.copyContact();
		System.out.println(cloneContact.getNo());
		System.out.println(cloneContact.getName());
		System.out.println(cloneContact.getTel());
		System.out.println(cloneContact.getEmail());
	}
}
package demo2;

import java.lang.reflect.Method;

import org.omg.CORBA.Context;

public class ContactReflect {
	public static void main(String[] args) throws Exception {
		Contact c = new Contact(10, "홍길동", "010-1111-2222", "hong@hanmail.com");
				
		Class<?> clazz = c.getClass();
		Method[] methods = clazz.getMethods();
		
		for (Method m : methods) {
			System.out.println(m.getName());
			
		}
	}
}

'자바 > api' 카테고리의 다른 글

demo4  (0) 2019.06.10
demo3  (0) 2019.06.10
demo1  (0) 2019.06.10
package demo1;
/**
 * 덧셈, 뺄셈 기능이 구현된 클래스입니다.
 * @author JHTA
 *
 */
public class Calculator {
	/**
	 * 두 수를 전달받아서 더한 값을 반환합니다.
	 * @param x 숫자
	 * @param y 숫자
	 * @return 덧셈 결과가 반환됨
	 */
	public int plus(int x, int y) {
		int z = x + y;
		return z;
	}
	/**
	 * 두 수를 전달받아서 뺀 값을 반환합니다.
	 * @param x 숫자
	 * @param y 숫자
	 * @return 뺄셈 결과가 반환됨
	 */
	public int minus(int x, int y) {
		int z = x - y;
		return z;
	}
}
package demo1;

public class Hello {

	public static void main(String[] args) {
		System.out.println("안녕, 이클립스");
		
		Calculator cal = new Calculator();
	}
}

'자바 > api' 카테고리의 다른 글

demo4  (0) 2019.06.10
demo3  (0) 2019.06.10
demo2  (0) 2019.06.10

+ Recent posts