老韩Java第七章作业汇总(自做) 快来打我* 2024-03-30 13:45 22阅读 0赞 韩老师(韩顺平)java第七章所有习题自己写的而且可以运行都放在这里了。 ### 问题1:编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java ### public class HomeWork01 { public static void main(String args[]){ double[] a = { 1.1,2.2,3.3,4.4,5.5}; //主函数 A01 b = new A01(); b.max(a); } //方法函数,最值获取 } class A01{ public void max(double a[]){ double mx = a[0]; for(int i = 1;i<a.length;i++){ if(a[i] > mx){ mx = a[i]; } } System.out.println(mx); } } ### 问题2 编写类A02 ,定义方法find,实现查找某字符数组中的元素查找,并返回索引,如果找不到返回-1 ### public class HomeWork01 { public static void main(String args[]){ String[] a = { "aa","bb","cc","dd","ee"}; String st = "aa"; A02 b = new A02(); System.out.println("找到的字符串下标位:" + b.find(a,st)); //主函数 } } class A02{ //-1其实是一个哨兵岗 public int find(String a[],String st){ int idx = -1; for(int i=0;i<a.length;i++){ if(st.equals(a[i])){ idx =i; break; } } return idx; } } ![在这里插入图片描述][6d04a8d1a46a4f64a001ad0c5ed5cdbc.png] ### 问题3 编写类Book,定义方法updatePrice,实现更改本书的价格,具体,如果价格>150,则更改为150,如果>100,更改为100,否则不变 ### public class HomeWork01 { public static void main(String args[]){ Book b = new Book(159.2); b.updatePrice(); System.out.println(b.price); } //方法函数,最值获取 } class Book{ double price; public Book(double v) { price = v; } public void updatePrice(){ if(this.price > 150.0) this.price = 150.0; else if(this.price > 100) this.price = 100.0; } } ![在这里插入图片描述][bb23c8452db94538a0de706a4f57e8f7.png] ### 问题4 编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样 ### public class HomeWork01 { public static void main(String args[]){ int[] a = { 1,2,3,4,5}; A03 tmp = new A03();//临时对象 int[] b = tmp.CopyArray(a); for (int ele:b ) { //打印输出 System.out.println(ele); } } } class A03{ public int[] CopyArray(int a[]){ int[] b = new int[a.length]; for(int i=0;i<a.length;i++) b[i] = a[i]; return b; } } ![在这里插入图片描述][3eecb4727c6744ab8988f022e5a9f7e1.png] ### 问题5 定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法 ### public class HomeWork01 { public static void main(String args[]){ Circle c = new Circle(3.0); c.printArea(); c.printPerimeter(); } } class Circle{ double r; public Circle(double r) { this.r = r; } public void printPerimeter(){ System.out.println("圆的周长" + 2*3.1415*this.r); } public void printArea(){ System.out.println("圆的面积" + this.r*3.1415*this.r); } } ![在这里插入图片描述][410e240265a94c31aedeef19be6a4f8d.png] ### 问题6 编程创建一个Cale计算类,其中定义2个变量表示两个操作数,定义四个方法实现求和差乘商,除数为0给提示,并创建两个对象。 ### public class HomeWork01 { public static void main(String args[]){ Cale c = new Cale(1.1,0.0); c.div(); Cale d = new Cale(1.1,2.2); d.add(); d.mul(); d.reduce(); } // } class Cale{ double sum1; double sum2; public Cale(double v, double v1) { sum1 = v; sum2 = v1; } public void add(){ System.out.println("两数之和 " + (this.sum1+this.sum2)); } public void reduce(){ System.out.println("两数之差 " + (this.sum1-this.sum2)); } public void mul(){ System.out.println("两数之积 " + (this.sum1*this.sum2)); } public void div(){ if((this.sum2-0.0)<1e-4){ System.out.println("除数为0"); }else{ System.out.println("两数之商" + (this.sum1/this.sum2)); } } } ![在这里插入图片描述][ce716186f1c047cc9bcd2c2587dd0b74.png] ### 问题7:设计一个Dog类,有名字颜色年龄,定义输出方法show()显示信息,并创建对象进行测试 ### public class HomeWork01 { public static void main(String args[]){ Dog dg = new Dog("旺财","红色",2); dg.printInfo(); } // } class Dog{ String name; String color; int age; public Dog(String s, String s1, int i) { this.name = s; this.color = s1; this.age = i; } void printInfo(){ System.out.println("狗的名字叫" + this.name + ",颜色为:" + this.color + ",狗的年龄为:" + this.age ); } } ![在这里插入图片描述][3b4bad96bb38448caca79a83fd54c0e6.png] ### 问题8:给定一个java程序代码如下所示,则编译运行后,输出结果是() ### public class Test { int count = 9; public void count1(){ count = 10; System.out.println("count1 = " + count); } public void count2(){ System.out.println("count2 = " + count++); } public static void main(String args[]){ new Test().count1(); Test t1 = new Test(); t1.count2(); t1.count2(); } } ![在这里插入图片描述][124e2470a1fc419ab1f0cedffa5fb3d4.png] ### 问题9 定义music类,里面有音乐名name,音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo ### public class Music { public static void main(String[] args){ Mus mu = new Mus("青花瓷",242); mu.Play(); mu.getInfo(); } } class Mus{ String name; int time; public Mus(String name, int i) { this.name = name; this.time = i; } void Play(){ System.out.println("音乐播放中...."); } void getInfo(){ System.out.println("歌曲名称:" + this.name + ",歌曲时长" + this.time/60 + "分" + this.time%60 + "秒"); } } ![在这里插入图片描述][c577bbd929f646d3a386fb138984cf90.png] ### 问题10 试写出以下代码运行结果() ### public class Demo{ public static void main(String args[]){ Dem d1 = new Dem(); Dem d2 = d1; d2.m(); System.out.println(d1.i); System.out.println(d1.i); } } class Dem { int i = 100; public void m(){ int j = i++; System.out.println("i="+i); System.out.println("j="+j); } } ![在这里插入图片描述][b81402ea26c9443ba54dc6f4c823fdb5.png] ### 问题11 在测试方法中,调用method方法,代码如下,编译正确,调用语句System.out.println(method(method(10.0,20.0),100)); ### public class HomeWork11 { public double method(double a,double b){ return a+b; } static int method(double a, int b){ return (int) (a + (double)b); } public static void main(String args[]) { System.out.println(method(method(10.0, (int) 20.0), 100)); } } ![在这里插入图片描述][9e544d344ec2479b8560650813fd3342.png] ### 问题12 创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),提供三个构造方法,1.(名字,性别,年龄,职位,薪水),2,(名字,性别,年龄),(3)(职位,薪水) ### public class HomeWork12 { public static void main(String args[]){ Employee e1 = new Employee("高级工程师",15500); Employee e2 = new Employee("张怀",'男',22); Employee e3 = new Employee("李华",'男',23,"中级工程师",15500); System.out.println(e3.age); System.out.println(e3.name); System.out.println(e3.gender); System.out.println(e3.position); System.out.println(e3.salary); } } class Employee{ String name; char gender; int age; String position; int salary; public Employee(String position, int salary) { this.position = position; this.salary = salary; } public Employee(String name, char gender, int age) { this.name = name; this.gender = gender; this.age = age; } public Employee(String name, char gender, int age, String position, int salary) { this(name,gender,age); this.position = position; this.salary = salary; System.out.println("构造函数....."); } } ![在这里插入图片描述][641083bdd1734ce698fc7c9788e86004.png] ### 问题13 ### ![在这里插入图片描述][119c0282499646248ea20bc01dc42e6e.png] import javax.swing.plaf.basic.BasicIconFactory; public class HomeWork13 { public static void main(String args[]){ PassObject ps = new PassObject(); Circle c = new Circle(); ps.printAreas(c,5); } } class Circle{ double r; double findArea(){ return 3.14159*r*r; } } class PassObject{ public void printAreas(Circle c,int times){ System.out.println("Radius\t\tArea"); for(int i =1;i<=times;i++){ c.r = (double) i; System.out.println(c.r + "\t\t" + c.findArea()); } } } ![在这里插入图片描述][ecd565b370cf4ae28d7e7061d6e4baf4.png] [6d04a8d1a46a4f64a001ad0c5ed5cdbc.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/17a0aff7e0814f08977ce1d100240ab5.png [bb23c8452db94538a0de706a4f57e8f7.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/1140c700f08644a49264caf2efc89bae.png [3eecb4727c6744ab8988f022e5a9f7e1.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/2c66af84484c403899dea36625cb8e92.png [410e240265a94c31aedeef19be6a4f8d.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/75dfb12beadf4eb7b2de8a220ff95214.png [ce716186f1c047cc9bcd2c2587dd0b74.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/4cf038fe0b884031bde74aeea8bdbf2b.png [3b4bad96bb38448caca79a83fd54c0e6.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/6a9531f111604e6a997ce3331564000d.png [124e2470a1fc419ab1f0cedffa5fb3d4.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/3a46cf0003dd4d4e9359c143d5e08d4d.png [c577bbd929f646d3a386fb138984cf90.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/c75f77e1f9c742a2bdf31fea208ae4f0.png [b81402ea26c9443ba54dc6f4c823fdb5.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/de258294b6d94953ab89ce7cc84de03a.png [9e544d344ec2479b8560650813fd3342.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/db677fd2e59d4384a9732acff60f624f.png [641083bdd1734ce698fc7c9788e86004.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/306700fb6adc43cbb39bd508778b4d1d.png [119c0282499646248ea20bc01dc42e6e.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/db5205d5309b40daabb1e48897333931.png [ecd565b370cf4ae28d7e7061d6e4baf4.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/30/bd410534972c4852a4f44811d0dc35e6.png
还没有评论,来说两句吧...