JAVAboke五

题目:

7-2 sdut-Collection-sort--C~K的班级(II)

经过不懈的努力,C~K终于当上了班主任。

现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?

输入格式:

第一行输入一个N,代表C~K导出的名单共有N行(N<100000).

接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。

输出格式:

第一行输出一个n,代表删除重复名字后C~K的班级共有几人。

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序

代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {

public static void main(String[] args) {
ArrayList<Student> users =new ArrayList<>();
int stunum;
int pd=0,i,j,old;
Scanner input=new Scanner(System.in);
stunum=input.nextInt();
String number,name,sex;
int nums=0;
for( i=0;i<stunum;i++){
number=input.next();
name=input.next();
old=input.nextInt();
sex=input.next();
if(i==0){
users.add(new Student(number,name,old,sex));
nums++;
}else {
for( j=0;j<nums;j++){
// System.out.println(users.get(j).num+" "+number);
if(number.equals(users.get(j).num))
pd++;
}
if(pd==0){
users.add(new Student(number,name,old,sex));
nums++;
}
pd=0;
}
}
/*
User a=new User("1",1);
int o=0,j=0,max=0;
for(i=0;i<end;i++){
for(j=i;j<end;j++){
if(Double.parseDouble(users.get(i).number)>=Double.parseDouble(users.get(j).number)){
max=j;
}
}
a=users.get(i);
users.set(i,users.get(max));
users.set(max,a);
}
*/
Student student=new Student("0001","nh",01,"F");
int min=0;
for(i=0;i<nums;i++){
for(j=i;j<nums;j++){
if(Integer.parseInt(users.get(i).num)>=Integer.parseInt(users.get(j).num)){
if(i!=j) {
student = users.get(i);
users.set(i, users.get(j));
users.set(j, student);
}
}
}

}
System.out.println(nums);
for(int p=0;p<nums;p++){
System.out.println(users.get(p).num+" "+users.get(p).name+" "+users.get(p).old+" "+users.get(p).sex);
}
}
}

class Student {

String name;

String num;

int old;

String sex;


public Student(String num, String name, int old, String sex) {

this.num = num;

this.name = name;

this.old = old;

this.sex = sex;

}
// @Override

// public String toString() {

// String s = sex ? "boy" : "girl";

// return "Num: " + num + " Name: " + name + " Sex: " + sex;
//}
}

7-3 阅读程序,按照题目需求修改程序
 
功能需求:
      使用集合存储3个员工的信息(有序);
      通过迭代器依次找出所有的员工。
 

提示:学生复制以下代码到编程区,并按需求进行调试修改。


// 1、导入相关包

//定义员工类
class Employee {

	private String name;
	private int age;

	public Employee() {
		super();
	}

	public Employee(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	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;
	}
}

//主函数
public class Main {

	public static void main(String[] args) {
				// 1、创建有序集合对象
				Collection c ;

      // 创建3个员工元素对象
		for (int i = 0; i < 3; i++) {
			Scanner sc = new Scanner(System.in);
			String employeeName = sc.nextLine();
			int employeeAge = sc.nextInt();
			
			Employee employee = new Employee(employeeName, employeeAge);
			c.add(employee);
		}			
				
				
				
				// 2、创建迭代器遍历集合
				Iterator it;
				
				//3、遍历
				while (it.hasnext) {
					
					//4、集合中对象未知,向下转型
					Employee e =  it.next();
					
					System.out.println(e.getName() + "---" + e.getAge());
				}
	}

}


// 1、导入相关包
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.HashMap;
//定义员工类
class Employee {

private String name;
private String age;
public Employee(String name, String age) {

this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
}

//主函数
public class Main {

public static void main(String[] args) {
// 1、创建有序集合对象
ArrayList<Employee> users =new ArrayList<>();
String employeeAge;
String employeeName;
Scanner sc = new Scanner(System.in);
// 创建3个员工元素对象
for (int i = 0; i < 3; i++) {
employeeName = sc.nextLine();
employeeAge = sc.nextLine();
users.add(new Employee(employeeName, employeeAge));
}
for(int j=0;j<3;j++) {
System.out.println(users.get(j).getName() + "---" + users.get(j).getAge());
}
}
}

 

1.期中考试总结:

题目:

    • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

    • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

        ```
            The line's color is:颜色值
            The line's begin point's Coordinate is:
            (x1,y1)
            The line's end point's Coordinate is:
            (x2,y2)
            The line's length is:长度值
        ```
      
       

      其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

    • 类图:
    •  

       作答:

    • import java.util.Scanner;

      public class Main {
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      Point P1=new Point();
      P1.setX(input.nextDouble());
      P1.setY(input.nextDouble());
      Point P2=new Point();
      P2.setX(input.nextDouble());
      P2.setY(input.nextDouble());
      Line L=new Line(P1,P2, input.next());
      L.display(L);
      }
      }
      class Point{
      private double x,y;

      public double getX() {
      return x;
      }

      public void setX(double x) {
      this.x = x;
      if(x<=0||x>200){
      System.out.println("Wrong Format");
      System.exit(0);
      }
      }

      public double getY() {
      return y;
      }

      public void setY(double y) {
      this.y = y;
      if(y<=0||y>200){
      System.out.println("Wrong Format");
      System.exit(0);
      }
      }
      public void display(Point P){

      }


      // public Point(double x, double y){
      // this.x=x;
      // this.y=y;
      // }
      }
      class Line{
      Point p1,p2;
      String color;
      double len;
      public Line(Point p1,Point p2,String color){
      this.p1=p1;
      this.p2=p2;
      this.color=color;
      this.len=Math.sqrt(Math.pow(p1.getX()- p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2));
      }
      public void display(Line L){
      System.out.println("The line's color is:"+L.color);
      System.out.println("The line's begin point's Coordinate is:");
      System.out.println("("+String.format("%.2f",L.p1.getX())+","+String.format("%.2f",L.p1.getY())+")");
      System.out.println("The line's end point's Coordinate is:");
      System.out.println("("+String.format("%.2f",L.p2.getX())+","+String.format("%.2f",L.p2.getY())+")");
      System.out.println("The line's length is:"+String.format("%.2f",L.len));
      }
      }
      /*
      The line's color is:颜色值
      The line's begin point's Coordinate is:
      (x1,y1)
      The line's end point's Coordinate is:
      (x2,y2)
      The line's length is:长度值

      */

 

(3)代码分析总结

 

 

这道题用了多个类Line类由点组成属性有斜率和y=kx+b衍生而成的a,b,c只不过IDEA好像没显示出来。。。

 (3)采坑心得:

看清题意运用类方法等进行程序设计,可以运用多种方法做题

运用调试等方法搞

 

(4)改进建议:

更多细节可以细化用更多方法区实现这样就不会杂乱无章,多用方法什么的使代码更使用

 

(5)总结:

三次作业更 好的将所学的内容应用起来使我们更好的掌握方法,特别是正则运算实用让人眼前一亮!要多学习新的知识并加以运用。

posted @ 2022-06-12 10:05  侠梓义雄  阅读(189)  评论(0)    收藏  举报