第3章练习

编程入门 行业动态 更新时间:2024-10-05 23:28:19

第3章练习

第3章练习

练习1:
// Write a program that uses the "short" and long form of print statementimport static net.mindview.util.Print.*;
import org.greggordon.tools.*;public class PrintTest {public static void main(String[] args) {print("Hello, from short form.");P.rintln("Hello from greggordon form.");System.out.println("Hello from long form.");}
}
练习2:
// Create a class containing a float and use it to demonstrate aliasingimport org.greggordon.tools.*;class Tube {float level;
}public class Assign {public static void main(String[] args) {Tube t1 = new Tube();Tube t2 = new Tube();t1.level = 0.9f;t2.level = 0.47f;P.rintln("1: t1.level: " + t1.level + ", t2.level: " + t2.level);t1 = t2;P.rintln("2: t1.level: " + t1.level + ", t2.level: " + t2.level);		t1.level = 0.27f; P.rintln("3: t1.level: " + t1.level + ", t2.level: " + t2.level);}
}
练习3:
// Create a class containing a float and use it to demonstrate aliasing during
// method callsimport static net.mindview.util.Print.*;class Box {float a;
}public class PassObject2 {static void f(Box y) {y.a = 2.71828f;}public static void main(String[] args) {Box x = new Box();x.a = 3.1416f;print("1: x.a = " + x.a);f(x);	print("2: x.a = " + x.a);}
}
练习4:
// Write a program that calculates velocity using a constant distance
// and a constant time.class VelocityCalculator {static float velocity (float d, float t) {if(t == 0) return 0f;else return d/t;} 
}public class VelocityTester {public static void main(String[] args) {float d = 565.3f;float t = 3.6f;System.out.println("Distance: " + d);System.out.println("Time: " + t);float v = VelocityCalculator.velocity(d, t);System.out.println("Velocity: " + v);		}
}
练习5:
/* Create a class called Dog containing two Strings: name and says.
* In main(), create two dog objects with names "spot" (who says "Ruff!") and
* "scruffy" (who says "Wurf!").  Then display their names and what they say.
*/import org.greggordon.tools.*;class Dog {String name;String says;void setName(String n) {name = n;}void setSays(String s) {says = s;}void showName() {P.rintln(name);}void speak() {P.rintln(says);}
}public class DogTest {public static void main(String[] args) {Dog spot = new Dog();spot.setName("Spot");spot.setSays("Ruff!");Dog scruffy = new Dog();scruffy.setName("Scruffy");scruffy.setSays("Wurf!");spot.showName();spot.speak();scruffy.showName(); scruffy.speak();}
}
练习6:
/* Create a class called Dog containing two Strings: name and says.
* Following exercise 5, create a new Dog reference and assign it to spot's
* object. Test for comparison using == and equals() for all references.
*/import org.greggordon.tools.*;class Dog {String name;String says;void setName(String n) {name = n;}void setSays(String s) {says = s;}void showName() {P.rintln(name);}void speak() {P.rintln(says);}
}public class DogCompare {public static void main(String[] args) {Dog spot = new Dog();spot.setName("Spot");spot.setSays("Ruff!");Dog scruffy = new Dog();scruffy.setName("Scruffy");scruffy.setSays("Wurf!");spot.showName();spot.speak();scruffy.showName(); scruffy.speak();Dog butch = new Dog();butch.setName("Butch");butch.setSays("Hello!");butch.showName();butch.speak();P.rintln("Comparison: ");P.rintln("spot == butch: " + (spot == butch));P.rintln("spot.equals(butch): " + spot.equals(butch));P.rintln("butch.equals(spot): " + butch.equals(spot));P.rintln("Now assign: spot = butch");spot = butch;P.rintln("Compare again: ");P.rintln("spot == butch: " + (spot == butch));P.rintln("spot.equals(butch): " + spot.equals(butch));P.rintln("butch.equals(spot): " + butch.equals(spot));P.rintln("Spot: ");spot.showName();spot.speak();P.rintln("Butch: ");butch.showName();butch.speak();}
}
练习7:
// Write a program that simulates coin-flippingimport java.util.*; 
import org.greggordon.tools.*;public class CoinToss {public static void main(String[] args) {Random rand = new Random();int coin = rand.nextInt();if(coin % 2 == 0) P.rintln("heads");else P.rintln("tails");		}
}
练习8:
// Show that hex and octal notations work with long values. 
// Use Long.toBinaryString to display the results.import static net.mindview.util.Print.*;public class LongValues {public static void main(String[] args) {long n1 = 0xffff; // hexadecimallong n2 = 077777; // octalprint("long n1 in hex = " + Long.toBinaryString(n1));print("long n2 in oct = " + Long.toBinaryString(n2));}
}
练习9:
// Display the largest and smallest numbers for both float and double 
// exponential notation.public class MinMax {public static void main(String[] args) {double max = java.lang.Double.MAX_VALUE;System.out.println("Max double = " + max);double min = java.lang.Double.MIN_VALUE; System.out.println("Min double = " + min);float maxf = java.lang.Float.MAX_VALUE;System.out.println("Max float = " + maxf);float minf = java.lang.Float.MIN_VALUE; System.out.println("Min float = " + minf);	}
}
练习10:
/* Write a program with two constant values, one with alternating binary ones and
* zeroes, with a zero in the least-significant digit, and the second, also
* alternating, with a one in the least-significant digit (hint: It's easiest to 
* use hexadecimal constants for this). Take these two values and combine them in
* all possible ways using the bitwise operators, and display the results using
* Integer.toBinaryString(). 
*/import org.greggordon.tools.*;public class BinaryTest {public static void main(String[] args) {int i = 1 + 4 + 16 + 64;int j = 2 + 8 + 32 + 128;P.rintln("i = " + Integer.toBinaryString(i));P.rintln("j = " + Integer.toBinaryString(j));P.rintln("i & j = " + Integer.toBinaryString(i & j));P.rintln("i | j = " + Integer.toBinaryString(i | j));P.rintln("i ^ j = " + Integer.toBinaryString(i ^ j));P.rintln("~i = " + Integer.toBinaryString(~i));P.rintln("~j = " + Integer.toBinaryString(~j));}
}
练习11:
/* Start with a number that has a binary one in the most significant position
* (hint: Use a hexadecimal constant). Using the signed right-shift operator,
* right shift it all the way through all of its binary positions each time
* displaying the result using Integer.toBinaryString().
*/ import org.greggordon.tools.*;public class RightShiftTest {public static void main(String [] args) {int h = 0x10000000;P.rintln(Integer.toBinaryString(h));for(int i = 0; i < 28; i++) {h >>>= 1;P.rintln(Integer.toBinaryString(h));}}
}
练习12:
/* Start with a number that is all binary ones. Left shift it, then use the
* unsigned right-shift operator to right shift through all of its binary
* positions, each time displaying the result using Integer.toBinarySting().
*/ import org.greggordon.tools.*;public class RightShiftTest2 {public static void main(String [] args) {int h = -1;P.rintln(Integer.toBinaryString(h));h <<= 10;P.rintln(Integer.toBinaryString(h));for(int i = 0; i < 32; i++) {h >>>= 1;P.rintln(Integer.toBinaryString(h));}}
}
练习13:
/* Write a method that displays char values in binary form. Demonstrate it
* using several different characters. 
*/ import org.greggordon.tools.*;public class CharBinaryTest {public static void main(String [] args) {char c = 'a';P.rintln(Integer.toBinaryString(c));c = 'b';P.rintln(Integer.toBinaryString(c));c = 'c';P.rintln(Integer.toBinaryString(c));c = 'd';P.rintln(Integer.toBinaryString(c));c +=1;P.rintln(Integer.toBinaryString(c));c = 'A';P.rintln(Integer.toBinaryString(c));for(int i = 0; i < 26; i++) {c +=1;P.rintln(Integer.toBinaryString(c));}}
}
练习14:
/* Write a method that takes two String arguments uses all the boolean
* comparisons to compare the two Stings and print the results. For the == and
* !=, also perform the equals() test. In main(), test your method with some
* different String objects.
*/import org.greggordon.tools.*;public class StringCompare {	static void f(boolean b) {if(b == true) P.rintln(true);else P.rintln(false);}static void stringTest(String s, String t) {f(s == t);f(s.equals(t));f(t.equals(s));f(s != t);// f(!s);//f(!t);// s = s && t;// s = s || t;// s = ~t;// s = s  & t;// s = s | t;// s = s ^ t;// s &= t;// s ^= t;// s |= t;}	public static void main(String[] args) {String s = "one", t = "two";StringWork.stringTest(s, t);		}
} 

更多推荐

第3章练习

本文发布于:2024-02-28 06:18:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1768345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!