/**
* 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 to 2018/7/19.
* Thinking in Java, Chapter 4, Practice 2
* Write a program whic h generating 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 Ran Dom ();
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<y)
P.print (x+ "<" +y);
else
P.print (x+ "=" +y);
}
}
}
import Java.util.Random;
/**
* Created by Sandy.liu to 2018/7/19.
* Thinking in Java, Version 4, Chapter 4, Practice 3
*/
IM Port 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 (ten);
int y = ran4.nextint (10);
if (x>y)
P.print (x+ ">" +y);
else if (x<y)
P.print (x+ "<" +y);
else
P.print (x+ "=" +y);
}}}
/**
* Created by Sandy.liu on 2018/7/19.
* Thinking in Java, Version 4, Chapter 4, Practice 4
* Write a program using both 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 the 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)
P.print ("End cannot be smaller than begin");
else if ((TestVal <=end) & (Testval>=begin))
return +1;
else if ((testval<begin) | | | (Testval>end))
return-1;
Else
P.print ("exceptional case");
return 13;
}
public static void Main (string[] args) {
Test (ten, 5,4);//begin<end
P.print ("Case2:" +test (5,4,10));//begin<testval<end
P.print ("CASE3:" +test (1,4,10));//testval<begin
P.print ("Case4:" +test (5,5,6));//testval=begin
P.print ("CASE5:" +test (5,1,5));//testval=end
P.print ("Case6:" +test (10,1,5));//testval>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");
Case 1:p.print ("one");
Case 2:p.print ("a");
Case 3:p.print ("three");
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,,,
* Each number, from the third one, is the sum of the previous and numbers.
* Create a method that takes an 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 would 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 have an even number of digits and are formed by multiplying A pair of numbers
* containing half the number of digits of the result.
* The digits is taken from the original number on any order. Pairs of railing Zeros is not
* Allowed. Examples include:1260 = 21 * 60, 1827 = 21 * 87,
* 2187 = 27 * 81.
* Write A program This 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)));
}
}
}
Thing in Java Fourth, control the execution process, the answer to the exercises