JAVA语言程序设计基础篇原书第八版上机编程题_第1页
JAVA语言程序设计基础篇原书第八版上机编程题_第2页
JAVA语言程序设计基础篇原书第八版上机编程题_第3页
JAVA语言程序设计基础篇原书第八版上机编程题_第4页
JAVA语言程序设计基础篇原书第八版上机编程题_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、High Level Programming Language JavaAssignments and Programming Exercises 高级语言程序设计Java作业和上机练习题原书第八版39Chapter 1 Introduction to Computers, Programs, and Java Programming Exercise: 1.11.1 为程序清单1-1创建名为Welcome.java的文件,可以使用任何编辑器,将该文件保存为文本格式。public class Exercise01_01 public static void main(String args)

2、System.out.println("Welcome to Java"); System.out.println("Welcome to Computer Science"); System.out.println("Programming is fun"); Chapter 2 Elementary Programming 第二章 基本程序设计Programming Exercises: 2.3, 2.72.3import java.util.Scanner;public class Exercise02_03 public st

3、atic void main(String args) / TODO Auto-generated method stub Scanner input =new Scanner(System.in); System.out.println("Enter a value for feet: "); int feet = input.nextInt(); double meters = feet * 0.305; System.out.println(feet +"feet is" + meters +"meters" ); 2.7imp

4、ort java.util.Scanner;public class Exercise02_07 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println("Enter the number of minutes: "); int minutes = input.nextInt(); int hours = minutes / 60 ; int days = hours / 24 ; int years = days / 365 ; int

5、remainingdays = days % 365 ; System.out.print(minutes + "minutes is approximately" + years + "and" + remainingdays + "days." ); Chapter 3 Selections 第三章 选择Programming Exercises: 3.6, 3.7, and 3.193.6import java.util.Scanner;public class Exercise03_06 public static void

6、main(String args) / TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("Enter weight in pounds "); double weight = input.nextDouble(); System.out.print("Enter height in feet and inches "); double feet = input.nextDouble(); double inches = inp

7、ut.nextDouble(); final double KILOGRAM_PER_POUND = 0.45359237; final double METER_PER_INCH = 0.0254; final double METER_PER_FEET = 0.3048; double weightInKilograms = weight * KILOGRAM_PER_POUND; double heightInMeters = feet * METER_PER_FEET + inches *METER_PER_INCH; double bmi = weightInKilograms /

8、(heightInMeters*heightInMeters); System.out.println("Your BMI is " + bmi); if (bmi < 16) System.out.println("Your are seriously underweight"); else if (bmi < 18) System.out.println("You are underweight"); else if (bmi < 24) System.out.println("You are norm

9、al weight "); else if (bmi <29) System.out.println("You are overweight"); else if (bmi <35) System.out.println("You are seriously overweight"); else System.out.println("You are gravely overweight"); 3.7import java.util.Scanner;public class Exercise03_07 publi

10、c static void main(String args) Scanner input = new Scanner(System.in); System.out.print("Enter an amout in double,for example 23.67"); double amount = input.nextDouble(); int remainingAmount = (int)(amount*100); int numbersOfOneDollars = remainingAmount /100; remainingAmount = remainingAm

11、ount %100; int numbersOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount %25; int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount %10; int numberOfNickels = remainingAmount /5; remainingAmount = remainingAmount %5; int numberOfPennies = remainingAmount; Stri

12、ng output = "Your amount " + amount + "consists of n " + numbersOfOneDollars + (numbersOfOneDollars>1)?"dollars":"dollar")+"n"+ numbersOfQuarters + (numbersOfQuarters>1)?"quarters":"quarter")+"n"+ numberOfDimes + (

13、numberOfDimes>1)?"dimes":"dime")+"n"+ numberOfNickels + (numberOfNickels>1)?"nickels":"nickel")+"n"+ numberOfPennies + (numberOfPennies>1)?"pennies":"penny")+"n" System.out.println( output ); 3-19 Writ

14、e a program that reads three edges for a triangle and determines whether the input is valid. The input is valid if the sum of any two edges is greater than the third edge. For example, if your input for the three edges is 1, 2, and 1, the output should be:Can edges 1,2, and 1 form a triangle? False3

15、.19 (三角形有效性验证)编写程序,读入三角形的三条边并确定输入是否有效。如果任意两条边的和大于第三条边则输入有效。例如,如果输入的三条边是1,2,1,输出应该是:Can edges 1, 2, and 1 from a triangle? false如果输入的三条边是2,2,1,输出应该是:Can edges 2, 2 and 1 from a triangle? Trueimport java.util.Scanner;public class Exercise03_19 public static void main(String args) Scanner input = new S

16、canner(System.in); System.out.println("Enter three edges : "); double sideA = input.nextDouble(); double sideB = input.nextDouble(); double sideC = input.nextDouble(); boolean triangle = (sideA + sideB > sideC && sideA + sideC > sideB && sideB + sideC > sideA); Sys

17、tem.out.println("Can edges" + sideA +","+sideB + "and" + sideC + "form a triangle ?"+ triangle); if(triangle = true) double perimeter = sideA+sideB+sideC; System.out.print("The perimeter of the triangle is" + perimeter); else System.out.println("

18、;invalid"); Chapter 4 Loops 第四章 循环Programming Exercises: 4.3, 4.17, and 4.19 (5.3, 5.17, and 5.19 in the 10th LiveLab )4.3public class Exercise05_03 public static void main(String args) System.out.println("kilogram pounds"); int i =1; double j= 2.2; while (i<200) System.out.println

19、(i+" "+j); i+; j=i*2.2; 4.17import java.util.Scanner;public class Exercise05_17 public static void main(String args) / TODO Auto-generated method stub System.out.print("Enter the number of lines: "); Scanner input = new Scanner(System.in); int lines = input.nextInt(); for (int ro

20、w = 1;row<=lines;row+) for(int space=1;space<= lines - row;space+ ) System.out.print(" "); for(int m = row; m>=1;m-) System.out.print(m); for(int n =2;n<=row;n+) System.out.print(n); System.out.println();4.19public class Exercise05_19 public static void main(String args) for (i

21、nt row = 1;row<=8;row+) for(int space=1;space<= 8 - row;space+ ) System.out.print(" "); for(int m =1; m<=row;m+) System.out.print(int)Math.pow(2,m-1)+" "); for(int n =row;n>=2;n-) System.out.print(int)Math.pow(2,n-2)+" "); System.out.println();Chapter 5 Meth

22、ods 第五章 方法Programming Exercises: 5.5, 5.9, and 5.19 (6.5, 6.9, and 6.19 in the 10th LivLab)5.5 (Sorting three numbers) Write the following method to display three numbers in increasing order. Use the following method header:public static void sort(double d1, double d2, double d3)2 (对三个数排序)编写以下方法,按升序

23、显示三个数。public static void sort(double num1, double num2, double num3) import java.util.Scanner;public class Exercise06_05 /* * param args */public static void main(String args) / TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("Enter three numbers"

24、); double i = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble();displaySortedNumbers(i,y,z); public static void displaySortedNumbers(double num1,double num2,double num3) if(num1>=num2&&num2>=num3) System.out.println(num3+" "+num2+" "+n

25、um1); if(num3>=num1&&num1>=num2) System.out.println(num2+" "+num1+" "+num3); if(num1>=num3&&num3>=num2) System.out.println(num2+" "+num3+" "+num1); if(num3>=num2&&num2>=num1) System.out.println(num1+" "+num2+

26、" "+num3); if(num2>=num3&&num3>=num1) System.out.println(num1+" "+num3+" "+num2); if(num2>=num1&&num1>=num3) System.out.println(num3+" "+num1+" "+num2); 5.9 public class Exercise06_09 public static void main(String args) S

27、ystem.out.print("Feet Meters Meters Feet"); System.out.println(); double chujiao = 1.0; double chumi = 20.0; for(int i = 1;i<=10;i+) double jiemi = footToMeter(chujiao); double jiejiao = meterToFoot(chumi); System.out.println(chujiao+" "+jiemi+" "+" "+chumi

28、+" "+jiejiao); chujiao+; chumi = chumi+5.0; public static double footToMeter(double foot) double meter = foot*0.305; return meter; public static double meterToFoot(double meter) double foot = meter/0.305; return foot; 5.19import java.util.Scanner;/* Design: . */* Coding: Please indent and

29、format your code properly */* Copy and paste your code to replace the template below */* Note that your class must be named Exercise06_19 */public class Exercise06_19 public static void main(String args) System.out.println("Enter three sides for a triangel: "); Scanner input = new Scanner(

30、System.in); double num1= input.nextDouble(); double num2= input.nextDouble(); double num3= input.nextDouble(); System.out.print("Is these num could form a triangel?"); if(isValid(num1,num2,num3) System.out.print("The input is valid.And the ares is: "+area(num1,num2,num3); else Sy

31、stem.out.print("The input is invalid."); public static boolean isValid(double num1,double num2,double num3) if(num1+num2>num3&num2+num3>num1&num1+num3>num2) return true; else return false; public static double area(double num1,double num2,double num3) double s=(num1+num2+n

32、um3)/2; double area = Math.sqrt(s*(s-num1)*(s-num2)*(s-num3); return area; Chapter 6 Single-Dimensional Arrays 第六章 一维数组Programming Exercises: 6.1, 6.3, and 6.11 (7.1, 7.3, and 7.11 in the 10th LivLab)6.1import java.util.Scanner;public class Exercise07_01 public static void main(String args) / TODO A

33、uto-generated method stub System.out.println("Enter the number of students: "); Scanner input = new Scanner(System.in); int number = input.nextInt(); int best = 0; int grades = new intnumber; System.out.println("Enter "+number+" scores: "); for(int i =0;i<grades.leng

34、th;i+) gradesi = input.nextInt(); if(gradesi>best) best = gradesi; for(int i = 0;i<grades.length;i+) if(gradesi>=best - 10) System.out.println("Student "+i+" score is "+gradesi+" and grade is A"); else if (gradesi>=best - 20) System.out.println("Student

35、 "+i+" score is "+gradesi+" and grade is B"); else if (gradesi>= best - 30) System.out.println("Student "+i+" score is "+gradesi+" and grade is C"); else if (gradesi>= best - 40) System.out.println("Student "+i+" score is &q

36、uot;+gradesi+" and grade is D"); else System.out.println("Student "+i+" score is "+gradesi+" and grade is F"); 6.3 Write a program that reads ten numbers and displays distinct numbers (i.e., if a numbers appears multiple times, it is displayed only once). Hint

37、: read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.6.3 (输出不同的数)编写一个程序,读入10个数并且显示其中相互不同的数(即一个数多次出现,仅显示一次)。提示:读入一个数,如果它是一个新数,则把它存储在数组中;如果数组中已有该数,则把它丢弃。输入结束后,数组中的数都是不同的数。import java.util.Scan

38、ner;public class Exercise07_03 public static void main(String args) / TODO Auto-generated method stub int count = new int100; System.out.println("Enter the integers between 1 and 100: "); Scanner input = new Scanner(System.in); int integer = input .nextInt(); while(integer != 0) countinteg

39、er-1+; integer = input.nextInt(); for (int i = 0; i < 100; i+) if (counti > 0) System.out.println(i+1) + " occurs " + counti + (counti = 1) ? " time " : " times "); 6.11import java.util.Scanner;public class Exercise07_11 public static void main(String args) Scanne

40、r input = new Scanner(System.in); System.out.print("Enter ten numbers: "); int numberOfNum = 10; int mean = 0; int deviation =0; double numbers = new doublenumberOfNum; for(int i = 0; i<numbers.length; i+) numbersi = input.nextDouble(); System.out.print("The mean is "+mean(num

41、bers); System.out.print("The standard deviation is "+deviation( numbers); public static double mean(double numbers) double sum = 0; for(int i = 0; i<numbers.length; i+) sum += numbersi; double mean = sum/10.0; return mean; public static double deviation(double numbers) double sum = 0; f

42、or(int i = 0; i<numbers.length; i+) sum += numbersi; double mean = sum/10.0; double x = 0; for(int i = 0; i<numbers.length; i+) x+=Math.pow(numbersi-mean ,2); double deviation = Math.sqrt(x/9); return deviation; Chapter 8 Objects and Classes 第八章 对象和类Programming Exercises: 8.1, 8.7, 8.9, and 8.

43、11 (9.1, 9.7, 9.9, and 9.11 in the 10th LivLab)8.1 The Rectangle classDesign a class named Rectangle to represent a rectangle. The class contains:l Two double data fields named width and height with default values to 1.0 to denote the width and the height of the rectangle.l A string data field named

44、 color that specifies the color of a rectangle. The default value is white.l A no-arg constructor that creates a default rectangle.l A constructor that creates a rectangle with the specified width and height.l The accessor and mutator methods for all three data fieldsl A method named getArea() that

45、returns the area of this rectangle.l A method named getPerimetet() that returns the perimeter of this rectangle.l A method named toString() that returns a string to descript the rectangle.The formula to compute the area of a rectangle: width*height . The toString() method is implemented as follows:R

46、eturn “Rectangle: width= ” + width + “ height = ”+ height;Write a test program that creates two Rectangle object. Assign width 5 and height 15 to the first object and width 4.3 and height 40.5 to the second object. Assign color yellow to both Rectangle objects .Displays the properties of both object

47、s and find their areas and perimeters.1 (矩形类Rectangle)编写名为Rectangle的类表示矩形,这个类包括: 两个double类型的数据域width和height表示矩形的宽和高,它们的默认值都为1; String类型的数据域color表示矩形的颜色,进一步假设所有矩形的颜色都是相同的,默认颜色为白色; 无参构造方法创建默认矩形; 一个构造方法创建指定width和height的矩形; 所有三个数据域的访问器方法和修改器方法; getArea()方法返回该矩形的面积; getPerimeter()方法返回它的周长;画出该类的UML图并实现它。编

48、写一个测试程序,创建两个Rectangle对象,设置第一个对象的宽为4,高为40,第二个对象的宽为3.5,高为35.9,所有Rectangle对象的颜色为红色。显示两个对象的属性并求它们的面积和周长。public class Exercise09_01 public static void main(String args) Rectangle myRectangle = new Rectangle(4, 40); System.out.println("The area of a rectangle with width " + myRectangle.width + &

49、quot; and height " + myRectangle.height + " is " + myRectangle.getArea(); System.out.println("The perimeter of a rectangle is " + myRectangle.getPerimeter(); Rectangle yourRectangle = new Rectangle(3.5, 35.9); System.out.println("The area of a rectangle with width "

50、; + yourRectangle.width + " and height " + yourRectangle.height + " is " + yourRectangle.getArea(); System.out.println("The perimeter of a rectangle is " + yourRectangle.getPerimeter(); class Rectangle double width;double height;Rectangle()width = 1.0;height = 1.0;Recta

51、ngle(double newWidth , double newHeight)width = newWidth;height = newHeight; double getArea()return width*height; double getPerimeter() return (width+height)*2.0; 8.7 (The Account class) Design a class nanmed Account that contains: l A private&#

52、160;int data field named id for the account (default 0). l A private double data field named balance for the account (default 0). l A private double data field named

53、 annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. l A private Date data field named datecreated that 

54、;stores the date when the account was createdl A no-arg constructor that creates a default account. l A constructor that creates an account with the specified id and init

55、ial balance. l The accessor and mutator methods for id, balance, and annualInterestRate, l The accessor method for datecreated. l A method named getmonthlylnterestRateo that returns the

56、 monthly interest ratel A method named withdraw that withdraws a specified amount from the accountl A method named deposit that deposits a specifred amount to the account.

57、0;Draw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000 and an annual interest rate of 4.5%. Use the withdraw method to withd

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论