09009
[Java ์ฃผ์ ๋ฌธ์ ํ์ด (1)] - ๋ฐฐ์ด, ์์ฑ์, ๋ฉ์๋ ๋ณธ๋ฌธ
๐ ๋ฌธ์
๋ฐ๋ณ๊ณผ ๋์ด ์ ๋ณด๋ฅผ ์ ์ฅํ ์ ์๋ Triangle ํด๋์ค๋ฅผ ์ ์ํ์.
๊ทธ๋ฆฌ๊ณ ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ๊ฐ ๊ฐ๋ฅํ ์์ฑ์๋ ์ ์ํ์. ๋์ผ๋ก ๋ฐ๋ณ๊ณผ ๋์ด ์ ๋ณด๋ฅผ ๋ณ๊ฒฝ์ํฌ ์ ์๋ ๋ฉ์๋์ ์ผ๊ฐํ์ ๋์ด๋ฅผ ๊ณ์ฐํด์ ๋ฐํํ๋ ๋ฉ์๋๋ ํจ๊ป ์ ์ํ์. (๋ฉ์๋์ ๋ฐํํ๊ณผ ๋งค๊ฐ๋ณ์์ ์๋ฃํ์ double๋ก ์ ์ธ)
// ๋ฐ๋ณ๊ณผ ๋์ด ์ ๋ณด๋ฅผ ์ ์ฅํ ์ ์๋ Triangle ํด๋์ค๋ฅผ ์ ์ํ์. ๊ทธ๋ฆฌ๊ณ ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ๊ฐ ๊ฐ๋ฅํ ์์ฑ์๋ ์ ์ํ์.
// ๋์ผ๋ก ๋ฐ๋ณ๊ณผ ๋์ด ์ ๋ณด๋ฅผ ๋ณ๊ฒฝ์ํฌ ์ ์๋ ๋ฉ์๋์ ์ผ๊ฐํ์ ๋์ด๋ฅผ ๊ณ์ฐํด์ ๋ฐํํ๋ ๋ฉ์๋๋ ํจ๊ป ์ ์ํ์.
class Triangle {
double bottom, height;
// public์ผ๋ก ์ ๊ทผ์ ํ์๋ฅผ ์ง์ ํด์ค์ผ ์ธ๋ถ์์๋ ์์ฑ์๋ฅผ ๋ถ๋ฌ์ฌ ์ ์๋ค.
public Triangle() {}
public Triangle(double bottom, double height) {
this.bottom = bottom;
this.height = height;
}
// public void setBottom(double bottom) {
// this.bottom = bottom;
// }
//
// public void setHeight(double height) {
// this.height = height;
// }
public double getArea() {
return bottom * height / 2;
}
}
public class TriangleText {
public static void main(String[] args) {
Triangle tr = new Triangle(10.2, 17.3);
System.out.println("์ผ๊ฐํ์ ๋์ด : " + tr.getArea());
}
}
โ ๋ต์ ์ฝ๋
class Triangle {
private double bottom;
private double height;
public Triangle(double bottom, double height) {
// super();
this.bottom = bottom;
this.height = height;
}
public void setBottom(double bottom) {
if(bottom <= 0) {
bottom = 1;
return;
}
this.bottom = bottom;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return bottom * height / 2;
}
}
public class TriangleTest {
public static void main(String[] args) {
Triangle tr = new Triangle(10.2, 17.3);
tr.setBottom(5);
tr.setHeight(6);
System.out.println("์ผ๊ฐํ์ ๋์ด : " + tr.getArea());
}
}
๐ ๋ฌธ์
์ ํด๋์ค๋ฅผ ๋ฐํ์ผ๋ก ์์ ์๋ฏธํ๋ Circleํด๋์ค๋ฅผ ์ ์ํ์ฌ๋ผ.
์ค์ฌ์ ์ ์ขํ์ ๋ฐ์ง๋ฆ(int)์ ๊ธธ์ด ์ ๋ณด๋ฅผ ์ ์ฅํ์ฌ๋ผ.
์ ๋๊ฐ๋ก ์ด๋ฃจ์ด์ง Ring ํด๋์ค๋ฅผ ์ ์ํ์ฌ๋ผ.
class Point
{
int xPos, yPos;
public Point(int x, int y)
{
xPos=x;
yPos=y;
}
public void showPointInfo() {System.out.println("["+xPos+","+yPos+"]");}
}
public class RingTest {
public static void main(String[] args) {
Ring ring=new Ring(1,1,4,2,2,9); //Inner Circle x์ขํ, y์ขํ, ๋ฐ์ง๋ฆ, Outter Circle x์ขํ, y์ขํ, ๋ฐ์ง๋ฆ
ring.showRingInfo();
}
}
โ ๋ต์ ์ฝ๋
class Point {
int xPos, yPos;
public Point(int x, int y) {
xPos=x;
yPos=y;
}
public void showPointInfo() {
System.out.println("["+xPos+","+yPos+"]");
}
}
class Circle {
Point p;
int r;
public Circle(int x, int y, int r) {
p = new Point(x, y); this.r = r;
}
public void showPointInfo() {
System.out.println("radius : " + r);
p.showPointInfo();
}
}
class Ring {
Circle c1, c2;
public Ring(int x1, int y1, int r1, int x2, int y2, int r2) {
c1 = new Circle(x1, y1, r1);
c2 = new Circle(x2, y2, r2);
}
public void showRingInfo() {
System.out.println("Inner Circle Info...");
c1.showPointInfo();
System.out.println("Outer Circle Info...");
c2.showPointInfo();
}
}
public class RingTest {
public static void main(String[] args) {
Ring ring = new Ring(1, 1, 4, 2, 2, 9); //Inner Circle x์ขํ, y์ขํ, ๋ฐ์ง๋ฆ, Outter Circle x์ขํ, y์ขํ, ๋ฐ์ง๋ฆ
ring.showRingInfo();
}
}
๐ ๋ฌธ์
SimpleNumber ํด๋์ค๋ ์์ฑ์๊ฐ private, ์ฆ ์ธ๋ถ์์ ์์ฑํ์ง ๋ชปํ๋ค. ํด๋์ค ์์์๋ ๊ฐ๋ฅ.
์์ ๋ฅผ ๋ณ๊ฒฝํ์ฌ num1, num2๊ฐ ๋์ผํ ์ธ์คํด์ค๊ฐ ๋๊ฒ ๋ณ๊ฒฝ. SimpleNumber์ ์ธ์คํด์ค๊ฐ ํ๋๋ง ์์ฑ๋๊ฒ ์ ํ.
class SimpleNumber {
int num = 0;
private SimpleNumber() {}
public void addNum(int n) { num += n;}
public void showNum() {
System.out.println(num);
}
// ํ๋์ static์ด ๋ถ์ผ๋ฉด
public static SimpleNumber getSimpleNumberInst() {
return new SimpleNumber();
}
}
public class OnlyOneInstance {
public static void main(String[] args) {
SimpleNumber num1 = SimpleNumber.getSimpleNumberInst();
num1.addNum(20);
SimpleNumber num2 = SimpleNumber.getSimpleNumberInst();
num2.addNum(30);
num1.showNum(); // 20 -> 50
num2.showNum(); // 30 -> 50
}
}
// SimpleNumber ํด๋์ค๋ ์์ฑ์๊ฐ private, ์ฆ ์ธ๋ถ์์ ์์ฑ๋ชปํจ. ํด๋์ค ์์์๋ ๊ฐ๋ฅ.
//์์ ๋ฅผ ๋ณ๊ฒฝํ์ฌ num1, num2๊ฐ ๋์ผํ ์ธ์คํด์ค๊ฐ ๋๊ฒ ๋ณ๊ฒฝ. SimpleNumber์ ์ธ์คํด์ค๊ฐ ํ๋๋ง ์์ฑ๋๊ฒ ์ ํ.
โ ๋ต์ ์ฝ๋ 1)
class SimpleNumber {
int num = 0;
private SimpleNumber() {}
public void addNum(int n) { num += n;}
public void showNum() {
System.out.println(num);
}
private static SimpleNumber instance = new SimpleNumber();
public static SimpleNumber getInstance() {
return instance;
}
}
public class OnlyOneInstance {
public static void main(String[] args) {
SimpleNumber num1 = SimpleNumber.getInstance();
num1.addNum(20);
SimpleNumber num2 = SimpleNumber.getInstance();
num2.addNum(30);
// SimpleNumber num3 = SimpleNumber.getInstance();
// num3.addNum(80);
num1.showNum(); // 20 -> 50
num2.showNum(); // 30 -> 50
// num3.showNum();
}
}
2)
class SimpleNumber
{
int num=0;
private SimpleNumber() {}
public void addNum(int n) {num+=n;}
public void showNum() {System.out.println(num);}
public static SimpleNumber snInst = null;
public static SimpleNumber getSimpleNumberInst()
{
if(snInst==null)
snInst=new SimpleNumber();
return snInst;
}
}
public class OnlyOneInstance {
public static void main(String[] args) {
SimpleNumber num1=SimpleNumber.getSimpleNumberInst();
num1.addNum(20);
SimpleNumber num2=SimpleNumber.getSimpleNumberInst();
num2.addNum(30);
num1.showNum();
num2.showNum();
}
}
3)
class SimpleNumber {
int num = 0;
private SimpleNumber() {}
public void addNum(int n) { num += n;}
public void showNum() {
System.out.println(num);
}
private static SimpleNumber instance = new SimpleNumber();
// ํ๋์ static์ด ๋ถ์ผ๋ฉด
public static SimpleNumber getSimpleNumberInst() {
return instance;
}
}
public class OnlyOneInstance {
public static void main(String[] args) {
SimpleNumber num1 = SimpleNumber.getSimpleNumberInst();
num1.addNum(20);
SimpleNumber num2 = SimpleNumber.getSimpleNumberInst();
num2.addNum(30);
num1.showNum(); // 20 -> 50
num2.showNum(); // 30 -> 50
}
}
๐ ๋ฌธ์
String str="990208-1012752";
StringBuilder๋ฅผ ํ์ฉํ์ฌ - ๋ฅผ ์ญ์ ํ String ์ธ์คํด์ค ์์ฑํ๋ผ.
public class RemoveBar {
public static void main(String[] args) {
String str="990208-1012752";
System.out.println(str);
}
}
โ ๋ต์ ์ฝ๋ 1)
public class RemoveBar {
public static void main(String[] args) {
String str = "990208-1012752";
StringBuilder sbuf = new StringBuilder(str);
for(int i = 0; i < sbuf.length(); i++) {
if (sbuf.charAt(i) == '-') {
sbuf.deleteCharAt(i);
break;
}
}
str = sbuf.toString();
System.out.println(str);
}
}
๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ํ์ธํด์ผํ๋ฏ๋ก length๊ฐ ์๋๋ผ length()๋ฅผ ์ฌ์ฉํ๋ค.
* deleteCharAt๋ StringBuilder ํด๋์ค์ ๋ด์ฅ๋ ๋ฉ์๋์ด๋ค.
2)
public class RemoveBar {
public static void main(String[] args) {
String str = "990208-1012752";
StringBuilder sbuf = new StringBuilder(str);
int idx = sbuf.lastIndexOf("-");
if (idx != -1) sbuf.deleteCharAt(idx);
str = sbuf.toString();
System.out.println(str);
}
}
๐ ๋ฌธ์
intํ 1์ฐจ์ ๋ฐฐ์ด์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ฐ์ ์ต๋๊ฐ, ์ต์๊ฐ์ ๊ตฌํ์ฌ ๋ฐํํ๋ ๋ฉ์๋ ๊ตฌํํ๊ธฐ
import java.util.Scanner;
public class ArrayMinMax {
public static int maxValue(int[] arr)
{
}
public static int minValue(int[] arr)
{
}
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int[] intArr=new int[7];
for(int i=0; i<intArr.length; i++)
{
System.out.print("์ ์ ์
๋ ฅ : ");
intArr[i]=keyboard.nextInt();
}
System.out.println("์ต์๊ฐ : "+minValue(intArr));
System.out.println("์ต๋๊ฐ : "+maxValue(intArr));
}
}
โ ๋ต์ ์ฝ๋
// intํ 1์ฐจ์ ๋ฐฐ์ด์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ฐ์ ์ต๋๊ฐ, ์ต์๊ฐ์ ๊ตฌํ์ฌ ๋ฐํํ๋ ๋ฉ์๋ ๊ตฌํํ๊ธฐ
import java.util.Scanner;
public class ArrayMinMax {
public static int maxValue(int[] arr) {
int max = arr[0];
for(int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int minValue(int[] arr) {
int min = arr[0];
for(int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] intArr = new int[7];
for(int i=0; i<intArr.length; i++)
{
System.out.print("์ ์ ์
๋ ฅ : ");
intArr[i]=keyboard.nextInt();
}
System.out.println("์ต์๊ฐ : "+ minValue(intArr));
System.out.println("์ต๋๊ฐ : "+ maxValue(intArr));
}
}
๐ ๋ฌธ์
addOneDArr ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ 2์ฐจ์ ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ ๋ ฅ๊ฐ๋งํผ ์ฆ๊ฐ์ํค๋ addTwoDArrํจ์๋ฅผ ์์ฑํ๋ผ.
public class TwoDimensionalArrayAdder {
public static void addOneDArr(int[] arr, int add)
{
for(int i=0; i<arr.length; i++)
arr[i]+=add;
}
public static void addTwoDArr(int[][] arr, int add)
{
}
public static void main(String[] args) {
int[][] arr= {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
addTwoDArr(arr, 2);
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
}
}
}
โ ๋ต์ ์ฝ๋
public class TwoDimensionalArrayAdder {
public static void addOneDArr(int[] arr, int add) {
for(int i=0; i<arr.length; i++)
arr[i]+=add;
}
public static void addTwoDArr(int[][] arr, int add) {
// for(int i = 0 ; i < arr.length; i++) {
// for(int j = 0; j < arr[i].length; j++){
// arr[i][j] += add;
// }
// }
for(int i = 0; i < arr.length; i++) addOneDArr(arr[i],2);
}
public static void main(String[] args) {
int[][] arr= {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
addTwoDArr(arr, 2);
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
}
}
}
๐ ๋ฌธ์
1 2 3
4 5 6
7 8 9
7 8 9
1 2 3
4 5 6
public class ShiftArray {
public static void shiftTwoDArr(int[][] arr)
{
}
public static void showTwoDArr(int[][] arr)
{
}
public static void main(String[] args) {
int[][] arr= {
{1,2,3},
{4,5,6},
{7,8,9}
};
System.out.println("1์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
System.out.println("2์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
System.out.println("3์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
}
}
โ ๋ต์ ์ฝ๋
public class ShiftArray {
public static void shiftTwoDArr(int[][] arr) {
int[] temp = arr[arr.length-1]; // ๋ง์ง๋ง ๋ฐฐ์ด์ ๋ํ ์ฃผ์ ์ ๋ณด ๋ด๊ธฐ
for(int i = arr.length-1; i > 0; i--) {
arr[i] = arr[i-1];
}
arr[0] = temp;
}
public static void showTwoDArr(int[][] arr) {
for(int i = 0 ; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] arr= {
{1,2,3},
{4,5,6},
{7,8,9}
};
System.out.println("1์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
System.out.println("2์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
System.out.println("3์ฐจ shift...");
shiftTwoDArr(arr);
showTwoDArr(arr);
}
}
๐ ๋ฌธ์
๋คํ์ฑ์ผ๋ก ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์๊ฒ ๊ตฌํํ์์ค.
์ ๋ ฅ
class Box
{
public void simpleWrap(){System.out.println("simple wrap");}
}
class PaperBox extends Box
{
public void paperWrap() {System.out.println("paper wrap");}
}
class GoldPaperBox extends PaperBox
{
public void goldWrap() {System.out.println("gold wrap");}
}
class InstanceOf
{
public static void wrapBox(Box box)
{
if(box instanceof GoldPaperBox)
((GoldPaperBox)box).goldWrap();
else if(box instanceof PaperBox)
((PaperBox)box).paperWrap();
else
box.simpleWrap();
}
public static void main(String[] args)
{
Box box1=new Box();
PaperBox box2=new PaperBox();
GoldPaperBox box3=new GoldPaperBox();
wrapBox(box1);
wrapBox(box2);
wrapBox(box3);
}
}
์ถ๋ ฅ
simple wrap
paper wrap
gold wrap
โ ๋ต์ ์ฝ๋
class Box {
public void Wrap() {
System.out.println("simple wrap");
}
}
class PaperBox extends Box {
public void Wrap() {
System.out.println("paper wrap");
}
}
class GoldPaperBox extends PaperBox {
public void Wrap() {
System.out.println("gold wrap");
}
}
class InstanceOf {
public static void wrapBox(Box box) {
box.Wrap();
}
public static void main(String[] args) {
Box box1=new Box();
PaperBox box2=new PaperBox();
GoldPaperBox box3=new GoldPaperBox();
wrapBox(box1);
wrapBox(box2);
wrapBox(box3);
}
}
๐ ๋ฌธ์
ManageStudent ํด๋์ค ์์ฑ mainํจ์ ํฌํจ
๋ฐ๋ก Student ํด๋์ค๋ฅผ ๋ง๋ค๊ณ name, address, phone, email ํ๋๋ฅผ ์ถ๊ฐ.
๋ค ํ๋๋ฅผ ์
๋ ฅ๊ฐ์ผ๋ก ์์ฑ์ ์ถ๊ฐ
๋ค ํ๋์ ๊ฐ์ด ๊ฐ์ผ๋ฉด ๊ฐ์ ๊ฐ์ฒด๋ก ์ธ์๋๋๋ก equals ๋ฉ์๋ ์ถ๊ฐ.
ManageStudent ํด๋์ค์ main์์
Student a=new tudent("Min","Seoul","010XXXXXXXXX","ask@godofjava.com");
Student b=new tudent("Min","Seoul","010XXXXXXXXX","ask@godofjava.com");
๊ฐ์ฒด ์์ฑํ ์
๋ ฅํ์ฌ Equal ๋ค๋ฅด๋ฉด Not Equal ์ ์ถ๋ ฅํ๋๋ก ํ
์คํธ.
โ ์ ๋ต ์ฝ๋
package exercises;
class Student {
String name, address, phone, email;
public Student(String name,String address,String phone,String email) {
this.name = name; this.address = address; this.phone = phone; this.email = email;
}
@Override
public boolean equals(Object obj) {
if (name == (((Student)obj).name) && address == (((Student)obj).address)
&& phone == (((Student)obj).phone) && email == (((Student)obj).email)) {
return true;
} else return false;
}
}
public class ManageStudent {
public static void main(String[] args) {
Student a = new Student("Min", "Seoul", "010XXXXXXXXX", "ask@godofjava.com");
Student b = new Student("Min", "Seoul", "010XXXXXXXXX", "ask@godofjava.com");
if (a.equals(b)) System.out.println("equal");
else System.out.println("not equal");
}
}
๐ป ์ถ๋ ฅ
equal
๐ ๋ฌธ์
์ฌ์ฉ์๋ก ๋ถํฐ ๋๊ฐ์ ์ ์๋ฅผ ์
๋ ฅ๋ฐ์ ๋ ์๋ฅผ ํฌํจํ์ฌ ๊ทธ ์ฌ์ด์ ์ ์ 100๊ฐ๋ฅผ ์์๋ก ์ถ๋ ฅํ๋
๋ฉ์๋๋ฅผ ๋ง๋ค๊ณ ํ
์คํธํ๊ธฐ.
import java.util.Random;
import java.util.Scanner;
public class Problem11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("๋ ๊ฐ์ ์ ์๋ฅผ ์
๋ ฅํ์ธ์");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
int length = num1 - num2 + 1;
Random rand = new Random();
for (int i = 0; i < 100; i++) {
System.out.print("์ถ๋ ฅ" + i + ":");
System.out.println(rand.nextInt(length) + num2);
}
}
}
'Back-End > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] compareTo(), compare() (0) | 2023.04.07 |
---|---|
[Java] ๋ฌธ์์ด(String) ๋น๊ต : equals()์ == ์ ์ฐจ์ด์ , String ํด๋์ค (6) | 2023.04.06 |
[Java] StringTokenizer (0) | 2023.04.06 |
[Java] ์์ธ์ฒ๋ฆฌ (0) | 2023.04.06 |
[Java] ์ถ์ํด๋์ค (0) | 2023.04.06 |