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

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

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

1.

class Rectangle {
	int w;
	int h;
	int area() { return w*h; }
	int perimeter() { return 2*(w+h); }
}
public class RectangleTest {
    public static void main(String[] args) {
        Rectangle myRect;
        myRect = new Rectangle();
        myRect.w = 10;
        myRect.h = 20;
        System.out.println("면적은 " + myRect.area());
    }
}

 

 

2.

class Date {
	int year;
	int month;
	int day;

	void print1() {
		System.out.println(year + "." + month + "." + day);
	}
}

public class DateTest {
	public static void main(String[] args) {
		Date d;
		d = new Date();
		d.year = 2012;
		d.month = 9;
		d.day = 5;
		d.print1();
	}
}

 

3.

class ComplexNumber {
	int real;
	int imag;

	void print() {
		System.out.println(real + "+ i" + imag);
	}
}

public class ComplexNumberTest {
	public static void main(String[] args) {
		ComplexNumber c;
		c = new ComplexNumber();
		c.real = 10;
		c.imag = 20;
		c.print();
	}
}

 

4.

lass Movie {
	int year;
	String title;

	void print() {
		System.out.println(year + ": " + title);
	}
}

public class MovieTest {
	public static void main(String[] args) {
		Movie m;
		m = new Movie();
		m.year = 2012;
		m.title = "Total Recall";
		m.print();
	}
}

 

5.

import java.util.Scanner;

public class StringTest {
	public static void main(String[] args) {
		String s;
		Scanner sc = new Scanner(System.in);
		System.out.println("문자열을 입력하시오: ");
		s = sc.next();
		for(int i=0;i<s.length();i++)
			System.out.print(s.charAt(s.length()-1-i));
	}
}

 

6.

import java.util.*;
public class ConVo {
	public static void main(String[] args){
		String s;
		char s2;
		int count1 = 0 , count2 = 0;
		Scanner scan = new Scanner(System.in);
		System.out.print("문자열을 입력하세요 : ");
		s = scan.next();
		for(int i = 0; i < s.length(); i++){
			s2 = s.charAt(i);
			if((s2 >= 'A' && s2 <= 'Z') || (s2 >= 'a' && s2 <= 'z')){
				if(s2=='a' || s2=='e' || s2 == 'i' || s2 == 'o' || s2 == 'u')
					count1++;
				else
					count2++;
			}
		}
		System.out.println("자음의 개수 : " + count2);
		System.out.println("모음의 개수 : " + count1);
	}
}

 

7.

import java.util.*;
public class Password {
	public static void main(String[] args){
		String s;
		String id="abcdef";
		Scanner scan = new Scanner(System.in);
		System.out.print("아이디를 입력하세요 : ");
		s = scan.next();
		if( s.equalsIgnoreCase(id) )
			System.out.println("로그인이 성공하였습니다");
		else
			System.out.println("로그인이 실패하였습니다");
	}
}

 

 

 

댓글