第三章 操作符 练习题

Posted xiaohai2003ly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三章 操作符 练习题相关的知识,希望对你有一定的参考价值。

/**
* 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: "+ 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");
}
}

以上是关于第三章 操作符 练习题的主要内容,如果未能解决你的问题,请参考以下文章

网络操作系统第三章习题

网络操作系统第三章课后习题解答

python第三天 文件的操作习题

操作系统第三单元思考题和练习题

JAVA-初步认识-第三章-语句-switch练习

笨办法学 Python(第三版)习题 17: 更多文件操作