ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Part 1 မှာ registration card တွေကို ဗီရိုထဲ စနစ်တကျ ထည့်ပြီးပါပြီ။ အခု card တစ်ကတ်ချင်းရဲ့ နောက်ကျောမှာ "Math 95၊ English 82" လို ဘာသာရပ်နဲ့ အမှတ်တွေကို ရေးပါမယ်။ ဆရာက Math အမှတ်ကို ပြန်စစ်ပြီး ပြင်ရင် အမှတ်အဟောင်းကို ခြစ်ပြီး အသစ်ရေးပါတယ်၊ ဘာသာ တစ်ခုကို နှစ်ကြောင်း မရေးပါဘူး။
နောက်ပြီး card က "ငါ့ပျမ်းမျှ ဘယ်လောက်လဲ၊ ငါ ဘယ် grade လဲ" ဆိုတာကို ကိုယ်တိုင် ဖြေနိုင်ရပါမယ်။ ဒါကြောင့် average နဲ့ grade တွက်တဲ့ method တွေကို Student class ထဲမှာပဲ ထည့်ပါတယ်။ အမှတ် တစ်ခုမှ မရှိသေးတဲ့ card ကတော့ "ကျ" မဟုတ်ဘဲ "မစစ်ရသေး" ဖြစ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: 88.33 ဖြစ်ရမယ့် average က 88 ဖြစ်နေတယ်
ပထမ draft က int total ကို size() နဲ့ စားပြီး double variable ထဲ ထည့်ပါတယ်။ Aung Aung ရဲ့ 95 + 82 + 88 = 265 ကို 3 နဲ့ စားရင် 88.33 ရရမှာပါ၊ ဒါပေမယ့် program က 88.0 လို့ ပြပါတယ်။ Threshold နားက student တစ်ယောက်အတွက်ဆိုရင် ဒီ 0.33 ကွာခြားချက်က grade ကိုတောင် ပြောင်းစေနိုင်ပါတယ်။
ဒုတိယ bug ကတော့ အမှတ်မရှိသေးတဲ့ Mya Mya အတွက်ပါ။ Map အလွတ်ဆိုရင် size() က 0 ဖြစ်လို့ 0 / 0 က ArithmeticException တက်ပြီး program ရပ်သွားပါတယ်။ "0 ပြန်ပေးလိုက်ရင် ပြီးတာပဲ" လို့ ပြင်လိုက်ရင်လည်း Mya Mya က F ရသွားပြီး မိဘတွေဆီ "ကျပါတယ်" ဆိုတဲ့ report ရောက်သွားမှာပါ။
public double calculateAverage() {
int total = 0;
for (int score : subjectScores.values()) {
total += score;
}
return total / subjectScores.size(); // int / int: 265 / 3 == 88
} // empty map: 0 / 0 throwsဘာကြောင့် return type double ဖြစ်နေတာတောင် မကယ်နိုင်တာလဲ
Java က expression ကို operand တွေရဲ့ type နဲ့ တွက်ပြီးမှ result ကို return type ဆီ ပြောင်းပါတယ်။ total နဲ့ size() နှစ်ခုလုံး int ဖြစ်လို့ division က integer division ဖြစ်ပြီး ဒဿမကို ဖြတ်ပစ်ပါတယ်။ ဒါပြီးမှ 88 ကို 88.0 အဖြစ် ချဲ့ပါတယ်။ Integer 0 နဲ့ စားတာကတော့ ArithmeticException ပါ။
Core concept: score တွေကို သိမ်း၊ average နဲ့ grade ကို လိုမှ တွက်
Part 2 ရဲ့ design ဆုံးဖြတ်ချက် အဓိကက "ဘာကို သိမ်းပြီး ဘာကို တွက်မလဲ" ပါ။ Score တွေက source data ဖြစ်လို့ map ထဲ သိမ်းပါတယ်။ Average နဲ့ grade ကတော့ score တွေကနေ ဆင်းသက်လာတဲ့ derived data ဖြစ်လို့ field အဖြစ် မသိမ်းဘဲ method ခေါ်တိုင်း ပြန်တွက်ပါတယ်။ ဒါကြောင့် score တစ်ခု ပြောင်းတိုင်း average ကို update လုပ်ဖို့ မေ့စရာ မရှိပါဘူး။
၁။ put() က ထည့်တာရော update ရော
Map မှာ key တစ်ခုအတွက် value တစ်ခုပဲ ရှိပါတယ်။ addScore("Math", 61) ကို Math ရှိပြီးသား student ပေါ်မှာ ခေါ်ရင် 45 ကို 61 နဲ့ အစားထိုးပြီး subject အရေအတွက် မပြောင်းပါဘူး။ Re-mark လုပ်တဲ့ case အတွက် ဒါက လိုချင်တဲ့ အပြုအမူပါ။
၂။ values() ကို ပေါင်းပြီး cast လုပ်ပြီးမှ စား
values() က score တွေကို Integer collection အဖြစ် ပေးပြီး for (int score : ...) က တစ်ခုချင်း unbox လုပ်ပါတယ်။ Total ကို (double) ပြောင်းပြီးမှ size() နဲ့ စားရင် floating-point division ဖြစ်ပါတယ်။
၃။ Empty case ကို အရင် ကိုင်တွယ်
Score မရှိရင် calculateAverage() က 0.0 ပြန်ပြီး getGrade() က "-" ပြန်ပါတယ်။ Average က number ပဲ ပြန်ပေးနိုင်လို့ 0.0 ကို သုံးရပေမယ့် grade ကတော့ "F" နဲ့ မရောရအောင် သီးခြား marker သုံးပါတယ်။
၄။ Threshold ကို အမြင့်ကနေ စစ်
>= 90 → A၊ >= 75 → B၊ >= 50 → C၊ ကျန်တာ F။ 88.33 က >= 75 မှာ ပထမဆုံး true ဖြစ်လို့ B ရပါတယ်။
Unmodifiable view ဆိုတာ
Collections.unmodifiableMap(subjectScores) က copy မဟုတ်ပါဘူး၊ map အစစ်ကို ကြည့်လို့ရပေမယ့် ပြင်လို့မရတဲ့ ပြတင်းပေါက်တစ်ခုပါ။ Caller က put() ခေါ်ရင် UnsupportedOperationException တက်ပါတယ်။ Score ပြောင်းချင်ရင် addScore() ကိုပဲ ဖြတ်ရလို့ Part 3 မှာ validation ထည့်တဲ့အခါ နေရာတစ်ခုတည်းမှာ ထည့်ရုံပါပဲ။
- addScore() က map ထဲ put လုပ်
- calculateAverage() က ခေါ်တိုင်း ပြန်တွက်
- getGrade() က average ကို threshold နဲ့ တိုက်
- Grade sheet က method တွေကိုပဲ ခေါ်
သိမ်းထားတဲ့ data နည်းလေ၊ မညီတော့တဲ့ data ဖြစ်ဖို့ အခွင့်အလမ်း နည်းလေပါပဲ။
အခြေခံ ပုံစံ
double average() {
if (values.isEmpty()) return 0.0;
int total = 0;
for (int v : values.values()) total += v;
return (double) total / values.size();
}
String label() {
if (values.isEmpty()) return "-";
double avg = average();
return avg >= TOP ? "A" : avg >= MID ? "B" : "C";
}Empty case ကို အရင်ကိုင်၊ cast လုပ်ပြီးမှ စား၊ ပြီးရင် threshold ကို အမြင့်ကနေ စစ်ပါ။ Derived value တွေကို field မလုပ်ဘဲ method ကနေ ပြန်တွက်ပါ။
Part 2 code: grade sheet
ဒီ example က Part 1 ရဲ့ Student၊ StudentManager၊ StudentApp ကို ဆက်သုံးပြီး "NEW in Part 2" လို့ မှတ်ထားတဲ့ အပိုင်းတွေကိုပဲ ထပ်ထည့်ထားပါတယ်။ File အပေါ်ဆုံးက import line တွေကို သတိထားပါ၊ ArrayList၊ LinkedHashMap၊ List၊ Map တို့က java.util ထဲမှာ ရှိလို့ import မလုပ်ရင် compiler က ရှာမတွေ့ဘဲ compile မဖြစ်ပါဘူး။ main ပါတဲ့ StudentApp ကိုလည်း java StudentApp.java နဲ့ run လို့ ရအောင် import တွေရဲ့ နောက်မှာ ပထမဆုံး class အဖြစ် ထားထားပါတယ်။ Ko Ko ရဲ့ Math ကို 45 ကနေ 61 ပြန်ပြင်တာ၊ Mya Mya ရဲ့ grade "-" ဖြစ်တာကို output မှာ ကြည့်ပါ။
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class StudentApp {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Student aung = new Student("Aung Aung", 101);
Student suSu = new Student("Su Su", 102);
Student koKo = new Student("Ko Ko", 103);
manager.addStudent(aung);
manager.addStudent(suSu);
manager.addStudent(koKo);
manager.addStudent(new Student("Mya Mya", 104)); // no scores yet
aung.addScore("Math", 95);
aung.addScore("English", 82);
aung.addScore("Science", 88);
suSu.addScore("Math", 72);
suSu.addScore("English", 91);
suSu.addScore("Science", 67);
koKo.addScore("Math", 45);
koKo.addScore("English", 58);
koKo.addScore("Math", 61); // re-marked: put() replaces the old 45
manager.printGradeSheet();
System.out.println("Ko Ko's scores: " + koKo.getScores());
}
}
class Student {
private final String name;
private final int id;
private final Map<String, Integer> subjectScores;
public Student(String name, int id) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("name must not be blank");
}
if (id <= 0) {
throw new IllegalArgumentException("id must be positive, got " + id);
}
this.name = name.strip();
this.id = id;
this.subjectScores = new LinkedHashMap<>();
}
public String getName() {
return name;
}
public int getId() {
return id;
}
// NEW in Part 2 ------------------------------------------------
public void addScore(String subject, int score) {
subjectScores.put(subject, score); // same subject again = update
}
public Map<String, Integer> getScores() {
return Collections.unmodifiableMap(subjectScores);
}
public double calculateAverage() {
if (subjectScores.isEmpty()) {
return 0.0;
}
int total = 0;
for (int score : subjectScores.values()) {
total += score;
}
return (double) total / subjectScores.size();
}
public String getGrade() {
if (subjectScores.isEmpty()) {
return "-"; // not graded yet, which is not the same as failing
}
double avg = calculateAverage();
if (avg >= 90) {
return "A";
} else if (avg >= 75) {
return "B";
} else if (avg >= 50) {
return "C";
} else {
return "F";
}
}
}
class StudentManager {
private final List<Student> students = new ArrayList<>();
public boolean addStudent(Student student) {
if (findById(student.getId()) != null) {
return false;
}
students.add(student);
return true;
}
public Student findById(int id) {
for (Student s : students) {
if (s.getId() == id) {
return s;
}
}
return null;
}
// printRoster() from Part 1 is unchanged and left out to keep this short.
// NEW in Part 2 ------------------------------------------------
public void printGradeSheet() {
for (Student s : students) {
System.out.printf("%d %-9s subjects=%d avg=%.2f grade=%s%n",
s.getId(), s.getName(), s.getScores().size(),
s.calculateAverage(), s.getGrade());
}
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Add or update in one call
subjectScores.put(subject, score); // same subject again = updateKo Ko ရဲ့ Math ကို နှစ်ခါ put လုပ်လို့ subjects=2 ပဲ ရှိပြီး Math=61 ဖြစ်နေပါတယ်။ (61 + 58) / 2 = 59.5 ဖြစ်လို့ C ရပါတယ်။
- Read-only view for callers
return Collections.unmodifiableMap(subjectScores);printGradeSheet() နဲ့ main က score တွေကို ဖတ်လို့ရပေမယ့် ပြင်လို့ မရပါဘူး။ LinkedHashMap ဖြစ်လို့ print ထုတ်တဲ့အခါ {Math=61, English=58} လို ထည့်ခဲ့တဲ့ order အတိုင်း ပေါ်ပါတယ်။
- Floating-point division
return (double) total / subjectScores.size();Cast က division မတိုင်ခင် ဖြစ်လို့ 265.0 / 3 = 88.333... ရပါတယ်။ %.2f က 88.33 လို့ ဒဿမ နှစ်နေရာနဲ့ ပြပါတယ်။
- Not graded is not failed
return "-"; // not graded yet, which is not the same as failingMya Mya ရဲ့ average က 0.00 ဖြစ်ပေမယ့် grade က F မဟုတ်ဘဲ "-" ပါ။ Report ဖတ်သူက "မစစ်ရသေး" နဲ့ "ကျ" ကို ခွဲနိုင်ပါတယ်။
- Formatted columns
System.out.printf("%d %-9s subjects=%d avg=%.2f grade=%s%n",%-9s က နာမည်ကို စာလုံး ၉ လုံးစာ နေရာမှာ ဘယ်ဘက်ကပ်ထားလို့ column တွေ တန်းညီပါတယ်။ %n က platform နဲ့ ကိုက်တဲ့ newline ပါ။
101 Aung Aung subjects=3 avg=88.33 grade=B
102 Su Su subjects=3 avg=76.67 grade=B
103 Ko Ko subjects=2 avg=59.50 grade=C
104 Mya Mya subjects=0 avg=0.00 grade=-
Ko Ko's scores: {Math=61, English=58}Boundary value တွေနဲ့ grade rule ကို စစ်ပါ
Grade rule ကို စစ်ဖို့ အကောင်းဆုံး value တွေက threshold တွေရဲ့ အပေါ်နဲ့ အောက်ပါ။ 90 က A ဖြစ်ရပြီး 89.99 က B ဖြစ်ရပါမယ်။ 75 နဲ့ 74.99၊ 50 နဲ့ 49.99 ကိုလည်း ဒီလိုပဲ စစ်ပါ။ >= အစား > ကို မှားရေးမိရင် 90 တိတိရတဲ့ ကျောင်းသားက B ရသွားမှာမို့ ဒီ test တွေက အဲဒီ bug ကို ချက်ချင်း ဖမ်းပါတယ်။
Double နဲ့ နှိုင်းယှဉ်တဲ့အခါ သတိထားစရာ တစ်ခုရှိပါတယ်။ Average တွေက အမြဲ score အရေအတွက်နဲ့ စားထားတာမို့ 89.99999 လို value တွေ ထွက်လာနိုင်ပါတယ်။ ကျောင်းရဲ့ policy က "ဒဿမ တစ်နေရာ round လုပ်ပြီးမှ grade ပေး" ဆိုရင် အဲဒီ rounding ကို getGrade() ထဲမှာ ထင်ထင်ရှားရှား ရေးထားပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Online shop တစ်ခုရဲ့ cart total၊ bank account ရဲ့ balance၊ student portal ရဲ့ GPA စတာတွေ အားလုံးက derived data ပါ။ Team အများစုက ဒါတွေကို source data (line item တွေ၊ transaction တွေ၊ score တွေ) ကနေ ပြန်တွက်ပြီး performance တကယ် လိုအပ်မှသာ cache လုပ်ပါတယ်။ Cache လုပ်ရင်လည်း source data ပြောင်းတိုင်း cache ကို update ဖို့ နေရာတစ်ခုတည်း (ဒီ project မှာဆိုရင် addScore()) ကနေပဲ လုပ်ပါတယ်။
Grading scale က ကျောင်းနဲ့ နှစ်အလိုက် ကွဲပြားပါတယ်။ Mini Project မှာ A က 80 ဖြစ်ပြီး ဒီ project မှာ 90 ဖြစ်တာကိုက သက်သေပါ။ တကယ့် system တွေမှာ threshold တွေကို code ထဲ hard-code မလုပ်ဘဲ configuration ဒါမှမဟုတ် database table ကနေ ဖတ်လေ့ရှိပါတယ်။ Mark တွေကို double အစား int နဲ့ သိမ်းတာ၊ average ကို report ထုတ်မှ format လုပ်တာကလည်း rounding ပြဿနာ နည်းအောင် လုပ်တဲ့ အလေ့အထ ကောင်းပါ။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
(double) cast မပါဘဲ int ကို int နဲ့ စားတာ
return total / subjectScores.size(); က compile ဖြစ်ပြီး 88.0 ကို ပြန်ပေးပါတယ်။ Return type double ဖြစ်နေလို့ မှန်တယ်ထင်ရပေမယ့် ဒဿမက division အချိန်မှာ ပျောက်ပြီးသားပါ။ Operand တစ်ခုကို (double) ပြောင်းပါ။
Threshold တွေကို အောက်ကနေ စစ်တာ
if (avg >= 50) ကို ပထမဆုံး ထားရင် 95 ရတဲ့ student လည်း C ရပါတယ်။ Chain က ပထမ true branch မှာ ရပ်လို့ပါ။ အမြင့်ဆုံး threshold ကနေ စပြီး boundary value တွေနဲ့ စမ်းပါ။
Score မရှိတဲ့ student ကို F ပေးတာ
Empty map အတွက် 0.0 ပြန်ပြီး getGrade() က အဲဒီ 0.0 ကို ဆက်စစ်ရင် စာမေးပွဲ မဖြေရသေးတဲ့ ကျောင်းသားကို F ပေးမိပါတယ်။ isEmpty() ကို getGrade() ထဲမှာလည်း သီးခြား စစ်ပါ။
Map ကိုယ်တိုင်ကို getter နဲ့ ပေးတာ
getScores() က subjectScores ကို တိုက်ရိုက် ပြန်ပေးရင် caller က getScores().put("Math", 500) လို့ ရေးပြီး addScore() ကို ကျော်နိုင်ပါတယ်။ Part 3 မှာ ထည့်မယ့် 0-100 စစ်ဆေးချက်ကိုလည်း ကျော်သွားနိုင်ပါတယ်။
Mini challenge
Best subject၊ top student နဲ့ class average
Student မှာ getBestSubject() ကို ထည့်ပြီး StudentManager မှာ findTopStudent() နဲ့ classAverage() ကို ထည့်ပါ။ Score မရှိတဲ့ student တွေကို top student ရွေးတဲ့အခါ၊ class average တွက်တဲ့အခါ ထည့်မတွက်ရပါ။
- getBestSubject() က score အမြင့်ဆုံး subject ကို ပြန်ပြီး score မရှိရင် "none" ပြန်ပါ
- အမှတ်တူရင် ပထမ ထည့်ခဲ့တဲ့ subject ကို ရွေးပါ
- findTopStudent() က graded student မရှိရင် null ပြန်ပါ
- classAverage() က graded student တွေရဲ့ average တွေကိုပဲ ပျမ်းမျှပါ
import java.util.*;
public class GradeChallenge {
public static void main(String[] args) {
// TODO: three students, one with no scores; print best subjects,
// the top student and the class average (two decimals)
}
}
class Student {
// name, id, subjectScores, addScore(), calculateAverage() from the lesson
boolean hasScores() { /* TODO */ return false; }
String getBestSubject() {
// TODO: walk entrySet(), keep the first highest score
return "none";
}
}
class StudentManager {
private final List<Student> students = new ArrayList<>();
Student findTopStudent() { /* TODO: skip students without scores */ return null; }
double classAverage() { /* TODO: guard against zero graded students */ return 0; }
}ရလဒ်: Aung Aung (Math 95၊ English 82) နဲ့ Su Su (Math 72၊ English 91၊ Science 91) ဆိုရင် best subject က Math နဲ့ English၊ top student က Aung Aung၊ class average က 86.58 ဖြစ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
entrySet() ကို loop ပတ်ပြီး e.getValue() > bestScore ကို strict > နဲ့ စစ်ရင် အမှတ်တူတဲ့အခါ ပထမ subject ကျန်နေပါတယ်။ classAverage() မှာ graded == 0 ကို စားခြင်း မလုပ်ခင် စစ်ပါ။
အဖြေ ကြည့်ရန်
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class GradeChallenge {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Student aung = manager.add(new Student("Aung Aung", 101));
Student suSu = manager.add(new Student("Su Su", 102));
manager.add(new Student("Mya Mya", 104)); // no scores
aung.addScore("Math", 95);
aung.addScore("English", 82);
suSu.addScore("Math", 72);
suSu.addScore("English", 91);
suSu.addScore("Science", 91);
System.out.println("Aung Aung best subject: " + aung.getBestSubject());
System.out.println("Su Su best subject: " + suSu.getBestSubject());
Student top = manager.findTopStudent();
System.out.println("Top student: " + (top == null ? "none" : top.getName()));
System.out.printf("Class average (graded only): %.2f%n", manager.classAverage());
}
}
class Student {
private final String name;
private final int id;
private final Map<String, Integer> subjectScores = new LinkedHashMap<>();
Student(String name, int id) {
this.name = name;
this.id = id;
}
String getName() { return name; }
boolean hasScores() { return !subjectScores.isEmpty(); }
void addScore(String subject, int score) {
subjectScores.put(subject, score);
}
double calculateAverage() {
if (subjectScores.isEmpty()) return 0.0;
int total = 0;
for (int score : subjectScores.values()) total += score;
return (double) total / subjectScores.size();
}
String getBestSubject() {
String best = "none";
int bestScore = -1;
for (Map.Entry<String, Integer> e : subjectScores.entrySet()) {
if (e.getValue() > bestScore) { // strict >, so the first of a tie wins
best = e.getKey();
bestScore = e.getValue();
}
}
return best;
}
}
class StudentManager {
private final List<Student> students = new ArrayList<>();
Student add(Student s) {
students.add(s);
return s;
}
Student findTopStudent() {
Student top = null;
for (Student s : students) {
if (!s.hasScores()) {
continue;
}
if (top == null || s.calculateAverage() > top.calculateAverage()) {
top = s;
}
}
return top;
}
double classAverage() {
double sum = 0;
int graded = 0;
for (Student s : students) {
if (s.hasScores()) {
sum += s.calculateAverage();
graded++;
}
}
return graded == 0 ? 0.0 : sum / graded;
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- addScore() ရဲ့ put() က subject အသစ် ထည့်တာရော re-mark လုပ်တာရော လုပ်ပါတယ်
- Average နဲ့ grade ကို field မလုပ်ဘဲ score တွေကနေ ခေါ်တိုင်း ပြန်တွက်ပါ
- (double) cast ကို division မတိုင်ခင် လုပ်ပြီး empty map ကို အရင် ကိုင်တွယ်ပါ
- Score မရှိတာနဲ့ ကျတာကို ခွဲပါ: "-" နဲ့ "F" က မတူပါဘူး
- Map ကို unmodifiable view နဲ့ပဲ ပေးလို့ score ပြောင်းတဲ့ လမ်းက addScore() တစ်ခုတည်းပါ