Last active
February 26, 2025 01:54
-
-
Save dnkm/8a5d13c4f4573f9a7915f0eb6e2b3722 to your computer and use it in GitHub Desktop.
Katherine - private class - 2025 Feb 25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bank { | |
private int balance; | |
public Bank(int initBalance) { | |
balance = initBalance; | |
} | |
public void add(int amt) { | |
if (amt <= 0) { | |
System.out.println("huh?"); | |
balance -= 1; | |
} else { | |
balance += amt; | |
} | |
} | |
public void remove(int amt) { | |
if (amt < 0) { | |
System.out.println("?"); | |
} else if (amt > balance) { | |
System.out.println("!"); | |
} else { | |
balance -= amt; | |
} | |
} | |
public int getBalance() { | |
return balance; | |
} | |
public static void main(String[] args) { | |
Bank b = new Bank(10); // { balance: 10 } | |
b.add(10); // {balance: 20} | |
b.add(-1); // "huh" 라고 에러나고 -1불 차감 {balance: 19} | |
b.remove(20); // error due to insufficient balance | |
System.out.println(b.getBalance()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
private int age; // instance var | |
private String name; | |
// constructor | |
public Person(String name, int age) { | |
this.age = age; | |
this.name = name; | |
System.out.println("누가 태어남"); | |
} | |
// setter methods | |
public void setAge(int age) { | |
this.age = age; | |
} | |
// getter methods | |
public int getAge() { | |
return age; | |
} | |
public String getName() { | |
return name; | |
} | |
// main method can also be in a different class | |
public static void main(String[] args) { | |
Person p = new Person("James", 10); | |
p.setAge(20); | |
System.out.println(p.getName() + " is " + p.getAge() + " years old"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class test { | |
public static void main(String[] args) { | |
Team t = new Team(10, 4); | |
t.win(); | |
t.win(2); | |
t.lose(); | |
System.out.println(t.getWinningPercentage()); | |
} | |
} | |
class Team { | |
int w; | |
int l; | |
Team() { | |
// 아무 것도 안하는 "empty constructor" | |
} | |
Team(int w, int l) { | |
this.w = w; | |
this.l = l; | |
// Overloaded constructor | |
} | |
void win() { | |
w += 1; | |
} | |
void win(int streak) { | |
w += streak; | |
} | |
void lose() { | |
l -= 1; | |
} | |
void lose(int streak) { | |
l += streak; | |
} | |
double getWinningPercentage() { | |
double wp = (double) w / (w + l); | |
return wp * 100; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment