题目都相当简单没啥说的直接放代码就行了...

3.1

 package ex0301;
//[3.1]使用“简短的”和正常的打印语句来写一个程序 import static java.lang.System.out;
public class Abbreviate
{
public static void main(String[] args)
{
System.out.println("Hello, world! [complete]");
out.println("Hello, world! [abbreviate]");
}
}

3.1

3.2

 package ex0302;
//[3.2]创建一个包含一个float域的类,并用这个类来展示别名机制
class ShowByname
{
private float elem;
public float getElem() { return elem; }
public void setElem(float _elem) { elem = _elem; }
} public class Byname
{
public static void main(String[] args)
{
ShowByname test1 = new ShowByname();
test1.setElem(1.333f);
System.out.println("test1.elem = " + test1.getElem()); ShowByname test2 = test1;
System.out.println("test2.elem = " + test2.getElem()); //仅修改引用test2并观察
test2.setElem(3.14159f);
System.out.println("test1.elem = " + test1.getElem());
System.out.println("test2.elem = " + test2.getElem());
}
}

3.2

3.3

 package ex0303;
//[3.3]创建一个包含一个float域的类,并用这个方法来展示方法调用时的别名机制 class ShowFunByname
{
public float elem = 3.14f;
} public class FunByname
{
static void f(ShowFunByname test)
{
test.elem = 1.414f;
} public static void main(String[] args)
{
ShowFunByname test = new ShowFunByname();
System.out.println("1: elem = " + test.elem); f(test);
System.out.println("2: elem = " + test.elem);
}
}

3.3

3.4

 package ex0304;

 //[3.4]编写一个计算速度的程序,它使用的距离和时间都是常量
class CalVelocity
{
private final double distance = 100.0;
private final double time = 3.5;
public double getVelocity() { return distance/time; }
} public class Velocity
{
public static void main(String[] args)
{
CalVelocity test = new CalVelocity();
System.out.println(test.getVelocity()+"km/h");
}
}

3.4

3.5-3.6

 package ex030506;

 //[3.5]创建一个名为Dog的类,它包含两个String域:name和says.在main方法中,创建两个Dog对象.
//一个名叫spot,叫声为“Ruff!”;另一个名为scruffy,叫声为"Wurf!".然后显示它们的名字和叫声.
//[3.6]在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象.测试用==和euqals()方法来比较所有引用的结果. class Dog
{
private String name;
private String says;
public Dog(String _name, String _says)
{
name=_name;
says=_says;
} public String getName() { return name; }
public String getSays() { return says; }
} public class TestEqual
{
public static void main(String[] args)
{
//[3.5]
Dog dogA = new Dog("spot" , "Ruff!");
Dog dogB = new Dog("scruffy" , "Wurf!");
System.out.println("dogA's name is " + dogA.getName()+" and says "+dogA.getSays());
System.out.println("dogB's name is " + dogB.getName()+" and says "+dogB.getSays()); //[3.6]
Dog dogindex = dogA;
System.out.print(dogindex==dogA); System.out.println(dogindex.equals(dogA));
System.out.print(dogA==dogB); System.out.println(dogA.equals(dogB));
System.out.print(dogindex==dogB); System.out.println(dogindex.equals(dogB));
}
}

3.5-3.6

3.7

 package ex0307;
//[0307]编写一个程序模拟随机扔硬币的结果 class Coin
{
private boolean state;
public boolean toss()
{
state=(Math.random() < 0.5);
return state;
}
}
public class TestCoin
{
public static void main(String[] args)
{
Coin onedollar = new Coin();
for(int i = 0; i < 20; ++i)
System.out.print(onedollar.toss() + " ");
}
}

3.7

3.8

 package ex0308;
//[3.8]展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果. public class TestOctHex
{
public static void main(String[] args)
{
long first = 256L;
long second = 0256L;
long third = 0x256L; System.out.println("first = " + Long.toBinaryString(first));
System.out.println("second = " + Long.toBinaryString(second));
System.out.println("third = " + Long.toBinaryString(third));
}
}

3.8

3.9

 package ex0309;
//[3.9]分别显示用float和double指数计数法所能表示的最大和最小的数字 public class ExpLimit
{
public static void main(String[] args)
{
double doublemax = Double.MAX_VALUE;
double doublemin = Double.MIN_VALUE;
float floatmax = Float.MAX_VALUE;
float floatmin = Float.MIN_VALUE; System.out.println("doublemax = " + doublemax);
System.out.println("doublemin = " + doublemin);
System.out.println("floatmax = " + floatmax);
System.out.println("floatmin = " + floatmin);
}
}

3.9

3.10

 package ex0310;
//[3.10]编写一个具有两个常量值的程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,其中最低有效位为1.
//取这两个值,用按位操作符以所有可能的方式结合运算它们,然后用Integer.toBinaryString()显示 public class TestBitOperation
{
public static void main(String[] args)
{
int first = 0x2AA;
int second =0x133;
System.out.println("first = " + Integer.toBinaryString(first));
System.out.println("sceond = " + Integer.toBinaryString(second));
System.out.println("first & second = " + Integer.toBinaryString(first & second));
System.out.println("first | second = " + Integer.toBinaryString(first | second));
System.out.println("first ^ second = " + Integer.toBinaryString(first ^ second));
System.out.println("~first = " + Integer.toBinaryString(~first));
System.out.println("~sceond = " + Integer.toBinaryString(~second));
}
}

3.10

3.11-3.12

 package ex031112;
//[3.11]以一个最高有效位为1的二进制数开始,用有符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都显示二进制字符串效果.
//[3.12]以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有二进制位都移出为止,每移一位都要显示二进制字符串效果. public class TestBitShift
{
public static void main(String[] args)
{
//[3.11]
int first = 0xF3A7;
while(first != 0)
{
first >>= 1;
System.out.println(Integer.toBinaryString(first));
} //[3.12]
int second = 0xFFFF;
second <<= 1;
System.out.println(Integer.toBinaryString(second));
while(second != 0)
{
second >>= 1;
System.out.println(Integer.toBinaryString(second));
}
}
}

3.11-3.12

3.13

 package ex0313;
//[3.13]编写一个方法,它以二进制形式显示char类型的值.使用多个不同的字符来展示它. class TestCharToBinaryString
{
public static String transform(char x)
{
return Integer.toBinaryString((int) x);
}
} public class CharToBinaryString
{
public static void main(String[] args)
{
char first = 'a';
char second = '&';
char third = '6'; System.out.println("a: " + TestCharToBinaryString.transform(first));
System.out.println("&: " + TestCharToBinaryString.transform(second));
System.out.println("6: " + TestCharToBinaryString.transform(third));
}
}

3.13

3.14

 package ex0314;
//[3.14]编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结构打印出来.
//做==和!=比较的同事,用equals()作测试.在main()里面用几个不同的字符串对象调用这个方法 import java.lang.String;
class StringCompare
{
public static void go(String first , String second)
{
System.out.println(first + " compareTo " + second + " : " + (first.compareTo(second)));
System.out.println(first + " != " + second + " : " + first != second);
System.out.println(first + " == " + second + " : " + first == second);
System.out.println(first + " equals " + second + " : " + first.equals(second));
System.out.println("");
}
} public class TestStringCompare
{
public static void main(String[] args)
{
String first = new String("Good");
String second = new String("Bad");
String third = first; StringCompare.go(first, second);
StringCompare.go(third, second);
StringCompare.go(first, third);
}
}

3.14

【TIJ4】第三章全部习题的更多相关文章

  1. C语言程序设计:现代方法(第2版)第三章全部习题答案

    前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解 ...

  2. 中级Perl 第三章课后习题

    3. 10. 1. 练习1 [25 分钟] 读当前目录的文件列表并转换成全路径.不能用shell 命令或外部程序读当前目 录.Perl 的File::Spec 和Cwd 两个模块对这个程序有帮助.每个 ...

  3. python语言程序设计基础(嵩天)第三章课后习题部分个人练习

    p69: *题3.5: 源代码: (1)print(30-3**2+8//3**2*10)     答案:21 (2)print(3*4**2/8%5)     答案:1.0 (3)print(2** ...

  4. java编程思想第四版第三章要点习题

    使用"简短的" 和正常的 打印语句来编写一个程序 package net.mindview.util; public class Print { /** * 不带有回车 * @pa ...

  5. 周志华-机器学习西瓜书-第三章习题3.5 LDA

    本文为周志华机器学习西瓜书第三章课后习题3.5答案,编程实现线性判别分析LDA,数据集为书本第89页的数据 首先介绍LDA算法流程: LDA的一个手工计算数学实例: 课后习题的代码: # coding ...

  6. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  7. 【转】《APUE》第三章笔记(4)及习题3-2

    原文网址:http://www.cnblogs.com/fusae-blog/p/4256794.html APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. ...

  8. 《APUE》第三章笔记(4)及习题3-2

    APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. dup和dup2函数:用来复制文件描述符的 sync函数,fsync函数和fdatasync函数:大致的功 ...

  9. 统计学习导论:基于R应用——第三章习题

    第三章习题 部分证明题未给出答案 1. 表3.4中,零假设是指三种形式的广告对TV的销量没什么影响.而电视广告和收音机广告的P值小说明,原假设是错的,也就是电视广告和收音机广告均对TV的销量有影响:报 ...

随机推荐

  1. Android目录结构(详解)

    Android目录结构(详解) 下面是HelloAndroid项目在eclipse中的目录层次结构: 由上图可以看出项目的根目录下共有九个文件(夹),下面就这九个文件(夹)进行详解: 1.1src文件 ...

  2. CF-1117C-Magic Ship

    二分 C - Magic Ship GNU C++11 Accepted 31 ms 1700 KB #include "bits/stdc++.h" using namespac ...

  3. 我的第一个Quartz代码

    创建Maven项目   打开Eclipse->File->Project->Maven ->Maven Project直接下一步输入Group Id和Artifact Id , ...

  4. 正负小数js正则表达式

    var reg = /^(([1-9]\d+(.[0-9]{1,4})?|\d(.[0-9]{1,4})?)|([-]([1-9]\d+(.[0-9]{1,4})?|\d(.[0-9]{1,4})?) ...

  5. mac下查找某个文件,which、whereis、find、locate

    which命令只是根据PATH环境变量查找. whereis命令只是根据标准可执行文件路径进行查找. 例如: 如果要找的不是可执行文件,而且想在整个系统上找,怎么办? find / -name xxx

  6. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  7. echarts柱状图宽度设置(react-native)

    const optionCategory = { color: ['#B5282A'], tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器, ...

  8. 自研测试框架ktest介绍(适用于UI和API)

    iTesting,爱测试,爱分享 在自动化测试的过程中,测试框架是我们绕不过去的一个工具,无论你是不需要写代码直接改动数据生成脚本,还是你需要检查测试结果甚至持续集成,测试框架都在发挥它的作用. 不同 ...

  9. 一致性 Hash 算法分析

    当我们在做数据库分库分表或者是分布式缓存时,不可避免的都会遇到一个问题: 如何将数据均匀的分散到各个节点中,并且尽量的在加减节点时能使受影响的数据最少. Hash 取模 随机放置就不说了,会带来很多问 ...

  10. Mybatis调用存储过程报错

    Mybatis调用存储过程 贴码 123456 Error querying database. Cause: java.sql.SQLException: User does not have ac ...