วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Functions (Medium)


Exercise 1: Write Java program to allow the user to input two integer values and then the program prints the results of adding, subtracting, multiplying, and dividing among the two values.

See the example below:
Enter value a:30
Enter value b:10
The result of adding is 40.
The result of subtracting is 20;
The result of multiplying is 300.
The result of dividing is 3.
Solution:

void setup()
{
  caculateValues();
}

void caculateValues() {

  int a = 30, b = 10;
  int resulta = 40, results = 20, resultm = 300;
  float resultd = 3;
  print("Enter a:");
  print("Enter b:");
  resulta=a+b;
  results=a-b;
  resultm=a*b;
  resultd=(float)a/b;
  println("The result of adding is "+resulta);
  println("The result of subtracting is "+results);
  println("The result of multiplying is "+resultm);
  println("The result of dividing is "+resultd);
}

Arrays (Medium)


Exercise 1: By using the bubble sort algorithm, write a Java program to sort an integer array of 10 elements in ascending.

Solution:

void setup() {

  int[] arr= { 12, 34, 23, 2, 4, 56, 80, 34, 45, 90};
  bubblesort(arr, arr.length);
  int i = 0;
  while (i<arr.length) {
    println(arr[i]);
    i++;
  }
}

void bubblesort(int[] dataset, int n) {
  int i = 0;
  while (i<n) {
    int j = n-1;
    while (j>i) {
      if (dataset[j]<dataset[j-1]) {
        int temp = dataset[j];
        dataset[j] = dataset[j-1];
        dataset[j-1] = temp;
      }
      j--;
    }
    i++;
  }
}

Condition (Medium)


Exercise 1: Write a Java program that determines a student’s grade.

The program will read three types of scores(quiz, mid-term, and final scores) and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
See the example output below:
Quiz score: 80
Mid-term score: 68
Final score: 90
Your grade is B.
Solution:

void setup() {
  showGrade();
}

void showGrade() {

  float quiz_score = 80; 
  float mid_score = 68; 
  float final_score = 90; 
  float avg = (quiz_score+mid_score+final_score)/3;

  if (avg>=90) {
    println("Your grade A.");
  }
  else if (avg>=70 && avg<90) {
    println("Your grade B.");
  }
  else if (avg>=50 && avg<70) {
    println("Your grade C.");
  }
  else if (avg<50) {
    println("Your grade F.");
  }
  else {
    println("Invalid");
  }
}

Variable (Hard)


Exercise 3: Write a Java program to declare two integer variables, one float variable, and one string variable and assign 10, 12.5, and "Java programming" to them respectively. Then display their values on the screen.

Solution:


void setup() {
  accessVariables();
}

void accessVariables() {

  int x = 10;
  float y = 12.5;
  String s = "Java programming";
  println(x);
  println(y);
  println(s);
}

Variable (Medium)


Exercise 2:Write a Java program to display the asterisk pattern as shown below:

*****
*****
*****
*****
*****
Solution:

void setup() {
  printAsterisk("*****");
}

void printAsterisk(String x) {
  println(x);
  println(x);
  println(x);
  println(x);
  println(x);

}

Variable (Easy)


Exercise 1: Write a Java program to display Hello World on the screen.

Solution:

void setup() {
  String a = "Hello World.";
  println(a);
}

Loop (Easy)

 Write a Java program by using two for loops to produce the output shown below:



*******

******

*****

****

***

**

*
Solution:

void setup() {
  printStars();
}

void printStars() {
  int i = 0;
  while (i<=6) {
    int j = 1;
    while (j<=7-i) {
      print("*");
      j++;
    }
    println("");
    i++;
  }
}

Loop (Medium)

Exercise 2: Write a Java program by using three for loops to print the following pattern:


1******

12*****

123****

1234***

12345**

123456*

1234567

Solution:


void setup() {
  printPattern();
}

void printPattern() {
  int i = 1;
  //int j = 1;
  //int k = 7-i;

  while ( i <= 7 ) {
    int j = 1;
    while ( j <= i ) {
      print(j);
      ++j;
    }
    int k = 7-i;
    while ( k>= 1 ) {
      print("*");
      k--;
    }
    println("");
    i++;
  }
}

Loop (Hard)


Write Java program to print the table of characters that are equivalent to the Ascii codes from 1 to 122.

Solution:

void setup() {
  Characters();
}

void Characters() {

  int i =1;
  while (i <=122)
  {
    print((char)i+"\t");
    if (i % 10 == 0) {
      print("");
    }
    i++; 
  }
}

GCD & LCD


void setup() {
  println("GCD is "+gcd(2, 3));
  println("LCD is "+lcd(2, 3));
}

int gcd(int x, int y) {
  if(x==0){
    return y;
  }
  if(y==0){
    return x;
  }
  if(x>y){
    return gcd(y, x%y);
  }else{
    return gcd(x, y%x);
  }
}

int lcd(int x, int y) {
  return x*y/gcd(x, y);
}

Palindrom


String data = "level";

void setup() {
  if(Palindrom(data)==true) {
    println("YES!!! "+data+" is Palindrom");
  }else{
    println("NO!!! "+data+" isn't Palindrom");
  }
}

boolean Palindrom(String y) {
  String z = reString(y);
  int i = 0;
  int count = 0;
  while(i<y.length()) {
    if(z.charAt(i)==data.charAt(i)){
      count++;
    }
    i++;
  }  
  if(count==data.length()) {
    return true;
  }else{
    return false;
  }
}

String reString(String x) {
  int i = 0;
  String re = "";
  while (i<x.length ()) {
    re = re + x.charAt(x.length()-i-1);
    i++;
  }
  return re;
}

Class Button

Button A = new Button("NASIT");
void setup() {
  size(200, 200);
  background(255);
}

void draw() {
  A.clicked();
  A.display();
}

class Button {    

  String name;
  int x;
  int y;
  int r;  

  Button(String n) {  
    this.name = n;
    x = 100;
    y = 100;
    r = 50;
  }

  void clicked() {
    if (mousePressed==true) {
      println(this.name);            
      mousePressed = false;
    }
  }
  void display() {
    fill(200);
    rectMode(CENTER);
    rect(x, y, r, r);
  }
}

Solar

String[] name = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
int[] moon = {0, 0, 1, 2, 66, 22, 27, 13};
int[][] data = {{35, 40, 45, 43, 75, 70, 55, 52}, {180, 230, 280, 350, 420, 500, 580, 640}};
int i = 0;
void setup() {
  size(700, 400);
  Star();
  fill(255, 0, 0);
  ellipse(-250, 200, 700, 700);
  text("Number of the moon", 300, 350);

}

void Star() {
  while(i<=7) {
    fill(random(0, 255), random(0, 255), random(0, 255));
    ellipse(data[1][i], height/2, data[0][i], data[0][i]);
    fill(0);
    text(name[i], data[1][i]-15, 100);
    textSize(16);
    text(moon[i], data[1][i]-10, 300);
    i++;
  }
}

XO

float n = 0.33333;
int count = 0;
int r =  140;
float[][] pos = {
  {
    83.3333, 250, 353.333
  }
  , {
    83.3333, 83.3333, 20
  }
};
void setup() {
  size(500, 500);
  background(0, 255, 0);
  strokeWeight(10);
  while (count<=1) {
    line(width*n, 0, width*n, height);
    line(0, height*n, width, height*n);
    n = n + 0.33333;
    count++;
  }
  OX();
}


void OX() {
  int i = 0;
  textSize(250);
  while (i<2) {
    ellipse(pos[0][i], pos[1][i], r, r);
    i++;
  }
  text('x', 345, 150);
}

Othello

float n = 0.125;
int count = 0;
int r = 55;
float[][] pos = {{218.75, 281.25, 218.75, 281.75},{218.75, 218.75, 281.75, 281.25}};
void setup() {
  size(500,500);
  background(0, 255, 0);
  strokeWeight(3);
  while (count<=6) {
    line(width*n, 0, width*n, height);
    line(0, height*n, width, height*n);
    n = n + 0.125;
    count++;
  }
  Circle();
}

void Circle() {
  int i = 0;
  while(i<4) {
    if(i%3==0){
      fill(0);
    }else{
     fill(255);
    }
    ellipse(pos[0][i], pos[1][i], r, r);
    i++;
  }
}

Barchart Array 2D

float[] l = {46,61,62,88,97,127,130,170,304,316}; // ประกาศตัว l เป็น Array เป็นค่าของข้อมูล
String[][] data = {{"Samut Songkhram","Chumphon","Phangnga","Kamphaeng Phet","Mae Hong Son","Suphan Buri","Uthai Thani","Trat","Phayao","Uttaradit"}, {"46","61","62","88","97","127","130","170","304","316"}};  // ประกาศตัว name เป็น Array เป็นชื่อของข้อมูล
float max = l[0] , min = l[0] , sum = 0 , average;  // ประกาศตัวแปรเป็น Global
void setup() {
  size(700,650);  // คำสั่งกำหนดขนาดของเฟรม
  background(200); // คำสั่งกำหนดพื้นหลัง
  smooth();
  Chart(); // เรียกใช้ฟังก์ชัน
}

void Chart() {
  int i = 0;
  int h = 25;
  int x = 30;
  int y = 60;
  while(i<l.length){  // จะเข้าเงื่อนไขต่อเมื่อค่า i น้อยกว่าจำนวนของตัวแปร l
    fill(random(0,256),random(0,256),random(0,256)); // ใส่พื้นหลังของวัตถุ
    rect(x,y,l[i],h);  // วาดสี่เหลี่ยม
    fill(0);  // ใส่พื้นหลังของวัตถุ
    textSize(24);  // สั่งให้เพิ่มขนาดของตัวหนังสือเป็นขนาด 24
    text(data[0][i],x,y);  // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (x,y)
    text(data[1][i],x+500,y+20);  // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (x+500,y+20)
    y = y + (h*2);  // ให้ y มีค่าเท่ากับ y + (h*2)
     if(l[i]<=min) { // จะเข้าเงื่อนไขต่อเมื่อค่า l[i] น้อยกว่าหรือเท่ากับ min
      min=l[i]; // ให้ min เท่ากับ l[i]
    }
     if(l[i]>=max) {  // จะเข้าเงื่อนไขต่อเมื่อค่า l[i] มากกว่าหรือเท่ากับ max
      max=l[i]; // ให้ max เท่ากับ l[i]
    }
    sum = sum + l[i];  // ให้ sum มีค่าเท่ากับ sum + l[i]
    i++;  // เพิ่มค่า i ทีละ 1
  }
 average = sum/l.length;  // ให้ average มีค่าเท่ากับ sum/l.length
 text("Average is "+average,180,630); // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (180,630)
 text("Min is "+min,180,570);  // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (180,570)
 text("Max is "+max,180,600);  // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (180,600)
 text("Provinces with the lowest jobless in 2011",80,30);  // สั่งให้แสดงตัวหนังสือบนเฟรมที่ตำแหน่ง (80,30)
}