2018. 4. 18. 17:49ㆍ자격인증
package bookA;
import java.util.Scanner;
public class CaesarCode {
static final int JUMP_COUNT = 3;
String inputWord = "";
public CaesarCode(String inputWord) {
this.inputWord = inputWord;
}
public String doProcessCaesarCode() {
inputWord = inputWord.toLowerCase();
char[] inputWordArr = inputWord.toCharArray();
char[] outputWordArr = new char[inputWordArr.length];
for(int i=0; i < inputWordArr.length; i++) {
outputWordArr[i] = (char) (inputWordArr[i] + JUMP_COUNT);
}
return String.valueOf(outputWordArr);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String inputWord = input.next();
input.close();
CaesarCode caesarCode = new CaesarCode(inputWord);
System.out.println(inputWord + " -> " + caesarCode.doProcessCaesarCode());
}
}
-------------------------------------------------------------------------------------
package bookA;
public class EggDelivery {
static final int ONE_EGG_PAY = 5;
static final int DROP_EGG_PAY = -50;
int deliveryCount;
int dropCount;
int deliveryedCount;
int todayPay;
public EggDelivery(int deliveryCount, int dropCount) {
this.deliveryCount = deliveryCount;
this.dropCount = dropCount;
this.deliveryedCount = deliveryCount - dropCount;
}
public void calPayToday() {
todayPay = (deliveryedCount * ONE_EGG_PAY) + (dropCount * DROP_EGG_PAY);
}
public void toResultString() {
System.out.println("배달한 계란의 수: " + deliveryCount + "개");
System.out.println("깨뜨린 계란의 수: " + dropCount + "개");
System.out.println("오늘의 일당: " + todayPay + "원");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EggDelivery firstDelivery = new EggDelivery(100, 50);
firstDelivery.calPayToday();
firstDelivery.toResultString();
}
}
------------------------------------------------------------------------------
package bookA;
import java.util.Scanner;
public class IPAddress {
static final int UNIT_SIZE = 8;
static final int IP4_UNIT = 4;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//11001011100001001110010110000000
Scanner input = new Scanner( System.in );
String t32bit = "";
t32bit = input.next();
input.close();
String[] t32bitArr = new String[IP4_UNIT];
int firstArg = 0;
String result = "";
for (int i = 0; i < IP4_UNIT; i++) {
t32bitArr[i] = t32bit.substring(firstArg, ((i+1)*UNIT_SIZE) );
firstArg += 8;
}
for (int i = 0; i < IP4_UNIT; i++) {
result += Integer.parseInt(t32bitArr[i],2);
if (i != 3) result += ".";
}
System.out.println("IP주소 : " + result);
}
}
-----------------------------------------------
package bookA;
import java.util.Scanner;
public class PrimeTest {
static final String MSG_PRIME = "은(는) 소수입니다.";
static final String MSG_NOPRIME = "은(는) 소수가 아닙니다.";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
// 소수를 판별할 논리형 변수
boolean isPrime = true;
int num = input.nextInt();
input.close();
// 1은 소수도 합성수도 아니다.
if (num == 1) isPrime = false;
// 1은 소수도 합성수도 아니므로 i는 2부터 시작한다.
// 2의 경우 반복문이 실행되지 않으므로 defalt값으로 실행된다.
for (int i = 2; i < num; i++) {
// 1과 num 자신 외에 나누어지는 수가 있는지 검사할 조건문
if (num % i == 0) {
// 나누어지는 수가 있을 경우 isPrime의 값을 false로 바꾼다.
isPrime = false;
// 한번이라도 이조건문이 실행될경우 num은 소수가 아니므로 반복문을 빠져나온다.
break;
}
}
// 위 조건문의 결과에 따라 아래의 조건문을 실행한다.
if (isPrime) {
System.out.println(num + MSG_PRIME);
} else {
System.out.println(num + MSG_NOPRIME);
}
}
}
-------------------------------------------------------------------------
package bookA;
public class SquareTest {
int n;
public SquareTest(int n) {
this.n = n;
}
public int calSquare(int n) {
int sumOfS = (int) Math.pow((n * (n + 1)) / 2, 2);
int sOfSum = (n * (2*n + 1) * (n + 1)) / 6;
return sumOfS - sOfSum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SquareTest st = new SquareTest(6);
System.out.println("1부터" + st.n + "까지 자연수에 대한 결과는?");
System.out.println(st.calSquare(st.n));
}
}