1) ternary operator
int x = ( y > 5 ) ? 1 : 2 ;
2) Class Calendar in java.util : class Clock
import java.util.*;
class Clock {
public static void main(String[] arg) {
// get current time and date
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
// display greeting
if (hour < 12) {
System.out.println("Good morning.\n");
}
else if (hour < 17) {
System.out.println("Good afternoon.\n");
}
else {
System.out.println("Good evening.\n");
}
// begin time message by showing the minutes
System.out.print("It’s");
if (minute != 0) {
System.out.print(" " + minute + " ");
System.out.print( (minute != 1) ? "minutes" : "minute");
System.out.print(" past");
}
// display the hour
System.out.print(" ");
System.out.print( (hour > 12) ? (hour - 12) : hour );
System.out.print(" o’clock on ");
// display the name of the month
switch (month) {
case 1:
System.out.print("January");
break;
case 2:
System.out.print("February");
break;
case 3:
System.out.print("March");
break;
case 4:
System.out.print("April");
break;
case 5:
System.out.print("May");
break;
case 6:
System.out.print("June");
break;
case 7:
System.out.print("July");
break;
case 8:
System.out.print("August");
break;
case 9:
System.out.print("September");
break;
case 10:
System.out.print("October");
break;
case 11:
System.out.print("November");
break;
case 12:
System.out.print("December");
}
// display the date and year
System.out.println(" " + day + ", " + year + ".");
}
}
3) break: class TargetLoop
public class TargetLoop {public static void main(String[] args) {int points = 0;int target = 100;//target looptargetLoop:while (target <= 100) {for (int i = 0; i < target; i++) {if (points > 50){break targetLoop;}points = points + i;System.out.print(points + " ");}}// System.out.println(i); //i doesn't exist now.System.out.println("\n"); //comment this line to see difference// Complex for Loopfor (int i = 0, j = 0; i * j < 50; i++, j += 2) {System.out.println(i + " * " + j + " = " + (i * j));}//empty sectionint i=6;for ( ; i < 8; i++) {System.out.println(i);}}}
4) System.curentTimeMillis() & .substring(begin, end): class Benchmark (test how fast is your computer)
public class Benchmark {public static void main(String[] args) {long startTime = System.currentTimeMillis();long endTime = startTime + 60000;long index = 0;while (true) {@SuppressWarnings("unused")double x = Math.sqrt(index);long now = System.currentTimeMillis();if (now > endTime) {break;}index++;}String strIndex = String.valueOf(index);// output the index in more readable styleint len = strIndex.length();int t = (int) Math.floor(len/3);int m = (int) (len % 3);if (t == 0){System.out.print(index);}else{for (int i = 1; i < t+2; i++){if (i == 1){if (m!=0){System.out.print(strIndex.substring(0,m)+"_");}}else if (i == t+1) {System.out.print(strIndex.substring(m+(i-2)*3,m+(i-1)*3));}else{System.out.print(strIndex.substring(m+(i-2)*3,m+(i-1)*3)+"_");}}}System.out.println(" loops in one minute.");}}
5) Arrays
default value of variable:
numeric = 0;
char = '\0';
boolean = false;
.toCharArray(): change a string to a char array (class SpaceRemover)
.sort(): sort a array (class Name)
.sort(): sort a array (class Name)
public class SpaceRemover {public static void main(String[] args) {String mostFamous = "Rudolph the Red-Nosed Reindeer";char[] mfl = mostFamous.toCharArray();for (int dex = 0; dex < mfl.length; dex++) {char current = mfl[dex];if (current != ' ') {System.out.print(current);}else {System.out.print('.');}}System.out.println();}}
import java.util.*;
public class Name {
public static void main(String[] args) {
String names[] = { "Lauren", "Audrina", "Heidi", "Whitney",
"Stephanie", "Spencer", "Lisa", "Brody", "Frankie",
"Holly", "Jordan", "Brian", "Jason" };
System.out.println("The original order:");
for (int i = 0; i < names.length; i++) {
System.out.print(i + ": " + names[i] + " ");
}
Arrays.sort(names);
System.out.println("\nThe new order:");
for (int i = 0; i < names.length; i++) {
System.out.print(i + ": " + names[i] + " ");
}
System.out.println();
}
}
6) Casting
One type of variable that cannot be used in any casting is Boolean values..intValue() and .parseInt(String)
class Wheel and class NewRoot
Integer suffix = new Integer(5309);
int newSuffix = suffix.intValue();
public class Wheel {
public static void main(String[] args) {
String phrase[] = {
"A STITCH IN TIME SAVES NINE",
"DON'T EAT YELLOW SNOW",
"JUST DO IT",
"EVERY GOOD BOY DOES FINE",
"I WANT MY MTV",
"I LIKE IKE",
"PLAY IT AGAIN, SAM",
"FROSTY THE SNOWMAN",
"ONE MORE FOR THE ROAD",
"HOME FIELD ADVANTAGE",
"VALENTINE'S DAY MASSACRE",
"GROVER CLEVELAND OHIO",
"SPAGHETTI WESTERN",
"AQUA TEEN HUNGER FORCE",
"IT'S A WONDERFUL LIFE"
};
int[] letterCount = new int[26];
for (int count = 0; count < phrase.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
for (int count2 = 0; count2 < letters.length; count2++) {
char lett = letters[count2];
if ( (lett >= 'A') & (lett <= 'Z') ) {
letterCount[lett - 'A']++;
}
}
}
for (char count = 'A'; count <= 'Z'; count++) {
System.out.print(count + ": " +
letterCount[count - 'A'] +
" ");
}
System.out.println();
}
}
public class NewRoot {
public static void main(String[] args) {
int number = 100;
if (args.length > 0) {
number = Integer.parseInt(args[0]);
}
System.out.println("The square root of "
+ number
+ " is "
+ Math.sqrt(number) );
}
}
No comments:
Post a Comment