• 有关于 this 的基础练习题


    目录

    题目1:实现求某个double 数组的最大值,并返回

    题目2:  要求查找某字符串是否在字符串数组中,并返回索引 , 如果 找不到 ,返回 -1

    题目3:  要求实现更改某本书的价格比如:如果价格 > 150 ,更改为 150 ,如果价格 > 100 ,更改为 100 , 否则不变

    题目4:  要求实现数组的复制功能 copyArr ,输入旧数组,返回一个新数组,元素和旧数组一样 

    题目5:  请提供显示圆周长功能的方法以及显示圆面积的方法

    题目6:  要求编程创建一个 Cale 计算类 ,在其中定义两个变量表示两个操作数,并且定义四个方法实现求和、差、乘、商(要求除数为0的话,需要提示)并创建两个对象

    题目7:  请定义一个  Music 类,类里面有音乐名 name 、音乐时长 times 属性,并有播放 play 功能和返回本身属性信息的功能方法 getInfo


    题目1:实现求某个double 数组的最大值,并返回

     思路分析:

    1.  类名 A01  

    2.  方法名:max  

    3.  形参(double [ ] )  

    4.  返回值 double

    1. package thisHome;
    2. public class Homework01 {
    3. public static void main(String[] args) {
    4. A01 a01 = new A01();
    5. double[] arr = {1.2,23.4,55.6};
    6. Double res = a01.max(arr);
    7. if (res != null) {
    8. System.out.println("arr的最大值=" + res);
    9. }else {
    10. System.out.println("arr 输入有误,数组不能够为null,或{}");
    11. }
    12. }
    13. }
    14. class A01 {
    15. public Double max(double[] arr) {
    16. //先判断arr是否为null 然后再判断length 是否 > 0
    17. if (arr != null && arr.length > 0) {
    18. //保证arr至少要有一个元素
    19. double max = arr[0]; //假定 第一个元素就是最大值
    20. for (int i = 1; i < arr.length; i++) {
    21. if (max < arr[i]) {
    22. max = arr[i];
    23. }
    24. }
    25. return max;
    26. }else {
    27. return null;
    28. }
    29. }
    30. }

    结果:

    题目2:  要求查找某字符串是否在字符串数组中,并返回索引 , 如果 找不到 ,返回 -1

    思路分析:
    1. 类名 A02
    2. 方法名 find
    3. 返回值 int
    4. 形参 (String ,String[] )

    代码实现:

    1. package thisHome;
    2. public class Homework02 {
    3. public static void main(String[] args) {
    4. String[] strs = {"jack", "tom", "mary", "milan"};
    5. A02 a02 = new A02();
    6. int index = a02.find("milan", strs);
    7. System.out.println("查找的index=" + index);
    8. }
    9. }
    10. class A02 {
    11. public int find(String findStr, String[] strs) {
    12. //直接遍历
    13. for (int i = 0; i < strs.length; i++) {
    14. if (findStr.equals(strs[i])) {
    15. return i;
    16. }
    17. }
    18. //如果没有 ,句返回 -1
    19. return -1;
    20. }
    21. }

    结果:

    题目3:  要求实现更改某本书的价格
    比如:如果价格 > 150 ,更改为 150 ,如果价格 > 100 ,更改为 100 , 否则不变

    思路分析:
    1. 类名  Book
    2. 属性 price ,name
    3. 方法名  updatePrice
    4. 形参 ()
    5. 返回值 void
    6. 提供一个构造器

    代码小分析: 在 new 创建对象里面,只要书籍的价格大于150,根据类里面的 if 判断语句,就会将价格更改为 150 ;倘若没有大于 150 ,但是大于 100 ,根据类里面的 if 判断语句,就会将价格更改为 100 ;如果书籍的价格 小于 100 ,那前后结果不变。

    代码实现:

    1. package thisHome;
    2. public class Homework04 {
    3. public static void main(String[] args) {
    4. Book book = new Book("笑傲江湖",160);
    5. book.info();
    6. book.updatePrice();//更新价格
    7. book.info();
    8. }
    9. }
    10. class Book {
    11. String name;
    12. double price;
    13. public Book(String name,double price){
    14. this.name = name ;
    15. this.price = price;
    16. }
    17. public void updatePrice() {
    18. //如果方法中,没有 pirce 局部变量,this.price 等价 price
    19. if (price > 150) {
    20. price = 150;
    21. } else if (this.price > 100) {
    22. this.price = 100;
    23. }
    24. }
    25. //显示书籍情况
    26. public void info() {
    27. System.out.println("书名=" + this.name + "价格=" + this.price);
    28. }
    29. }

    结果:

    题目4:  要求实现数组的复制功能 copyArr ,
    输入旧数组,返回一个新数组,元素和旧数组一样 

    代码分析:

    原来的数组 old 里面存放着数据,然后创建(new)一个新的空间,存放原来数组 old 拷贝过来的数据,再把  new 的地址返回给 main ,再用 arr 来接收拷贝的新数组,这样一来,新数组、旧数组都有了。

    代码实现:

    1. package thisHome;
    2. public class Homework05 {
    3. public static void main(String[] args) {
    4. int[] oldArr = {10, 30, 40};
    5. A04 a04 = new A04();
    6. int[] newArr = a04.copyArr(oldArr);
    7. //遍历 newArr 验证
    8. System.out.println("返回的newArr元素情况");
    9. for (int i = 0; i < newArr.length; i++) {
    10. System.out.print(newArr[i] + "\t");
    11. }
    12. }
    13. }
    14. class A04 {
    15. public int[] copyArr(int[] oldArr) {
    16. //在堆中 ,创建一个长度为 oldArr.leng 数组
    17. int[] newArr = new int[oldArr.length];
    18. //遍历 oldArr ,将元素拷贝到 newArr
    19. for (int i = 0; i < oldArr.length; i++) {
    20. newArr[i] = oldArr[i];
    21. }
    22. return newArr;
    23. }
    24. }

    结果:

     

    题目5:  请提供显示圆周长功能的方法以及显示圆面积的方法

    分析:

    定义一个圆类 Cirle ,定义属性:半径

    代码实现:

    1. package thisHome;
    2. public class Homework06 {
    3. public static void main(String[] args) {
    4. Cirle cirle = new Cirle(3 );
    5. System.out.println("面积="+cirle.arrea());
    6. System.out.println("周长="+cirle.len());
    7. }
    8. }
    9. class Cirle {
    10. double radius;
    11. public Cirle(double radius){
    12. this.radius = radius;
    13. }
    14. public double arrea() {//面积
    15. return Math.PI * radius * radius;
    16. }
    17. public double len(){//周长
    18. return 2 * Math.PI * radius;
    19. }
    20. }

    结果:

    题目6:  要求编程创建一个 Cale 计算类 ,在其中定义两个变量表示两个操作数,并且定义四个方法实现求和、差、乘、商(要求除数为0的话,需要提示)并创建两个对象

    代码实现:

    1. package thisHome;
    2. public class Homework07 {
    3. public static void main(String[] args) {
    4. Cale cale = new Cale(2, 0);
    5. System.out.println("和="+cale.sum());
    6. System.out.println("差="+cale.minus());
    7. System.out.println("乘="+cale.mul());
    8. Double divRes = cale.div();
    9. if (divRes != null){
    10. System.out.println("除="+cale.div());
    11. }
    12. }
    13. }
    14. class Cale {
    15. double num1;
    16. double num2;
    17. public Cale(double num1, double num2) {
    18. this.num1 = num1;
    19. this.num2 = num2;
    20. }
    21. //和
    22. public double sum(){
    23. return num1 + num2;
    24. }
    25. //差
    26. public double minus(){
    27. return num1 - num2;
    28. }
    29. //乘积
    30. public double mul(){
    31. return num1 * num2;
    32. }
    33. //除法
    34. public Double div(){
    35. //判断
    36. if(num2 == 0){
    37. System.out.println("不能为 0");
    38. return null;
    39. }else {
    40. return num1 / num2;
    41. }
    42. }
    43. }

    结果:

    题目7:  请定义一个  Music 类,类里面有音乐名 name 、音乐时长 times 属性,
    并有播放 play 功能和返回本身属性信息的功能方法 getInfo

    代码实现:

    1. package thisHome;
    2. class Music {
    3. String name;
    4. int times;
    5. public Music(String name, int times) {
    6. this.name = name;
    7. this.times = times;
    8. }
    9. //播放 play 功能
    10. public void play() {
    11. System.out.println("音乐" + name + "正在播放中……时长为" + times + "秒");
    12. }
    13. //返回本身属性信息的功能方法 getInfo
    14. public String getInfo() {
    15. return "音乐" + name + "播放时间为" +times;
    16. }
    17. }
    18. public class Homework10 {
    19. public static void main(String[] args) {
    20. Music music = new Music("笑傲江湖", 300);
    21. music.play();
    22. System.out.println(music.getInfo());
    23. }
    24. }

    结果:

  • 相关阅读:
    Java-1116
    猿创征文| 指针,这还拿不下你?
    JUC——并发编程—第四部分
    通俗易懂玩QT:QT程序发布打包
    memmove函数详解 看这一篇就够了-C语言(函数讲解、函数实现、使用用法举例、作用、自己实现函数 )
    C++之make_unique、namespace、class类总结(二百四十二)
    LED电子屏幕可以通过什么方式进行人屏互动
    DAY43:隐藏通信隧道概述
    SpringCloudGateway--谓词(断言)
    MYSQL 是如何保证binlog 和redo log同时提交的?
  • 原文地址:https://blog.csdn.net/m0_57448314/article/details/126834200