课后习题

1.1 DisplayThreeMessage.java

  1. public class DisplayThreeMessage {
  2. public static void main(String[] args) {
  3. System.out.println("Welcome to Java");
  4. System.out.println("Welcome to Computer Science");
  5. System.out.println("Programming is fun");
  6. }
  7. }

1.2 DisplayFiveMessage.java

  1. public class DisplayFIveMessage {
  2. public static void main(String[] args) {
  3. for (int i= 0;i<5;i++)
  4. System.out.println("Welcome to Java");
  5. }
  6. }

1.3 DisplayPattern.java

  1. public class DisplayPattern {
  2. public static void main(String[] args) {
  3. System.out.println(" J A V V A ");
  4. System.out.println(" J A A V V A A ");
  5. System.out.println("J J AAAAA V V AAAAA ");
  6. System.out.println(" J J A A V A A");
  7. }
  8. }

1.4 PrintTable.java

  1. public class PrintTable {
  2. public static void main(String[] args) {
  3. System.out.println("a\ta^2\ta^3");
  4. int i, square, cube;
  5. for (i = 1; i < 5; i++) {
  6. square = i * i;
  7. cube = i * i * i;
  8. System.out.print(i+"\t");
  9. System.out.print(square+"\t");
  10. System.out.println(cube+"\t");
  11. }
  12. }
  13. }

1.5 ComputationalExpression.java

  1. public class ComputationalExpression {
  2. public static void main(String[] args) {
  3. System.out.print("(9.5 * 4.5 - 2.5 * 3) / (45.5-3.5) = ");
  4. System.out.println((9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5));
  5. }
  6. }

1.6 SeriesSummation.java

  1. public class SeriesSummation {
  2. public static void main(String[] args) {
  3. int i,sum = 0;
  4. for(i = 1;i<10;i++)
  5. sum += i;
  6. System.out.println(sum);
  7. }
  8. }

1.7 ApproximationToPi.java

  1. public class ApproximationToPi {
  2. public static void main(String[] args) {
  3. double basic = 4, i, sum = 1, result;
  4. for (i = 1.0; i < 50000000; i++)
  5. sum += ((Math.pow(-1, i)) / (i * 2 + 1));
  6. result = sum * basic;
  7. System.out.println("The PI is:" + result);
  8. }
  9. }

1.8 CircleAreaAndCircumference.java

  1. import java.util.Scanner;
  2. /**
  3. * 源题目:半径5.5下的周长及面积
  4. * 扩展:任意半径下的圆的周长及面积
  5. */
  6. public class CircleAreaAndCircumference {
  7. public static void main(String[] args) {
  8. Scanner input = new Scanner(System.in);
  9. System.out.print("Enter the Radius: ");
  10. double Radius = input.nextDouble();
  11. double Circumference = Radius * 2 * Math.PI;
  12. double Area = Radius * Radius * Math.PI;
  13. System.out.println("The Circumference of this Circle is: " + Circumference
  14. + "\nAnd the Area is:" + Area);
  15. }
  16. }

1.9 RectangleAreaWithCircumference.java

  1. import java.util.Scanner;
  2. /**
  3. * 源题目:宽4.5 高7.9的矩形的面积、周长
  4. * 扩展:任意宽和高下的矩形面积、周长
  5. */
  6. public class RectangleAreaWithCircumference {
  7. public static void main(String[] args) {
  8. Scanner input = new Scanner(System.in);
  9. System.out.print("Enter the Width: ");
  10. double Width = input.nextDouble();
  11. System.out.print("Enter the Height: ");
  12. double Height = input.nextDouble();
  13. double Area = Width * Height;
  14. System.out.println("The Area of the Rectangle is : " + Area);
  15. }
  16. }

1.10 AverageSpeedByMile.java

  1. import java.util.Scanner;
  2. /**
  3. * 源题目:时间45分30秒,跑了14km,问平均速度---英里/小时
  4. * 扩展:键入时间,键入跑的总km数,输出平均速度
  5. */
  6. public class AverageSpeedByMile {
  7. public static void main(String[] args) {
  8. print();
  9. }
  10. static void print() {
  11. Scanner input = new Scanner(System.in);
  12. System.out.print("Enter the Hour: ");//输入小时数
  13. double Hour = input.nextDouble();
  14. System.out.print("Enter the Minute: ");//输入分钟数
  15. double Minute = input.nextDouble();
  16. System.out.print("Enter the Second: ");//输入秒数
  17. double Second = input.nextDouble();
  18. System.out.print("Enter the mile: ");//输入千米数
  19. double kilometer = input.nextDouble();
  20. double tempMinute = Second / 60;
  21. double tempHour = tempMinute + (Minute / 60);
  22. double Mile = kilometer / 1.6;
  23. Hour = Hour + tempHour;
  24. double Speed = Mile / Hour;
  25. System.out.println("The Average speed in Mile per hour is :" + Speed);
  26. }
  27. }

1.11 PopulationEstimates.java

  1. /**
  2. * 每7秒一个人诞生,每13秒一个死亡,每45秒一个移民迁入。
  3. * 显示未来5年的每年人口数。假设当前人口是: 312 032 486。每年有365天。
  4. * 假设有点问题,当前人口数是每7秒生 13秒死 45秒迁移,但是会随着人口基数的增长,从而改变这个参数值。
  5. * 可以对其进行数学建模,进行人口预测
  6. */
  7. public class PopulationEstimates {
  8. public static void main(String[] args) {
  9. System.out.println("The current population of American is 321 032 486.");
  10. double Nowpopulation = 321032486,yearsecond;
  11. int year;
  12. yearsecond = 60 * 60 * 24 * 365;
  13. double born = yearsecond / 7;//这里不做取整处理
  14. double dead = yearsecond / 13;
  15. double immigration = yearsecond / 45;
  16. double Addpopulation = born - dead + immigration;
  17. System.out.println(Addpopulation);
  18. for(year = 1;year <=5;year++){
  19. Nowpopulation = Nowpopulation + Addpopulation;
  20. System.out.println("The total population in the next "+ year + " years is :"+Nowpopulation);
  21. }
  22. }
  23. }

1.12 AverageSpeedByKilometer.java

备注:和1.10大体相同,代码段还有重构优化的空间,过段时间重构。

  1. import java.util.Scanner;
  2. public class AverageSpeedByKilometer {
  3. public static void main(String[] args) {
  4. print();
  5. }
  6. static void print() {//写成方法,避免代码冗余
  7. Scanner input = new Scanner(System.in);
  8. System.out.print("Enter the Hour: ");//输入小时数
  9. double Hour = input.nextDouble();
  10. System.out.print("Enter the Minute: ");//输入分钟数
  11. double Minute = input.nextDouble();
  12. System.out.print("Enter the Second: ");//输入秒数
  13. double Second = input.nextDouble();
  14. System.out.print("Enter the mile: ");//输入英里数
  15. double mile = input.nextDouble();
  16. double tempMinute = Second / 60;
  17. double tempHour = tempMinute + (Minute / 60);
  18. double kilometer = mile * 1.6;
  19. Hour = Hour + tempHour;
  20. double Speed = kilometer / Hour;
  21. System.out.println("The Average speed in Kilometer per hour is :" + Speed);
  22. }
  23. }

1.13 SystemOfLinearEquations.java

  1. import java.util.Scanner;
  2. /**
  3. * 利用Cramer规则解 2 * 2的线性方程组,假定ad - bc != 0;
  4. * ax = by = e cx + dy = f;
  5. * x = (ed - bf) / (ad-bc),y = (af-ec) / (ad-bc);
  6. *
  7. */
  8. public class SystemOfLinearEquations {
  9. public static void main(String[] args) {
  10. double a,b,c,d,e,f,x,y;
  11. Scanner input = new Scanner(System.in);
  12. System.out.println("Enter the parameter values of the equation: ");
  13. System.out.print("a:"); a = input.nextDouble();
  14. System.out.print("b:"); b = input.nextDouble();
  15. System.out.print("c:"); c = input.nextDouble();
  16. System.out.print("d:"); d = input.nextDouble();
  17. System.out.print("e:"); e = input.nextDouble();
  18. System.out.print("f:"); f = input.nextDouble();
  19. x = (e * d - b * f) / (a * d - b * c);
  20. y = (a * f - e * c) / (a * d - b * c);
  21. System.out.println("The result x is : "+x+"\nAnd y is : "+y);
  22. }
  23. }

关键术语

想弄成两列的,结果调完格式,发现不对头。只能改回来了。—-2021.4.10
Application Program Interface(API)(应用程序接口)
assembler(汇编器)
assembly language(汇编语言)
bit(比特)
block(块)
block comment(块注释)
bus(总线)
byte(字节)
bytecode(字节码)
bytecode verify(字节码验证器)
cable modem(电缆调制解调器)
Central Processing Unit(CPU)(中央处理器)
class loader(类加载器)
comment(注释)
compiler(编译器)
console(控制台)
dial-up modem(拨号调制解调器)
dot pitch(点距)
DSL(Digital Subscriber Line)(数字用户线)
encoding scheme(编码规范)
hardware(硬件)
high-level language(高级语言)
Integrated Development Environment(IDE)(集成开发环境)
interpreter(解释器)
java command(java命令)
Java Development Toolkit(JDK)(Java开发工具包)
Java language specification(Java语言规范)
Java Runtime Environment(JRE)(Java运行环境)
Java Virtual Machine(JVM)(Java虚拟机)
javac command(javac命令)
library(库)
line comment(行注释)
logic error(逻辑错误)
low-level language(低级语言)
machine language(机器语言)
main method(主方法)
memory(内存)
motherboard(主板)
Network Interface Card(NIC)(网络接口卡)
Operation System(OS)(操作系统)
pixel(像素)
program(程序)
programming(程序设计,编程)
runtime error(运行时错误)
screen resolution(屏幕分辨率)
software(软件)
source code(源代码)
source program(源程序)
state(语句)
statement terminator(语句结束符)
storage device(存储设备)
syntax error(语法错误)

本章小结

1.计算机是存储处理数据的电子设备。
2.计算机包括硬件软件两部分。
3.硬件是计算机中可以触摸到的物理部分。
4.计算机程序,也就是通常所说的软件,是一些不可见的指令,他们控制硬件完成任务。
5.计算机程序设计就是编写让计算机执行的指令(即代码)。
6.中央处理器 (CPU)是计算机的大脑。它从内存获取指令并且执行这些指令。
7.计算机使用0或1,是因为数字设备有两个稳定的状态,习惯上就是指0和1。
8.一个比特是指二进制数0或1。
9.一个字节是指8比特的序列。
10.千字节大约是1000字节,兆字节大约是100万字节,千兆字节大约是10亿字节,万亿字节大约是一万亿字节。
11.内存中存放CPU要执行的数据和程序指令。
12.内存单元是字节的有序序列。
13.内存是不能长久保存数据的,因为断电时信息就会丢失。
14.程序和数据永久地保存在存储设备里,当计算机真正需要使用它们时被移入内存。
15.机器语言是一套内嵌在每台计算机的基本指令集。
16.汇编语言是一种低级程序设计语言,它用助记符表示每一条机器语言的指令。
17.高级语言类似于英语,易于学习和编写程序。
18.用高级语言编写的程序称为源程序
19.编译器是将源程序翻译成机器语言程序的软件。
20.操作系统(OS)是管理和控制计算机活动的程序。
21.Java是平台无关的,这意味着只需编写一次程序,就可以在任何计算机上运行。
22.Java源程序文件名必须和程序中的公共类名一致,并且以扩展名.java结束。
23.每个类都被编译成一个独立的字节码文件,该文件名与类名相同,扩展名为 .class。
24.使用javac命令可以从命令行编译Java源代码文件。
25.使用java命令可以从命令行运行java类。
26.每个Java程序都是一组类定义的几何。关键字class引入类的定义,类的内容包含在块内。
27.一个块以左花括号({)开始,以右花括号(})结束。
28.方法包含在类中。每个可执行的Java程序必须有一个main方法。main方法是程序开始执行的入口。
29.Java中的每条语句都是以分号(;)结束的,也称为该符号的语句结束符
30.保留字或者称关键字,对编译器而言有特殊含义,在程序中不能用于其他目的。
31.在Java中,在一行上用两个斜杠(//)引导注释,称为行注释;在一行或多行用 // 包含注释,称为块注释或者段注释。编译器会忽略注释。
32.Java源程序是区分大小写的。
33.编程错误可以分为三类:语法错误运行时错误逻辑错误。编译器报告的错误称为语法错误或者编译错误。运行时错误是指引起程序非正常终止的错误。当一个程序没有按照预期的方式执行时,参数逻辑错误。