• 【TIJ4】第三章全部习题


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

    3.1

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

    3.2

     1 package ex0302;
     2 //[3.2]创建一个包含一个float域的类,并用这个类来展示别名机制
     3 class ShowByname
     4 {
     5     private float elem;
     6     public float getElem() { return elem; }
     7     public void setElem(float _elem) { elem = _elem; }
     8 }
     9 
    10 public class Byname 
    11 {
    12     public static void main(String[] args)
    13     {
    14         ShowByname test1 = new ShowByname();
    15         test1.setElem(1.333f);
    16         System.out.println("test1.elem = " + test1.getElem());
    17         
    18         ShowByname test2 = test1;
    19         System.out.println("test2.elem = " + test2.getElem());
    20         
    21         //仅修改引用test2并观察
    22         test2.setElem(3.14159f);
    23         System.out.println("test1.elem = " + test1.getElem());
    24         System.out.println("test2.elem = " + test2.getElem());
    25     }
    26 }
    3.2

    3.3

     1 package ex0303;
     2 //[3.3]创建一个包含一个float域的类,并用这个方法来展示方法调用时的别名机制
     3 
     4 class ShowFunByname
     5 {
     6     public float elem = 3.14f;
     7 }
     8 
     9 public class FunByname 
    10 {
    11     static void f(ShowFunByname test)
    12     {
    13         test.elem = 1.414f;
    14     }
    15     
    16     public static void main(String[] args)
    17     {
    18         ShowFunByname test = new ShowFunByname();
    19         System.out.println("1: elem = " + test.elem);
    20         
    21         f(test);
    22         System.out.println("2: elem = " + test.elem);
    23     }
    24 }
    3.3

    3.4

     1 package ex0304;
     2 
     3 //[3.4]编写一个计算速度的程序,它使用的距离和时间都是常量
     4 class CalVelocity
     5 {
     6     private final double distance = 100.0;
     7     private final double time = 3.5;
     8     public double getVelocity() { return distance/time; }
     9 }
    10 
    11 public class Velocity 
    12 {
    13     public static void main(String[] args)
    14     {
    15         CalVelocity test = new CalVelocity();
    16         System.out.println(test.getVelocity()+"km/h");
    17     }
    18 }
    3.4

    3.5-3.6

     1 package ex030506;
     2 
     3 //[3.5]创建一个名为Dog的类,它包含两个String域:name和says.在main方法中,创建两个Dog对象.
     4 //一个名叫spot,叫声为“Ruff!”;另一个名为scruffy,叫声为"Wurf!".然后显示它们的名字和叫声.
     5 //[3.6]在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象.测试用==和euqals()方法来比较所有引用的结果.
     6 
     7 class Dog
     8 {
     9     private String name;
    10     private String says;
    11     public Dog(String _name, String _says)
    12     {
    13         name=_name;
    14         says=_says;
    15     }
    16     
    17     public String getName() { return name; }
    18     public String getSays() { return says; }
    19 }
    20 
    21 public class TestEqual 
    22 {
    23     public static void main(String[] args)
    24     {
    25         //[3.5]
    26         Dog dogA = new Dog("spot" , "Ruff!");
    27         Dog dogB = new Dog("scruffy" , "Wurf!");
    28         System.out.println("dogA's name is " + dogA.getName()+" and says "+dogA.getSays());
    29         System.out.println("dogB's name is " + dogB.getName()+" and says "+dogB.getSays());
    30         
    31         //[3.6]
    32         Dog dogindex = dogA;
    33         System.out.print(dogindex==dogA); System.out.println(dogindex.equals(dogA));
    34         System.out.print(dogA==dogB); System.out.println(dogA.equals(dogB));
    35         System.out.print(dogindex==dogB); System.out.println(dogindex.equals(dogB));
    36     }
    37 }
    3.5-3.6

    3.7

     1 package ex0307;
     2 //[0307]编写一个程序模拟随机扔硬币的结果
     3 
     4 class Coin
     5 {
     6     private boolean state;
     7     public boolean toss()
     8     {
     9         state=(Math.random() < 0.5);
    10         return state;
    11     }
    12 }
    13 public class TestCoin
    14 {
    15     public static void main(String[] args)
    16     {
    17         Coin onedollar = new Coin();
    18         for(int i = 0; i < 20; ++i)
    19             System.out.print(onedollar.toss() + " ");
    20     }
    21 }
    3.7

    3.8

     1 package ex0308;
     2 //[3.8]展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果.
     3 
     4 public class TestOctHex 
     5 {
     6     public static void main(String[] args)
     7     {
     8         long first = 256L;
     9         long second = 0256L;
    10         long third = 0x256L;
    11         
    12         System.out.println("first = " + Long.toBinaryString(first));
    13         System.out.println("second = " + Long.toBinaryString(second));
    14         System.out.println("third = " + Long.toBinaryString(third));
    15     }
    16 }
    3.8

    3.9

     1 package ex0309;
     2 //[3.9]分别显示用float和double指数计数法所能表示的最大和最小的数字
     3 
     4 public class ExpLimit 
     5 {
     6     public static void main(String[] args)
     7     {
     8         double doublemax = Double.MAX_VALUE;
     9         double doublemin = Double.MIN_VALUE;
    10         float floatmax = Float.MAX_VALUE;
    11         float floatmin = Float.MIN_VALUE;
    12         
    13         System.out.println("doublemax = " + doublemax);
    14         System.out.println("doublemin = " + doublemin);
    15         System.out.println("floatmax = " + floatmax);
    16         System.out.println("floatmin = " + floatmin);
    17     }
    18 }
    3.9

    3.10

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

    3.11-3.12

     1 package ex031112;
     2 //[3.11]以一个最高有效位为1的二进制数开始,用有符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都显示二进制字符串效果.
     3 //[3.12]以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有二进制位都移出为止,每移一位都要显示二进制字符串效果.
     4 
     5 public class TestBitShift 
     6 {
     7     public static void main(String[] args)
     8     {
     9         //[3.11]
    10         int first = 0xF3A7;
    11         while(first != 0)
    12         {
    13             first >>= 1;
    14             System.out.println(Integer.toBinaryString(first));
    15         }
    16         
    17         //[3.12]
    18         int second = 0xFFFF;
    19         second <<= 1;
    20         System.out.println(Integer.toBinaryString(second));
    21         while(second != 0)
    22         {
    23             second >>= 1;
    24             System.out.println(Integer.toBinaryString(second));
    25         }
    26     }
    27 }
    3.11-3.12

    3.13

     1 package ex0313;
     2 //[3.13]编写一个方法,它以二进制形式显示char类型的值.使用多个不同的字符来展示它.
     3 
     4 class TestCharToBinaryString
     5 {
     6     public static String transform(char x)
     7     {
     8         return Integer.toBinaryString((int) x);
     9     }
    10 }
    11 
    12 public class CharToBinaryString 
    13 {
    14     public static void main(String[] args)
    15     {
    16         char first = 'a';
    17         char second = '&';
    18         char third = '6';
    19         
    20         System.out.println("a: " + TestCharToBinaryString.transform(first));
    21         System.out.println("&: " + TestCharToBinaryString.transform(second));
    22         System.out.println("6: " + TestCharToBinaryString.transform(third));
    23     }
    24 }
    3.13

    3.14

     1 package ex0314;
     2 //[3.14]编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结构打印出来.
     3 //做==和!=比较的同事,用equals()作测试.在main()里面用几个不同的字符串对象调用这个方法
     4 
     5 import java.lang.String;
     6 class StringCompare
     7 {
     8     public static void go(String first , String second)
     9     {
    10         System.out.println(first + " compareTo " + second + " : " + (first.compareTo(second)));
    11         System.out.println(first + " != " + second + " : " + first != second);
    12         System.out.println(first + " == " + second + " : " + first == second);
    13         System.out.println(first + " equals " + second + " : " + first.equals(second));
    14         System.out.println("");
    15     }
    16 }
    17 
    18 
    19 public class TestStringCompare 
    20 {
    21     public static void main(String[] args)
    22     {
    23         String first = new String("Good");
    24         String second = new String("Bad");
    25         String third = first;
    26         
    27         StringCompare.go(first, second);
    28         StringCompare.go(third, second);
    29         StringCompare.go(first, third);
    30     }
    31 }
    3.14
  • 相关阅读:
    Balanced Substring
    解决vscode可以编译通过c++项目,但头文件有红色波浪线的问题
    Poj2299---Ultra-QuickSort
    树状数组
    内置函数
    函数式编程,尾调用,map函数,filter函数,reduce函数
    函数作用域和匿名函数
    函数参数/局部变量与全局变量/前向引用(函数即变量)
    函数的定义
    字符串格式化
  • 原文地址:https://www.cnblogs.com/catnip/p/4345068.html
Copyright © 2020-2023  润新知