I have read in a few places that the default value of a boolean variable is false so I decided to check for myself. My first attempt is shown in prog58 below:
andrew@UBUNTU:~/Java$ cat prog58.java
public class prog58
{
public static void main (String args[])
{
boolean b1;
System.out.println("b1 is " + b1);
}
}
andrew@UBUNTU:~/Java$ javac prog58.java
prog58.java:6: variable b1 might not have been initialized
System.out.println("b1 is " + b1);
^
1 error
andrew@UBUNTU:~/Java$
My second attempt is shown in prog59 below, which does suggest that the default value of a boolean variable is false:
andrew@UBUNTU:~/Java$ cat prog59.java
public class prog59
{
static boolean b1;
public static void main (String args[])
{
System.out.println("b1 is " + b1);
}
}
andrew@UBUNTU:~/Java$ javac prog59.java
andrew@UBUNTU:~/Java$ java prog59
b1 is false
andrew@UBUNTU:~/Java$
The two programs are slightly different. Once I have worked out why one seems to work and the other does not, I will let you know.
Wednesday, 31 December 2014
Java boolean Variable Default Value
Labels:
boolean,
default value,
Java,
static
Location:
West Sussex, UK
Tuesday, 30 December 2014
Java for Loop (again)
I have a book of 1001 numbered chess problems, which I work on at random. I needed a way to record which ones I had completed so I wrote the program below to print out all the numbers. I pasted them into a word processor document and printed a copy, which I keep with the book. Now, each time I solve one, I cross it off the list:
andrew@UBUNTU:~/Java$ cat prog57.java
public class prog57
{
public static void main (String args[])
{
int line_length = 0;
for(int x=1;x<=1001;x++)
{
if (line_length > 70)
{
line_length = 0;
System.out.println(x + " ");
}
else
{
System.out.print(x + " ");
}
if (x < 10)
line_length += 2;
else
if (x < 100)
line_length +=3;
else
if (x < 1000)
line_length +=4;
else
line_length +=5;
}
System.out.println(" ");
}
}
andrew@UBUNTU:~/Java$ javac prog57.java
andrew@UBUNTU:~/Java$ java prog57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
1001
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat prog57.java
public class prog57
{
public static void main (String args[])
{
int line_length = 0;
for(int x=1;x<=1001;x++)
{
if (line_length > 70)
{
line_length = 0;
System.out.println(x + " ");
}
else
{
System.out.print(x + " ");
}
if (x < 10)
line_length += 2;
else
if (x < 100)
line_length +=3;
else
if (x < 1000)
line_length +=4;
else
line_length +=5;
}
System.out.println(" ");
}
}
andrew@UBUNTU:~/Java$ javac prog57.java
andrew@UBUNTU:~/Java$ java prog57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
1001
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Saturday, 27 December 2014
How to See the Length of an Array in Java
The simple example below creates an array of names then uses the .length method to see how long it is:
andrew@UBUNTU:~/Java$ cat prog56.java
public class prog56
{
public static void main (String args[])
{
String[] names =
{"Andrew", "Brian", "Colin"};
System.out.println
("names has " + names.length + " entries");
}
}
andrew@UBUNTU:~/Java$ javac prog56.java
andrew@UBUNTU:~/Java$ java prog56
names has 3 entries
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat prog56.java
public class prog56
{
public static void main (String args[])
{
String[] names =
{"Andrew", "Brian", "Colin"};
System.out.println
("names has " + names.length + " entries");
}
}
andrew@UBUNTU:~/Java$ javac prog56.java
andrew@UBUNTU:~/Java$ java prog56
names has 3 entries
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Friday, 26 December 2014
Joining Java Conditional Statements With & And &&
Program prog53 below checks whether string my_name is more than 6 characters long. If so, it then looks to see if the seventh character is an X. The two conditional statements are joined by && so the second test is only carried out if the first is true:
andrew@UBUNTU:~/Java$ cat prog53.java
public class prog53
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.length() > 6)
&& (my_name.charAt(6) == 'X'))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog53.java
andrew@UBUNTU:~/Java$ java prog53
7th character not = X
andrew@UBUNTU:~/Java$
Program prog54 does the tests in the wrong order and produces a run-time error:
andrew@UBUNTU:~/Java$ cat prog54.java
public class prog54
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.charAt(6) == 'X')
&& (my_name.length() > 6))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog54.java
andrew@UBUNTU:~/Java$ java prog54
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:694)
at prog54.main(prog54.java:6)
andrew@UBUNTU:~/Java$
Program prog55 is similar to prog53 but joins the two conditional statements with & (a single ampersand). This evaluates the 2nd conditional statement even if the first is false so it also produces a run-time error:
andrew@UBUNTU:~/Java$ cat prog55.java
public class prog55
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.length() > 6)
& (my_name.charAt(6) == 'X'))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog55.java
andrew@UBUNTU:~/Java$ java prog55
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:694)
at prog55.main(prog55.java:6)
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat prog53.java
public class prog53
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.length() > 6)
&& (my_name.charAt(6) == 'X'))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog53.java
andrew@UBUNTU:~/Java$ java prog53
7th character not = X
andrew@UBUNTU:~/Java$
Program prog54 does the tests in the wrong order and produces a run-time error:
andrew@UBUNTU:~/Java$ cat prog54.java
public class prog54
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.charAt(6) == 'X')
&& (my_name.length() > 6))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog54.java
andrew@UBUNTU:~/Java$ java prog54
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:694)
at prog54.main(prog54.java:6)
andrew@UBUNTU:~/Java$
Program prog55 is similar to prog53 but joins the two conditional statements with & (a single ampersand). This evaluates the 2nd conditional statement even if the first is false so it also produces a run-time error:
andrew@UBUNTU:~/Java$ cat prog55.java
public class prog55
{
public static void main (String args[])
{
String my_name = "Andrew";
if ((my_name.length() > 6)
& (my_name.charAt(6) == 'X'))
System.out.println ("7th character = X");
else
System.out.println ("7th character not = X");
}
}
andrew@UBUNTU:~/Java$ javac prog55.java
andrew@UBUNTU:~/Java$ java prog55
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:694)
at prog55.main(prog55.java:6)
andrew@UBUNTU:~/Java$
Labels:
&,
&&,
conditional statements,
Java,
run-time error
Location:
West Sussex, UK
Wednesday, 24 December 2014
Golden Ratio
Given
a rectangle with sides of length a and b, where a > b, the ratio of
its sides is a/b. If you join a square to it with sides of length a to
create a new rectangle, the ratio of its sides will be (a+b)/a. If a/b =
(a+b)/a, this is known as the golden ratio. Here is a picture,
which I copied from Wikipedia, to illustrate the concept:
The
program below starts off with 1.5 as an approximation to the golden
ratio then uses iteration to get as close as possible to the true value:
Java > cat golden_ratio.java
public class golden_ratio
{
public static void main (String args[])
{
double ratio1;
double ratio2;
double ratio3 = 1.5d;
double difference;
double percentage_difference;
int iterations = 0;
do
{
ratio1 = ratio3;
ratio2 = 1 / (ratio1 - 1);
ratio3 = (ratio1 + ratio2) / 2d;
difference = Math.abs (ratio3 - ratio1);
percentage_difference = difference / ratio3 * 100d;
iterations++;
}
while (percentage_difference > 0.0000000000001d);
System.out.println ("Golden ratio is " + ratio3);
System.out.println ("Iterations = " + iterations);
}
}
Java > javac golden_ratio.java
Java > java golden_ratio
Golden ratio is 1.6180339887498956
Iterations = 155
Java >
Labels:
golden ratio,
Java
Location:
West Sussex, UK
Tuesday, 23 December 2014
= and == in Java
In Java, the equals sign (=) is used to assign a value to a variable. Two consecutive equals signs (==) are used to check for equality. You can see what I mean in the example below:
andrew@UBUNTU:~/Java$ cat prog52.java
public class prog52
{
public static void main (String args[])
{
int one = 1;
System.out.println ("one = " + one);
int two = 2;
if (two == 2)
System.out.println ("two = 2");
else
System.out.println ("two does not equal 2");
}
}
andrew@UBUNTU:~/Java$ javac prog52.java
andrew@UBUNTU:~/Java$ java prog52
one = 1
two = 2
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat prog52.java
public class prog52
{
public static void main (String args[])
{
int one = 1;
System.out.println ("one = " + one);
int two = 2;
if (two == 2)
System.out.println ("two = 2");
else
System.out.println ("two does not equal 2");
}
}
andrew@UBUNTU:~/Java$ javac prog52.java
andrew@UBUNTU:~/Java$ java prog52
one = 1
two = 2
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Sunday, 21 December 2014
Precision in Floating Point Numbers in Java
A float is a single-precision floating-point number. A double is a double-precision floating-point number. I read somewhere that computers cannot store floating-point numbers precisely so I checked this out myself in program prog51:
andrew@UBUNTU:~/Java$ cat prog51.java
public class prog51
{
public static void main (String args[])
{
float f1 = 81.3f * 1.94f;
System.out.println ("f1 = " + f1);
double d1 = 81.3d * 1.94d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog51.java
andrew@UBUNTU:~/Java$ java prog51
f1 = 157.72202
d1 = 157.72199999999998
andrew@UBUNTU:~/Java$
Judging by this, neither of these data types would be suitable for applications where accuracy is important such as the production of bank statements. The only way I found to calculate the correct answer to the multiplication sum above was as shown in prog52:
andrew@UBUNTU:~/Java$ cat prog52.java
public class prog52
{
public static void main (String args[])
{
float f1 = 813f * 194f / 1000f;
System.out.println ("f1 = " + f1);
double d1 = 813d * 194d / 1000d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog52.java
andrew@UBUNTU:~/Java$ java prog52
f1 = 157.722
d1 = 157.722
andrew@UBUNTU:~/Java$
When I find a better way, I will let you know.
andrew@UBUNTU:~/Java$ cat prog51.java
public class prog51
{
public static void main (String args[])
{
float f1 = 81.3f * 1.94f;
System.out.println ("f1 = " + f1);
double d1 = 81.3d * 1.94d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog51.java
andrew@UBUNTU:~/Java$ java prog51
f1 = 157.72202
d1 = 157.72199999999998
andrew@UBUNTU:~/Java$
Judging by this, neither of these data types would be suitable for applications where accuracy is important such as the production of bank statements. The only way I found to calculate the correct answer to the multiplication sum above was as shown in prog52:
andrew@UBUNTU:~/Java$ cat prog52.java
public class prog52
{
public static void main (String args[])
{
float f1 = 813f * 194f / 1000f;
System.out.println ("f1 = " + f1);
double d1 = 813d * 194d / 1000d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog52.java
andrew@UBUNTU:~/Java$ java prog52
f1 = 157.722
d1 = 157.722
andrew@UBUNTU:~/Java$
When I find a better way, I will let you know.
Labels:
double,
float,
floating point,
Java,
precision
Location:
West Sussex, UK
Saturday, 20 December 2014
Java switch Statement
This is a variation on the previous post but using the switch statement instead of if / else if / else. You can use the switch statement to compare a variable with a series of fixed values preceded by the word case. Specific actions can be carried out after each case statement if the variable matches the value specified. The default statement at the end says what to do if the variable does not match any of the values provided:
andrew@UBUNTU:~/Java$ cat prog50.java
import java.util.Random;
public class prog50
{
public static void main (String args[])
{
int zero = 0;
int one = 0;
int two = 0;
Random random_number = new Random();
int upper_limit = 3;
int x;
for(x=1;x<=30000;x++)
{
int y = random_number.nextInt(upper_limit);
switch (y)
{
case 0:
zero++;
break;
case 1:
one++;
break;
case 2:
two++;
break;
default:
System.out.println("Program error");
break;
}
}
System.out.println("Number of 0's = " + zero);
System.out.println("Number of 1's = " + one);
System.out.println("Number of 2's = " + two);
}
}
andrew@UBUNTU:~/Java$ javac prog50.java
andrew@UBUNTU:~/Java$ java prog50
Number of 0's = 9989
Number of 1's = 10109
Number of 2's = 9902
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat prog50.java
import java.util.Random;
public class prog50
{
public static void main (String args[])
{
int zero = 0;
int one = 0;
int two = 0;
Random random_number = new Random();
int upper_limit = 3;
int x;
for(x=1;x<=30000;x++)
{
int y = random_number.nextInt(upper_limit);
switch (y)
{
case 0:
zero++;
break;
case 1:
one++;
break;
case 2:
two++;
break;
default:
System.out.println("Program error");
break;
}
}
System.out.println("Number of 0's = " + zero);
System.out.println("Number of 1's = " + one);
System.out.println("Number of 2's = " + two);
}
}
andrew@UBUNTU:~/Java$ javac prog50.java
andrew@UBUNTU:~/Java$ java prog50
Number of 0's = 9989
Number of 1's = 10109
Number of 2's = 9902
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Friday, 19 December 2014
Combining Java Conditional Statements With Else
The program below uses IF, ELSE IF and ELSE to test the distribution of random numbers produced by Java:
Java > cat prog49.java
public class prog49
{
public static void main (String args[])
{
int one = 0;
int two = 0;
int three = 0;
int x;
for(x=1;x<=30000;x++)
{
int random1 = (int)(Math.random()*3)+1;
if (random1 == 1)
one++;
else
if (random1 == 2)
two++;
else
if (random1 == 3)
three++;
else
System.out.println("Program error");
}
System.out.println("Number of 1's = " + one);
System.out.println("Number of 2's = " + two);
System.out.println("Number of 3's = " + three);
}
}
Java > javac prog49.java
Java > java prog49
Number of 1's = 9986
Number of 2's = 10053
Number of 3's = 9961
Java >
Labels:
conditional statements,
else,
else if,
if,
Java
Location:
West Sussex, UK
Another Way to Seed the Random Number Generator
You can also seed the random number generator by using the setSeed method as shown below. This way makes it easy to create a given set of random numbers more than once in the same program run:
Java > cat prog48.java
Java > cat prog48.java
import java.util.Random;
public class prog48
{
public static void main (String args[])
{
Random random_number = new Random();
System.out.println("Seeding random_number");
random_number.setSeed(123);
System.out.println("Random number 1 = "
+ random_number.nextInt());
System.out.println("Random number 2 = "
+ random_number.nextInt());
System.out.println("Random number 3 = "
+ random_number.nextInt());
System.out.println("Reseeding random_number");
random_number.setSeed(123);
System.out.println("Random number 4 = "
+ random_number.nextInt());
System.out.println("Random number 5 = "
+ random_number.nextInt());
System.out.println("Random number 6 = "
+ random_number.nextInt());
}
}
Java > javac prog48.java
Java > java prog48
Seeding random_number
Random number 1 = -1188957731
Random number 2 = 1018954901
Random number 3 = -39088943
Reseeding random_number
Random number 4 = -1188957731
Random number 5 = 1018954901
Random number 6 = -39088943
Java >
Labels:
Java,
java.util.Random,
random number,
setSeed
Location:
West Sussex, UK
Wednesday, 17 December 2014
How to Seed the Random Number Generator in Java
When you use a computer to produce random numbers, they are not really random in the way that tossing a coin or throwing a dice are. They are just a sequence of pseudo-random numbers generated by a function.
You can produce random numbers as shown in prog45 below. The random number generator then decides for itself where to start in the sequence so a different set of random numbers is produced each time you run the program:
andrew@UBUNTU:~/Java$ cat prog45.java
import java.util.Random;
public class prog45
{
public static void main (String args[])
{
Random random1 = new Random();
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog45.java
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.9759678541914762
Random number 2: 0.03779439928385264
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.16178369817427063
Random number 2: 0.6776376518531995
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.9896967917912625
Random number 2: 0.2914450715965342
andrew@UBUNTU:~/Java$
Alternatively, you can seed the random number generator when you create a variable as shown in bold in prog46. This tells Java where to start in the sequence so it produces the same set of random numbers each time you run the program:
andrew@UBUNTU:~/Java$ cat prog46.java
import java.util.Random;
public class prog46
{
public static void main (String args[])
{
Random random1 = new Random(1);
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog46.java
andrew@UBUNTU:~/Java$ java prog46
Random number 1: 0.7308781907032909
Random number 2: 0.41008081149220166
andrew@UBUNTU:~/Java$ java prog46
Random number 1: 0.7308781907032909
Random number 2: 0.41008081149220166
andrew@UBUNTU:~/Java$
If you use a different number to seed the random number generator, you get a different series of random numbers:
andrew@UBUNTU:~/Java$ cat prog47.java
import java.util.Random;
public class prog47
{
public static void main (String args[])
{
Random random1 = new Random(999);
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog47.java
andrew@UBUNTU:~/Java$ java prog47
Random number 1: 0.7106328293942568
Random number 2: 0.7271143712820883
andrew@UBUNTU:~/Java$ java prog47
Random number 1: 0.7106328293942568
Random number 2: 0.7271143712820883
andrew@UBUNTU:~/Java$
You can produce random numbers as shown in prog45 below. The random number generator then decides for itself where to start in the sequence so a different set of random numbers is produced each time you run the program:
andrew@UBUNTU:~/Java$ cat prog45.java
import java.util.Random;
public class prog45
{
public static void main (String args[])
{
Random random1 = new Random();
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog45.java
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.9759678541914762
Random number 2: 0.03779439928385264
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.16178369817427063
Random number 2: 0.6776376518531995
andrew@UBUNTU:~/Java$ java prog45
Random number 1: 0.9896967917912625
Random number 2: 0.2914450715965342
andrew@UBUNTU:~/Java$
Alternatively, you can seed the random number generator when you create a variable as shown in bold in prog46. This tells Java where to start in the sequence so it produces the same set of random numbers each time you run the program:
andrew@UBUNTU:~/Java$ cat prog46.java
import java.util.Random;
public class prog46
{
public static void main (String args[])
{
Random random1 = new Random(1);
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog46.java
andrew@UBUNTU:~/Java$ java prog46
Random number 1: 0.7308781907032909
Random number 2: 0.41008081149220166
andrew@UBUNTU:~/Java$ java prog46
Random number 1: 0.7308781907032909
Random number 2: 0.41008081149220166
andrew@UBUNTU:~/Java$
If you use a different number to seed the random number generator, you get a different series of random numbers:
andrew@UBUNTU:~/Java$ cat prog47.java
import java.util.Random;
public class prog47
{
public static void main (String args[])
{
Random random1 = new Random(999);
System.out.println("Random number 1: "
+ random1.nextDouble());
System.out.println("Random number 2: "
+ random1.nextDouble());
}
}
andrew@UBUNTU:~/Java$ javac prog47.java
andrew@UBUNTU:~/Java$ java prog47
Random number 1: 0.7106328293942568
Random number 2: 0.7271143712820883
andrew@UBUNTU:~/Java$ java prog47
Random number 1: 0.7106328293942568
Random number 2: 0.7271143712820883
andrew@UBUNTU:~/Java$
Labels:
Java,
java.util.Random,
random number generation
Location:
West Sussex, UK
Saturday, 23 August 2014
How to Close a Java Frame
I wanted to be able to close my Java window by clicking on the x in the top left hand corner. To do this, I added the WindowListener interface as shown below. The window does not look much different (except that it is blue this time) but I can close it now without returning to the UNIX prompt and using Ctrl+C. As usual, click on the image to enlarge it and bring it into focus:
andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.*;
import java.awt.event.*;
public class AndrewsFrame
extends Frame
implements WindowListener
{
public AndrewsFrame()
{
super("Andrew's Frame");
setSize(500,300);
setBackground(Color.blue);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent x)
{
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent x) { }
public void windowDeactivated(WindowEvent x) { }
public void windowOpened(WindowEvent x) { }
public void windowClosed(WindowEvent x) { }
public void windowIconified(WindowEvent x) { }
public void windowDeiconified(WindowEvent x) { }
public static void main (String args[])
{
AndrewsFrame af = new AndrewsFrame();
}
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame
import java.awt.*;
import java.awt.event.*;
public class AndrewsFrame
extends Frame
implements WindowListener
{
public AndrewsFrame()
{
super("Andrew's Frame");
setSize(500,300);
setBackground(Color.blue);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent x)
{
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent x) { }
public void windowDeactivated(WindowEvent x) { }
public void windowOpened(WindowEvent x) { }
public void windowClosed(WindowEvent x) { }
public void windowIconified(WindowEvent x) { }
public void windowDeiconified(WindowEvent x) { }
public static void main (String args[])
{
AndrewsFrame af = new AndrewsFrame();
}
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame
Labels:
Java,
WindowListener
Location:
West Sussex, UK
Thursday, 21 August 2014
How to Change the Background Colour in Java
I decided I did not like the background colour of my frame so I added the line shown in bold italics, compiled it and ran it:
andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.*;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
{
super("Andrew's Frame");
setSize(500,300);
setBackground(Color.red);
setVisible(true);
}
public static void main (String args[])
{
AndrewsFrame af = new AndrewsFrame();
}
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame
This time the background was red (as usual, click on the image to enlarge it and bring it into focus):
import java.awt.*;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
{
super("Andrew's Frame");
setSize(500,300);
setBackground(Color.red);
setVisible(true);
}
public static void main (String args[])
{
AndrewsFrame af = new AndrewsFrame();
}
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame
This time the background was red (as usual, click on the image to enlarge it and bring it into focus):
Labels:
Java,
setBackground
Location:
West Sussex, UK
Wednesday, 20 August 2014
How to Display an Empty Frame in Java
I created the file shown below using vi then compiled it and ran it:
andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.Frame;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
{
super("Andrew's Frame");
setSize(500,300);
setVisible(true);
}
public static void main (String args[])
{
AndrewsFrame af = new AndrewsFrame();
}
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame
This displayed the small empty frame shown below. I found that I could move it by dragging and dropping the title bar and minimize and maximize it too. I could also resize it by clicking the edge of the frame and dragging it in the usual way. However, to close the frame, I had to return to the UNIX prompt and type Ctrl+C. I hope to show a better way to do this in a future post. As usual, click on the image to enlarge it and bring it into focus:
Labels:
import java.awt.Frame. setSize,
Java,
setVisible,
vi
Location:
West Sussex, UK
Thursday, 14 August 2014
Browser Based Dice Thrower Using Java
I wanted to use Java to create a simple dice simulator, which I could run in a browser, so I wrote the following program and compiled it. It generates a random integer between 1 and 6, converts it into a string then displays it:
andrew@UBUNTU:~/Java$ cat Dice.java
import java.awt.Graphics;
public class Dice
extends java.applet.Applet
{
public void paint(Graphics g)
{
int int1 = (int) (Math.random() * 6 + 1);
String string1 = String.valueOf(int1);
g.drawString(string1,20,40);
}
}
andrew@UBUNTU:~/Java$ javac Dice.java
andrew@UBUNTU:~/Java$
Then I wrote a few lines of HTML:
andrew@UBUNTU:~/Java$ cat Dice.html
<html>
<head>
<title>Dice Thrower</title>
</head>
<body>
<applet name="Dice Thrower"
code="Dice.class"
width = 400 height = 200>
</applet>
</body>
</html>
andrew@UBUNTU:~/Java$
<html>
<head>
<title>Dice Thrower</title>
</head>
<body>
<applet name="Dice Thrower"
code="Dice.class"
width = 400 height = 200>
</applet>
</body>
</html>
andrew@UBUNTU:~/Java$
... so I could run it like this. Each time I reloaded the page, the program created a new number. As usual, click on the image to enlarge it and bring it into focus:
Finally, I loaded it on the free web space which comes with my home Internet connection. If you would like to try it yourself, click on the link below (it should look like the screen above):
Labels:
dice,
extends java.applet.Applet,
g.drawString,
Java,
java.awt.Graphics,
Math.random,
String.valueOf
Location:
West Sussex, UK
Friday, 4 July 2014
Use Java to Work Out Your Lottery Numbers
To enter the UK lottery, you need to choose six different numbers between 1 and 49.
This program uses a for loop to create 100 lines of lottery numbers.
It stores each line’s values in an array then uses a boolean and a while loop to ensure that each number is only used once per line.
The individual numbers are chosen using Math.random().
In common with many other programming languages, this generates a
random value between 0 and 1 so it has to be converted to give a value
between 1 and 49:
Solaris > cat prog44.java
public class prog44
{
public static void main (String args[])
{
int x;
for(x=1;x<=100;x++)
{
int[] numbers_used = new int[6];
int y;
for (y=0;y<=5;y++)
{
boolean number_found = false;
while (!number_found)
{
int lottery_number =(int)(Math.random()*49)+1;
number_found = true;
int z;
for (z=0;z<y;z++)
{
if (lottery_number == numbers_used[z])
{
number_found = false;
break;
}
}
if (number_found)
{
numbers_used [y] = lottery_number;
if (lottery_number <10) System.out.print(" ");
System.out.print(lottery_number+" ");
}
}
}
System.out.print("\n");
}
}
}
Solaris > javac prog44.java
Solaris > java prog44
38 15 8 36 40 32
47 31 8 22 25 29
34 24 35 19 42 28
4 8 10 6 40 43
35 8 20 19 34 15
27 2 31 42 43 39
13 5 47 1 40 36
22 5 6 1 42 3
41 9 45 29 4 3
27 11 18 26 43 22
34 41 18 30 1 38
11 44 4 42 16 48
21 4 47 41 40 19
49 30 38 12 31 24
35 24 37 49 38 36
36 2 9 30 32 17
17 37 27 32 3 20
31 5 24 44 20 45
22 15 19 9 20 44
24 35 48 36 4 6
41 8 28 42 14 19
34 5 12 2 43 13
23 21 48 11 9 42
39 18 38 34 9 10
10 40 25 49 23 31
7 11 5 17 37 45
15 20 21 9 6 40
14 6 18 15 22 8
5 27 11 8 22 13
46 47 27 9 20 18
17 7 14 18 11 41
44 7 32 8 27 1
33 15 34 19 45 31
44 34 2 7 24 15
49 32 6 41 20 16
38 34 48 25 39 17
4 33 46 43 49 28
26 5 22 46 1 16
30 12 25 19 40 2
49 25 3 31 39 18
48 30 1 13 39 16
25 14 24 37 18 19
3 1 7 43 25 29
46 10 13 22 40 16
8 1 39 21 48 23
37 2 39 14 20 23
11 44 15 21 12 17
27 4 40 3 1 10
36 47 34 13 11 19
43 31 13 29 45 42
1 27 5 33 35 26
1 36 45 49 48 27
22 16 43 17 10 32
44 35 17 15 2 40
1 42 45 14 12 13
29 7 31 16 48 17
3 4 13 42 5 23
20 7 17 28 45 47
22 6 49 48 15 18
12 24 20 44 28 38
3 11 14 25 44 17
10 12 46 5 22 11
45 5 41 39 47 14
49 46 24 43 1 15
40 30 45 35 29 25
18 30 12 38 28 20
7 2 45 49 15 22
25 45 42 6 37 43
43 7 29 19 44 12
4 24 40 41 20 35
17 11 25 31 40 19
9 48 24 33 43 17
43 25 27 37 13 18
5 9 34 12 29 15
23 48 26 31 36 27
15 4 11 29 18 26
49 24 5 44 30 27
15 40 20 37 46 27
17 12 33 10 31 5
36 17 23 4 44 16
29 38 9 4 26 47
17 47 4 19 14 27
11 31 4 25 23 44
27 45 26 6 20 7
29 42 40 37 1 22
38 12 42 3 22 20
40 15 23 17 18 22
27 22 8 29 48 34
43 9 34 46 30 6
26 35 24 5 3 16
4 19 49 28 25 39
21 40 9 37 22 32
8 42 35 12 43 37
38 21 44 18 46 48
1 40 41 48 3 13
19 48 1 27 18 33
26 31 43 19 46 25
40 2 47 26 21 49
20 46 10 8 34 24
22 15 18 33 39 41
Solaris >
Labels:
boolean,
for loop,
Java,
lottery,
Math.random(),
while loop
Location:
West Sussex, UK
Wednesday, 2 July 2014
Java for Loop
Here is a very basic example of a Java for loop:
andrew@UBUNTU:~/Java$ cat prog43.java
public class prog43
{
public static void main (String args[])
{
for(int x=3;x<=19;x=x+5)
{
System.out.println("x = "+x);
}
}
}
andrew@UBUNTU:~/Java$ javac prog43.java
andrew@UBUNTU:~/Java$ java prog43
x = 3
x = 8
x = 13
x = 18
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Monday, 30 June 2014
java.lang.NullPointerException
If you set a string to null then try to compare it with another value, you will get a java.lang.NullPointerException if you do it like this:
Solaris > cat prog39.java
public class prog39
{
public static void main (String args[])
{
String fred = null;
if (fred.equals("OK"))
System.out.println("Fred is OK");
else
System.out.println("Fred is not OK");
}
}
Solaris > javac prog39.java
Solaris > java prog39
Exception in thread "main" java.lang.NullPointerException
at prog39.main(prog39.java:6)
Solaris >
If you just make the string empty, the code will work, as you can see below:
Solaris > cat prog40.java
public class prog40
{
public static void main (String args[])
{
String fred1 = "";
System.out.println
("Length of Fred1 is " + fred1.length());
if (fred1.equals("OK"))
System.out.println("Fred1 is OK");
else
System.out.println("Fred1 is not OK");
String fred2 = new String();
System.out.println
("Length of Fred2 is " + fred2.length());
if (fred2.equals("OK"))
System.out.println("Fred2 is OK");
else
System.out.println("Fred2 is not OK");
}
}
Solaris > javac prog40.java
Solaris > java prog40
Length of Fred1 is 0
Fred1 is not OK
Length of Fred2 is 0
Fred2 is not OK
Solaris >
... and if the string contains a value, it works too:
Solaris > cat prog41.java
public class prog41
{
public static void main (String args[])
{
String fred = "OK";
if (fred.equals("OK"))
System.out.println("Fred is OK");
else
System.out.println("Fred is not OK");
}
}
Solaris > javac prog41.java
Solaris > java prog41
Fred is OK
Solaris >
... but if you really need to cater for a null string, you could try the following:
Solaris > cat prog42.java
public class prog42
{
public static void main (String args[])
{
String fred1 = null;
if (fred1 != null && fred1.equals("OK"))
System.out.println("Fred1 is OK");
else
System.out.println("Fred1 is not OK");
String fred2 = "OK";
if (fred2 != null && fred2.equals("OK"))
System.out.println("Fred2 is OK");
else
System.out.println("Fred2 is not OK");
String fred3 = null;
if ("OK".equals(fred3))
System.out.println("Fred3 is OK");
else
System.out.println("Fred3 is not OK");
String fred4 = "OK";
if ("OK".equals(fred4))
System.out.println("Fred4 is OK");
else
System.out.println("Fred4 is not OK");
}
}
Solaris > javac prog42.java
Solaris > java prog42
Fred1 is not OK
Fred2 is OK
Fred3 is not OK
Fred4 is OK
Solaris >
Labels:
java.lang.NullPointerException
Location:
West Sussex, UK
Sunday, 29 June 2014
How to Concatenate Strings in Java
This example shows how to concatenate strings i.e. how to join them together:
andrew@UBUNTU:~/Java$ cat prog38.java
public class prog38
{
public static void main (String args[])
{
String first_name = "Andrew";
String surname = "Reid";
String full_name = first_name + " " + surname;
System.out.println ("full_name = " + full_name);
}
}
andrew@UBUNTU:~/Java$ javac prog38.java
andrew@UBUNTU:~/Java$ java prog38
full_name = Andrew Reid
andrew@UBUNTU:~/Java$
Labels:
Barry Burd,
concatenate,
Java,
String,
System.out.println
Location:
West Sussex, UK
Subscribe to:
Posts (Atom)