题目1:

编写MobilePhone类,包含brand(品牌)和price(价格)属性:

  1. 该类实现Comparable接口规定该类对象大小,即手机能按照价格来排序。

(2)创建一个只可放 MobilePhone 类的链表,链表当中添加3个 MobilePhone 对象,依次遍历每个对象,并在屏幕上输出排序前链表中的数据;

(3)调用Collections的sort()方法将链表当中的对象按 price 值排序;并在屏幕上输出排序后链表中的数据;

(4)调用 Collections 的 binarySearch()方法,查找当前对象的 price 值是否和链表中的某一对象的 price 的值相同,若相同,屏幕上输出对应的下标值;

首先 复盘一下 (按身高排序)

package Timu;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class FindMobilePhone {
    public static void main(String[] args) {
        List<MobilePhone> list = new LinkedList<MobilePhone>();
        list.add(new MobilePhone("OPPO",3999));
        list.add(new MobilePhone("vivo",2999));
        list.add(new MobilePhone("XiaoMi",4999));

        Iterator<MobilePhone> iterator = list.iterator();
        System.out.println("排序前:链表当中的数据");
        while(iterator.hasNext())
        {
            MobilePhone s = iterator.next();
            System.out.println(s.brand+" "+"价格:"+" "+s.price);
        }
        Collections.sort(list);
        System.out.println("排序后:链表当中的数据");
        iterator = list.iterator();

        while(iterator.hasNext())
        {
            MobilePhone s = iterator.next();
            System.out.println(s.brand+" "+"价格:"+" "+s.price);
        }

        MobilePhone demo = new MobilePhone("HuaWei",4999);
        int index = Collections.binarySearch(list,demo,null);
        if(index>=0)
        {
            System.out.println(demo.brand+"和链表当中"+list.get(index).brand+"价格一模一样");
            System.out.println("对应的下标为:"+index);
        }
    }
}
package Timu;

public class MobilePhone implements Comparable<MobilePhone>{
    String brand;//品牌
    int price=0;//价格

    MobilePhone(String brand, int price)
    {
        this.brand = brand;
        this.price = price;
    }
    public int compareTo(MobilePhone m)
    {
        return (this.price - m.price);
    }

}

题目2:

某公司的雇员分为5类,每类员工都有相应的封装类,这5个类的信息如下所示。

(1) Employee:这是所有员工总的父类。

① 属性:员工的姓名,员工的生日月份

② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励 100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5倍工资发放。

① 属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。

① 属性:月销售额、提成率。

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。

① 属性:底薪。

本题要求根据上述雇员分类,编写一个程序,实现以下功能:

(1)创建一个 Employee 数组,分别创建若干不同的 Employee 对象,并打印某个月的工资。

(2)每个类都完全封装,不允许非私有化属性

package Timu;
//这是所有员工总的父类

public class Employee {
    private String name;//定义姓名并且 私有化
    private int month;//生日月份

//    无参构造
    public Employee(){}
    public Employee(String name, int month){
        this.name = name;
        this.month = month;
    }
    public String getName(){
        return name;
    }
    public int getMonth(){
        return month;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setMonth(int month){
        this.month = month;
    }
    public double getSalary(int month)
    {
        double salary = 0;
        if(this.month == month)
        {
            salary = salary +100;
        }
        return salary;
    }
}

package Timu;

public class SalariedEmployee extends Employee{
    private double monthSalary;

    public SalariedEmployee(){}

    public SalariedEmployee(String name, int month, double monthSalary)
    {
        super(name, month);
        this.monthSalary = monthSalary;
    }
    public double getMonthSalary(){
        return monthSalary;
    }
    public void setMonthSalary(double monthSalary)
    {
        this.monthSalary = monthSalary;
    }
    public double getSalary(int month){
        double salary = monthSalary+super.getSalary(month);
        return salary;
    }

}
package Timu;

public class HourlyEmployee extends Employee{
    private double hourlySalary;
    private  int hours;

    private HourlyEmployee(){}

    public HourlyEmployee(String name,int month,double hourlySalary,int hours)
    {
        super(name, month);
        this.hourlySalary = hourlySalary;
        this.hours = hours;
    }
    public double getHourlySalary(){
        return hourlySalary;
    }
    public int getHours(){
        return hours;
    }
    public void setHourlySalary(double hourlySalary)
    {
        this.hourlySalary = hourlySalary;
    }
    public void setHours(int hours)
    {
        this.hours = hours;
    }
    
    public double getSalary(int month){
        if(hours < 0)
        {
            System.out.println("数据错误");
            return 0;
        }
        else if(hours <= 160)
        {
            return hourlySalary*hours + super.getSalary(month);
        }
        else return hourlySalary*160+hourlySalary*1.5*(hours-160)+super.getSalary(month);
    }
}
package Timu;

public class SalesEmployee extends Employee{
    private double sales;//销售额
    private double rate;//提成率
    public SalariedEmployee(){}
    public SalariedEmployee(String name,int month,double sales,double rate){
        super(name, month);
        this.sales = sales;
        this.rate = rate;
    }
    public double getSales(){
        return sales;
    }
    public double getRate(){
        return rate;
    }
    public void setSales(double sales)
    {
        this.sales = sales;
    }
    public  void setRate(double rate){
        this.rate = rate;
    }

    public double getSalary(int month)
    {
        return this.getSales()*(1+this.getRate())+super.getSalary(month);
    }

}
package Timu;

public class BasePlusSalesEmployee extends SalesEmployee{
    private double baseSalary;
    public BasePlusSalesEmployee(){}

    public BasePlusSalesEmployee(String name,int month,double sales,double rate,double baseSalary){
        super(name,month,sales,rate);
        this.baseSalary = baseSalary;
    }
    public double gatBaseSalary(){
        return baseSalary;
    }
    public void setBaseSalary(){
        this.baseSalary = baseSalary;
    }
    public double getSalary(int month){
        return baseSalary+super.getSalary(month);
    }

}
package Timu;

public class Test {
    public static void main(String[] args) {
        //声明一个Employee类型的数组,并创建不同子类型的对象
        Employee[] employee = {new SalariedEmployee("张三",1,6000),
                new HourlyEmployee("李四",2,50,180),
                new SalesEmployee("王五" ,3,6500,0.15),
                new BasePlusSalesEmployee("赵六",4,5000,0.15,2000)};
        //打印每个员工的工资
        for(int i = 0; i < employee.length ;i++)
            System.out.println(Math.round(employee[i].getSalary(10)));
    }
}

题目3:

设计一个 Shape 接口和它的两个实现类 Square 和 Circle。要求如下:

1)Shape 接口中有一个抽象方法 area(),方法接收有一个 double 类型的参数,返回一个 double 类型的结果。

2)Square 和 Circle 中实现了 Shape 接口的 area()抽象方法,分别求正方形和圆形的面积并返回。

3)在测试类中创建 Square 和 Circle 对象,计算边长为 2 的正方形面积和半径为 3 的圆形面积。

package Timu;
//Shape 接口中有一个抽象方法 area(),方法接收有一个 double 类型的参数,返回一个 double 类型的结果
public interface Shape {
    public double area(double x);
}

package Timu;

public class Square implements Shape{
    public double area(double x)
    {
        double s = 0;
        s = x*x;
        return s;//求正方形面积
    }
}
package Timu;

public class Circle implements Shape{
    public double area(double x)
    {
        double s = 0;
        s = 3.14*(x*x);
        return  s ;//求圆形面积
    }
}
package Timu;

public class Interface {
    public static void main(String[] args) {
        Square  sq = new Square();
        Circle cc = new Circle();
        System.out.println("边长为 2 的正方形面积为:"+sq.area(2));
        System.out.println("半径为 3 的圆形面积为:"+cc.area(3));
    }
}

题目4:

编写一个程序,分别使用字节流和字符流拷贝一个文本文件。

要求如下:

1)使用 FileInputStream、FileOutputStreaem 和 FileReader、FileWriter 分别进行拷贝。

2)使用字节流拷贝时,定义一个 1024 长度的字节数组作为缓冲区,使用字符流拷贝。

package Timu;

import java.io.*;

//分别使用字节流和字符流拷贝一个文本文件
public class IO {
    public static void main(String[] args) throws IOException{
//        throws IOException 或者 Exception
        FileInputStream fis = new FileInputStream("D:\\MegaData2022\\qmfx.txt");
        FileOutputStream fos = new FileOutputStream("D:\\MegaData2022\\第一.txt");
        byte[] len = new byte[1024];
        int s;
        while ((s=fis.read(len))!=-1)
        {
            fos.write(len,0,s);
        }
        fis.close();
        fos.close();
        System.out.println("-------------------");
        BufferedReader br = new BufferedReader(new FileReader("D:\\MegaData2022\\qmfx.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\MegaData2022\\第一.txt"));

        String r;//这是String类型
        while((r=br.readLine())!=null)
        {
            bw.write(r);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
}

题目5:

//自定义异常

自定义一个图书馆借书的异常类NoBookException(继承 Exception,类中有一个接收String 类型参数的构造方法,使用 super 关键字调用父类的构造方法。)和 Borrower 类,在 Borrower的 borrow()方法中使用自定义异常,要求入下:

1) Borrower 类中定义一个 borrow(int index)方法,方法接收一个 int 类型的参数,表示借书的索引,当 index>25 时,borrow()方法用 throw 关键字抛出 NoBookException 异常,创建异常对象时,调用有参的构造方法,传入“无此书”。

  1. 在测试类中创建Borrower 对象,并调用borrow()方法测试 NoBookException异常,使用 try…catch语句捕获异常,调用 NoBookException 类的 getMessage()方法打印出异常信息
package qmfx2;
//自定义异常
class NoBookException extends Exception{

//    public NoBookException(){}
    public NoBookException(String s)
    {
        super(s);
    }

}
class Borrower{
    public void borrow(int index) throws NoBookException {
        if(index>25)
        {
            throw new NoBookException("无此书");
        }
    }
}
public class Test4 {
    public static void main(String[] args) {
        Borrower b = new Borrower();
        try {
            b.borrow(26);
        } catch (NoBookException e) {
            System.out.println(e.getMessage());
//            e.printStackTrace();
        }
    }
}

题目6:

设计一个程序,要求有以下功能:

声明一个长度为10的整型数组,数组中的元素通过解析字符串参数获得;

寻找数组中的最大值元素和这个元素的下标

输出最大值元素的值和它的下标值

Tip:我们使用 Integer 类的 parseInt() 方法,该方法在转换后返回整数。由于它处理单个值,我们使用 for 循环将 string 数组的所有元素转换为 int,并同时将它们分配给 int 数组

package qmfx2;

import java.util.Scanner;

//将字符串转化为整型数组
//String str="123456";
//int[] a = new int[str.length()];
//for(int i=0;i<str.length();i++)
// {
//      a[i]=str.charAt(i)-'0';
//  }
public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = null;
        s1=sc.nextLine();
//        String s1 = "0123456789";
//        字符串
        String[] s2 = s1.split("");
//        字符数组
        int[] s3 = new int[s2.length];
//        整型数组
        for(int i=0;i<s2.length;i++)
        {
            s3[i] = Integer.parseInt(s2[i]);
        }
//        将字符转化为整型数字
        int max = s3[0];
        int index = 0;
        for(int i=1;i<s3.length;i++)
        {
            if(s3[i]>max)
            {
                max = s3[i];
                index = i;
            }
        }
        System.out.println("最大值为:"+max+" "+"对应的下标值为:"+index);
//        for(int i=0;i<s3.length;i++)
//        {
//            System.out.print(s3[i]+" ");
//        }
//        完美输出
    }
}
//1、int i = Integer.valueOf(str).intValue();
//2、double b = Double.parseDouble(str); 将字符串转换为double型
//3、int b = Integer.parseInt(str);   将字符串转换为int型

//String str = "123abc";
//    String[] arr = str.split("");
//for (int i = 0; i < arr.length; i++) { // String数组
//        System.out.print(arr[i]); // 输出 1 2 3 a b c
//        }

//public class SimpleTesting{
//    public static void main(String[] args) {
//
//        String[] arr = new String[] {"2","34","55"};
//        int[] arr2 = new int[3];
//        for (int i = 0; i < arr.length; i++) {
//            arr2[i] = Integer.parseInt(arr[i]);
//        }
//        for (int i = 0; i < arr2.length; i++) {
//            System.out.println(arr2[i]);
//        }
//    }}

题目7:

编写一个多线程程序,模拟火车售票窗口售票的功能。创建线程 1 和线程 2,通过这两个线程共同售出 100 张票。

package Timu;

public class Threaddd {
    public static void main(String[] args) {
        Ticket t = new Ticket();
        Thread t1 = new Thread(t,"线程1");
        t1.start();
        Thread t2 = new Thread(t,"线程2");
        t2.start();

    }
}
class Ticket implements Runnable{
    private int tickets = 100;
    Object lock = new Object();
    @Override
    public void run() {
        while (true)
        {
            synchronized (lock)
            {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(tickets>0)
                {
                    System.out.println(Thread.currentThread().getName()+"正在发售第 "+(tickets--)+"张票 ");
                }
                else{
                    break;
                }
//                if(tickets<=0)
//                    break;
            }
        }
    }
}
package Timu;

public class Threadd {
    public static void main(String[] args) {
        Megadata m = new Megadata();
        Thread s1 = new Thread(m,"线程1");
        s1.start();
        Thread s2 = new Thread(m,"线程2");

        s2.start();
    }
}
class Megadata implements Runnable{
    private int num = 100;
//    private int s = 0;
    @Override
    public void run() {
        while (num>0) {
//            Thread th = Thread.currentThread();
//            String th_name = th.getName();
            System.out.println(Thread.currentThread().getName()+"正在发售第 "+(num--)+"张票 ");
        }
    }
}