Java 编程基础及应用 主编 强彦 赵娟娟 高等教育出版社 课后习题部分代码实现

本文主要是介绍Java 编程基础及应用 主编 强彦 赵娟娟 高等教育出版社 课后习题部分代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一章   引论

第二章   Java语言基础

9 编写一个程序,输出你的姓名、年龄、所在学院、所在专业和所在班级

   

public class Student {

       private String name;

       private int age;

       private String xueyuan;

       private String zhuanye;

       private String banji;

 

       public Student(){

             

       }

       public Student(String name,int age,String xueyuan,String zhuanye,String banji){

              this.name=name;

              this.age=age;

              this.xueyuan=xueyuan;

              this.zhuanye=zhuanye;

              this.banji=banji;

             

       }

       public void ShowInfo(){

              System.out.println(this);

       }

       public String toString(){

             

              return "[学生]\n姓名:" +name+"\n年龄: "+age+"\n所在学院:"+xueyuan+"\n所在专业:"+zhuanye+"\n所在班级:"+banji;           

       }

       public String getName(){

              return name;

       }

       public void setName(){

              this.name=name;

       }

              public int geyAge(){

              return age;

       }

       public void setAge(){

              this.age=age;

       }

      

       public String getXueyuan(){

              return xueyuan;

       }

       public void setXueyuan(){

              this.xueyuan=xueyuan;

       }       

       public String getZhuanye(){

              return zhuanye;

       }

       public void setZhuanye(){

              this.zhuanye=zhuanye;

       }

      

      

       public String getBanji(){

              return banji;

       }

       public void setBanji(){

              this.banji=banji;

       }

}

public class Text {

       public static void main(String age[]){

              Student s=new Student("chen陈三,"基础科学与技术学院","计算机科学与技术","计科一班");

              s.ShowInfo();

       }

 

}

第三章  运算符、表达式和语句

7、分别用if、switch两种语句编写将0-11的整数转换为十二个月份的程序段,假定数字0对应一月份

package cpf;

 

import java.util.Scanner;

 

public class P53di7ti {

    public static void main(String[] args) {

 

        int mouth;

 

        do{

 

        System.out.println("请输入月份:");

 

        mouth=new Scanner(System.in).nextInt();

 

        switch(mouth){

 

        case 0:System.out.println("一月");break;

 

        case 1:System.out.println("二月");break;

 

        case 2:System.out.println("三月");break;

 

        case 3:System.out.println("四月");break;

 

        case 4:System.out.println("五月");break;

 

        case 5:System.out.println("六月");break;

 

        case 6:System.out.println("七月");break;

 

        case 7:System.out.println("八月");break;

 

        case 8:System.out.println("九月");break;

 

        case 9:System.out.println("十月");break;

 

        case 10:System.out.println("十一月");break;

 

        case 11:System.out.println("十二月");break;

 

        default:System.out.println("请重新输入月份");break;

        }

        }while (mouth>=0&&mouth<=11);

        }   

}

第四章   数组

21、有一个整数数组,其中存放着1,3,5,7,9,11,13,15,17,19.将该序列倒叙存放输出

package cpf;

 

public class P7421 {

    public static void main(String args[]) {

    int [] arr= {1,3,5,7,9,11,13,15,17,19};

   

    for(int i=9;i>=0;i--) {

       

        System.out.println(arr[i]);

    }

 

}

    }

 

 

 

 

22、编写一个程序,提示用户输入学生的数量、姓名和他们的成绩,并按照成绩的降序打印学生姓名

package cpf;

 

import java.util.Scanner;

 

public class P7422 {

    private String name;

    private int score;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getScore() {

        return score;

    }

    public void setScore(int score) {

       

        this.score = score;

    }

    public void sort(P7422[] stu,int count)

    {

        P7422 temp;

        for(int i=0;i<count;i++)

        {

            for(int j=0;j<count-i-1;j++)

            {

                if (stu[j].getScore()<stu[j+1].getScore())

                {

                   temp=stu[j];

                   stu[j]=stu[j+1];

                   stu[j+1]=temp;

                }

            }

        }

    }

    public void print(P7422[] stu,int count){

        for (int i=0; i<count;i++) {

            System.out.println(stu[i].getName()+" "+stu[i].getScore());

        }

    }

}

package cpf;

 

import java.util.Scanner;

 

public class P742211 {

    public static void main(String[] args){

        P7422 s=new P7422();

        P7422[] stu= new P7422[20];

        Scanner input= new Scanner(System.in);

        System.out.println("请输入学生个数:");

        int count=input.nextInt();

        int num=0;

        while(num<count) {

        System.out.println("请输入第"+(num+1)+"个学生的姓名和分数:");

            Scanner scan=new Scanner(System.in);

            String strLine=scan.nextLine();

            String[] strLineArr = strLine.split(" ");

            P7422 st = new P7422();

            st.setName(strLineArr[0]);

            st.setScore(Integer.parseInt(strLineArr[1]));

            stu[num]=st;

            num++;

        }

         s.sort(stu,count);

        System.out.println("排序后的学生信息为:");

        s.print(stu,count);

    }

}

23、编写一个程序,求出整数数组中最小匀速的下标。如果这样的元素个数大于1的时候,则返回下标最小的数的下标

package cpf;

 

public class P7423zuixiaoshuxiabiao {

  public static void main (String args[]){

        int i,min;

        int n=0;

        int cpf[]={88,55,1,22,11,99,66,33,125,1};

        min=cpf[0];

        for(i=1;i<10;i++)

        {

            if(min>cpf[i])

            {

                min=cpf[i];

                n=i;

               

            }

        }

        System.out.println("最小数的下标为: "+n);

           

        }

    }

 

 

24、有两个数组:数组a,7,9,11,13,15,17,19,数组b为2.4.6.8.10.将两个数组组合并为数组c按升序排列

package cpf;

public class P7424paixu {

    public static void main(String args[]){

        int i,j,temp;

        int a[]={1,7,9,11,13,15,17,19};

        int b[]={2,4,6,8,10};

        int c[]=new int[15];

        for(i=0;i<a.length;i++){

            c[i]=a[i];

            }

        for (i=a.length,j=0;j<b.length;j++,i++)

        {

            c[i]=b[j];

        }

        for(i=0;i<c.length-1;i++)

        {

         for(j=0;j<c.length-1-i;j++)

         {

             if(c[j]>c[j+1])

             {

                 temp=c[j+1];

                 c[j+1]=c[j];

                 c[j]=temp;

             }

         }

        }

        for(i=0;i<c.length;i++)

         {

             System.out.print(c[i]+" ");

         }     

    }

 

}

第五章 类和duix对象

10、构造一个银行账户类,类中包括如下内容:

(1) 成员变量:用户的账户名称、用户的账户余额(private类型)

(2)成员方法:开户(设置账户名称及余额),利用构造方法完成。

(3)查询余额

public class P95di10ti {

    private String name;// 账户名称

     private float balance;// 用户的账户余额

     public P95di10ti(String name, float balance) { // 开户(设置账户名称及余额),

      this.name = name;

      this.balance = balance;

     }

     public String search() {// 查询余额

      return "账户 名称:" + this.name + "\t\t " + "余额为 " + this.balance+"元整";

     }

     public String getName() {

      return name;

     }

     public void setName(String name) {

      this.name = name;

     }

     public float getBalance() {

      return balance;

     }

     public void setBalance(float balance) {

      this.balance = balance;

     }

 

}

package cpf;

 

public class Text2 {

 

    public static void main(String[] args) {

          P95di10ti acc1 = new P95di10ti("陈三", 99999999999f);// 利用构造方法开户

          System.out.println(acc1.search());//查询余额

         }

 

}

 

 

 

 

11、声明一个图书馆类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对iangde编号赋值,在主方法中定义对象数组,并求出总册数。

package cpf;

 

public class P95di11ti {

    private String name;

     private int no = 000;

     private float price;

     private static int count = 0;

     private static int sumCnt = 0;

     public P95di11ti(String name, float price, int count) {

      this.sumCnt = this.sumCnt + count;

      this.no = sumCnt;

      this.name = name;

      this.price = price;

      this.count = count;

     }

     public void print() {

      System.out.println("书名: " + this.getName() + "\t" + "编号: "

        + this.getNo() + "\t" + "价格: " + this.getPrice() + "\t"

        + "册数: " + this.getCount());

      System.out.println("\n" + "总册书为:" + sumCnt);

     }

     public String getName() {

      return name;

     }

     public void setName(String name) {

      this.name = name;

     }

     public float getPrice() {

      return price;

     }

     public void setPrice(float price) {

      this.price = price;

     }

     public static int getCount() {

      return count;

     }

     public static void setCount(int count) {

      P95di11ti.count = count;

     }

     public int getNo() {

      return no;

     }

     public void setNo(int no) {

      this.no = no;

     }

     public static int getSumCnt() {

      return sumCnt;

     }

     public static void setSumCnt(int sumCnt) {

      P95di11ti.sumCnt = sumCnt;

     }

 

}

/*public class Book1 {

    public static void main(String args[]) {

          Book book1 = new Book("Java编程基础及应用", 36.0f, 10);

          book1.print();

          Book book2 = new Book("Java大学实用教程", 20.5f, 4);

          book2.print();

          Book book3 = new Book("名侦探柯南", 36.0f, 6);

          book3.print();

          Book book4 = new Book("JavaEE基础实用教程", 26.8f, 10);

          book4.print();

         }

*/

第六章    类的继承与多态

12、定义一个长方形类,该类拥有长、宽、高3个属性及计算体积的方法;定义一个子类继承该长方体类,增加成员变量重量,并增加计算长方体表面积的方法。

package cpf;

 

public class P106di12ti {

    double length;

    double width;

 

    double height;

 

    public P106di12ti(int i, int j, int k) {

        // TODO Auto-generated constructor stub

    }

 

    public void P106di12ti(double length,double width,double height)

 

    {

 

    this.length= length;

 

    this.width= width;

 

    this.height=height;

 

    }

 

    public  Object computeVolume()

 

    {

 

    return this.length*this.height*this.width;

 

    }

 

    }

package cpf;

public class Text3 {

    public static void main(String args[]) {

        P106di12ti r1=new P106di12ti(8,10,10);

 

        System.out.println("长方体体积为"+r1.computeVolume());

 

        Cuboid r2=new Cuboid(10,10,10,10);

 

        System.out.println("长方体表面积为"+r2.computeArea());

            }

}

第七章  内部类和异常

编写一个使用自定义异常的小程序

(1)定义mytest

   Import java.io.*;

   Public class mytest{

Privatestatic int quotient(int number,int denominator)throw

DivideByZeroException{

if(denominator==0)

thrownewDivideByZeroException();

return(number/denominator);

}

publicstaticvoidmain(Stringargs[]){

intnumber1=0,number2=0,result=0;

try{

System.out.println("输入第一个数字:");

number1=Integer.valueOf(Keyboard.getString()).intValue();

System.out.println("输入第二个数字:");

number2=Integer.valueOf(Keyboard.getString()).intValue();

result=quotient(number1,number2);

}

catch(NumberFormatExceptione){

System.out.println(e.toString());

System.exit(-1);

}

System.out.println(number1+"/"+number2+"="+result);

}

}

}

第八章   多线程 

      1)一个线程输出100个1-26,另一个线程输出100个A-Z

   (2)一个行程使用继承Thread类的写法,另一个线程使用实现Runnable接口的写法

package cpf;

 

public class ThreadTest {

    class T1 extends Thread {

        public void run() {

            for (int i = 0; i < 3; i++) {

                for (int j = 1; j <= 26; j++)

                {

                    System.out.println(j);

                    }

                }

            }

        }

    class T2 implements Runnable {

        char[] c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        @Override

        public void run() {

            for (int i = 0; i < 3; i++) {

                for (int j = 0; j < c.length; j++) {

                    System.out.println(c[j]);

                    }

                }

            }

        }

    public void test() {

        Thread t1 = new T1();

        Thread t2 = new Thread(new T2());

        t1.start();

        t2.start();

        }

    public static void main(String[] args) {

        ThreadTest threadTest = new ThreadTest();

        threadTest.test();

        }

    }

第九章  接口和实现

试编写一个People接口,定义一个抽象类Employee,定义一个具体类Mannagment继承Employee类,并编写测试运行类。

package cpf;

 

public interface People {

    void people();

 

}

package cpf;

 

 class  Management implements People{

    public void people() {

        System.out.println("我是一名管理层的领导!");

    }

 

}

package cpf;

 

 class Employee implements People{

    public void people() {

        System.out.println("我是一名普通员工!");

    }

}

package cpf;

 

public class InterfaceTest {

 

    public static void main(String arge[]) {

        People a=new Employee();

        People b=new Management();

        a.people();

        b.people();

    }

}

这篇关于Java 编程基础及应用 主编 强彦 赵娟娟 高等教育出版社 课后习题部分代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829862

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2