第三章 操作符 练习题

/** * Created by Sandy.Liu on 2018/7/10. *//** * Thinking in java version 4, chapter 3, 3.4 * Write a program that calculates velocity using a constant distance * */public class Chap3Pra4VelocityCalc {    static float velocity(float d, float t){        if(t == 0) return 0f;        else return d/t;    }

    }
/** * Created by Sandy.Liu on 2018/7/10. */public class Chap3Pra4VelocityCalcTest {    public static void main(String[] args){        float d = 300f;        float t = 2.5f;        System.out.println("Distance d: "+d);        System.out.println("Time t: "+t);        float v = Chap3Pra4VelocityCalc.velocity(d,t);        System.out.println("Velocity v: "+v);    }}
/** * Created by Sandy.Liu on 2018/7/10. *  Thinking in java version 4, chapter 3, 3.4, practice 5 *  Create a dog class which includes two string object "name" and "says", create two dog objects */public class Chap3Prac5Dog {    String name, says;}
/** * Created by Sandy.Liu on 2018/7/10. */public class Chap3Prac5DogTest {    public static void main(String[] args){        Chap3Prac5Dog dog1 = new Chap3Prac5Dog();        Chap3Prac5Dog dog2 = new Chap3Prac5Dog();        dog1.name = "Spot";        dog1.says = "Ruff!";        dog2.name = "Scruff";        dog2.says = "Wurf!";        System.out.println("Dog "+dog1.name + " says: "+dog1.says);        System.out.println("Dog "+dog2.name + " says: "+dog2.says);

    }

}
/** * Created by Sandy.Liu on 2018/7/10. *  Thinking in java version 4, chapter 3, 3.4, pracetice 6 *  Create a class called Dog containing two Strings: name and says. *  Following exercise 5, create a new God reference and assign it to spot‘s object. *  Testing comparison using == and equals() fow all references. */public class Chap3Prac6DogIndex {    String name;    String says;    void setName(String n){        name = n;    }    void setSays(String s){        says = s;    }    void showName(){        System.out.println(name);    }    void showSays(){        System.out.println(says);    }}
/** * Created by Sandy.Liu on 2018/7/10. */public class Chap3Prac6DogTest {    public static void main(String[] args){

        Chap3Prac6DogIndex spot = new Chap3Prac6DogIndex();        spot.setName("Spot");        spot.setSays("Ruff!");        Chap3Prac6DogIndex scruffy = new Chap3Prac6DogIndex();        scruffy.setName("Scruffy");        scruffy.setSays("Wurf!");        spot.showName();        spot.showSays();        scruffy.showName();        scruffy.showSays();

        Chap3Prac6DogIndex butch = new Chap3Prac6DogIndex();        butch.setName("Butch");        butch.setSays("hello");        butch.showName();        butch.showSays();

        System.out.println("Comparison: ");        P.print("spot == butch: "+(spot == butch));        P.print("spot.equals(butch): "+(spot.equals(butch)));        P.print("butch.equals(spot): " + (butch.equals(spot)));

        P.print("Now assign: butch = spot");        butch = spot;

        P.print("Compare again: ");        P.print("spot == butch: "+(spot == butch));        P.print("spot.equals(butch): "+(spot.equals(butch)));        P.print("butch.equals(spot): " + (butch.equals(spot)));

        P.print("spot: ");        spot.showName();        spot.showSays();

        P.print("butch: ");        butch.showName();        butch.showSays();    }}
/** * Created by Sandy.Liu on 2018/7/11. * Write a program that simulates coin-plipping */import java.util.*;public class Chap3Prac7CoinFlap {    public static void main(String[] args){        Random ran = new Random();        int num1;        num1 = ran.nextInt(30);        P.print("num1: "+num1);        if(num1%2==0) P.print("This is National emblem");        else P.print("This is heads");

    }}
/** * Created by Sandy.Liu on 2018/7/11. * Write a program that simulates coin-plipping */import java.util.*;public class Chap3Prac7CoinFlap {    public static void main(String[] args){        Random ran = new Random();        int num1;        num1 = ran.nextInt(30);        P.print("num1: "+num1);        if(num1%2==0) P.print("This is National emblem");        else P.print("This is heads");

    }}
/** * Created by Sandy.Liu on 2018/7/11. * thinking in java, version 4, chapter 3, practice 9 * display the minimum and maximum both for float and double by using e. */public class Chap3Prac9EMeans10 {    public static void main(String[] args){        float minf = Float.MIN_VALUE;        float maxf = Float.MAX_VALUE;

        double mind = Double.MIN_VALUE;        double maxd = Double.MAX_VALUE;

        P.print("minf: "+minf);        P.print("maxf: "+maxf);

        P.print("mind: "+mind);        P.print("maxd: "+maxd);

    }}
/** * Created by Sandy.Liu on 2018/7/11. * Write a program with two constant values, one has alternative binary ones and zeros, * with a one in the last significant digit, and another also be alternative,with a zero in the last significant digit * Display them with Integer.toBinaryString() * Take these two values and combine them in all possible ways using the bitwise operators, and dispaly * the result using Integer.toBinary.String() */public class Chap3Prac10 {    public static void main(String[] args){        int i1 = 0x2f;        int i2 = 0x2e;        P.print("i1: "+Integer.toBinaryString(i1));        P.print("i2: "+Integer.toBinaryString(i2));        P.print("i1 & i2: "+Integer.toBinaryString(i1&i2));        P.print("i1 | i2: "+Integer.toBinaryString(i1|i2));        P.print("i1 ^ i2: "+Integer.toBinaryString(i1^i2));        P.print("~i1: "+Integer.toBinaryString(~i1));        P.print("~i2: "+Integer.toBinaryString(~i2));    }}
/** * Created by Sandy.Liu on 2018/7/12. * Thinking in java, version 4, chapter 3, practice 11 * Right shift a hex number which has digital 1 in the highest place, until all of its binary positions * are out of number 1. Display result every step. */public class Chap3Pra11RightShift {    public static void main(String[] args){        int i = 0x10000000;        print("i: ",i);        for(int j=0;j<29;j++){            print("i>>=1: ",i>>=1);        }    }

    static void print(String s, int i){        P.print(s+"int: "+i+", binary:\n"+ Integer.toBinaryString(i));    }}
/** * Created by Sandy.Liu on 2018/7/12. * Thinking in java, version 4, Chapter 3, practice 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.toBinaryString(). */public class Chap3Prac12UnsignedRightShift {    public static void main(String[] args){        int i = 0xffff;        P.print("int i: "+i +", binary i: "+ Integer.toBinaryString(i));        i<<=1;        P.print("i<<1: int i: "+i +", binary i: "+ Integer.toBinaryString(i));        for(int j = 0;j<17;j++){            i>>>=1;            P.print("i>>>1: "+"int i: "+i+" , Binary: "+Integer.toBinaryString(i));        }    }

}
/** * Created by Sandy.Liu on 2018/7/13. * Thinking in java, chapter 3, practice 13 * Write a method that displays char values in binary form. * Demonstrate it using several different characters. */public class Chap3Prac13CharToBinary {    public static void main(String[] args){        char c=‘c‘;       print("c: ", c);        char c1 = ‘a‘;        print("a: ", c1);        char c2 = ‘b‘;        print("0: ",c2);        char c3=‘A‘;        print("A: ", c3);        for(int i=0;i<26;i++){            print(""+i+": ",(char)i);        }

    }

    static void print(String s, char c){        P.print(s+ Integer.toBinaryString(c));    }}
/** * Created by Sandy.Liu on 2018/7/17. * Write a method which receive two string values, use all kind of boolean values‘s compare relations to compare these * two values, and print the result. Use equals() to test when use == and !=. * Use different string values to test this method. */public class Chap3Prac14StringValue {    static void StringValuesTest(String s1, String s2){        P.print(s1 +"=="+s2+": "+(s1==s2));        P.print(s1+"!="+s2+": "+(s1!=s2));        P.print(s1+".equals("+s2+"): "+s1.equals(s2));        P.print(s2+".equals("+s1+"): "+s2.equals(s1));        P.print(s1+"+"+s2+": "+(s1+s2));    }    public static void main(String[] args){        StringValuesTest("one", "two");        StringValuesTest("one", "one");        StringValuesTest("one", "onetwo");    }}

原文地址:https://www.cnblogs.com/xiaohai2003ly/p/9383359.html

时间: 2024-11-09 00:45:59

第三章 操作符 练习题的相关文章

perl_note——第三章 操作符

第三章 操作符 一.算术操作符 二.整数比较操作符 三.字符串比较操作符 四.逻辑操作符 五.位操作符 六.赋值操作符 七.自增自减操作符 八.字符串联结和重复操作符 九.逗号操作符 十.条件操作符 十一.操作符的次序 一.算术操作符 :+(加).-(减).*(乘)./(除).**(乘幂).%(取余).-(单目负)   (1)乘幂的基数不能为负,如 (-5) ** 2.5 # error:   (2)乘幂结果不能超出计算机表示的限制,如10 ** 999999 # error   (3)取余的操

perl5 第三章 操作符

第三章 操作符 by flamephoenix 一.算术操作符二.整数比较操作符三.字符串比较操作符四.逻辑操作符五.位操作符六.赋值操作符七.自增自减操作符八.字符串联结和重复操作符九.逗号操作符十.条件操作符十一.操作符的次序 一.算术操作符 :+(加).-(减).*(乘)./(除).**(乘幂).%(取余).-(单目负)  (1)乘幂的基数不能为负,如 (-5) ** 2.5 # error:  (2)乘幂结果不能超出计算机表示的限制,如10 ** 999999 # error  (3)取

Thinking In Java笔记(第三章 操作符)

第三章 操作符 3.2使用Java操作符 操作符接受一个或者多个参数,并生成一个新值.参数的形式和普通方法调用不同,但是效果是相同的.普通的加减乘除和正负号都是和其他编程语言类似. 有些操作符可能会产生"副作用",改变操作数的值,这些擦佐夫最普通的用途就是用来产生副作用的.使用此类操作符产生的值和没有副作用的操作符产生的值没区别. 几乎左右的操作符都只能操作"基本类型",例外的是"=","==","!=",

java编程思想--第三章 操作符

额...继续搞些容易忽略的东西在下面,这章没打算精读,赶紧过,好戏应该在后面. 1.基本类型的赋值 比如 : int a =1; int b = a; 是进行值的复制,以后改变了a的值对b没有影响 引用类型的复制 比如: List<String> list1 = new  ArrayList<String>(); List<String> list2 = list1; 是进行的引用的复制,list1与list2 会同时指向 那个 ArrayList ,一个改变了它的状态

C# 本质论 第三章 操作符和控制流

操作符通常分为3大类:一元操作符(正.负).二元操作符(加.减.乘.除.取余)和三元操作符( condition?consequence:alternative(consequence和alternative表达式类型要一致) ),它们对应的操作数分别是一个.两个和三个. 要是永远括号增加代码可读性. 要用符合格式化而不是加法操作符来拼接字符串. 可以用char类型相减求两字母距离. float具有七位精度: 错误代码: float n1 = 0.987654321; 正确代码: float n

第三章课后练习题

1.画出流程图编程实现,如果用户名等于字符 ' 青 ',且密码等于数字 123,则输出" 欢迎你,青 ":否则输出 " 对不起,你不是青 ". 2.画出流程图并编程实现,如果年龄满 7 岁,或者年龄满 5 岁并且性别是"男",就可以搬动桌子.          3.从键盘上输入一个整数,分别赋给整形变量a.b.c,然后将输入的整数从小到大的顺序放在变量a.b.c中,并输出三个变量的值. 4.从键盘上输入一个整数,判断是否能被 3 或者 5 整除.

《Java编程思想》笔记 第三章 操作符

1.操作符种类: 运算顺序1-7 1.1 一元操作符(单目操作符)  - 负号, + 正号,--递减,++递增 1.2 算术操作符 + - *  /  % 1.3 移位操作符  <<左移(低位补0),>>右移(负数高位1,正数高位补0), >>>无符号右移 (无论正负高位补0)(对二进制) (可与 = 结合使用 <<=     >>=   >>>=  类似 i += 1) 移位操作符详解 1.4 关系操作符 >  &

ThinkingInJava第三章操作符

优先级:() 算术操作符+-*/% 赋值= String类的链接符 + += 区分引用类型的t1=t2 t1.level=t2.level是不同的. ++ -- 自增自减 a++ ++a区别 关系操作符 > < >= <= == != (比较东西) 逻辑操作符 || && !  这个是短路或短路与  |  &单个的不会短路 直接常量:toBinaryString(); L F D long a=100L; 指数计数法:1.39E-42f;1.39*10的-4

第三章 函数练习题

文件处理相关 1,编码问题 (1)请问python2与python3中的默认编码是什么? 1 2 python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII python 3.x默认的字符编码是unicode,默认的文件编码也是utf-8 (2)为什么会出现中文乱码,你能举例说明乱码的情况有哪几种? 1 2 3 4 5 6 7 8 9 无论以什么编码在内存里显示字符,存到硬盘上都是2进制,所以编码不对,程序就会出错了. (ascii编码(美国),GBK编码(中国),shift_