/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Write a program to print from 1 to 100 */ public class Chap4Prac1Printfrom1To100 { public static void main(String[] args){ for(int i=1;i<=100;i++) P.print(i+" "); } }
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, chapter 4, practice 2 * Write a program which generating 25 random int numbers. Use if-else statement to classify it as * greater than, less than or equal to a second randomly generated value. */ import java.util.*; public class Chap4Prac2Generate25int { public static void main(String[] args){ Random ran1 = new Random(); Random ran2 = new Random(); P.print("ran1: "+ran1); P.print("ran2: "+ran2); for(int i=0;i<25;i++){ int x = ran1.nextInt(); int y = ran2.nextInt(); if(x>y) P.print(x+">"+y); else if(x
import java.util.Random; /** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 3 */ import java.util.*; public class Chap4Prac3 { public static void main(String[] args){ Random ran3 = new Random(); Random ran4 = new Random(); while(true){ int x = ran3.nextInt(10); int y = ran4.nextInt(10); if(x>y) P.print(x+">"+y); else if(x
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 4 * Write a program using two nested for loops and the modules operator(%) to detect and print * prime numbers */ public class Chap4Prac4For { public static void main(String[] args){ for(int i = 1;i<100;i++){ int factor = 0; for(int j=1;j<=i;j++){ if(i%j==0) factor++; } if(factor<=2) P.print(i); } } }
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 5 */ public class Chap4Prac5 { static void binaryPrint(int q){ if(q==0) P.print(0); else{ int nlz = Integer.numberOfLeadingZeros(q); q<<=nlz; for(int p=0;p<32-nlz;p++){ int n = (Integer.numberOfLeadingZeros(q)==0)?1:0; System.out.println(n); q<<=1; } } P.print(""); } public static void main(String[] args){ int i=1+4+16+64; int j = 2+8+32+128; int k = 0x100; int m = 0; P.print("Using Integer.toBinaryString(): "); P.print("i = "+Integer.toBinaryString(i)); P.print("j = "+Integer.toBinaryString(j)); P.print("k = "+Integer.toBinaryString(k)); P.print("m= "+Integer.toBinaryString(m)); P.print("i & j = "+(i&j)+"="+Integer.toBinaryString(i&j)); P.print("i | j = "+(i|j)+"="+Integer.toBinaryString(i|j)); P.print("i ^ j = "+(i^j)+"="+Integer.toBinaryString(i^j)); P.print("~i = "+Integer.toBinaryString(~i)); P.print("~j = "+Integer.toBinaryString(~j)); P.print("Using binaryPrint():"); P.print("i = "+i+" = "); System.out.print(i); P.print("j = "+j+" = "); System.out.print(j); P.print("k = " + k + " = "); System.out.print(k); P.print("m = " + m + " = "); System.out.print(m); P.print("i & j = " + (i & j) + " = "); System.out.print(i & j); P.print("i | j = " + (i | j) + " = "); System.out.print(i | j); P.print("i ^ j = " + (i ^ j) + " = "); System.out.print(i ^ j); P.print("~i = " + ~i + " = "); System.out.print(~i); P.print("~j = " + ~j + " = "); System.out.print(~j); } }
/** * Created by Sandy.Liu on 2018/7/22. * Thinking in java, version 4, chapter 4, practice 6 * Modify method test(), make them accept two other parameters begin and end, check if * testval is between begin and end. */ public class Chap4Prac6 { static int test(int testval, int begin, int end){ if (end =begin)) return +1; else if((testval end)) return -1; else P.print("exceptional case"); return 13; } public static void main(String[] args){ test(10, 5,4);//begin end } }
/** * Created by Sandy.Liu on 2018/7/22. * Thinking in java, version 4, chapter 4, practice 7 * Modify excise 1, use break to make program exists at value 99 */ public class Chap4Prac7{ static void test(int x){ for(int i=0;i<=x;i++){ if(i==99) break; P.print(i); } } static void test1(int x){ for(int i=0;i<=x;i++){ if(i==99) return; P.print(i); } } public static void main(String[] args) { test(100); test1(1000); } }
/** * Created by Sandy.Liu on 2018/7/23. * Thinking in java, version 4, chapter 4, practice 8 * Create a switch statement that prints a message for each case, and put the switch inside a for loop * to try each case. Put a break after each case and test it. And then remove the breaks and test again. */ public class Chap4Prac8Switch { public static void main(String[] args){ for(int i = 0;i<100;i++){ switch (i){ case 0: P.print("zero");break; case 1: P.print("one");break; case 2: P.print("two");break; case 3: P.print("three");break; default: P.print(i+"more than three"); } } } }
import java.lang.reflect.Array; import java.util.ArrayList; /** * Created by Sandy.Liu on 2018/7/23. * Thinking in java, version 4, chapter 4, practice 9 * A Fibonacci sequence is the sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on, * each number (from the third one ) is the sum of the previous two numbers. * Create a method that takes a integer as parameter,and display all Fibonacci numbers starting * from the beginning to the gave parameter. e.g., if you run javaFibonacci 5 (where Fibonacci * is the name of the class) the output will be 1, 1, 2, 3, 5. */ public class Chap4Prac9Fibonacci { static void gFib(int i) { if (i <= 0) P.print("please input a number bigger than 0"); int[] a = new int[i]; if (i <= 2) { for (int k = 0; k < i; k++) a[k] = 1; } else { a[0] = 1; a[1] = 1; for (int j = 2; j < i; j++) a[j] = a[j - 2] + a[j - 1]; } for (int m = 0; m < i; m++){ System.out.print(a[m]); } P.print(" "); } public static void main(String[] args){ gFib(0); gFib(1); gFib(2); gFib(5); } }
/** * Created by Sandy.Liu on 2018/7/25. * Thinking in java, version 4, chapter 4, practice 10 * A vampire number has an even number of digits and is formed by multiplying a pair of numbers * containing half the number of digits of the result. * The digits are taken from the original number in any order. Pairs of railing zeros are not * allowed. Examples include:1260 = 21 * 60, 1827 = 21 * 87, * 2187 = 27 * 81. * Write a program that finds all the 4-digit vampire numbers. */ public class Chap4Prac10VaimpireNumber { static int a(int i) { return i / 1000; } static int b(int i) { return (i % 1000) / 100; } static int c(int i) { return ((i % 1000) % 100) / 10; } static int d(int i) { return ((i % 1000) % 100) % 10; } static int com(int i, int j){ return i*10+j; } static void productTest(int i, int m, int n){ if(i==m*n){ P.print(i+"="+m+"*"+n); } } public static void main(String[] args) { for(int i = 1001;i<9999;i++){ productTest(i,com(a(i),b(i)),com(c(i),d(i))); productTest(i,com(a(i),b(i)),com(d(i),c(i))); productTest(i,com(a(i),c(i)),com(b(i),d(i))); productTest(i,com(a(i),c(i)),com(d(i),b(i))); productTest(i,com(a(i),d(i)),com(b(i),c(i))); productTest(i,com(a(i),d(i)),com(c(i),b(i))); productTest(i,com(b(i),a(i)),com(c(i),d(i))); productTest(i,com(b(i),a(i)),com(d(i),c(i))); productTest(i,com(b(i),c(i)),com(d(i),a(i))); productTest(i,com(b(i),d(i)),com(c(i),a(i))); productTest(i,com(c(i),a(i)),com(d(i),b(i))); productTest(i,com(c(i),b(i)),com(d(i),a(i))); } } }