"๊ฐ์ ํ: ๋ช
ํ JAVAESSENTIAL(ํฉ๊ธฐํ)"๊ต์ฌ์ ์๋ก๋ ๋ฌธ์ ๋ค์ ํ์ด์ ์ฌ๋ฆฝ๋๋ค. ๋ฌธ์ ์ ๋ฒํธ๋ง ํ๊ธฐํ๊ณ ๋ต์๋ง ์ ๋ ํํ๋ก ์ ๋ก๋ ํ๊ณ ์์ต๋๋ค. ๊ฐ์ธ ํ์ด์ด๋ฏ๋ก ์ค๋ต์ด ์์ ์ ์์ผ๋ฉฐ ์ค๋ต ๋ฐ๊ฒฌ ์ ๋๊ธ ๋จ๊ฒจ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค๐ - #์ด ํ์๋ ๊ฑด ์์ง ํ์ง ๋ชปํ ๋ฌธ์ or ํท๊ฐ๋ฆฌ๋ ๋ฌธ์ ์ ๋๋ค! ์ถํ์ ๋ค์ ํ์ด์ ์ฌ๋ฆด ์์ ์ด์์! -ujeyhx- |
๐์ค์ต๋ฌธ์
1)
class Song {
String title;
public Song(String t) {
title = t;
}
public String getTitle() {
return title;
}
}
public class TEST {
public static void main(String[] args) {
Song mySong = new Song("Nessun Dorma");
Song yourSong = new Song("๊ณต์ฃผ๋ ์ ๋ชป ์ด๋ฃจ๊ณ ");
System.out.println("๋ด ๋
ธ๋๋ " + mySong.getTitle());
System.out.println("๋์ ๋
ธ๋๋ " + yourSong.getTitle());
}
}
2)
import java.util.Scanner;
class Phone {
private String name, tel;
public Phone (String name, String tel) {
this.name = name;
this.tel = tel;
}
public String getName() {return name; }
public String getTel() {return tel;}
}
public class TEST {
public static void main(String[] args) {
Phone []num = new Phone[2];
Scanner sc = new Scanner(System.in);
for (int i=0; i<num.length;i++) {
System.out.print("์ด๋ฆ๊ณผ ์ ํ๋ฒํธ ์
๋ ฅ>> ");
String x = sc.next();
String y = sc.nextLine();
num[i]= new Phone(x,y);
}
for (int i=0; i<num.length;i++) {
System.out.println(num[i].getName() + "์ ๋ฒํธ " + num[i].getTel());
}
}
}
3)
import java.util.Scanner;
class Rect {
private int width, height;
public Rect(int width , int height) {
this.width = width; //this -> private
this.height = height;
}
public int getArea() {return width*height;}
}
public class TEST {
public static void main(String[] args) {
Rect []rect = new Rect[4];
Scanner sc = new Scanner(System.in);
for (int i=0; i<rect.length; i++) {
System.out.print( i+1 + " ๋๋น์ ๋์ด>> ");
int x = sc.nextInt(); //๋๋น
int y = sc.nextInt(); //๋์ด
rect[i]= new Rect(x,y);
}
System.out.println("์ ์ฅ๋์์ต๋๋ค...");
int sum = 0;
for (int i=0; i<rect.length; i++) {
sum += rect[i].getArea();
}
System.out.println("์ฌ๊ฐํ ์ ์ฒด ํฉ์ " + sum);
sc.close();
}
}
# 4)
//๊ฐ ์ ์ฅ ์ ๋๋ก ์๋จ , exit ์
๋ ฅํ ๋ elseif ๋ถ๋ถ๋ ๋์์ ์ถ๋ ฅ๋จ
import java.util.Scanner;
class Phone {
private String name, tel;
public Phone (String name, String tel) {
this.name = name;
this.tel = tel;
}
public String getName() {return name; }
public String getTel() {return tel;}
}
public class TEST {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("์ธ์์>> ");
int nmbrPpl = 0;
nmbrPpl = sc.nextInt();
Phone []num = new Phone[nmbrPpl];
for (int i=0; i<num.length;i++) {
System.out.print("์ด๋ฆ๊ณผ ์ ํ๋ฒํธ(๋ฒํธ๋ ์ฐ์์ ์ผ๋ก ์
๋ ฅ)>> ");
String x = sc.next();
String y = sc.nextLine();
num[i]= new Phone(x,y);
}
System.out.println("์ ์ฅ๋์์ต๋๋ค...");
while (true) {
System.out.print("๊ฒ์ํ ์ด๋ฆ>>");
String name = sc.nextLine();
for (int i=0; i<num.length;i++) {
String [] name1 = new String[num.length];
name1[i] = num[i].getName();
if(name.equals(name1[i])) {
System.out.println(name1[i]+ "์ ๋ฒํธ๋ "+ num[i].getTel());
break;
}
else if (!(name.equals(name1[i]))) {
System.out.println(name + "์ด ์์ต๋๋ค");
break;
}
if(name.equals("exit")) {
System.out.print("ํ๋ก๊ทธ๋จ ์ข
๋ฃํฉ๋๋ค...");
break;
}
}
}
}
}
5)
class Circle {
private int radius;
public Circle(int radius) {this.radius = radius;}
public int getRadius() {return this.radius;}
public void setRadius (int radius) { this.radius = radius;}
}
class CircleManager{
static void copy(Circle src, Circle dest) {
dest.setRadius(src.getRadius());
}
static boolean equals(Circle a, Circle b) {
if (a.getRadius()==b.getRadius()) {
return true;
}
else {
return false;
}
}
}
public class TEST{
public static void main(String[] args) {
Circle pizza = new Circle(5);
Circle waffle =new Circle(1);
boolean res = CircleManager.equals(pizza, waffle);
if (res == true)
System.out.println("pizza์ waffle ํฌ๊ธฐ ๊ฐ์");
else
System.out.println("pizza์ waffle ํฌ๊ธฐ ๋ค๋ฆ");
CircleManager.copy(pizza, waffle);
res = CircleManager.equals(pizza, waffle);
if(res==true)
System.out.println("pizza์ waffle ํฌ๊ธฐ ๊ฐ์");
else
System.out.println("pizza์ waffle ํฌ๊ธฐ ๋ค๋ฆ");
}
}
6)
public class TEST{
private int width, height;
private char fillchar;
public TEST() {
this(10,1);
}
public TEST(int width, int height) {
this.width = width;
this.height = height;
}
public void draw() {
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
System.out.print(fillchar);
}
System.out.println();
}
}
public void fill(char c) {
fillchar = c;
}
public static void main(String[] args) {
TEST a = new TEST();
TEST b = new TEST(20,3);
a.fill('*');
b.fill('%');
a.draw();
b.draw();
}
}
Bonus 1)
import java.util.Scanner;
class Player{
private String name;
public Player (String name) { this.name = name; }
public String getName() { return name;}
}
public class TEST{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Player [] p = new Player[2];
for (int i = 0; i<p.length; i++) {
System.out.print("์ ์์ด๋ฆ์
๋ ฅ >>");
p[i] = new Player(sc.next());
}
int n = 0;
while(true) {
System.out.print(p[n].getName() + "<Enter ์ธ์ ์๋ฌดํค๋ ์น์ธ์>");
sc.next();
int [] val = new int [3];
for(int i=0; i<val.length; i++) {
val[i] = (int)(Math.random()*3);
System.out.print(val[i] + "\t");
}
System.out.println();
if(val[0] == val[1] && val[1] == val[2]) {
System.out.print(p[n].getName() + "์น๋ฆฌํ์์ต๋๋ค.");
break;
}
n++;
n = n%2;
}
sc.close();
}
}