目录
13.list集合,Comparable接口,实现compareTo方法,Collections类
15.用包装类解决Date数据的输入输出,map集合,list集合,Collections类的方法
10.list集合
import java.util.ArrayList;
import java.util.List;
public class EmployeeManage {
private static List<Employee> list=new ArrayList<>();
public double getTotalScore(){
double sum=0;
for (int i=0;i<list.size();i++){
sum=sum+list.get(i).age;
}
return sum;
}
public double getAverageAge(){
return getTotalScore()/(int)(list.size());
}
public void addEmployee(Employee e){
list.add(e);
}
public static void main(String[] args) {
EmployeeManage em=new EmployeeManage();
Employee e1=new Employee("1","1",100);
Employee e2=new Employee("1","1",500);
Employee e3=new Employee("1","1",100);
em.addEmployee(e1);
em.addEmployee(e2);
em.addEmployee(e3);
System.out.println(em.getAverageAge());
}
}
class Employee{
private String no;
private String name;
public double age;
public Employee(String no, String name, double age) {
this.no = no;
this.name = name;
this.age = age;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public Employee() {
}
}
11.用面向对象的思路算养宠物的花费
public class Test2 {
public static void main(String[] args) {
Pet dog1=new Pet();
Pet cat1=new Pet();
dog1.setCount(2);
dog1.setPrice(1.5);
dog1.sum();
cat1.setCount(0.5);
cat1.setPrice(2.5);
cat1.sum();
System.out.println(2*dog1.sum()+cat1.sum());
}
}
class Pet{
private String food;
private double price;
private double count;
public Pet() {
}
public double sum(){
return this.price*this.count;
}
public Pet(String food, double price, double count) {
this.food = food;
this.price = price;
this.count = count;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCount() {
return count;
}
public void setCount(double count) {
this.count = count;
}
}
12.字节流
import java.io.*;
import java.util.Scanner;
public class T12 {
public static void main(String[] args) throws IOException {
// 将数据写入文件中
DataOutputStream dop = new DataOutputStream(new FileOutputStream("d:\\my.bin"));
Scanner sc=new Scanner(System.in);
int sum=0;
for (int i=0;i<20;i++){
int temp=sc.nextInt();
dop.writeInt(temp);
sum=sum+temp;
}
dop.close();
System.out.println("输入成功,这二十个数的和为:"+sum);
// 从文件中读入数据
DataInputStream dip = new DataInputStream(new FileInputStream("d:\\my.bin"));
int count = 0;
// 判断是否读入完毕
// 用while循环,当抛出EOFException时表示数据读入完毕
try {
while (true) {
count = dip.readInt();
System.out.println(count);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取结束");
} finally {
dip.close();
}
}
}
13.list集合,Comparable接口,实现compareTo方法,Collections类
public class Student implements Comparable<Student> {
private String name;
private String sex;
private int age;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
// TODO 自动生成的方法存根
return this.age - o.age;
/*实现按照 age 进行比较大小的功能,并且是 age 大的对象也大。
* 反之,用 return o.age-this.age;则表示 age 小的对象大。*/
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
}
public static void main(String[] args) {
List<Student> list1 = new ArrayList<Student>();
Student student1 = new Student("张三", "男",20);
Student student2 = new Student("李四","男",22);
list1.add(student1);
list1.add(student2);
for (Student o: list1) {
System.out.println("姓名:" + o.getName()+"性别:"+o.getSex()+"年龄:"+o.getAge());
}
Collections.sort(list1);
}
14.数组,求平均值
import java.util.Scanner;
//main方法,数组、输入输出语句、循环语句的用法
public class Calculate {
// public static Scanner scanner;
//申明一个私有的静态引用
public static void main(String[] args) {
Scanner in=new Scanner(System.in); //用Scanner获取键盘输入
System.out.println("输入数组的大小:");
int count1 = in.nextInt();
int i;
double sum = 0; //定义总和
double cal = 0; //定义平均值
int value[] = new int[1000]; //定义一个value数组大小=1000,value数组用来存放待计算的数字
for(i = 0;i < count1;i++) //数组是从0开始数的,所以i<count时,用for循环来输入数组的内容
{
System.out.println("输入数组的"+(i+1)+"个数字:");
int a = in.nextInt(); //a的值=获取的键盘输入的值
value[i] = a; //value数组的第i个数的值=a的值
sum = sum + value[i]; //value[i]的累加
}
System.out.println("总和="+sum);
cal = sum / count1;
System.out.println("平均值="+cal);
for(i = 0;i < count1;i++) //遍历整个数组
{
if(cal<value[i]) //当平均值<数组中的元素时
{
System.out.println("数组中比平均值大的元素为"+value[i]);
break; //结束循环
}
}
}
}
15.用包装类解决Date数据的输入输出,map集合,list集合,Collections类的方法
import javax.xml.crypto.Data;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Worker {
protected static Format format=new SimpleDateFormat("yyyy-MM-dd");
private String name;
private Date birthday;
private float wage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public float getWage() {
return wage;
}
public void setWage(float wage) {
this.wage = wage;
}
public Worker() {
}
public Worker(String name, Date birthday, float wage) {
this.name = name;
this.birthday = birthday;
this.wage = wage;
}
public void print(){
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("name:"+this.name+"birthday:"+format.format(this.birthday)+"wage"+this.wage);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class WorkerManage {
public float getAverageWage(List<Worker> list){
float totalwage=0;
for (int i=0;i<list.size();i++){
totalwage=totalwage+list.get(i).getWage();
}
return totalwage/list.size();
}
public static void main(String[] args) {
Date date=new Date();
Scanner sc=new Scanner(System.in);
//new一个放数据类型为workerlist存worker对象
List<Worker> list=new ArrayList<>();
//从键盘读入,生成5个worker,add到list里
for (int i=0;i<5;i++){
String name=sc.next();
String brithday=sc.next();
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
try {
date=dateFormat.parse(brithday);
} catch (ParseException e) {
e.printStackTrace();
}
float wage=sc.nextFloat();
Worker worker=new Worker(name,date,wage);
list.add(worker);
}
//new一个workerTemp对象
String workerTempBirthday="1996-8-22";
Date workerTempDate=new Date();
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
try {
workerTempDate=dateFormat.parse(workerTempBirthday);
} catch (ParseException e) {
e.printStackTrace();
}
Worker workerTemp=new Worker("张三",workerTempDate,3000);
//将workerTemp对象替换list中索引为2的元素
list.set(2,workerTemp);
//倒序排列
Collections.reverse(list);
//循环遍历list
System.out.println();
for (int i=0;i<list.size();i++){
list.get(i).print();
}
//输出list中worker的平均工资
WorkerManage demo=new WorkerManage();
System.out.println("工人的平均工资为:"+demo.getAverageWage(list));
}
}
16接口,抽象类
package 接口与抽象类;
public interface Shape {
public double getArea();//面积
public String draw();
public double getPerimeter();//周长
}
package 接口与抽象类;
public abstract class ShapeClass {
public abstract double getArea();
public abstract String draw();
public abstract double getPerimeter();
}
package 接口与抽象类;
public class Circle implements Shape {
public double r = 0;
private final static double Pi=3.14;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circle(double r) {
this.r=r;
}
public Circle() {
// TODO 自动生成的构造函数存根
}
@Override
public double getArea() {
// TODO 自动生成的方法存根
return Pi*r*r;
}
@Override
public String draw() {
// TODO 自动生成的方法存根
return "I am a circle";
}
@Override
public double getPerimeter() {
// TODO 自动生成的方法存根
return 2*Pi*r;
}
}
package 接口与抽象类;
public class CircleClass extends ShapeClass {
private int r;
// private final static double Pi = 3.14;//圆周率
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public CircleClass(int r) {
this.r = r;
}
public CircleClass() {
}
@Override
public double getArea() {
// TODO 自动生成的方法存根
return Math.PI * r * r;
}
@Override
public String draw() {
// TODO 自动生成的方法存根
return "I am a Circle";
}
public double getPerimeter() {
return 2 * Math.PI * r;
}
}
package 接口与抽象类;
public class Rectangle implements Shape {
private double x;
private double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public Rectangle(double x, double y) {
this.x = x;
this.y = y;
}
public Rectangle() {
}
@Override
public double getArea() {
// TODO 自动生成的方法存根
return x * y;
}
@Override
public String draw() {
// TODO 自动生成的方法存根
return "I am a rectangle";
}
@Override
public double getPerimeter() {
// TODO 自动生成的方法存根
return 2 * x + 2 * y;
}
}
package 接口与抽象类;
//长方形类
public class RectangleClass extends ShapeClass {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public RectangleClass(int x, int y) {
this.x = x;
this.y = y;
}
public RectangleClass() {
}
@Override
public double getArea() {
// TODO 自动生成的方法存根
return x * y;
}
@Override
public String draw() {
// TODO 自动生成的方法存根
return "I am a RectangleClass";
}
public double getPerimeter() {
return 2 * (x + y);
}
}
package 接口与抽象类;
public class Test {
public static void main(String[] args) {
System.out.println("接口");
// 接口
Shape r1 = new Rectangle(3, 4);
Shape c1 = new Circle(4);
System.out.println(r1.draw());
System.out.println("长方形的面积:" + r1.getArea());
System.out.println("长方形的周长:" + r1.getPerimeter());
System.out.println(c1.draw());
System.out.println("圆的面积" + c1.getArea());
System.out.println("圆的周长" + c1.getPerimeter());
System.out.println("------------");
System.out.println("抽象类");
// 抽象类
ShapeClass r2 = new RectangleClass(3, 4);
ShapeClass c2 = new CircleClass(3);
System.out.println(r2.draw());
System.out.println("长方形的面积:" + r2.getArea());
System.out.println("长方形的周长:" + r2.getPerimeter());
System.out.println(c2.draw());
System.out.println("圆的面积" + c2.getArea());
System.out.println("圆的周长" + c2.getPerimeter());
}
}