TriangleMain.java
package com.day03;
import java.util.Scanner;
class Triangle{
double bottom,height;
Triangle(){}
public Triangle(double bottom, double height){
this.bottom=bottom;
this.height=height;
}
public double getArea() {
return (double)bottom*height/2;
}
public void setBottom(double bottom) {
this.bottom=bottom;
}
public void setHeight(double height) {
this.height=height;
}
}
public class TriangleMain {
public static void main(String[] args) {
//Triangle tr1=new Triangle(10.2, 17.3);
//System.out.println("삼각형의넓이:"+tr1.getArea());
//tr1.setBottom(7.5);
//tr1.setHeight(11.2);
//System.out.println("삼각형의넓이:"+tr1.getArea());
//밑변과 높이를 입력받아 삼격형의 넓이 구하기
Scanner sc = new Scanner(System.in);
System.out.println("밑변과 높이를 입력하시오.>");
double bottom = sc.nextDouble();
double height = sc.nextDouble();
Triangle tr = new Triangle(bottom,height);
System.out.println("삼각형의넓이:"+tr.getArea());
}
}
TVMain.java
package com.day03;
public class TVMain {
public static void main(String[] args) {
TV tv1 = new TV("LG",2021,65);
tv1.show(); //LG에서 만든 2021 년도형 65인치 TV 이렇게 출력
TV tv2 = new TV("삼성",2020);
tv2.show();
TV tv3 = new TV("LG",2022,55);
tv3.show();
}
}
TV.java
package com.day03;
public class TV {
String maker;
int year,inch;
public TV(String maker, int year, int inch) {
this.maker=maker;
this.year=year;
this.inch=inch;
}
public TV(String maker, int year) {
this.maker=maker;
this.year=year;
}
public void show() {
if(inch==0)
System.out.println(maker+"에서 만든"+year+" 년도형 TV");
else
System.out.println(maker+"에서 만든"+year+" 년도형 "+inch+"인치 TV");
// int num=0; //지역변수는 반드시!! 초기값 있어야 함
// System.out.println(num);
}
}
Account.java
package com.day03.cooperation;
public class Account {
private String name;
private int money;
public Account(String name, int money) {
this.name=name;
this.money=money;
}
public void inputMoney(int money) {
this.money+=money;
}
public void outputMoney(int money) {
//this.money-=money;
if(this.money < money) {
System.out.println(name+" 님 잔액부족");
return; //종료
}
else {
this.money-=money;
}
}
public void getMoney() {
System.out.println(name+"님의 잔액은 "+money+" 입니다.");
}
// public static void main(String[] args) {
// Account ac1 = new Account("홍길동",6000);
// ac1.inputMoney(5000);
// ac1.outputMoney(17000);
// ac1.getMoney(); //홍길동님의 잔액은 -2000원 입니다.
// System.out.println(ac1.name);
//
// }
//getter
public String getName() {
return name;
}
public int get_Money() { //getMoney는 이미 있어서 get_Money로 씀
return money;
}
//setter
public void setMoney(int money) {
this.money=money;
}
}
AccountMain.java
package com.day03.cooperation;
public class AccountMain {
public static void main(String[] args) {
Account ac1 = new Account("홍길동",6000);
ac1.inputMoney(5000);
ac1.outputMoney(17000);
ac1.getMoney(); //홍길동님의 잔액은 -2000원 입니다.
//System.out.println(ac1.name);
//getter
System.out.println(ac1.getName());
System.out.println(ac1.get_Money());
//setter
ac1.setMoney(9999);
System.out.println(ac1.get_Money());
}
}