본문 바로가기
솔루션모음/파워자바 프로그래밍

[파워자바] 24장 프로그래밍 솔루션 답지

by 이얏호이야호 2023. 2. 16.

1.

(1)

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {
	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader br = new BufferedReader(fr);
			while (true) {
				String line = br.readLine();
				if (line == null)
					break;
				int value = Integer.parseInt(line);
				list.add(value);
			}
			fr.close();
			Collections.sort(list);
			FileWriter fr1 = new FileWriter("output.txt");
			PrintWriter br1 = new PrintWriter(fr1);
			for (int k = 0; k < list.size(); k++) {
				br1.println(list.get(k));
			}
			br1.flush();
			fr1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

 

(2)

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {
	public static void main(String[] args) {
		ArrayList<Double> list = new ArrayList<Double>();
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader br = new BufferedReader(fr);
			while (true) {
				String line = br.readLine();
				if (line == null)
					break;
				double value2 = Double.parseDouble(line);
				list.add(value2);
			}
			fr.close();
			Collections.sort(list);
			FileWriter fr1 = new FileWriter("output.txt");
			PrintWriter br1 = new PrintWriter(fr1);
			for (int k = 0; k < list.size(); k++) {
				br1.println(list.get(k));
			}
			br1.flush();
			fr1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

 

2.

// 암호화 프로그램
import java.io.*; 

class encryptCaesar{ 
   public static void main(String[] args){ 
      try{ 
      FileReader fr = new FileReader(args[0]); 
      BufferedReader br = new BufferedReader(fr); 
      String plaintext = br.readLine(); 
      plaintext = plaintext.toUpperCase(); 
      int shiftKey = Integer.parseInt(args[1]); 
      shiftKey = shiftKey % 26;  
       
      String cipherText = "" 
  
      for (int i=0; i<plaintext.length(); i++){ 
         int asciiValue = (int) plaintext.charAt(i); 
         if (asciiValue < 65 || asciiValue > 90){ 
            cipherText += plaintext.charAt(i); 
            continue 
         } 
          
         int basicValue = asciiValue - 65; 
         int newAsciiValue = 65 + ((basicValue + shiftKey) % 26)  ; 
         cipherText += (char) newAsciiValue; 
  
       } 
       System.out.print(cipherText);              
      } 
     catch(Exception e){e.printStackTrace();} 
  } 
} 

// 복호화 프로그램
import java.io.*; 
class decryptCaesar{ 
   public static void main(String[] args){ 
      try{ 
      FileReader fr = new FileReader(args[0]); 
      BufferedReader br = new BufferedReader(fr); 
      String ciphertext = br.readLine(); 
      ciphertext = ciphertext.toUpperCase(); 
      int shiftKey = Integer.parseInt(args[1]); 
      shiftKey = shiftKey % 26;  
       
      String plainText = ""; 
  
      for (int i=0; i<ciphertext.length(); i++){ 
         int asciiValue = (int) ciphertext.charAt(i); 
         if (asciiValue < 65 || asciiValue > 90){ 
            plainText += ciphertext.charAt(i); 
            continue; 
         } 
          
         int basicValue = asciiValue - 65; 
        
         int newAsciiValue = -1000; 
  
         if (basicValue - shiftKey < 0){ 
           
            newAsciiValue = 90 - (shiftKey - basicValue) + 1; 
         } 
         else{ 
           newAsciiValue = 65 + (basicValue - shiftKey); 
          } 
         plainText += (char) newAsciiValue; 
  
       } 
      System.out.print(plainText);              
      } 
     catch(Exception e){e.printStackTrace();} 
  } 
}

 

3.

import java.io.*;
import java.util.*;
public class UserInform {
	public static void main(String[] args)throws IOException{
		int num2;
		String search;
		String  num, name, tel, email;
		Scanner scan = null;
		PrintWriter in = new PrintWriter(new FileWriter("user.txt"));
		Scanner s = new Scanner(System.in);
		while(true){
			System.out.println("사용자 번호를  입력하세요 ");
			num = s.next();
			System.out.println("사용자 이름을  입력하세요 ");
			name = s.next();
			System.out.println("사용자 전화번호를  입력하세요 ");
			tel = s.next();
			System.out.println("사용자 이메일을  입력하세요 ");
			email= s.next();
			System.out.println("입력이 끝났으면 1 계속입력하시려면 0");
			num2 = s.nextInt();
			in.print(num + "," + name + "," + tel + "," + email+"");
			in.flush();
			if(num2 == 1)
				break;
		}
		System.out.println("검색하실 사용자 번호를 입력하세요");
		num2 = s.nextInt();
		search = num2+"";
		scan = new Scanner(new BufferedReader(new FileReader("user.txt")));
		scan.useDelimiter(",");
		while(scan.hasNext()){
			num = scan.next();
			name = scan.next();
			tel = scan.next();
			email = scan.next();
			if(num.equals(search))
				System.out.println("사용자 번호 "+num2+"의 전화번호는"+tel +"입니다.");
		}
		if(in!= null)
			in.close();
		if(scan !=null)
			scan.close();		
	}
}

 

 

 

 

 

 

4.

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {
	public static void main(String[] args) {
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader br = new BufferedReader(fr);
			FileWriter fr1 = new FileWriter("output.txt");
			PrintWriter br1 = new PrintWriter(fr1);
			int counter = 0;
			while (true) {
				counter++;
				String line = br.readLine();
				if (line == null)
					break;
				br1.format("%6d: " + line + "\n", counter);
			}
			fr.close();
			br1.flush();
			fr1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

 

5.

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {
	public static void main(String[] args) {
		try {
			FileReader fr1 = new FileReader("input1.txt");
			BufferedReader br1 = new BufferedReader(fr1);
			FileReader fr2 = new FileReader("input2.txt");
			BufferedReader br2 = new BufferedReader(fr2);
			FileWriter fr3 = new FileWriter("output.txt");
			PrintWriter br3 = new PrintWriter(fr3);
			while (true) {
				String line = br1.readLine();
				if (line == null)
					break;
				br3.println(line);
			}
			while (true) {
				String line = br2.readLine();
				if (line == null)
					break;
				br3.println(line);
			}
			br3.flush();
			fr1.close();
			fr2.close();
			fr3.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

 

6.

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {

	public static void main(String[] args) throws FileNotFoundException {
		boolean equal=true;
			FileInputStream fr1 = new FileInputStream("input1.txt");
			FileInputStream fr2 = new FileInputStream("input2.txt");
			while (true) {
				try {
					byte c1 = (byte) fr1.read();
					byte c2 = (byte) fr2.read();
					if( c1 ==-1 || c2==-1 )
							break;
					if( c1 != c2 ){
						equal = false;
						break;
					}
				} catch (IOException e) {
					break;
				}
			}
			System.out.println(equal);
			try {
				fr1.close();
				fr2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}

 

7.

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class Test {
	static String solution;

	static boolean check(String s, StringBuffer a, char ch) {
		int i;

		for (i = 0; i < s.length(); i++) {
			if (s.charAt(i) == ch)
				a.setCharAt(i, ch);
		}
		for (i = 0; i < s.length(); i++) 
			if( s.charAt(i) != a.charAt(i) )
				return false; 
		return true;
	}

	public static void main(String[] args) throws IOException {
		char ch;
		Scanner sc = new Scanner(System.in);
		BufferedReader in = null;
		String[] words = new String[100];
		int count = 0;

		in = new BufferedReader(new FileReader("sample.txt"));
		for (int i = 0; i < 100; i++) {
			String s = in.readLine();
			if (s == null)
				break;
			words[i] = s;
			count++;
		}
		int index = (new Random()).nextInt(count);
		solution = words[index];
		StringBuffer answer = new StringBuffer(solution.length());
		for (int i = 0; i < solution.length(); i++) 
			answer.append(' ');
		for (int i = 0; i < solution.length(); i++) {
			if (solution.charAt(i) != ' ')
				answer.setCharAt(i, '_');
		}

		while (true) {
			//System.out.println("현재의 상태: " + solution);
			System.out.println("현재의 상태: " + answer);
			System.out.printf("글자를 추측하시오: ");
			String c = sc.next();
			if (check(solution, answer, c.charAt(0)) == true)
				break;
		}
		System.out.println("현재의 상태: " + answer);
	}
}

댓글