老韩Java第七章作业汇总(自做)

快来打我* 2024-03-30 13:45 191阅读 0赞

韩老师(韩顺平)java第七章所有习题自己写的而且可以运行都放在这里了。

问题1:编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. double[] a = {
  4. 1.1,2.2,3.3,4.4,5.5};
  5. //主函数
  6. A01 b = new A01();
  7. b.max(a);
  8. }
  9. //方法函数,最值获取
  10. }
  11. class A01{
  12. public void max(double a[]){
  13. double mx = a[0];
  14. for(int i = 1;i<a.length;i++){
  15. if(a[i] > mx){
  16. mx = a[i];
  17. }
  18. }
  19. System.out.println(mx);
  20. }
  21. }

问题2 编写类A02 ,定义方法find,实现查找某字符数组中的元素查找,并返回索引,如果找不到返回-1

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. String[] a = {
  4. "aa","bb","cc","dd","ee"};
  5. String st = "aa";
  6. A02 b = new A02();
  7. System.out.println("找到的字符串下标位:" + b.find(a,st));
  8. //主函数
  9. }
  10. }
  11. class A02{
  12. //-1其实是一个哨兵岗
  13. public int find(String a[],String st){
  14. int idx = -1;
  15. for(int i=0;i<a.length;i++){
  16. if(st.equals(a[i])){
  17. idx =i;
  18. break;
  19. }
  20. }
  21. return idx;
  22. }
  23. }

在这里插入图片描述

问题3 编写类Book,定义方法updatePrice,实现更改本书的价格,具体,如果价格>150,则更改为150,如果>100,更改为100,否则不变

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. Book b = new Book(159.2);
  4. b.updatePrice();
  5. System.out.println(b.price);
  6. }
  7. //方法函数,最值获取
  8. }
  9. class Book{
  10. double price;
  11. public Book(double v) {
  12. price = v;
  13. }
  14. public void updatePrice(){
  15. if(this.price > 150.0)
  16. this.price = 150.0;
  17. else if(this.price > 100)
  18. this.price = 100.0;
  19. }
  20. }

在这里插入图片描述

问题4 编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. int[] a = {
  4. 1,2,3,4,5};
  5. A03 tmp = new A03();//临时对象
  6. int[] b = tmp.CopyArray(a);
  7. for (int ele:b
  8. ) {
  9. //打印输出
  10. System.out.println(ele);
  11. }
  12. }
  13. }
  14. class A03{
  15. public int[] CopyArray(int a[]){
  16. int[] b = new int[a.length];
  17. for(int i=0;i<a.length;i++)
  18. b[i] = a[i];
  19. return b;
  20. }
  21. }

在这里插入图片描述

问题5 定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. Circle c = new Circle(3.0);
  4. c.printArea();
  5. c.printPerimeter();
  6. }
  7. }
  8. class Circle{
  9. double r;
  10. public Circle(double r) {
  11. this.r = r;
  12. }
  13. public void printPerimeter(){
  14. System.out.println("圆的周长" + 2*3.1415*this.r);
  15. }
  16. public void printArea(){
  17. System.out.println("圆的面积" + this.r*3.1415*this.r);
  18. }
  19. }

在这里插入图片描述

问题6 编程创建一个Cale计算类,其中定义2个变量表示两个操作数,定义四个方法实现求和差乘商,除数为0给提示,并创建两个对象。

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. Cale c = new Cale(1.1,0.0);
  4. c.div();
  5. Cale d = new Cale(1.1,2.2);
  6. d.add();
  7. d.mul();
  8. d.reduce();
  9. }
  10. //
  11. }
  12. class Cale{
  13. double sum1;
  14. double sum2;
  15. public Cale(double v, double v1) {
  16. sum1 = v;
  17. sum2 = v1;
  18. }
  19. public void add(){
  20. System.out.println("两数之和 " + (this.sum1+this.sum2));
  21. }
  22. public void reduce(){
  23. System.out.println("两数之差 " + (this.sum1-this.sum2));
  24. }
  25. public void mul(){
  26. System.out.println("两数之积 " + (this.sum1*this.sum2));
  27. }
  28. public void div(){
  29. if((this.sum2-0.0)<1e-4){
  30. System.out.println("除数为0");
  31. }else{
  32. System.out.println("两数之商" + (this.sum1/this.sum2));
  33. }
  34. }
  35. }

在这里插入图片描述

问题7:设计一个Dog类,有名字颜色年龄,定义输出方法show()显示信息,并创建对象进行测试

  1. public class HomeWork01 {
  2. public static void main(String args[]){
  3. Dog dg = new Dog("旺财","红色",2);
  4. dg.printInfo();
  5. }
  6. //
  7. }
  8. class Dog{
  9. String name;
  10. String color;
  11. int age;
  12. public Dog(String s, String s1, int i) {
  13. this.name = s;
  14. this.color = s1;
  15. this.age = i;
  16. }
  17. void printInfo(){
  18. System.out.println("狗的名字叫" + this.name + ",颜色为:" + this.color + ",狗的年龄为:" + this.age );
  19. }
  20. }

在这里插入图片描述

问题8:给定一个java程序代码如下所示,则编译运行后,输出结果是()

  1. public class Test {
  2. int count = 9;
  3. public void count1(){
  4. count = 10;
  5. System.out.println("count1 = " + count);
  6. }
  7. public void count2(){
  8. System.out.println("count2 = " + count++);
  9. }
  10. public static void main(String args[]){
  11. new Test().count1();
  12. Test t1 = new Test();
  13. t1.count2();
  14. t1.count2();
  15. }
  16. }

在这里插入图片描述

问题9 定义music类,里面有音乐名name,音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo

  1. public class Music {
  2. public static void main(String[] args){
  3. Mus mu = new Mus("青花瓷",242);
  4. mu.Play();
  5. mu.getInfo();
  6. }
  7. }
  8. class Mus{
  9. String name;
  10. int time;
  11. public Mus(String name, int i) {
  12. this.name = name;
  13. this.time = i;
  14. }
  15. void Play(){
  16. System.out.println("音乐播放中....");
  17. }
  18. void getInfo(){
  19. System.out.println("歌曲名称:" + this.name + ",歌曲时长" + this.time/60 + "分" + this.time%60 + "秒");
  20. }
  21. }

在这里插入图片描述

问题10 试写出以下代码运行结果()

  1. public class Demo{
  2. public static void main(String args[]){
  3. Dem d1 = new Dem();
  4. Dem d2 = d1;
  5. d2.m();
  6. System.out.println(d1.i);
  7. System.out.println(d1.i);
  8. }
  9. }
  10. class Dem {
  11. int i = 100;
  12. public void m(){
  13. int j = i++;
  14. System.out.println("i="+i);
  15. System.out.println("j="+j);
  16. }
  17. }

在这里插入图片描述

问题11 在测试方法中,调用method方法,代码如下,编译正确,调用语句System.out.println(method(method(10.0,20.0),100));

  1. public class HomeWork11 {
  2. public double method(double a,double b){
  3. return a+b;
  4. }
  5. static int method(double a, int b){
  6. return (int) (a + (double)b);
  7. }
  8. public static void main(String args[]) {
  9. System.out.println(method(method(10.0, (int) 20.0), 100));
  10. }
  11. }

在这里插入图片描述

问题12 创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),提供三个构造方法,1.(名字,性别,年龄,职位,薪水),2,(名字,性别,年龄),(3)(职位,薪水)

  1. public class HomeWork12 {
  2. public static void main(String args[]){
  3. Employee e1 = new Employee("高级工程师",15500);
  4. Employee e2 = new Employee("张怀",'男',22);
  5. Employee e3 = new Employee("李华",'男',23,"中级工程师",15500);
  6. System.out.println(e3.age);
  7. System.out.println(e3.name);
  8. System.out.println(e3.gender);
  9. System.out.println(e3.position);
  10. System.out.println(e3.salary);
  11. }
  12. }
  13. class Employee{
  14. String name;
  15. char gender;
  16. int age;
  17. String position;
  18. int salary;
  19. public Employee(String position, int salary) {
  20. this.position = position;
  21. this.salary = salary;
  22. }
  23. public Employee(String name, char gender, int age) {
  24. this.name = name;
  25. this.gender = gender;
  26. this.age = age;
  27. }
  28. public Employee(String name, char gender, int age, String position, int salary) {
  29. this(name,gender,age);
  30. this.position = position;
  31. this.salary = salary;
  32. System.out.println("构造函数.....");
  33. }
  34. }

在这里插入图片描述

问题13

在这里插入图片描述

  1. import javax.swing.plaf.basic.BasicIconFactory;
  2. public class HomeWork13 {
  3. public static void main(String args[]){
  4. PassObject ps = new PassObject();
  5. Circle c = new Circle();
  6. ps.printAreas(c,5);
  7. }
  8. }
  9. class Circle{
  10. double r;
  11. double findArea(){
  12. return 3.14159*r*r;
  13. }
  14. }
  15. class PassObject{
  16. public void printAreas(Circle c,int times){
  17. System.out.println("Radius\t\tArea");
  18. for(int i =1;i<=times;i++){
  19. c.r = (double) i;
  20. System.out.println(c.r + "\t\t" + c.findArea());
  21. }
  22. }
  23. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java作业

    文章目录 第一题:修改手机默认语言 第二题:设置信用卡密码 第三题:飞速的高铁 第四题:计算钟表时间 第五题:多功能参数 第六题:计算