目录
题目2: 要求查找某字符串是否在字符串数组中,并返回索引 , 如果 找不到 ,返回 -1
题目3: 要求实现更改某本书的价格比如:如果价格 > 150 ,更改为 150 ,如果价格 > 100 ,更改为 100 , 否则不变
题目4: 要求实现数组的复制功能 copyArr ,输入旧数组,返回一个新数组,元素和旧数组一样
题目6: 要求编程创建一个 Cale 计算类 ,在其中定义两个变量表示两个操作数,并且定义四个方法实现求和、差、乘、商(要求除数为0的话,需要提示)并创建两个对象
题目7: 请定义一个 Music 类,类里面有音乐名 name 、音乐时长 times 属性,并有播放 play 功能和返回本身属性信息的功能方法 getInfo
思路分析:
1. 类名 A01
2. 方法名:max
3. 形参(double [ ] )
4. 返回值 double
- package thisHome;
-
- public class Homework01 {
- public static void main(String[] args) {
- A01 a01 = new A01();
- double[] arr = {1.2,23.4,55.6};
- Double res = a01.max(arr);
- if (res != null) {
- System.out.println("arr的最大值=" + res);
- }else {
- System.out.println("arr 输入有误,数组不能够为null,或{}");
- }
- }
- }
-
- class A01 {
- public Double max(double[] arr) {
- //先判断arr是否为null 然后再判断length 是否 > 0
- if (arr != null && arr.length > 0) {
-
- //保证arr至少要有一个元素
- double max = arr[0]; //假定 第一个元素就是最大值
- for (int i = 1; i < arr.length; i++) {
- if (max < arr[i]) {
- max = arr[i];
- }
- }
- return max;
- }else {
- return null;
- }
- }
- }
结果:

思路分析:
1. 类名 A02
2. 方法名 find
3. 返回值 int
4. 形参 (String ,String[] )
代码实现:
- package thisHome;
-
- public class Homework02 {
- public static void main(String[] args) {
- String[] strs = {"jack", "tom", "mary", "milan"};
- A02 a02 = new A02();
- int index = a02.find("milan", strs);
- System.out.println("查找的index=" + index);
- }
- }
-
-
- class A02 {
- public int find(String findStr, String[] strs) {
- //直接遍历
- for (int i = 0; i < strs.length; i++) {
- if (findStr.equals(strs[i])) {
- return i;
- }
- }
- //如果没有 ,句返回 -1
- return -1;
- }
- }
结果:

思路分析:
1. 类名 Book
2. 属性 price ,name
3. 方法名 updatePrice
4. 形参 ()
5. 返回值 void
6. 提供一个构造器代码小分析: 在 new 创建对象里面,只要书籍的价格大于150,根据类里面的 if 判断语句,就会将价格更改为 150 ;倘若没有大于 150 ,但是大于 100 ,根据类里面的 if 判断语句,就会将价格更改为 100 ;如果书籍的价格 小于 100 ,那前后结果不变。
代码实现:
- package thisHome;
-
- public class Homework04 {
- public static void main(String[] args) {
- Book book = new Book("笑傲江湖",160);
- book.info();
- book.updatePrice();//更新价格
- book.info();
- }
- }
-
-
- class Book {
- String name;
- double price;
- public Book(String name,double price){
- this.name = name ;
- this.price = price;
- }
-
- public void updatePrice() {
- //如果方法中,没有 pirce 局部变量,this.price 等价 price
- if (price > 150) {
- price = 150;
- } else if (this.price > 100) {
- this.price = 100;
- }
- }
-
- //显示书籍情况
- public void info() {
- System.out.println("书名=" + this.name + "价格=" + this.price);
- }
- }
结果:

代码分析:
原来的数组 old 里面存放着数据,然后创建(new)一个新的空间,存放原来数组 old 拷贝过来的数据,再把 new 的地址返回给 main ,再用 arr 来接收拷贝的新数组,这样一来,新数组、旧数组都有了。
代码实现:
- package thisHome;
-
- public class Homework05 {
- public static void main(String[] args) {
- int[] oldArr = {10, 30, 40};
- A04 a04 = new A04();
- int[] newArr = a04.copyArr(oldArr);
- //遍历 newArr 验证
- System.out.println("返回的newArr元素情况");
- for (int i = 0; i < newArr.length; i++) {
- System.out.print(newArr[i] + "\t");
- }
- }
- }
-
-
- class A04 {
- public int[] copyArr(int[] oldArr) {
- //在堆中 ,创建一个长度为 oldArr.leng 数组
- int[] newArr = new int[oldArr.length];
- //遍历 oldArr ,将元素拷贝到 newArr
- for (int i = 0; i < oldArr.length; i++) {
- newArr[i] = oldArr[i];
- }
- return newArr;
- }
- }
结果:

分析:
定义一个圆类 Cirle ,定义属性:半径
代码实现:
- package thisHome;
-
- public class Homework06 {
- public static void main(String[] args) {
- Cirle cirle = new Cirle(3 );
- System.out.println("面积="+cirle.arrea());
- System.out.println("周长="+cirle.len());
-
- }
- }
-
-
- class Cirle {
- double radius;
- public Cirle(double radius){
- this.radius = radius;
- }
-
- public double arrea() {//面积
- return Math.PI * radius * radius;
- }
- public double len(){//周长
- return 2 * Math.PI * radius;
- }
- }
结果:

代码实现:
- package thisHome;
-
- public class Homework07 {
- public static void main(String[] args) {
- Cale cale = new Cale(2, 0);
- System.out.println("和="+cale.sum());
- System.out.println("差="+cale.minus());
- System.out.println("乘="+cale.mul());
- Double divRes = cale.div();
- if (divRes != null){
- System.out.println("除="+cale.div());
- }
- }
- }
-
-
- class Cale {
- double num1;
- double num2;
-
- public Cale(double num1, double num2) {
- this.num1 = num1;
- this.num2 = num2;
- }
- //和
- public double sum(){
- return num1 + num2;
- }
- //差
- public double minus(){
- return num1 - num2;
- }
- //乘积
- public double mul(){
- return num1 * num2;
- }
-
- //除法
- public Double div(){
- //判断
- if(num2 == 0){
- System.out.println("不能为 0");
- return null;
- }else {
- return num1 / num2;
- }
- }
- }
结果:

代码实现:
- package thisHome;
-
-
- class Music {
- String name;
- int times;
-
- public Music(String name, int times) {
- this.name = name;
- this.times = times;
- }
-
- //播放 play 功能
- public void play() {
- System.out.println("音乐" + name + "正在播放中……时长为" + times + "秒");
- }
-
- //返回本身属性信息的功能方法 getInfo
- public String getInfo() {
- return "音乐" + name + "播放时间为" +times;
- }
- }
-
- public class Homework10 {
- public static void main(String[] args) {
- Music music = new Music("笑傲江湖", 300);
- music.play();
- System.out.println(music.getInfo());
- }
- }
结果:
