📄 module5.lst
字号:
// more errors on smallQ
System.out.print("Contents of smallQ: ");
for(i=0; i < 5; i++) {
ch = smallQ.get();
if(ch != (char) 0) System.out.print(ch);
}
}
}
listing 13
// Introduce String.
class StringDemo {
public static void main(String args[]) {
// declare strings in various ways
String str1 = new String("Java strings are objects.");
String str2 = "They are constructed various ways.";
String str3 = new String(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
listing 14
// Some String operations.
class StrOps {
public static void main(String args[]) {
String str1 =
"When it comes to Web programming, Java is #1.";
String str2 = new String(str1);
String str3 = "Java strings are powerful.";
int result, idx;
char ch;
System.out.println("Length of str1: " +
str1.length());
// display str1, one char at a time.
for(int i=0; i < str1.length(); i++)
System.out.print(str1.charAt(i));
System.out.println();
if(str1.equals(str2))
System.out.println("str1 == str2");
else
System.out.println("str1 != str2");
if(str1.equals(str3))
System.out.println("str1 == str3");
else
System.out.println("str1 != str3");
result = str1.compareTo(str3);
if(result == 0)
System.out.println("str1 and str3 are equal");
else if(result < 0)
System.out.println("str1 is less than str3");
else
System.out.println("str1 is greater than str3");
// assign a new string to str2
str2 = "One Two Three One";
idx = str2.indexOf("One");
System.out.println("Index of first occurence of One: " + idx);
idx = str2.lastIndexOf("One");
System.out.println("Index of last occurence of One: " + idx);
}
}
listing 15
// Demonstrate String arrays.
class StringArrays {
public static void main(String args[]) {
String str[] = { "This", "is", "a", "test." };
System.out.println("Original array: ");
for(int i=0; i < str.length; i++)
System.out.print(str[i] + " ");
System.out.println("\n");
// change a string
str[1] = "was";
str[3] = "test, too!";
System.out.println("Modified array: ");
for(int i=0; i < str.length; i++)
System.out.print(str[i] + " ");
}
}
listing 16
// Use substring().
class SubStr {
public static void main(String args[]) {
String orgstr = "Java makes the Web move.";
// construct a substring
String substr = orgstr.substring(5, 18);
System.out.println("orgstr: " + orgstr);
System.out.println("substr: " + substr);
}
}
listing 17
// Display all command-line information.
class CLDemo {
public static void main(String args[]) {
System.out.println("There are " + args.length +
" command-line arguments.");
System.out.println("They are: ");
for(int i=0; i<args.length; i++)
System.out.println(args[i]);
}
}
listing 18
// A simple automated telphone directory.
class Phone {
public static void main(String args[]) {
String numbers[][] = {
{ "Tom", "555-3322" },
{ "Mary", "555-8976" },
{ "Jon", "555-1037" },
{ "Rachel", "555-1400" }
};
int i;
if(args.length != 1)
System.out.println("Usage: java Phone <name>");
else {
for(i=0; i<numbers.length; i++) {
if(numbers[i][0].equals(args[0])) {
System.out.println(numbers[i][0] + ": " +
numbers[i][1]);
break;
}
}
if(i == numbers.length)
System.out.println("Name not found.");
}
}
}
listing 19
// Uppercase letters.
class UpCase {
public static void main(String args[]) {
char ch;
for(int i=0; i < 10; i++) {
ch = (char) ('a' + i);
System.out.print(ch);
// This statement turns off the 6th bit.
ch = (char) ((int) ch & 65503); // ch is now uppercase
System.out.print(ch + " ");
}
}
}
listing 20
// Display the bits within a byte.
class ShowBits {
public static void main(String args[]) {
int t;
byte val;
val = 123;
for(t=128; t > 0; t = t/2) {
if((val & t) != 0) System.out.print("1 ");
else System.out.print("0 ");
}
}
}
listing 21
// Lowercase letters.
class LowCase {
public static void main(String args[]) {
char ch;
for(int i=0; i < 10; i++) {
ch = (char) ('A' + i);
System.out.print(ch);
// This statement turns on the 6th bit.
ch = (char) ((int) ch | 32); // ch is now lowercase
System.out.print(ch + " ");
}
}
}
listing 22
// Use XOR to encode and decode a message.
class Encode {
public static void main(String args[]) {
String msg = "This is a test";
String encmsg = "";
String decmsg = "";
int key = 88;
System.out.print("Original message: ");
System.out.println(msg);
// encode the message
for(int i=0; i < msg.length(); i++)
encmsg = encmsg + (char) (msg.charAt(i) ^ key);
System.out.print("Encoded message: ");
System.out.println(encmsg);
// decode the message
for(int i=0; i < msg.length(); i++)
decmsg = decmsg + (char) (encmsg.charAt(i) ^ key);
System.out.print("Decoded message: ");
System.out.println(decmsg);
}
}
listing 23
// Demonstrate the bitwise NOT.
class NotDemo {
public static void main(String args[]) {
byte b = -34;
for(int t=128; t > 0; t = t/2) {
if((b & t) != 0) System.out.print("1 ");
else System.out.print("0 ");
}
System.out.println();
// reverse all bits
b = (byte) ~b;
for(int t=128; t > 0; t = t/2) {
if((b & t) != 0) System.out.print("1 ");
else System.out.print("0 ");
}
}
}
listing 24
// Demonstrate the shift << and >> operators.
class ShiftDemo {
public static void main(String args[]) {
int val = 1;
for(int i = 0; i < 8; i++) {
for(int t=128; t > 0; t = t/2) {
if((val & t) != 0) System.out.print("1 ");
else System.out.print("0 ");
}
System.out.println();
val = val << 1; // left shift
}
System.out.println();
val = 128;
for(int i = 0; i < 8; i++) {
for(int t=128; t > 0; t = t/2) {
if((val & t) != 0) System.out.print("1 ");
else System.out.print("0 ");
}
System.out.println();
val = val >> 1; // right shift
}
}
}
listing 25
/*
Project 5-3
A class that displays the binary representation of a value.
*/
class ShowBits {
int numbits;
ShowBits(int n) {
numbits = n;
}
void show(long val) {
long mask = 1;
// left-shit a 1 into the proper position
mask <<= numbits-1;
int spacer = 0;
for(; mask != 0; mask >>>= 1) {
if((val & mask) != 0) System.out.print("1");
else System.out.print("0");
spacer++;
if((spacer % 8) == 0) {
System.out.print(" ");
spacer = 0;
}
}
System.out.println();
}
}
// Demonstrate ShowBits.
class ShowBitsDemo {
public static void main(String args[]) {
ShowBits b = new ShowBits(8);
ShowBits i = new ShowBits(32);
ShowBits li = new ShowBits(64);
System.out.println("123 in binary: ");
b.show(123);
System.out.println("\n87987 in binary: ");
i.show(87987);
System.out.println("\n237658768 in binary: ");
li.show(237658768);
// you can also show low-order bits of any integer
System.out.println("\nLow order 8 bits of 87987 in binary: ");
b.show(87987);
}
}
listing 26
// Prevent a division by zero using the ?.
class NoZeroDiv {
public static void main(String args[]) {
int result;
for(int i = -5; i < 6; i++) {
result = i != 0 ? 100 / i : 0;
if(i != 0)
System.out.println("100 / " + i + " is " + result);
}
}
}
listing 27
// Prevent a division by zero using the ?.
class NoZeroDiv2 {
public static void main(String args[]) {
for(int i = -5; i < 6; i++)
if(i != 0 ? true : false)
System.out.println("100 / " + i +
" is " + 100 / i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -