ํด๋์ค ๋ฌธ์ ํ๊ธฐ3
2023. 1. 30. 12:34ใjava
1~3๋ฒ Member
package class3;
public class Class3_1_2 {
private String name;
private String id;
private String pw;
public Class3_1_2() { //์์ฑ์
name = "";
id = " ";
pw = " ";
}
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getPw() {
return pw;
}
public void disPlayInfo() {
System.out.println("์ด๋ฆ : " + name);
System.out.println("์์ด๋ : " + id);
System.out.println("๋น๋ฐ๋ฒํธ : " + pw);
}
}
package class3;
public class Class3_3 {
public static void main(String[] args) {
Class3_1_2 c = new Class3_1_2();
c.setName("์๋ฐ");
c.setId("java");
c.setPw("123");
c.disPlayInfo();
}
}
4~5๋ฒ Member
package class3;
//ํด๋์ค๋ฌธ์ 3_4~5
public class Member {
private String name;
private String id;
private String password;
private int age;
public Member(String name, String id) {
this.name = name;
this.id = id;
}
}
6๋ฒ
//ํด๋์ค3_6
package class3;
public class MemberService {
public boolean login(String id, String password) {
if(id.equals("hong") && password.equals("12345")){
return true;
} else {
return false;
}
}
public void logout(String id){
System.out.println("๋ก๊ทธ์์ ๋์์ต๋๋ค.");
}
}
package class3;
public class MemberServiceExample {
public static void main(String[] args) {
MemberService memberService = new MemberService();
boolean result = memberService.login("hong","12345");
if(result) { //result์ true๊ฐ ์ ์ฅ
System.out.println("๋ก๊ทธ์ธ ๋์์ต๋๋ค.");
memberService.logout("hong");
}
else {
System.out.println("id ๋๋ password๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.");
}
}
}
7๋ฒ
package class3_7;
public class Add {
private int a;
private int b;
public void setValue(int a, int b){
this.a = a;
this.b = b;
}
public int calculate(){
return a+b;
}
}
package class3_7;
public class Sub {
private int a;
private int b;
public void setValue(int a, int b){
this.a = a;
this.b = b;
}
public int calculate(){
return a-b;
}
}
package class3_7;
public class Mul {
private int a;
private int b;
public void setValue(int a, int b){
this.a = a;
this.b = b;
}
public int calculate(){
return a*b;
}
}
package class3_7;
public class Div {
private int a;
private int b;
public void setValue(int a, int b){
this.a = a;
this.b = b;
}
public int calculate(){
return a/b;
}
}
package class3_7;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("-์คํ์์-");
System.out.print("์ฒซ๋ฒ์งธ ์ : ");
int a = sc.nextInt();
System.out.print("๋๋ฒ์งธ ์ : ");
int b = sc.nextInt();
System.out.print("์ฐ์ฐ์ : ");
String oper = sc.next();
switch(oper){
case "+" :
Add add = new Add(); //a b๊ฐ ์
ํ
๋์ด์ผ ํจ. ๊ทธ๋์ผ ๊ฐ์ด ๋์ด.
add.setValue(a, b);
System.out.println(a + " " + oper + " " + b + " = " + add.calculate());
break;
case "-" :
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(a + " " + oper + " " + b + " = " + sub.calculate());
break;
case "*" :
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(a + " " + oper + " " + b + " = " + mul.calculate());
break;
case "/" :
Div div = new Div();
div.setValue(a, b);
System.out.println(a + " " + oper + " " + b + " = " + div.calculate());
break;
default :
System.out.println("์ฐ์ฐ์๋ฅผ ์ ํํ ์
๋ ฅํ์์ค.");
}
}
}
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํด๋์ค4 ๋ฌธ์ ํ๊ธฐ (ํ์๊ด๋ฆฌํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ) (0) | 2023.01.30 |
---|---|
ํด๋์ค์ ๋ฐฐ์ด ์ด์ฉํ๋ ๋ฌธ์ ์ฐ์ต (0) | 2023.01.30 |
์ ๊ทผ ์์ค ์ง์์ (classTest2) (0) | 2023.01.30 |
ํด๋์ค ๋ฌธ์ ํ๊ธฐ2 (0) | 2023.01.29 |
๋ฉ์๋ ๋ฌธ์ ํ๊ธฐ3(์ฌํ) (0) | 2023.01.29 |