class Fraction {
int frac;//fraction
int sec;//section
Fraction( int fraction, int section) {// fraction/section
this.frac = fraction;
this.sec = section;
}
Fraction( int fraction) {// fraction/1
this.frac = fraction;
this.sec = 0;
}
void display() {
println( this.frac + "/" + this.sec);
}
void plus (Fraction b) {
int frac;
int sec;
if (this.sec == b.sec) {
frac = this.frac + b.frac;
sec = this.sec;
} else {
top = (this.frac * b.sec) + (b.frac * this.sec);
sec = this.sec * b.sec;
}
}
void minus(Fraction b) {
int frac;
int sec;
if (this.sec == b.sec) {
frac = this.frac - b.frac)
sec = this.sec ;
} else {
frac = (this.frac * b.sec) - (b.frac * this.sec);
sec = this.sec * b.sec;
}
}
void multiply(Fraction b) {
int frac;
int sec;
top = this.frac * b.frac
sec = this.sec * b.sec
}
void divide(Fraction b) {
println(this.frac * b.sec+ "/" + this.sec * b.frac);
}
}
void setup() {
Fraction a; // Create object
Fraction b;
a = new Fraction(1, 2); // 1/2
b = new Fraction(2, 3);
a.plus(b); // a + b
a.minus(b); // a - b
a.multiply(b); // a * b
a.divide(b); // a / b
}
Tree&dream
วันอังคารที่ 25 พฤศจิกายน พ.ศ. 2557
Lab.5 Class employee
class Employee {
String name;
int age;
String position;
float salary;
String address;
Employee(String name) {
this.name = name;
}
void age(int Age) {
this.age = Age;
}
void position(String Position) {
this.position = Position;
}
void salary(float Salary) {
this.salary = Salary;
}
void address(String Address) {
this.address = Address;
}
void printEmp_data() {
println("Name : " + this.name );
println("Age : " + this.age );
println("Position : " + this.position);
println("Address : " + this.address);
println("Salary : " + this.salary + " Baht");
}
}
void setup() {
Employee number1 = new Employee ("B");
number1.age(25);
number1.position("Engineer");
number1.address("");
number1.salary(20000);
number1.printEmp_data();
}
String name;
int age;
String position;
float salary;
String address;
Employee(String name) {
this.name = name;
}
void age(int Age) {
this.age = Age;
}
void position(String Position) {
this.position = Position;
}
void salary(float Salary) {
this.salary = Salary;
}
void address(String Address) {
this.address = Address;
}
void printEmp_data() {
println("Name : " + this.name );
println("Age : " + this.age );
println("Position : " + this.position);
println("Address : " + this.address);
println("Salary : " + this.salary + " Baht");
}
}
void setup() {
Employee number1 = new Employee ("B");
number1.age(25);
number1.position("Engineer");
number1.address("");
number1.salary(20000);
number1.printEmp_data();
}
Lab.5 Class balloon
int R ;
int G ;
int B ;
class Balloon {
float x;
float y;
int r_x;
int r_y;
float moveX;
float moveY;
Balloon(float x, float y, int r_x, int r_y, float moveX, float moveY) {
this.x = x;
this.y = y;
this.r_x = r_x;
this.r_y = r_y;
this.moveX = moveX;
this.moveY = moveY;
}
void display () {
ellipse(this.x, this.y, this.r_x, this.r_y);
triangle(this.x - (this.r_x/5), this.y + (2*(this.r_y/3)), this.x, this.y + (this.r_y/2), this.x + (this.r_x/5), this.y + (2*(this.r_y/3)));
line(this.x, this.y + (this.r_y/2), this.x, this.y + 1.5*this.r_y);
}
void move () {
this.y = this.y - this.moveY;
//this.x = this.x + this.moveX;
}
}
Balloon []a = new Balloon[3];
void setup () {
size(400, 300);
a[0] = new Balloon(100, 250, 50, 70, 0.5, 1);
a[1] = new Balloon(150, 300, 40, 60, -0.5, 1);
a[2] = new Balloon(200, 200, 60, 80, 1, 2);
}
void draw () {
background(255);
for (Balloon i : a) {
i.display();
i.move();
}
}
int G ;
int B ;
class Balloon {
float x;
float y;
int r_x;
int r_y;
float moveX;
float moveY;
Balloon(float x, float y, int r_x, int r_y, float moveX, float moveY) {
this.x = x;
this.y = y;
this.r_x = r_x;
this.r_y = r_y;
this.moveX = moveX;
this.moveY = moveY;
}
void display () {
ellipse(this.x, this.y, this.r_x, this.r_y);
triangle(this.x - (this.r_x/5), this.y + (2*(this.r_y/3)), this.x, this.y + (this.r_y/2), this.x + (this.r_x/5), this.y + (2*(this.r_y/3)));
line(this.x, this.y + (this.r_y/2), this.x, this.y + 1.5*this.r_y);
}
void move () {
this.y = this.y - this.moveY;
//this.x = this.x + this.moveX;
}
}
Balloon []a = new Balloon[3];
void setup () {
size(400, 300);
a[0] = new Balloon(100, 250, 50, 70, 0.5, 1);
a[1] = new Balloon(150, 300, 40, 60, -0.5, 1);
a[2] = new Balloon(200, 200, 60, 80, 1, 2);
}
void draw () {
background(255);
for (Balloon i : a) {
i.display();
i.move();
}
}
Lab.5 Class complex
class Complex {
int a;
int b;
Complex(int a, int b) {
this.a= a;
this.b= b;
}
Complex(int a) {
this.a= a;
}
Complex add(Complex d) {
int r; // result
int i;
r= this.a+d.a;
i= this.b+d.b ;
Complex z= new Complex(r, i);
return z;
}
Complex reduce(Complex d) {
int r;
int i;
r= this.a-d.a;
i=this.b-d.b;
Complex z=new Complex(r, i);
return z;
}
String toString() {
String s;
if (this.b == 0) {
s = this.a + "";
} else if (this.b > 0) {
s = this.a + "+" + this.b +"i";
} else {
s = this.a + "" + this.b +"i";
}
return s;
}
}
void setup() {
Complex z, y, x, w, v;
z=new Complex(5, 3);
y=new Complex(7);
w=new Complex(6, 7);
x=z.add(y);
println(x);
v=x.reduce(w);
println(v);
}
int a;
int b;
Complex(int a, int b) {
this.a= a;
this.b= b;
}
Complex(int a) {
this.a= a;
}
Complex add(Complex d) {
int r; // result
int i;
r= this.a+d.a;
i= this.b+d.b ;
Complex z= new Complex(r, i);
return z;
}
Complex reduce(Complex d) {
int r;
int i;
r= this.a-d.a;
i=this.b-d.b;
Complex z=new Complex(r, i);
return z;
}
String toString() {
String s;
if (this.b == 0) {
s = this.a + "";
} else if (this.b > 0) {
s = this.a + "+" + this.b +"i";
} else {
s = this.a + "" + this.b +"i";
}
return s;
}
}
void setup() {
Complex z, y, x, w, v;
z=new Complex(5, 3);
y=new Complex(7);
w=new Complex(6, 7);
x=z.add(y);
println(x);
v=x.reduce(w);
println(v);
}
วันอังคารที่ 28 ตุลาคม พ.ศ. 2557
class stu
void setup(){
int [][]hours_in={{1,2,5,3,4,2,5},
{2,3,4,5,1,2,5},
{1,5,3,2,4,5,4},
{3,2,1,3,5,4,5},
{5,1,2,4,6,4,5}};
for(int i=0;i<hours_in.length;i++){
println("People " +(i+1)+ " use the internet "+sum_hrs(hours_in[i])+" hours per week");
println(" approximately average = "+round(avg_hrs_approximately(hours_in[i]))+" hours per day");
}
}
int sum_hrs(int []a){
int sum=0;
for(int i = 0; i < a.length; i++){
sum=sum+a[i];
}
return sum;
}
float avg_hrs_approximately(int []b){
float average=0;
for(int j = 0; j < b.length; j++){
average=average+b[j];
}
average=average/b.length;
return average;
}
int [][]hours_in={{1,2,5,3,4,2,5},
{2,3,4,5,1,2,5},
{1,5,3,2,4,5,4},
{3,2,1,3,5,4,5},
{5,1,2,4,6,4,5}};
for(int i=0;i<hours_in.length;i++){
println("People " +(i+1)+ " use the internet "+sum_hrs(hours_in[i])+" hours per week");
println(" approximately average = "+round(avg_hrs_approximately(hours_in[i]))+" hours per day");
}
}
int sum_hrs(int []a){
int sum=0;
for(int i = 0; i < a.length; i++){
sum=sum+a[i];
}
return sum;
}
float avg_hrs_approximately(int []b){
float average=0;
for(int j = 0; j < b.length; j++){
average=average+b[j];
}
average=average/b.length;
return average;
}
hours of internet use
void setup(){
int [][]hours_in={{1,2,5,3,4,2,5},
{2,3,4,5,1,2,5},
{1,5,3,2,4,5,4},
{3,2,1,3,5,4,5},
{5,1,2,4,6,4,5}};
for(int i=0;i<hours_in.length;i++){
println("People " +(i+1)+ " use the internet "+sum_hrs(hours_in[i])+" hours per week");
println(" approximately average = "+round(avg_hrs_approximately(hours_in[i]))+" hours per day");
}
}
int sum_hrs(int []a){
int sum=0;
for(int i = 0; i < a.length; i++){
sum=sum+a[i];
}
return sum;
}
float avg_hrs_approximately(int []b){
float average=0;
for(int j = 0; j < b.length; j++){
average=average+b[j];
}
average=average/b.length;
return average;
}
int [][]hours_in={{1,2,5,3,4,2,5},
{2,3,4,5,1,2,5},
{1,5,3,2,4,5,4},
{3,2,1,3,5,4,5},
{5,1,2,4,6,4,5}};
for(int i=0;i<hours_in.length;i++){
println("People " +(i+1)+ " use the internet "+sum_hrs(hours_in[i])+" hours per week");
println(" approximately average = "+round(avg_hrs_approximately(hours_in[i]))+" hours per day");
}
}
int sum_hrs(int []a){
int sum=0;
for(int i = 0; i < a.length; i++){
sum=sum+a[i];
}
return sum;
}
float avg_hrs_approximately(int []b){
float average=0;
for(int j = 0; j < b.length; j++){
average=average+b[j];
}
average=average/b.length;
return average;
}
Chess table
void setup() {
size(400, 400);
int num = 8;//number of slot in line
int [][]keep_XO_Table = new int[num][num];
draw_G(keep_XO_Table[num-1]);
}
void draw_G(int []num) {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
stroke(0);
if(i%2==0){
if(j%2==0){
fill(255);
}else{fill(100);}
}else if(i%2!=0){
if(j%2==0){
fill(100);
}else{fill(255);}
}
rect(i*(width/num.length), j*(height/num.length), width/num.length, height/num.length);
}
}
}
สมัครสมาชิก:
บทความ (Atom)