当前位置:   article > 正文

java语言程序设计基础篇第九章编程练习题

九章编程
  1. 1
  2. import java.util.Scanner;
  3. public class Main{
  4. public static void main(String[] args){
  5. Rectangle R1 = new Rectangle(40,4);
  6. Rectangle R2 = new Rectangle(35.9,3.5);
  7. System.out.println(R1.getArea() + "\n" + R2.getArea());
  8. System.out.println(R1.getPerimeter() + "\n" + R2.getPerimeter());
  9. }
  10. }
  11. class Rectangle{
  12. double width = 1;
  13. double height = 1;
  14. Rectangle() {
  15. }
  16. Rectangle(double h, double w){
  17. height = h;
  18. width = w;
  19. }
  20. double getArea(){
  21. return width*height;
  22. }
  23. double getPerimeter(){
  24. return 2*(width+height);
  25. }
  26. }
  27. 2
  28. import java.util.Scanner;
  29. public class Main {
  30. public static void main(String[] args) {
  31. Stock obj = new Stock("ORCL","Oracle Corporation");
  32. obj.previousClosingPrice = 34.5;
  33. obj.currentPrice = 34.35;
  34. System.out.println(obj.getChangePercent());
  35. }
  36. }
  37. class Stock{
  38. String symbol;
  39. String name;
  40. double previousClosingPrice = 0.0;
  41. double currentPrice = 0.0;
  42. public Stock(String sym, String na){
  43. symbol = sym;
  44. name = na;
  45. }
  46. public String getChangePercent(){
  47. return Math.abs(((currentPrice-previousClosingPrice)/previousClosingPrice))*100+"%";
  48. }
  49. }
  50. 3
  51. import java.util.Scanner;
  52. import java.util.Date;
  53. public class Main{
  54. public static void main(String[] args){
  55. long num = 10000;
  56. Date date = new Date(num);
  57. System.out.println(date.toString());
  58. date.setTime(num*10);
  59. System.out.println(date.toString());
  60. date.setTime(num*100);
  61. System.out.println(date.toString());
  62. date.setTime(num*1000);
  63. System.out.println(date.toString());
  64. date.setTime(num*num);
  65. System.out.println(date.toString());
  66. date.setTime(num*num*10);
  67. System.out.println(date.toString());
  68. date.setTime(num*num*100);
  69. System.out.println(date.toString());
  70. date.setTime(num*num*1000);
  71. System.out.println(date.toString());
  72. }
  73. }
  74. 4
  75. import java.util.Scanner;
  76. import java.util.Random;
  77. public class Main{
  78. public static void main(String[] args){
  79. Random random = new Random(1000);
  80. for(int i = 0; i < 50; ++i){
  81. System.out.print(random.nextInt(100) + " ");
  82. if(i%10 == 0)
  83. System.out.println();
  84. }
  85. }
  86. }
  87. 5
  88. import java.util.Scanner;
  89. import java.util.GregorianCalendar;
  90. //我很纳闷为什么输出结果是一样的
  91. public class Main{
  92. public static void main(String[] args){
  93. GregorianCalendar calendar = new GregorianCalendar();
  94. System.out.println(calendar.YEAR + ":" + calendar.MONTH + ":" + calendar.DAY_OF_MONTH);
  95. calendar.setTimeInMillis(1234567898765L);
  96. System.out.println(calendar.YEAR + ":" + calendar.MONTH + ":" + calendar.DAY_OF_MONTH);
  97. }
  98. }
  99. 6
  100. import java.util.Scann
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/798235
推荐阅读
相关标签
  

闽ICP备14008679号