zoukankan      html  css  js  c++  java
  • JAVA语言程序设计课后习题----第四单元解析(仅供参考)

    1  本题水题,主要理解题目的意思即可,访问方法和修改方法可以通过快捷方式alt+insert选中你需要的成员变量即可

     1 public class Person {
     2     public String name;
     3     public int age;
     4     public static void main(String[] args) {
     5 //        new一个对象,对象名是person
     6         Person person =new Person();
     7 //        给name变量赋值
     8         person.setName("zzy");
     9 //        给age变量赋值
    10         person.setAge(21);
    11 //        对象的引用
    12         person.speak();
    13     }
    14 
    15     public String getName() {
    16         return name;
    17     }
    18 
    19     public void setName(String name) {
    20         this.name = name;
    21     }
    22 
    23     public int getAge() {
    24         return age;
    25     }
    26 
    27     public void setAge(int age) {
    28         this.age = age;
    29     }
    30 
    31   public void speak() {
    32       System.out.println(getName());
    33       System.out.println(getAge());
    34   }
    35 }

    2  本题水题,跟着题目的意思写出需求即可,注意的是在无参的构造函数调用有参的构造函数的方法,本题的centerX与centerY没有用到

     1 public class Circle {
     2     public double centerX,centerY,radius;
     3     public static void main(String[] args) {
     4         System.out.println("调用带参数的构造函数圆的各种数据:");
     5         Circle circle=new Circle(1.0);
     6         System.out.println("圆的半径:"+circle.getRadius());
     7         System.out.println("圆的面积:"+circle.getRrea());
     8         System.out.println("圆的周长:"+circle.getPerimeter());
     9         System.out.println("调用不带参数的构造函数圆的各种数据:");
    10         Circle circle1=new Circle();
    11         System.out.println("圆的半径:"+circle1.getRadius());
    12         System.out.println("圆的面积:"+circle1.getRrea());
    13         System.out.println("圆的周长:"+circle1.getPerimeter());
    14 
    15 
    16     }
    17 //访问
    18     public double getRadius() {
    19         return radius;
    20     }
    21 
    22     public void setRadius(double radius) {
    23         this.radius = radius;
    24     }
    25     public double getRrea(){
    26         return (Math.PI*radius*radius);
    27     }
    28     public double getPerimeter(){
    29         return 2*Math.PI;
    30     }
    31 
    32     public Circle(double radius) {
    33         this.radius = radius;
    34     }
    35     public Circle() {
    36 //      注意在无参的构造函数中调用有参的构造函数要用this
    37 //        具体在书本71详解
    38         this(1.0);
    39     }
    40 }

    3  本题水题,注意在无参的构造函数调用有参的构造函数的方法

     1 public class Rectangle {
     2     public double length,width;
     3 
     4     public static void main(String[] args) {
     5         Rectangle rectangle =new Rectangle(1.0,1.0);
     6         System.out.println("定义带参数的构造函数矩形:");
     7         System.out.println("长方形的长为:"+rectangle.getLength());
     8         System.out.println("长方形的宽为:"+rectangle.getWidth());
     9         System.out.println("长方形的面积为:"+rectangle.getArea());
    10         System.out.println("长方形的周长为:"+rectangle.getPerimeter());
    11         System.out.println("定义不带参数的构造函数矩形");
    12         Rectangle rectangle1 =new Rectangle();
    13 
    14         System.out.println("长方形的长为:"+rectangle1.getLength());
    15         System.out.println("长方形的宽为:"+rectangle1.getWidth());
    16         System.out.println("长方形的面积为:"+rectangle1.getArea());
    17         System.out.println("长方形的周长为:"+rectangle1.getPerimeter());
    18 
    19     }
    20 //          有参的构造方法
    21     public Rectangle(double length, double width) {
    22         this.length = length;
    23         this.width = width;
    24     }
    25 //    无参的构造方法
    26 public Rectangle(){
    27         this(1.0,1.0);
    28 }
    29 
    30     public double getLength() {
    31         return length;
    32     }
    33 
    34     public void setLength(double length) {
    35         this.length = length;
    36     }
    37 
    38     public double getWidth() {
    39         return width;
    40     }
    41 
    42     public void setWidth(double width) {
    43         this.width = width;
    44     }
    45 //    矩形的周长
    46     public double getPerimeter(){
    47         return 2*(length+width);
    48     }
    49 //    矩形的面积
    50     public double getArea(){
    51         return length*width;
    52     }
    53 }

    4  本题水题,主要是加深对构造方法的使用

     1 public class Triangle {
     2     public double a,b,c,s;
     3 
     4     public static void main(String[] args) {
     5         Triangle triangle = new Triangle(3,4,5);
     6         System.out.println("调用带三个参数的构造函数:");
     7         System.out.println("三角形的面积为:"+triangle.area());
     8         System.out.println("调用默认构造函数:");
     9         Triangle triangle1 =new Triangle();
    10         System.out.println("三角形的面积为:"+triangle1.area());
    11 
    12     }
    13 //    有参的构造方法
    14     public Triangle(double a, double b, double c) {
    15         this.a = a;
    16         this.b = b;
    17         this.c = c;
    18         s=(a+b+c)/2;
    19     }
    20 //    无参的构造方法
    21     public Triangle() {
    22         this.a = 0.0;
    23         this.b = 0.0;
    24         this.c = 0.0;
    25         s=(a+b+c)/2;
    26     }
    27 //    三角形的面积
    28     public double area(){
    29         return Math.sqrt(s*(s-a)*(s-b)*(s-c));
    30     }
    31 }

    5  本题主要是加深对访问方法与修改方法的使用

     1 public class Stock {
     2     public String symbol,name;
     3     public double perviousPrice,currentPrice;
     4 
     5     public static void main(String[] args) {
     6         Stock stock =new Stock("600000","浦发银行");
     7         stock.setPerviousPrice(25.5);
     8         stock.setCurrentPrice(28.6);
     9         System.out.print("代号为:"+stock.symbol+"的"+stock.name);
    10         System.out.print("当前的市值变化百分比为:"+String.format("%.1f",stock.getChangPercent()));
    11 
    12     }
    13 //    返回从当前一日价格到当前价格变换的百分比
    14     public double getChangPercent(){
    15         return getCurrentPrice()-getPerviousPrice();
    16     }
    17 //     有参的构造方法
    18     public Stock(String symbol, String name) {
    19         this.symbol = symbol;
    20         this.name = name;
    21     }
    22 //      返回储存股票的前一日收盘价
    23     public double getPerviousPrice() {
    24         return perviousPrice;
    25     }
    26 //      设置储存股票的前一日收盘价
    27     public void setPerviousPrice(double perviousPrice) {
    28         this.perviousPrice = perviousPrice;
    29     }
    30 //      返回存储股票的当前价格
    31     public double getCurrentPrice() {
    32         return currentPrice;
    33     }
    34 //      设置存储股票的当前价格
    35     public void setCurrentPrice(double currentPrice) {
    36         this.currentPrice = currentPrice;
    37     }
    38 }

    6  Fibonacci数就是第一项第二项都是1从第三项开始是前两项之和,可以通过调用方法来实现,里面用if else语句

     1 public class Fibonacci {
     2 //    调用方法
     3     public static long fib(int n){
     4 //        如果n==1或者2就返回1
     5         if(n==1||n==2)
     6             return 1;
     7         else
     8             return fib(n-2)+fib(n-1);
     9     }
    10     public static void main(String[] args) {
    11         for (int i = 1; i <= 20; i++) {
    12             System.out.print(fib(i)+" ");
    13 //            每10项进行一次换行
    14             if (i % 10 ==0)
    15                 System.out.println();
    16         }
    17 
    18     }
    19 }

    7  本题主要考的是方程的判别式问题 与方程的根求解问题,记住这些基本公式问题就不大

     1 public class QuadraticEquation {
     2    private double a,b,c;
     3     public static void main(String[] args) {
     4         QuadraticEquation quadraticEquation =new QuadraticEquation(1,3,1);
     5         if (quadraticEquation.getDiscriminant()<0)
     6             System.out.println("方程无根");
     7         if (quadraticEquation.getDiscriminant()==0)
     8             System.out.println(quadraticEquation.getRoot1());
     9         if (quadraticEquation.getDiscriminant()>0)
    10             System.out.println("x1="+quadraticEquation.getRoot1()+" "+"x2="+quadraticEquation.getRoot2());
    11 
    12     }
    13 //      带有参数的构造方法
    14     public QuadraticEquation(double a, double b, double c) {
    15         this.a = a;
    16         this.b = b;
    17         this.c = c;
    18     }
    19 
    20     public double getA() {
    21         return a;
    22     }
    23 
    24     public double getB() {
    25         return b;
    26     }
    27 
    28     public double getC() {
    29         return c;
    30     }
    31 //    判别式
    32     public double getDiscriminant(){
    33         return b*b-4*a*c;
    34     }
    35 //    方程一个根
    36     public double getRoot1(){
    37         return (-b+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;
    38     }
    39 //    方程第二个根
    40     public double getRoot2(){
    41         return (-b-+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;
    42     }
    43 }

    8  本题就是对成员变量的基本运用注意题目意思即可

     1 public class TV {
     2     private int channel,volumeLevel;
     3     private boolean on;
     4 
     5     public static void main(String[] args) {
     6     }
     7 //    无参构造函数
     8     public TV(){
     9     }
    10 //    电视开
    11     public void turnOn(){
    12         on = true;
    13     }
    14 //    电视关
    15     public void turnOff(){
    16         on = false;
    17     }
    18 //      设置频道
    19     public void setChannel(int channel) {
    20         this.channel = channel;
    21     }
    22 //      设置音量
    23     public void setVolumeLevel(int volumeLevel) {
    24         this.volumeLevel = volumeLevel;
    25     }
    26 //    频道+开关
    27     public void channelUp(){
    28         channel++;
    29     }
    30 //    频道-开关
    31     public void channelDown(){
    32         channel--;
    33     }
    34 //    声音+开关
    35     public void volumeUp(){
    36         volumeLevel++;
    37     }
    38 //    声音-开关
    39     public void volumeDown(){
    40         volumeLevel--;
    41     }
    42 }

    9  本题主要是对基本概念的理解,比如偶数、奇数的判断

      1 public class MyInteger {
      2     private int value;
      3 
      4     public static void main(String[] args) {
      5 
      6         MyInteger myInteger =new MyInteger(10);
      7         System.out.println("value:");
      8         System.out.println("偶数:"+myInteger.isEven());
      9         System.out.println("奇数:"+myInteger.isOdd());
     10         System.out.println("素数:"+myInteger.isPrime());
     11         System.out.println("返回参数15:");
     12         System.out.println("偶数:"+myInteger.isEven(15));
     13         System.out.println("奇数:"+myInteger.isOdd(15));
     14         System.out.println("素数:"+myInteger.isPrime(15));
     15        // char []a={'a','b','c'};
     16      // myInteger.parseInt(a);
     17         char A[]={'1','2','3'};
     18         //myInteger.parseInt(A);
     19         System.out.println(myInteger.parseInt(A));
     20         String s = "123";
     21         System.out.println(myInteger.parseInt(s));
     22     }
     23 
     24     public MyInteger(int value) {
     25         this.value = value;
     26     }
     27 
     28     public int getValue() {
     29         return value;
     30     }
     31 //    判断value是否为偶数
     32     public boolean isEven(){
     33         return value%2==0;
     34     }
     35 //    判断value是否为奇数
     36     public boolean isOdd(){
     37         return value%2!=0;
     38     }
     39 //    判断返回参数整数是否为素数
     40     public boolean isPrime(){
     41         int i;
     42         for(i=2;i<value;i++) {
     43             if (value % i == 0)
     44                 break;
     45         }
     46             if (value==i)
     47                 return true;
     48             return false;
     49     }
     50 //    判断返回参数整数是否为偶数
     51     public boolean isEven(int number){
     52         return number%2==0;
     53 
     54     }
     55 //    判断返回参数整数是否为奇数
     56     public boolean isOdd(int number){
     57         return number%2!=0;
     58 
     59     }
     60 //    判断返回参数整数是否为素数
     61     public boolean isPrime(int number){
     62         int i;
     63         for(i=2;i<number;i++) {
     64             if (number % i == 0)
     65                 break;
     66         }
     67         if (number==i)
     68             return true;
     69         return false;
     70 
     71     }
     72 //    判断返回参数整数对象是否为偶数
     73     public boolean isEven(MyInteger myInteger){
     74         return myInteger.value % 2 ==0;
     75 
     76     }
     77 //    判断返回参数整数对象是否为奇数
     78     public boolean isOdd(MyInteger myInteger){
     79         return myInteger.value % 2 != 0;
     80     }
     81 //    判断返回参数整数对象是否为素数
     82     public boolean isPrime(MyInteger myInteger){
     83 
     84         int i;
     85         for(i=2;i<myInteger.value;i++) {
     86             if (myInteger.value % i == 0)
     87                 break;
     88         }
     89         if (myInteger.value==i)
     90             return true;
     91         return false;
     92     }
     93 //    比较当前对象整数与参数整数
     94     public boolean equals(int b){
     95         return b==value;
     96 
     97     }
     98 //    比较当前对象整数与参数整数对象
     99     public boolean equals(MyInteger myInteger){
    100         return myInteger.value==value;
    101 
    102     }
    103 //    将参数字符数组转换为整数
    104     public int parseInt(char []a){
    105         String s=new String(a);
    106         return parseInt(s);
    107     }
    108 //    将参数字符串转换成整数
    109     public int parseInt(String s){
    110         return Integer.valueOf(s);
    111     }
    112 
    113 }

    10  本题主要考的是回文数与素数,我们首先判断这个数是否为素数,在这个基础上再判断是否为回文数,素数就是只能被1和本身整除的数,回文数就是一个数第一位与最后一位相等,第二位与倒数第二位相等,比如11、101

     1 public class HuiSu {
     2     public int i,j,k,count=0;
     3 
     4     public static void main(String[] args) {
     5         HuiSu huiSu =new HuiSu();
     6         huiSu.print();
     7     }
     8     public void print(){
     9         for ( j = 2; j < 1000 ; j++) {
    10             for ( k = 2; k <1000 ; k++) {
    11                 if (j%k==0)
    12                     break;
    13             }
    14 //            判断是是否是素数
    15             if (j == k)
    16             {
    17 //                判断是否为回文数
    18                 if (j / 10 == 0)
    19                 {
    20                     count++;
    21                     System.out.print(j+" ");
    22                     if (count % 10==0)
    23                         System.out.println();
    24                 }
    25               if (j / 10==j%10)
    26                 {
    27                     count++;
    28                     System.out.print(j+" ");
    29                     if (count % 10==0)
    30                         System.out.println();
    31                 }
    32                  if(j / 100==j % 10)
    33                 {
    34                     count++;
    35                     System.out.print(j+" ");
    36                     if (count % 10 == 0 && count != 20)
    37                         System.out.println();
    38                 }
    39             }
    40         }
    41     }
    42 }

    10  本题虽然是最后一题,但是仍然是水题,就是对成员变量的运用

     1 import java.time.LocalDate;
     2 
     3 public class Account {
     4     private int id;
     5     private double balance,annulRate;
     6     private LocalDate dateCreated;
     7 
     8     public static void main(String[] args) {
     9         Account account = new Account(123,0.0);
    10         System.out.println("尊敬的:"+account.id+"用户"+"恭喜你开通账户");
    11         System.out.println("你的账户余额为:"+account.balance);
    12         //创建账户的时间
    13         account.getDateCreated();
    14         //存款
    15         account.deposit(500);
    16         //取款
    17         account.withdraw(10);
    18         System.out.println("你要办理的业务:");
    19         account.setAnnulRate(3.6);
    20         account.getMonthlyInterestRate();
    21 
    22     }
    23 //  有参构造函数
    24     public Account(int id, double balance) {
    25         this.id = id;
    26         this.balance = balance;
    27     }
    28 //    无参构造函数
    29     public Account() {
    30     }
    31 
    32     public int getId() {
    33         return id;
    34     }
    35 //  返回账户余额
    36     public double getBalance() {
    37         return balance;
    38     }
    39 //  返回存款的年利率
    40 public double getAnnulRate() {
    41         return annulRate;
    42     }
    43 //获取当前日期
    44     public LocalDate getDateCreated() {
    45        // LocalDate  dateCreated= LocalDate.now();
    46         LocalDate today = LocalDate.now();
    47        today.getYear();
    48         System.out.println("您账户创建的日期为:"+LocalDate.now());
    49        // System.out.println(today.getYear()+"/"+today.getMonthValue()+"/"+today.getDayOfMonth()+"/");
    50         return dateCreated;
    51     }
    52 //    返回月利率的方法
    53 public double getMonthlyInterestRate(){
    54     System.out.println("您存款的月利率为:"+annulRate/12);
    55         return annulRate/12;
    56 
    57 }
    58 //取款方法
    59 public void withdraw(double amount){
    60         balance -= amount;
    61     System.out.println("取款金额为:"+amount);
    62     System.out.println("当前余额为:"+balance);
    63 
    64 }
    65 //  存款方法
    66 public void deposit(double amount){
    67         balance += amount;
    68     System.out.println("存款金额为:"+amount);
    69     System.out.println("当前余额为:"+balance);
    70 }
    71 //  设置账户ID
    72     public void setId(int id) {
    73         this.id = id;
    74     }
    75 //  设置账户的余额
    76     public void setBalance(double balance) {
    77         this.balance = balance;
    78     }
    79 //      修改存款的年利率
    80     public void setAnnulRate(double annulRate) {
    81         System.out.println("您的存款年利率为:"+annulRate);
    82         this.annulRate = annulRate;
    83     }
    84 }
  • 相关阅读:
    怎样才能让您的网站看起来很专业 ?
    JavaScript slice() 方法
    Jquery日历控件
    100w数据,查询只要1秒(转)
    我的WCF之旅(1):创建一个简单的WCF程序(转载)
    名企面试官精讲典型编程题之C#篇(转自CSDN)
    day01
    Delphi初浅入门笔记之十二:多媒体编程五(绘制文字篇)
    Delphi初浅入门笔记之四:过程与函数(函数篇)
    Delphi初浅入门笔记之三:过程和函数(过程篇)
  • 原文地址:https://www.cnblogs.com/PerZhu/p/10865271.html
Copyright © 2011-2022 走看看