void setup() {
size(250, 150);
background(50);
int minNumber = 0;
int maxNumber = 20;
int sum = 0;
int space = 30;
textAlign(CENTER);
text("Sum of Prime Number", width/2, height/2-space);
text(minNumber+" - "+maxNumber, width/2, height/2);
while (minNumber <= maxNumber) {
if (prime(minNumber)) {
sum += minNumber;
}
minNumber++;
}
text("Sum = "+sum, width/2, height/2+space);
}
boolean prime(int value) {
int num = 2;
while (num <= value) {
if (value%num == 0) {
if (value == num) {
return true;
} else {
return false;
}
}
num++;
}
return false;
}
แสดงบทความที่มีป้ายกำกับ Lab 4 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Lab 4 แสดงบทความทั้งหมด
วันจันทร์ที่ 14 กันยายน พ.ศ. 2558
Lab 4 - Loan Payment
void setup() {
size(450, 300);
background(0);
float loanAmount = 10000;
float interestPercent = 12;
int loanTerm = 12;
loan(loanAmount, interestPercent, loanTerm);
}
void loan(float loanAmount, float interestPercent, int loanTerm) {
int space = 15;
text("Loan Amount = $"+nf(loanAmount, 1, 2), 10, space);
text("Loan Term = "+loanTerm+" months", 10, space*3);
text("Interest Rate = "+interestPercent+"%", 10, space*5);
head(30, space*7);
float interestRate = interestPercent/100;
float effectiveInterest = interestRate/12;
float monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)));
float interest = 0;
float totalInterest = 0;
float principal = 0;
float balance = loanAmount;
int term = 1;
while (term <= loanTerm) {
interest = balance * effectiveInterest;
principal = monthlyPayment-interest;
totalInterest += interest;
balance -= principal;
if (balance <= 0) {
balance = 0;
}
showInfo(term, balance, principal, interest, totalInterest, 30, space*(7+term));
term++;
}
}
void showInfo(int term, float balance, float principal, float interest, float totalInterest, int x, int y) {
int space = 80;
fill(#FFFF00);
text(term, x, y);
text("$"+nf(principal, 1, 2), x+space, y);
text("$"+nf(interest, 1, 2), x+space*2, y);
text("$"+nf(balance, 1, 2), x+space*3, y); text("$"+nf(totalInterest, 1, 2), x+space*4, y);
}
void head(int x, int y) {
int space = 80;
fill(#FF0000);
text("Payment No.", x, y);
text("Principal", x+space, y);
text("Interest", x+space*2, y);
text("Balance", x+space*3, y); text("Total Interest", x+space*4, y);
}
size(450, 300);
background(0);
float loanAmount = 10000;
float interestPercent = 12;
int loanTerm = 12;
loan(loanAmount, interestPercent, loanTerm);
}
void loan(float loanAmount, float interestPercent, int loanTerm) {
int space = 15;
text("Loan Amount = $"+nf(loanAmount, 1, 2), 10, space);
text("Loan Term = "+loanTerm+" months", 10, space*3);
text("Interest Rate = "+interestPercent+"%", 10, space*5);
head(30, space*7);
float interestRate = interestPercent/100;
float effectiveInterest = interestRate/12;
float monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)));
float interest = 0;
float totalInterest = 0;
float principal = 0;
float balance = loanAmount;
int term = 1;
while (term <= loanTerm) {
interest = balance * effectiveInterest;
principal = monthlyPayment-interest;
totalInterest += interest;
balance -= principal;
if (balance <= 0) {
balance = 0;
}
showInfo(term, balance, principal, interest, totalInterest, 30, space*(7+term));
term++;
}
}
void showInfo(int term, float balance, float principal, float interest, float totalInterest, int x, int y) {
int space = 80;
fill(#FFFF00);
text(term, x, y);
text("$"+nf(principal, 1, 2), x+space, y);
text("$"+nf(interest, 1, 2), x+space*2, y);
text("$"+nf(balance, 1, 2), x+space*3, y); text("$"+nf(totalInterest, 1, 2), x+space*4, y);
}
void head(int x, int y) {
int space = 80;
fill(#FF0000);
text("Payment No.", x, y);
text("Principal", x+space, y);
text("Interest", x+space*2, y);
text("Balance", x+space*3, y); text("Total Interest", x+space*4, y);
}
วันอาทิตย์ที่ 13 กันยายน พ.ศ. 2558
Lab 4 - Balloon
void setup() {
size (400, 600);
}
void draw() {
background(255);
int x = 200;
int y = mouseY;
if (y < 100) {
y = 100;
fill(#00AA00);
} else {
fill(255);
if (y>500) {
y=500;
fill(#0000AA);
}
}
draw_balloon(x, y);
draw_balloon(x+100, y);
draw_balloon(x-100, y);
}
void draw_balloon(int x, int y) {
int sized = 100;
int string_length = 150;
line(x, y, x, y + string_length);
ellipse(x, y, sized, sized);
}
size (400, 600);
}
void draw() {
background(255);
int x = 200;
int y = mouseY;
if (y < 100) {
y = 100;
fill(#00AA00);
} else {
fill(255);
if (y>500) {
y=500;
fill(#0000AA);
}
}
draw_balloon(x, y);
draw_balloon(x+100, y);
draw_balloon(x-100, y);
}
void draw_balloon(int x, int y) {
int sized = 100;
int string_length = 150;
line(x, y, x, y + string_length);
ellipse(x, y, sized, sized);
}
Lab 4 - MultipleTable
void setup() {
multi(7, 12);
}
int i=0;
void multi(int n, int row) {
while (i<=row) {
int result = n * i ;
println( n,"*",i,"=",result) ;
i += 1 ;
}
}
multi(7, 12);
}
int i=0;
void multi(int n, int row) {
while (i<=row) {
int result = n * i ;
println( n,"*",i,"=",result) ;
i += 1 ;
}
}
Lab 4 - (Exercise) Countdown
void setup(){
cal_num(10);
}
void cal_num (int i){
while(i>0){
int x=i;
while (x<=i && x!=0){
print(x,"");
x--;
}
i--;
println();
}
}
cal_num(10);
}
void cal_num (int i){
while(i>0){
int x=i;
while (x<=i && x!=0){
print(x,"");
x--;
}
i--;
println();
}
}
Lab 4 - Summary of Integers
void setup(){
cal_sum(10);
cal_sum(15);
}
void cal_sum (int i){
int a=0;
int x=1;
while (x<=i){
a=a+x;
x++;
}println("Sum 1 to",i,"=",a);
}
โปรแกรมจะแสดงผลอกมาเป็น Sum 1 to (i) = (a)
แต่เมื่อนำมาลงบล็อกแล้ว ต้องเปลี่ยนเครื่องหมายจาก
" , " เป็น " + " แทน
เพราะในบล็อกจะแสดงผลแค่ Sum 1 to
cal_sum(10);
cal_sum(15);
}
void cal_sum (int i){
int a=0;
int x=1;
while (x<=i){
a=a+x;
x++;
}println("Sum 1 to",i,"=",a);
}
** ERROR **
เมื่อพิมพ์ println("Sum 1 to",i,"=",a); ใน Processingโปรแกรมจะแสดงผลอกมาเป็น Sum 1 to (i) = (a)
แต่เมื่อนำมาลงบล็อกแล้ว ต้องเปลี่ยนเครื่องหมายจาก
" , " เป็น " + " แทน
เพราะในบล็อกจะแสดงผลแค่ Sum 1 to
สมัครสมาชิก:
บทความ (Atom)