IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 2021-08-14 Java练习题 -> 正文阅读

[Java知识库]2021-08-14 Java练习题

1、按要求编写一个Java应用程序:

(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。

(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

package zpb.practice.no1;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 15:38
 */
public class Rectangle {
    private int length;
    private int width;

    public int area() {
        return getLength() * getWidth();
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

}

package zpb.practice.no1;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 15:42
 */
public class Cuboid extends Rectangle {
    private int height;

    public Cuboid(int length, int width, int height) {
        this.setLength(length);
        this.setWidth(width);
        this.height = height;
    }

    public int volume() {
        return area() * getHeight();
    }

    public int getHeight() {
        return height;
    }
}

package zpb.practice.no1;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 15:44
 */
public class Test {
    public static void main(String[] args) {
        Cuboid c = new Cuboid(1, 2, 3);
        System.out.println("长方体体积为" + c.volume());
    }
}

2、有员工类:员工编号,姓名,工资,年龄。

(1)创建十个员工,存储在List集合中。

(2)请按照如下要求排序:

工资高的在前面;

如果工资相同,年龄大的在前面;

如果工资和年龄都相同,姓名小的在前面。

package zpb.practice.no2;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 15:49
 */
public class Employee {
    private int ID;
    private String name;
    private double salary;
    private int age;

    public Employee(int ID, String name, double salary, int age) {
        this.ID = ID;
        this.name = name;
        this.salary = salary;
        this.age = age;
    }

    @Override
    public String toString() {
        return "员工:" +
                "ID-" + ID +
                ",姓名-" + name +
                ",工资-" + salary +
                ",年龄-" + age;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public int getAge() {
        return age;
    }
}

package zpb.practice.no2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 15:54
 */
public class ListEmployee {
    public static void main(String[] args) {
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1, "马云", 300, 27));
        list.add(new Employee(2, "马化腾", 10000, 23));
        list.add(new Employee(3, "乔布斯", 1000000, 21));
        list.add(new Employee(4, "周杰伦", 50000, 22));
        list.add(new Employee(5, "巴菲特", 70000, 77));
        list.add(new Employee(6, "爱因斯坦", 2000, 22));
        list.add(new Employee(7, "梵高", 10, 28));
        list.add(new Employee(8, "成龙", 8000000, 24));
        list.add(new Employee(9, "李小龙", 200, 19));
        list.add(new Employee(10, "马斯克", 300000, 18));
        listSort(list);
    }

    public static void listSort(List<Employee> list) {
        list.sort((s1, s2) -> {
            if (s1.getSalary() < s2.getSalary()) {
                return 1;
            } else if (s1.getSalary() == s2.getSalary() && s1.getAge() < s2.getAge()) {
                return 1;
            } else if (s1.getSalary() == s2.getSalary() && s1.getAge() == s2.getAge() && s1.getName().compareTo(s2.getName()) > 0) {
                return 1;
            } else {
                return -1;
            }
        });
        System.out.println("排序后如下:");
        list.forEach(System.out::println);
    }
}

3、服务器上存储的有一个文本文件java.txt,请在客户端:

(1)统计文件中包含的英文字母数量,数字数量,汉字数量,其他字符数量。

(2)将统计信息储存在map集合中。

(3)通过map将统计信息打印出来。

package zpb.practice.no3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 16:07
 */
public class TXTServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8888);
        Socket s = server.accept();

        File f = new File("src/zpb/practice/no3/server.txt");
        FileInputStream in = new FileInputStream(f);
        OutputStream out = s.getOutputStream();

        byte[] b = new byte[16];
        int i = in.read(b);
        while (i != -1) {
            out.write(b, 0, i);
            i = in.read(b);
        }

        out.close();
        in.close();

        s.close();
        server.close();
    }
}

package zpb.practice.no3;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/8/14 16:10
 */
public class TXTClient {
    public static void main(String[] args) throws IOException {
        Socket c = new Socket("localhost", 8888);
        InputStream in = c.getInputStream();
        File f = new File("src/zpb/practice/no3/client.txt");
        FileOutputStream out = new FileOutputStream(f);

        byte[] b = new byte[16];
        int i = in.read(b);
        while (i != -1) {
            out.write(b, 0, i);
            i = in.read(b);
        }
        System.out.println("下载完成");

        out.close();
        in.close();

        c.close();

        String E1 = "[\u4e00-\u9fa5]";// 中文
        String E2 = "[a-zA-Z]";// 英文
        String E3 = "[0-9]";// 数字

        FileInputStream fis = new FileInputStream(f);
        // 将字节流转化为字符流
        InputStreamReader isr = new InputStreamReader(fis);
        // 转化为缓存模式
        BufferedReader br = new BufferedReader(isr);

        Map<String, Integer> map = new HashMap<>();

        // 将读出来的字符复制到ss
        String ss = "";
        String s;
        while ((s = br.readLine()) != null) {
            ss += s;
        }
        // 遍历字符串
        String temp;
        for (int j = 0; j < ss.length(); j++) {
            temp = String.valueOf(ss.charAt(j));
            if (temp.matches(E1)) {// 如果该字符匹配中文
                if (map.containsKey("汉字")) {
                    int num = map.get("汉字");
                    num++;
                    map.put("汉字", num);
                } else {
                    map.put("汉字", 1);
                }
            } else if (temp.matches(E2)) {// 如果该字符匹配英文
                if (map.containsKey("英文")) {
                    int num = map.get("英文");
                    num++;
                    map.put("英文", num);
                } else {
                    map.put("英文", 1);
                }
            } else if (temp.matches(E3)) {// 如果该字符匹配数字
                if (map.containsKey("数字")) {
                    int num = map.get("数字");
                    num++;
                    map.put("数字", num);
                } else {
                    map.put("数字", 1);
                }
            } else {
                if (map.containsKey("其他字符")) {
                    int num = map.get("其他字符");
                    num++;
                    map.put("其他字符", num);
                } else {
                    map.put("其他字符", 1);
                }
            }
        }
        for (Map.Entry<String, Integer> next : map.entrySet()) {
            System.out.println(next.getKey() + "出现次数为" + next.getValue());
        }
    }
}

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-15 15:22:28  更:2021-08-15 15:24:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 7:33:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码