Java语言程序设计(第12版)个人复习 第一章
文章目录
- Java语言程序设计(第12版)个人复习 第一章
- 第一章知识点
- 关键术语
- 课后习题
- 1.1 (Display three messages)
- 1.2 (Display five messages)
- 1.3(Display a pattern)
- 1.4(Print a table)
- 1.5(Compute expressions)
- 1.6(Summation of a series)
- 1.7(Approximate pi)
- 1.8(Area and perimeter of a circle)
- 1.9(Area and perimeter of a rectangle)
- 1.10(Average speed in miles)
- 1.11(Population projection)
- 1.12(Average speed in kilometers)
- 1.13(Algebra: solve 2 * 2 linear equations)
第一章知识点
语法错误
1.10 . 1 语法错误
在编译过程中出现的错误称为语法错误 ( syntax error ) 或编译媒(compile error ) 。 语法错误是由创建代码时的错误引起的 , 例如 : 拼错关键字 ,忽略了一些必要的标点符号 , 或者左花括号没有对应的右花括号 。 这些错误通常很容易检测到 ,因为编译器会告诉你这些错误在哪儿,以及是什么原因造成的。
1 public class ShowSyntaxErrors {
2 public static main(String[] args) {
3 System.out.println("Welcome to Java);
4 }
5 }
四个错误被报告 , 但实际上程序有两个错误 :
第 2 行 main 方法前遗漏关键字 void 。第 3 行的字符串 Welcome to Java 应该加上引号 。由于一个错误常常会显示很多行的编译错误 , 因此 , 从最上面的行开始向下纠正错误是一个很好的习惯 。 解决程序前面出现的错误 , 可能就改正了程序后面出现的其他错误.
运行时错误
运行时错误 ( runtime error ) 是引起程序非正常中断的错误 。 运行应用程序时 , 当环境检测到一个不可能执行的操作时 , 就会出现运行时错误 。 输入错误是典型的运行时错误 。 当程序等待用户输入一个值 , 而用户输人了一个程序不能处理的值时 , 就会发生输入错误 。 例如 : 如果程序希望读入的是一个数值 , 而用户输人的却是一个字符串 , 就会导致程序出现数据类型错误 。
另一个常见的运行时错误是 0 作除数,当整数除法中除数为 0时可能引发这种情况。例如:下面程序清单 1-5 中的程序将会导致运行时错误,如图所示。
1 public class ShowRuntimeErrors {
2 public static void main(String[] args) {
3 System.out.println(1 / 0);
4 }
5 }
逻辑错误
当程序没有按预期的方式执行时就会发生逻辑错误(logic error):这种错误发生的原因有很多种。例如,假设你编写如程序清单1-6 中的程序,将摄氏 35 度转换为华氏度
1 public class ShowLogicErrors {
2 public static void main(String[] args) {
3 System.out.print("Celsius 35 is Fahrenheit degree ");
4 System.out.println((9 / 5) * 35 + 32);
5 }
6 }
你将得到结果华氏 67 度,而这是错误的,结果应该是 95.0。Java 中,整数相除是返回 除法的整数部分,即小数部分被截掉,因此 Java 中 9/5 的结果是丨。要得到正确的结果,需 要使用 9.0/5, 这样得到结果 1.8。
通常情况下 ,因为编译器可以明确指出错误的位置以及出错的原因 , 所以语法错误是很容易发现和纠正的 。 运行时错误也不难找 , 因为在程序异常中止时 , 错误的原因和位置都会显示在控制台上 。 然而 , 査找逻辑错误就很富有挑战性 。 在下面的章节中 , 我们将学习跟踪程序以及找到逻辑错误的技巧
关键术语
课后习题
1.1 (Display three messages)
Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun.
public class DisplayThreeMessages {
public static void main(String[] args) {
System.out.println("Welcome to Java");
System.out.println("Welcome to Computer Science");
System.out.println("Programming is fun");
}
}
1.2 (Display five messages)
Write a program that displays Welcome to Java five times.
public class DisplayFiveMessages {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
System.out.println("Welcome to Java");
}
}
}
1.3(Display a pattern)
Write a program that displays the following pattern:
public class DisplayAPattern {
public static void main(String[] args) {
System.out.println(" J A V V A ");
System.out.println(" J A A V V A A");
System.out.println("J J AAAAA V V AAAAA");
System.out.println(" JJ A A V A A");
}
}
1.4(Print a table)
Write a program that displays the following table:
public class PrintATable {
public static void main(String[] args) {
System.out.println("a"+"\t"+"a^2"+"\t"+"a^3");
for (int i = 1; i < 5; i++) {
mothed(i);
}
}
public static void mothed(int a){
System.out.println(a+"\t"+a*a+"\t"+a*a*a);
}
}
“\t"代替空格” "打印表单整齐格式
1.5(Compute expressions)
Write a program that displays the result of
public class ComputeExpressions {
public static void main(String[] args) {
double result = (9.5*4.5-2.5*3.0)/(45.5 - 3.5);
System.out.print(result);
}
}
1.6(Summation of a series)
Write a program that displays the result of
public class SummationOfASeries {
public static void main(String[] args) {
int result = 0;
for(int i = 1; i < 10; i++) {
result += i;
}
System.out.print("The result of 1+2+3+4+5+6+7+8+9 is"+" "+result);
}
}
1.7(Approximate pi)
pi can be computed using the following formula:
public class ApproximatePi {
public static void main(String[] args) {
double num1 = 4.0*(1.0-1.0/3.0+1.0/5.0-1.0/7.0+1.0/9.0-1.0/11.0);
double num2 = 4.0*(1.0-1.0/3.0+1.0/5.0-1.0/7.0+1.0/9.0-1.0/11.0+1.0/13.0);
System.out.println(num1);
System.out.println(num2);
}
}
1.8(Area and perimeter of a circle)
Write a program that displays the area and perimeter of a circle that has a radius of 5.5 using the following formulas:
perimeter = 2 * radius * pi
area = radius * radius * pi
public class AreaAndPerimeterOfACircle {
public static void main(String[] args) {
double radius = 5.5;
System.out.println("The area is:"+area(radius));
System.out.println("The perimeter is:"+perimeter(radius));
}
public static double area(double radius){
double area = radius*radius*Math.PI;
return area;
}
public static double perimeter(double radius) {
double perimeter = 2 * radius * Math.PI;
return perimeter;
}
}
1.9(Area and perimeter of a rectangle)
Write a program that displays the area and perimeter of a rectangle with a width of 4.5 and a height of 7.9 using the following formula:
area = width * height
public class AreaAndPerimeterOfARectangle {
public static void main(String[] args) {
double width = 4.5;
double height = 7.9;
System.out.println("The area is:"+area(width,height));
System.out.println("The perimeter is:"+perimeter(width,height));
}
public static double area(double width,double height){
double area = width*height;
return area;
}
public static double perimeter(double width,double height) {
double perimeter = 2 * width+2 * height;
return perimeter;
}
}
1.10(Average speed in miles)
Assume that a runner runs 14 kilometers in 45 minutes and 30 seconds. Write a program that displays the average speed in miles per hour. (Note 1 mile is equal to 1.6 kilometers.)
public class AverageSpeedInMiles {
public static void main(String[] args) {
double hours = 45.0/60+30.0/60/60;
double distance = 14.0/1.6;
double speed = distance/hours;
System.out.print(speed);
}
}
1.11(Population projection)
The U.S. Census Bureau projects population based on the following assumptions:
■ One birth every 7 seconds
■ One death every 13 seconds
■ One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume that the current population is 312,032,486, and one year has 365 days. Hint: In Java, if two integers perform division, the result is an integer. The fractional part is truncated. For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.
public class PopulationProjection {
public static void main(String[] args) {
double time = 365*24*60*60;
int born =(int)(time/7);
int death =(int)(time/13);
int immigration =(int) (time/45);
double year1 = 312032486 + born - death + immigration;
double year2 = year1 + born - death + immigration;
double year3 = year2 + born - death + immigration;
double year4 = year3 + born - death + immigration;
double year5 = year4 + born - death + immigration;
System.out.println("The number of people after five years:"+ year1);
System.out.println("The number of people after five years:"+ year2);
System.out.println("The number of people after five years:"+ year3);
System.out.println("The number of people after five years:"+ year4);
System.out.println("The number of people after five years:"+ year5);
}
}
1.12(Average speed in kilometers)
Assume that a runner runs 24 miles in 1 hour, 40minutes, and 35 seconds. Write a program that displays the average speed in kilometers per hour. (Note 1 mile is equal to 1.6 kilometers.)
public class AverageSpeedInKilometers {
public static void main(String[] args) {
double hour = 1 + 40.0/60 + 35.0/60/60;
double distance = 24*1.6;
System.out.print("The speed is:"+(distance/hour));
}
}
1.13(Algebra: solve 2 * 2 linear equations)
You can use Cramer’s rule to solve the following 2 * 2 system of linear equation provided that ad – bc is not 0:
Write a program that solves the following equation and displays the value for x and y: (Hint: replace the symbols in the formula with numbers to compute x and y. This exercise can be done in Chapter 1 without using materials in later chapters.)
public class Algebra {
public static void main(String[] args) {
double a = 3.4;
double b = 50.2;
double e = 44.5;
double c = 2.1;
double d = 0.55;
double f = 5.9;
double x = (e*d - b*f)/(a*d - b*c);
double y = (a*f - e*c)/(a*d - b*c);
System.out.println("x is :"+x);
System.out.println("y is :"+y);
}
}