Showing posts with label Class 3. Show all posts
Showing posts with label Class 3. Show all posts

Tuesday, July 5, 2011

Assignment - Week 2

1. Create a program, which can be used to determine if a number is prime number or not.

2.a Create a program for Fibonacci value finding for any number
2.b Create a program for Factorial value finding for any number. 

3. Create a program using a character array to make it reverse in such a way that 
char[] input = {'t','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n',' ','f','o','x',' ','j','u','m','p','s',' ','o','v','e','r',' ','t','h','e',' ','l','a','z','y',' ','d','o','g'}
char[] output={'e','h','t',' ','k','c','i','u','q',' ','n','w','o','r','b',' ','x','o','f',' ','s','p','m','u','j',' ','r','e','v','o',' ','e','h','t',' ','y','z','a','l',' ','g','o','d'};
Note: for assignment #3, you can only use character array & loops.

Letter Check


package com.evs.objava33.class3;

import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import sun.java2d.pipe.LoopPipe;

import com.sun.org.apache.bcel.internal.generic.GOTO;

public class LetterCheck {

/**
*
* @param args
*/
public static void main(String[] args) {
char symbol = 'A';
if (isUpperCase(symbol)) {
System.out.println("This is capital letter");
} else if (isLowerCase(symbol)) {
System.out.println("This is small letter");
}

// if ( boolean expression ) {
// statement(s) ;
// }
// else if ( boolean expression ) {
// }

int i = 0, j = 0;
if (i > 1) {
j = i + 1;
} else {
j = 1;
}
j = (i > 1) ? i + 1 : 1;
j = (i > 1) ? ((i > 2) ? i + 2 : 2) : 1;

// i =1, j=1
// i =2, j=4,
// i=3, j=8
System.out.println(i == 1 ? j + 1 : i == 2 ? i + 4 : 8);

if (i == 0) {
System.out.println("Zero");
} else if (i == 1) {
System.out.println("One");
} else if (i == 2) {
System.out.println("Two");
} else {
System.out.println("unknown");
}

System.out.println(i == 0 ? "Zero" : i == 1 ? "One" : i == 2 ? "Two"
: "Unknown");

switch (i) {
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
break;
}

// // 1. while
// int a = 0; // initialization
// while (a < 10) { // expression / condition
// System.out.println(a); // Statement
// a = a + 1; // increment / iteration operation
// }
//
// // 2. do while
// int b = 0; // init
// do {
// System.out.println(b);// statement
// b++;// increment
// } while (b < 10); // condition / expression
//
// // 3. for
// // for ( initialization ; condition ; increment )
// for (int c = 0, d = 0; c < 10 && d < 10; c++, d++) {
// System.out.println(c);
// }
//

// // 4. For each

loop1: for (int a = 0; a < 3; a++) {
loop2: for (int b = 0; b < 5; b++) {
loop3: for (int c = b; c < 2; c++) {
if (a == c) {
continue loop1;
}
if (a == b) {
break loop1;
}
System.out.println(a + ":" + b + ":" + c);
}
}
}
}
}