/**
*
*/
package org.JavaBCSX.one;
import java.awt.print.PrinterGraphics;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Random;
import static net.mindview.util.Print.*;
/**
*
- @作者 TonyHou
- @功能
- @版本
*/
class Person{
private String name;
private String sex;
private int age;
public static String country;
}
class Tank{
float a;
}
class Latter{
float c;
}
class Dog{
String name;
String says;
}
class Dog1{
String name;
String says;
}
public class Dome1 {
int a;
char b;
public int getA() {
return a;
}
public char getB() {
return b;
}
static void latter(Latter y) {
y.c = 11.11f;
}
public static void main(String[] args) {
/*
* Java编程思想2.11练习
*/
//第一题
System.out.println(new Dome1().getA());
System.out.println(new Dome1().getB());
//从执行的效果可以看出,int域的值初始化为0,而char域初始化为空字符
//第二题
System.out.println("Hello word!");
//第三题
Person person = new Person();
person.country = "日本";
Person person1 = new Person();
person1.country = "中国";
System.out.println(person.country);
System.out.println(person1.country);
//第四题
Boolean bo1 = true;
boolean bo2 = bo1;
System.out.println(bo1);
System.out.println(bo2);
Integer i = 100;
int o = i;
System.out.println(i);
System.out.println(o);
//第五题
// System.out.println(“Please enter three caracters :”);
int[] arg = {1,2,3};
System.out.println(arg[0] + " " + arg[1] + " " + arg[2]);
//打印时间
System.out.println(new Date());
//第三章练习 第一题 使用简短和正常打印语句
// 普通:java打印语句:System.out.println(" hello world ! this is my first println “);
// 简单:引入import static net.mindview.util.Print.* 打印语句 :println(” simplye println ");(需要下载mindview类库)
System.out.println("这是使用普通打印");
print("这是使用简单打印");
//练习二 创建一个包含float域的类 展示别名机制
Tank tank = new Tank();
Tank tank1 = new Tank();
tank.a = 9;
tank1.a =30;
System.out.println("Tank="+tank.a+"Tank1="+tank1.a);
// tank= tank1;
// System.out.println(“Tank=”+tank.a+“Tank1=”+tank1.a);
tank1.a = 8;
System.out.println("Tank="+tank.a+"Tank1="+tank1.a);
//做修改 避免别名问题
tank.a = tank1.a;
tank.a = 7;
System.out.println("Tank="+tank.a+"Tank1="+tank1.a);
//练习三 创建float域名的类 展示方法调用的别名机制
Latter lat = new Latter();
lat.c = 12.12f;
print("未调用latter时lat.c="+lat.c);
latter(lat);
print("调用latter时lat.c="+lat.c);
//练习4 计算速度 使用的距离和时间都是常量
float distance = 100.0f;
float time = 60.5f;
float speed = distance/time;
System.out.println("速度为:"+speed);
//练习5 创建Dog类 包含两个属性
Dog dog1 = new Dog();
Dog dog2 = new Dog();
dog1.name = "spot";
dog1.says="Ruff!";
dog2.name = "scruffy";
dog2.says="Wurf!";
print(dog1.name+"叫声为:"+dog1.says);
print(dog2.name+"叫声为:"+dog2.says);
//练习6
Dog1 dog3 = new Dog1();
Dog1 dog4 = new Dog1();
dog3.name="sopt";
dog3.says="Ruff!";
dog4.name = "scruffy";
dog4.says="Wurf!";
print("dog1和dog3的名字与叫声是否相同"+dog1.name.equals(dog3.name)+dog1.says.equals(dog3.says));
System.out.println("dog2和dog4的名字是否相同"+dog2.name == dog4.name);
System.out.println("dog2和dog4的叫声是否相同"+dog2.says == dog4.says);
System.out.println("==比较的是内存地址,如果地址相同就返回true,反之false。而,equals()比较的是字符串的内容,相同返回true,反之false。");
//练习7,模拟硬币投掷结果
Random random = new Random();
int sum = random.nextInt(2);
if(sum==0) {
print("正面"+sum);
}else {
print("反面"+sum);
}
//练习8用八进制或16进制计数法显示long
long sum8 = 011;
long sum16 = 0x2e;
//toBinaryString(int i),这个方法的功能是十进制转换为二进制输出
System.out.println(Long.toBinaryString(sum8));
System.out.println(Long.toBinaryString(sum16));
//练习9 分别显示使用float和double指数计数法所能表示的最大最小值
float fmix = Float.MAX_VALUE;
double dmax = Double.MAX_VALUE;
float fmin = Float.MIN_VALUE;
double dmin = Double.MIN_VALUE;
System.out.println("float能表示的最大值最小值分别为"+fmix+"和"+fmin);
System.out.println("double能表示的最大值最小值分别为"+dmax+"和"+dmin);
//练习10 编写一个具有两个常量的值 一个为二进制10 最低有效位为0 一个具有交替的10 最低位为1
int s0 = 0x10;
int s1 = 0x11;
System.out.println(Integer.toBinaryString(s0));
System.out.println(Integer.toBinaryString(s1));
System.out.println(Integer.toBinaryString(s0|s1));
System.out.println(Integer.toBinaryString(s0&s1));
System.out.println(Integer.toBinaryString(~s1));
System.out.println(Integer.toBinaryString(~s0));
System.out.println(Integer.toBinaryString(s0^s1));
System.out.println(Integer.toBinaryString(s0>>s1));
System.out.println(Integer.toBinaryString(s0<<s1));
}
}