1-------------------------------------------------------------------

public class HelloJava{

public static void main(String args[]){

System.out.println("Hello Java!");

}

}

2-------------------------------------------------------------------

public class CastingExam {

public static void main(String args[]) {

long bigNum = 3147483648L; // int형의 최대값보다 큰 값으로 초기화한다.

int smallNum = (int)bigNum; // bigNum의 값을 int형으로 캐스팅한다.

System.out.println(bigNum + " -> " + smallNum);

}

}

3-------------------------------------------------------------------

import java.io.*;

import java.util.Random;

public class ImportExam{

public static void main(String args[]){

Random rand = new Random();

int randNum = rand.nextInt(100);

PrintWriter pr = new PrintWriter(System.out);

pr.write("난수 : " + randNum);

pr.close();

}

}

4-------------------------------------------------------------------

import java.io.*;

public class Exam{

public static void main(String args[]) {

int i=0;

double num=0;

DataInputStream dis = new DataInputStream(System.in);

while(i++ < 5) {

try {

System.out.println("학점을 입력하세요.");

num = Double.parseDouble(dis.readLine());

} catch(IOException ioe) { System.out.println(ioe.getMessage()); }

if(num < 0 || num > 4.5) {

System.out.println("->잘못된 입력입니다.");

}

else if(num >= 4) {

System.out.println("->학점 : A\n");

}

else if(num >= 3) {

System.out.println("->학점 : B\n");

}

else if(num >= 2) {

System.out.println("->학점 : C\n");

}

else if(num >= 1) {

System.out.println("->학점 : D\n");

}

else {

System.out.println("->학점 : F\n");

}

}

}

}

5-------------------------------------------------------------------

public class WrapperExam{

public static void main(String args[]) {

String strNum = "2005";

String strBool = "true";

int intNum = Integer.parseInt(strNum);

float floatNum = Float.parseFloat(strNum);

Boolean boolObj = Boolean.valueOf(strBool);

boolean bool = boolObj.booleanValue();

System.out.println("Integer.parseInt(strNum) -> " + intNum);

System.out.println("Float.parseFloat(strNum) -> " + floatNum);

System.out.println("Boolean.getBoolean(strBool) -> " + bool);

}

}

6-------------------------------------------------------------------

public class GrammarOfJava{

public static void main(String args[]){

//연산자 사용하기

int intVar01 = 2005;

int intVar02 = 1995;

int intResult = 0;

double doubleResult = 0;

System.out.println("[ 연산자 사용하기 ]");

System.out.println("\t1. 산술 연산자");

intResult = intVar01 + intVar02;

System.out.println("\t\tintVar01 + intVar02 = " + intResult);

intResult = intVar01 + intVar02;

System.out.println("\t\tintVar01 - intVar02 = " + intResult);

intResult = intVar01 * intVar02;

System.out.println("\t\tintVar01 * intVar02 = " + intResult);

doubleResult = intVar01 / intVar02;

System.out.println("\t\tintVar01 / intVar02 = " + doubleResult);

intResult = intVar01 % intVar02;

System.out.println("\t\tintVar01 % intVar02 = " + intResult);

System.out.println("\t2. 연산후 대입 연산자");

intResult += 7;

System.out.println("\t\tintResult += 7 => " + intResult);

intResult -= 7;

System.out.println("\t\tintResult -= 7 => " + intResult);

intResult *= 7;

System.out.println("\t\tintResult *= 7 => " + intResult);

doubleResult /= 7;

System.out.println("\t\tdoubleResult /= 7 => " + doubleResult);

intResult %= 7;

System.out.println("\t\tintResult %= 7 => " + intResult);

System.out.println("\t3. 증가 연산자");

intVar01 = 100;

System.out.println("\t\tintVar01 = " + intVar01);

intResult = 0;

intResult = ++intVar01;

System.out.println("\t\tintResult = ++intVar01");

System.out.println("\t\tintVar01 = " + intVar01);

System.out.println("\t\tintResult = " + intResult);

intVar01 = 100;

System.out.println("\n\t\tintVar01 = " + intVar01);

intResult = 0;

intResult = intVar01++;

System.out.println("\t\tintResult = intVar01++");

System.out.println("\t\tintVar01 = " + intVar01);

System.out.println("\t\tintResult = " + intResult);

System.out.println("\t4. 감소 연산자");

intVar01 = 100;

System.out.println("\t\tintVar01 = " + intVar01);

intResult = 0;

intResult = --intVar01;

System.out.println("\t\tintResult = --intVar01");

System.out.println("\t\tintVar01 = " + intVar01);

System.out.println("\t\tintResult = " + intResult);

intVar01 = 100;

System.out.println("\n\t\tintVar01 = " + intVar01);

intResult = 0;

intResult = intVar01--;

System.out.println("\t\tintResult = intVar01--");

System.out.println("\t\tintVar01 = " + intVar01);

System.out.println("\t\tintResult = " + intResult);

}

}

7-------------------------------------------------------------------

public class IfExam {

public static void main(String args[]) {

int a = 20;

int b = 100;

if(a==b) {

System.out.println(a + "와 " + b + "는 같다.");

}

else {

System.out.println(a + "와 " + b + "는 같지 않다.");

}

}

}

8-------------------------------------------------------------------

public class ElseIfExam {

public static void main(String args[]) {

int a = 20;

int b = 100;

if(a==b) {

System.out.println(a + "와 " + b + "는 같다.");

}

else if(a<b) {

System.out.println(a + "는 " + b + "보다 작다.");

}

else {

System.out.println(a + "는 " + b + "보다 크다.");

}

}

}

9-------------------------------------------------------------------

public class WhileExam {

public static void main(String args[]) {

int a = 0;

while(a<10) {

System.out.println("2 * " + a + " = " + (2*a));

a++;

}

}

}

10------------------------------------------------------------------

public class WhileExam1 {

public static void main(String args[]) {

int a = 0;

while(true) {

System.out.println("2 * " + a + " = " + (2*a));

if(a>=9)

break;

a++;

}

}

}

11------------------------------------------------------------------

public class LogicOperExame{

public static void main(String args[]){

int a = 100;

int b = 20;

int c = 0;

if(a<b & (c=a-b)>0){

System.out.println("조건식 [a<b & (c=a-b)>0]가 참이다.\n");

}else{

System.out.println("조건식 [a<b & (c=a-b)>0]가 거짓이다.\n");

}

System.out.println("c = " + c);

c = 0 ;

if(a>b | (c=a-b)>0){

System.out.println("조건식 [a>b | (c=a-b)>0]가 참이다.");

System.out.println("c = " + c);

}

c = 0;

if(a>b || (c=a-b)>0){

System.out.println("\n조건식 [a>b || (c=a-b)>0]가 참이다.");

System.out.println("c = " + c);

}

}

}

12------------------------------------------------------------------

public class SwitchExam {

public static void main(String args[]) {

int num = 2;

switch(num) {

case 0:

System.out.println("num의 값은 0입니다. ");

break;

case 1:

System.out.println("num의 값은 1입니다. ");

break;

case 2:

System.out.println("num의 값은 2입니다. ");

break;

default:

System.out.println("num의 값은 0,1,1가 아닙니다.");

}

}

}

13------------------------------------------------------------------

public class DoWhileExam {

public static void main(String args[]) {

int a = 0;

do {

System.out.println("2 * " + a + " = " + (2 * a));

a++;

} while(a < 9);

}

}

14------------------------------------------------------------------

public class ContinueExam {

public static void main(String args[]) {

int num = 0;

while(num <= 100) {

num++;

if((num % 3) != 0 || (num % 7) != 0)

continue;

System.out.println(num + "는 3과 7의 공배수입니다.");

}

}

}

15------------------------------------------------------------------

public class ForExam01 {

public static void main(String args[]) {

for(int num = 1; num <= 9; num++) {

System.out.println("2 * " + num + " = " + (2 * num));

}

}

}

16------------------------------------------------------------------

public class ForExam02 {

public static void main(String args[]) {

int num = 1;

for(; num <= 9 ;) {

System.out.println("2 * " + num + " = " + (2 * num));

num++;

}

}

}

17------------------------------------------------------------------

public class ForExam03 {

public static void main(String args[]) {

for(int num1 = 1; num1 <= 9; num1++) {

for(int num2 = 2; num2 <=5; num2++) {

System.out.print(num2+"*"+num1+"="+(num2*num1)+ "\t");

}

System.out.print("\n");

}

}

}

18------------------------------------------------------------------

public class ArrayExam01 {

public static void main(String args[]) {

int stuNum[] = {1, 2, 3};

String stuName[];

stuName = new String[3];

double stuScore[] = new double[3];

stuName[0] = new String("정진철");

stuName[1] = new String("강성우");

stuName[2] = new String("김태수");

stuScore[0] = 100.0;

stuScore[1] = 95.5;

stuScore[2] = 97.0;

System.out.println("번호\t이름\t성적\t");

for(int index = 0; index < stuNum.length; index++) {

String text = stuNum[index] + "\t";

text = text + stuName[index] + "\t";

text += stuScore[index];

System.out.println(text);

}

}

}

19------------------------------------------------------------------

public class ArrayExam02 {

public static void main(String args[]) {

int srcNum[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int destNum[][] = new int[3][3];

System.out.println("원본배열");

for(int row = 0; row < srcNum.length; row++) {

for(int col = 0; col < srcNum[row].length; col++) {

System.out.print(srcNum[row][col] + " ");

destNum[col][row] = srcNum[row][col];

}

System.out.print("\n");

}

System.out.println("열과 행이 바뀐 배열");

for(int row = 0; row < srcNum.length; row++) {

for(int col = 0; col < srcNum[row].length; col++) {

System.out.print(destNum[row][col] + " ");

}

System.out.print("\n");

}

}

}

20------------------------------------------------------------------

public class StringExam{

public static void main(String args[]) {

String firstStr = "문자열";

String secondStr = "문자열";

String thirdStr = new String("문자열");

if(firstStr == secondStr)

System.out.println("firstStr과 secondStr은 같은 문자열 객체입니다.");

else

System.out.println("firstStr과 secondStr은 다른 문자열 객체입니다.");

if(firstStr == thirdStr)

System.out.println("firstStr과 secondStr은 같은 문자열 객체입니다.");

else

System.out.println("firstStr과 secondStr은 다른 문자열 객체입니다.");

if(firstStr.equals(thirdStr))

System.out.println("firstStr과 secondStr은 같은 문자열 객체입니다.");

else

System.out.println("firstStr과 secondStr은 다른 문자열 객체입니다.");

}

}

21------------------------------------------------------------------

public class ClassTest {

public static void main(String args[]) {

System.out.println("new 연산자를 사용해 객체를 생성합니다.");

ClassExam obj = new ClassExam();

obj.method();

}

}

class ClassExam {

private String memberVar;

public ClassExam() {

System.out.println("생성자는 객체가 생성될때 제일먼저 호출됩니다");

System.out.println("memberVar의 값을 \"멤버변수\"로 초기화 합니다.");

memberVar = "멤버변수";

}

public void method() {

System.out.println("memberVar : " + memberVar);

}

}

22------------------------------------------------------------------

import firstpackage.*;

public class Test {

public static void main(String args[]) {

String s = "Test is completed";

PackageTest pt = new PackageTest();

pt.print(s);

}

}

/////////////////////////////////////////

package firstpackage;

public class PackageTest {

public void print(String s) {

System.out.println("This is test of user define package");

System.out.println(s);

}

}

23------------------------------------------------------------------

/* 학생 정보를 관리하는 클래스. 이름, 번호, 과목별 성적을 속성으로 가지고, 이름과 번호를 인자로 가지는 생성자,  과목별 성적을 입출력하는 메서드와 총점과 평균을 반환하는 메서드를 포함한다. */

package tools;

public class Student {

private int intNum;	private String strName;

private double doubleKor, doubleEng, doubleCom;

public Student(int num, String name) {intNum=num;  strName=name;}

public void setKor(double score) {  doubleKor = score;	}

public void setEng(double score) {	doubleEng = score;	}

public void setCom(double score) {	doubleCom = score;	}

public int getNum() {	return intNum;	}

public String getName() {	return strName;	}

public double getKor() {	return doubleKor;	}

public double getEng() {	return doubleEng;	}

public double getCom() {	return doubleCom;	}

public double getSum() {

double sum = doubleKor + doubleEng + doubleCom;

return sum;

}

public double getAvg() {

double avg = getSum()/3.0D;

return avg;

}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*Student 클래스를 사용하여 성적표 출력 프로그램을 구현한다.*/

import tools.Student;	import java.io.*;

public class StudentExam {

public static void main(String args[]) {

Student[] students=null;

DataInputStream dis = new DataInputStream(System.in);

try {

System.out.print("학생은 몇 명 입니까?\t");

students = new Student[Integer.parseInt(dis.readLine())];

}catch(Exception e) {}

for(int i=0; i < students.length; i++) {

System.out.println((i+1) + "번 학생 정보 입력");

try {

System.out.print("이  름 :\t");

students[i] = new Student((i+1),

new String(dis.readLine().getBytes("8859_1"),

"KSC5601"));

System.out.print("국  어 :\t");

students[i].setKor(Double.parseDouble(dis.readLine()));

System.out.print("영  어 :\t");

students[i].setEng(Double.parseDouble(dis.readLine()));

System.out.print("컴퓨터 :\t");

students[i].setCom(Double.parseDouble(dis.readLine()));

}catch(Exception e) {}

}

for(int i=0; i < students.length; i++) {

System.out.println("\n================== 성 적 표 ==================");

System.out.println("번  호 :\t"+ students[i].getNum());

System.out.println("이  름 :\t"+ students[i].getName());

System.out.println("국  어 :\t"+ students[i].getKor());

System.out.println("영  어 :\t"+ students[i].getEng());

System.out.println("컴퓨터 :\t"+ students[i].getCom());

System.out.println("총  점 :\t"+ students[i].getSum());

System.out.println("평  균 :\t"+ students[i].getAvg());

System.out.println("==============================================\n");

}

}

}

24------------------------------------------------------------------

class Human {

public String nationality;

public void setNationality(String nationality) {

this.nationality = nationality;

}

public String getNationality() {

return nationality;

}

}

class Korean extends Human {

private String name;

public Korean(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

public class extendsTest {

public static void main(String[] args) {

Korean man = new

Korean("정진철");

man.setNationality("대한민국");

System.out.println(man.getName()+"씨의 국적은 "+man.getNationality()+"이다.");

}

}

25------------------------------------------------------------------

import java.util.*;

public class VectorTest {

public static void main(String args[]) {

Vector objList = new Vector(5);

String str1 = "String 형 객체 1";

Date now = new Date();

String str2 = "String 형 객체 2";

objList.add(str1);

objList.add(now);

objList.add(str2);

System.out.println("--- objList의 크기 " + objList.size() + " ---\n");

for(int index = 0; index < objList.size(); index++) {

Object obj = objList.get(index);

if(obj instanceof String) {

System.out.println((String)obj);

} else {

System.out.println(((Date)obj).toString());

}

}

System.out.print("\n");

Enumeration enu = objList.elements();

while(enu.hasMoreElements()) {

Object obj = enu.nextElement();

if(obj instanceof String) {

System.out.println((String)obj);

} else {

System.out.println(((Date)obj).toString());

}

}

}

}