1、按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
package zpb.practice.no1;
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;
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;
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;
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;
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;
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;
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<>();
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());
}
}
}
|