วันเสาร์ที่ 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++;
  }
}