Java programming ideas personal exercises
I hope you will be grateful for your criticism ......)
Chapter 3 exercise 9 (1) show the maximum and minimum numbers represented by float and double indexes respectively
public class MaxMinFloatDouble { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub float fmax = Float.MAX_VALUE; float fmin = Float.MIN_VALUE; double dmax = Double.MAX_VALUE; double dmin = Double.MIN_VALUE; System.out.println(fmax); System.out.println(fmin); System.out.println(dmax); System.out.println(dmin); /*result: 3.4028235E38 1.4E-45 1.7976931348623157E308 4.9E-324*/ } }
Exercise (10) Compile a program with two constant values, one with alternating binary bits 1 and 0, with the lowest valid bits 0, the other also has alternate binary bits 1 and 0, but the minimum valid bits is 1 (Tip: Use a hexadecimal constant to represent the simplest method ). take two values, use the bitwise operator to combine them in all possible ways, and then use Integer. toBinaryString () display
public static void main(String[] args) { int i1 = 0xaaaaaaaa; int i2 = 0x55555555; System.out.println(Integer.toBinaryString(i1)); System.out.println(Integer.toBinaryString(i2)); /** * result * 101010101010101010101010101010101010101010101010101010101010101 */ }
Exercise 14 (3) Compile a method to receive two string parameters, compare the two strings with the comparison of various boolean values, and print the results. Do = and! = At the same time, use equals () for testing. Use several different string objects in main () to call this method.
/*** @ Param args */public static void main (String [] args) {String str1 = "hello"; String str2 = "word "; string str3 = new String ("hello"); testStr (str1, str2); System. out. println ("--------------------------"); testStr (str1, str3);} public static void testStr (String s1, String s2) {// boolean b1 = s1> s2; // boolean b2 = s1
Chapter 4 Exercise 1: (1) write a program and print the values from 1 to 100
Public static void main (String [] args) {test (100);} public static void test (int num) {int I = 0; while (I
Exercise 2: (2) write a program to generate a random number of 25 int types. For each random number, use the if-else statement to classify it as greater than or less, or is equal to the value randomly generated immediately following it.
Public static void main (String [] args) {test ();} public static void test () {int num [] = new int [25]; for (int I = 0; I <25; I ++) {Random r = new Random (); int n = r. nextInt (); num [I] = n; if (I> 0) {compare (num [I-1], num [I]) ;}} public static void compare (int n1, int n2) {// System. out. println (n1 + "," + n2 + "\ n n1> n2:" + (n1> n2) + "\ t" + "n1 = n2: "+ (n1 = n2) +" \ t "+" n1
N2) {System. out. println (n1 + "greater than" + n2);} else if (n1
Exercise 3: (1) Modify Exercise 2 and include the code in a while infinite loop. Run it until it is interrupted on the keyboard (usually by pressing Ctrl + c ).
Public static void main (String [] args) {test ();} public static void test () {int num [] = new int [2]; int I = 0; boolean flag = false; while (true) {Random r = new Random (); int n = r. nextInt (); if (flag) {num [I] = n; compare (num [0], num [1]);} else {flag = true; num [I] = n;} I = 0? 1:0 ;}} public static void compare (int n1, int n2) {// System. out. println (n1 + "," + n2 + "\ n n1> n2:" + (n1> n2) + "\ t" + "n1 = n2: "+ (n1 = n2) +" \ t "+" n1
N2) {System. out. println (n1 + "greater than" + n2);} else if (n1
Exercise 4: (3) write a program and use two nested for loops and the remainder operator (%) to detect and print prime numbers (only divisible by itself and 1, the integer that cannot be divisible by other numbers ).
Public static void main (String [] args) {test (33);} public static boolean test (int num) {for (int I = 2; I
Exercise 5: (4) repeat exercise 10 in chapter 1. Do not use the Integer. toBinaryString () method. Instead, use the ternary and bitwise operators to display the values of binary 1 and 0.