자바프로그래밍 5주차 과제ver2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

5주차 실전 문제 과제_(3장 - 1, 2, 3, 5, 7), (4장 - 1, 3)

3장 3장 3번 정수 오류로 작성 4장 1번 노래 변경버전

1. 영문 소문자를 하나 입력받고 그 문자보다 알파벳 순위가 낮은 모든 문자를 출력하는 프로그램을


작성하라.

─────────────────────────────────────────────

package project;
import java.util.Scanner;
public class hun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("알파벳 한 문자를 입력하세요>>");


String s = sc.next();
char c = s.charAt(0);

for (int i = 0; i <= c-'a'; i++) {


for (int j = i; j <= c-'a'; j++) {
System.out.print((char)('a'+j));
}
System.out.println();
}
}
}
─────────────────────────────────────────────

아래 사진
─────────────────────────────────────────────
2. 정수를 10개 입력받아 배열에 저장한 후, 배열을 검색하여 3의 배수만 출력하는
프로그램을 작성하라.

─────────────────────────────────────────────

package project;
import java.util.Scanner;
public class hun {
public static void main(String[] argv){
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.print("정수 10개 입력 하세요.>>");
for(int i = 0; i<10; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i < 10; i++){
if(arr[i]%3==0)
System.out.print(arr[i]+" ");
}
}
}
─────────────────────────────────────────────
사진

────────────────────────────────────────────
3. 정수를 입력받아 짝수이면 “짝”, 홀수이면 “홀”을 출력하는 프로그램을 작성하라. 사용자가 정수를 입력하지
않는 경우에는 프로그램을 종료하라. 정답을 통해 try-catch-finally를 사용하는 정통적인 코드를 볼 수 있다.

─────────────────────────────────────────────

package project;
import java.util.InputMismatchException;
import java.util.Scanner;
public class scan {
public static void main(String[] argv){
Scanner sc= new Scanner(System.in);
System.out.print("정수를 입력해주시게. >> ");
try {
int number = sc.nextInt();
if ( number % 2 == 0 )
System.out.println("짝");
else if ( number % 2 != 0 )
System.out.println("홀");
}
catch (InputMismatchException e){
System.out.println("수를 입력하지 않아 프로그램을 종료합니다.");
}
finally{
sc.close();
}
}
}

─────────────────────────────────────────────
─────────────────────────────────────────────
5. 정수를 10개 입력받아 배열에 저장하고 증가 순으로 정렬하여 출력하라.

─────────────────────────────────────────────

package project;
import java.util.Scanner;
public class hun {
public static void main(String[] argv){
try (Scanner sc = new Scanner(System.in)) {
int[] arr = new int[10];
int i, j, temp;

System.out.print("정수 10개 입력>>");

for(i = 0; i < 10; i++)


arr[i] = sc.nextInt();

for(i = 0; i < 9; i++){


for(j = i+1; j < 10; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

for(i = 0; i < 10; i++)


System.out.print(arr[i] + " ");
}
}
}

─────────────────────────────────────────────

아래 사진
─────────────────────────────────────────────
7. 1부터 99까지, 369게임에서 박수를 쳐야 하는 경우, 순서대로 화면에 출력하라. 2장 실습
문제 9를 참고하라.

─────────────────────────────────────────────

package project;
import java.util.Scanner;
public class hun {
public static void main(String[] argv){
try (Scanner sc = new Scanner(System.in)) {
}
int first, second;
for(int i = 1; i <= 99; i++){
first = i/10;
second = i%10;
if(first %3 == 0 && first !=0){
if(first != 0 && second % 3 != 0)
System.out.println(i + " 박수 한 번");
else if(first != 0 && second % 3==0)
if(second !=0)
System.out.println(i + " 박수 두 번");
else
System.out.println(i + " 박수 한 번");
}
else if(second % 3 == 0 && second != 0){
if(second != 0)
System.out.println(i + " 박수 한 번");
else
System.out.println(i + " 박수 없음");
}
else //if(first % 3 != 0 && second %3 != 0)
System.out.println(i + " 박수 없음");
}
}
}

─────────────────────────────────────────────

아래 사진
─────────────────────────────────────────────
4장

1.아래 실행 결과와 같이 출력하는 다음 main()을 가진 Song 클래스를 작성하라. Song 클래스는 노래


제목 title 필드, 생성자, getTitle() 메소드로 구성된다.

────────────────────────────────────────────

package project;
public class Song {
private String songname;
public Song(String songname){
this.songname = songname;
}
private String getTitle(){
return songname;
}
public static void main(String[] argv){
Song mySong = new Song(" Adam Lambert - Better Than I Know Myself ");
Song yourSong = new Song(" Final Song (다시, 노래…) · M.C The Max ");
System.out.println(" 내가지금 듣고있는 노래는 " + mySong.getTitle());
System.out.println(" 너는지금 무슨노래를 듣고있니? " + yourSong.getTitle());
}
}
───────────────────────────────────────────

─────────────────────────────────────────────
3.사각형을 표현하는 다음 Rect 클래스를 활용하여, Rect 객체 배열을 생성하고, 사용자로부터 4개의
사각형을 입력받아 배열에 저장한 뒤, 배열을 검색하여 사각형 면적의 합을 출력하는 main() 메소드를
가진 RectArray 클래스를 작성하라.

─────────────────────────────────────────────

package project;
import java.util.Scanner;
public class Rect {
private int width, height;
public Rect(int width, int height){
this.width = width;
this.height = height;
}
public int getArea(){return width*height;}
public static void main(String[] argv){
try (Scanner sc = new Scanner(System.in)) {
Rect[] r = new Rect[4];
int sum = 0;

for(int i = 0; i < 4; i++){


System.out.print((i+1) + " 너비와 높이 >>");
r[i] = new Rect(sc.nextInt(),sc.nextInt());
sum += r[i].getArea();
}
System.out.println("저장하였습니다..");

System.out.println("사각형의 전체 합은 " + sum);


}
}
}

─────────────────────────────────────────────

아래 사진
─────────────────────────────────────────────

You might also like