《Java技术》第二次作业计科1501赵健宇

(一)学习总结

  • 1.使用Eclipse关联jdk源代码,查看String类的equals()方法

    equals()方法截图

    ”比较的是地址。equals方法他同样使用号进行内存地址的比较。但是equals方法重写如果号比较不相等,还会进行一下值的比较。
    所以equals方法具体的作用要看当前的那个类是如何实现重写父类中该方法的。
    简单说,equals方法比较两个对象的内容,
    比较两个对象在内存中的首地址是否相同。

    ==的使用结果:

        String str1 = "hello";
        String str2 = new String("hello");
        String str3 = str2;
        String str4 = "hello";
        System.out.println("str1 == str2 --> " + (str1 == str2)); //FALSE
        System.out.println("str1 == str3 --> " + (str1 == str3)); //false
        System.out.println("str2 == str3 --> " + (str2 == str3));//true
    
    
      如果使用equals()则所有值都为true。
    
  • 2.构造方法用来对变量进行初始化的.相当于给变量赋值.
    如果你没定义构造方法,在每个类的定义当中,都默认有一个无参数的构造方法

       > 如    public Student(){
               };
         
      **构造方法的重载**(1)在Java中,允许在一个类中定义多个构造方法。
                        (2)在创建对象时,系统会自动根据所调用的构造方法中包含的参数类型,个数,选择匹配的构造方法创建对象。
    
    public class Test {
        public static void main(String args[]) { 
           Foo obj = new Foo();       
        }     
    }
    class Foo{
        int value;
        public Foo(int intValue){
            value = intValue;
        }
    }
    

    以上构造方法不能通过编译,如果你定义了一个有参的构造函数,为了保证正确性,系统不会创建无参构造函数,这时候,如果你还想允许无参构造,就必须显式的声明一个
    解决方法: Foo obj = new Foo(100);

  • 3.运行下列程序,结果是什么?查阅资料,分析为什么。

    public class Test {
        public static void main(String args[]) { 
            double a = 0.1;
            double b = 0.1;
            double c = 0.1;
            if((a + b + c) == 0.3){
                System.out.println("等于0.3");
            }else {
                System.out.println("不等于0.3");
            }
        }     
    }
    

    (a+b+c)为0.30000000000000004,不等于0.3。原因在于我们的计算机是二进制的。浮点数没有办法是用二进制进行精确表示。
    其实java的float只能用来进行科学计算或工程计算,在大多数的商业计算中,一般采用java.math.BigDecimal类来进行精确计算。

        在使用BigDecimal类来进行计算的时候,主要分为以下步骤:
    
            1、用float或者double变量构建BigDecimal对象。
    
           2、通过调用BigDecimal的加,减,乘,除等相应的方法进行算术运算。
    
           3、把BigDecimal对象转换成float,double,int等类型。
    
              转载说明 [**原文链接**](http://www.cnblogs.com/chenssy/archive/2012/09/09/2677279.html)
    

    经过修改

import java.math.BigDecimal;

public class Test {
	public static void main(String args[]) {
		double a = 0.1;
		double b = 0.1;
		double c = 0.1;
		if ((Math.round(Math.add(a, b, c), 1)) == 0.3) {
			System.out.println("等于0.3");
		} else {
			System.out.println("不等于0.3");
		}
	}

	
}
class Math{
	public static double add(double value1, double value2, double value3) {
		BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
		BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
		BigDecimal b3 = new BigDecimal(Double.valueOf(value3));
		return b1.add(b2).add(b3).doubleValue();//.add(b3)
	}
	public static double round(double d,int len){
		BigDecimal b1 = new BigDecimal(d);
		BigDecimal b2 = new BigDecimal(1);
		return b1.divide(b2, len,BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}

运行结果:等于0.3

  • 4.用类创建数组
public class Test {
    public static void main(String[] args) {
        MyClass[] arr=new MyClass[3];
        arr[1].value=100;
    }
}
class MyClass{
    public int value=1;
}

经讨论

public class Test {
    public static void main(String[] args) {
        MyClass[] arr={new MyClass(),new MyClass(),new MyClass()};
        System.out.println(arr.length);
        arr[1].value=100;
        for(int i = 0;i<arr.length;i++){
        	System.out.println(arr[i].value);
        }
        
    }
}
class MyClass{
    public int value=1; 
}
  • 5.在一个10000次的循环中,需要进行字符串的连接操作,那么,应该使用String类还是StringBuffer类,为什么?性能有差异吗?能否写出测试代码证明你的结论。(可查阅资料)
    用StringBuffer类,String的内容一旦声明就不可改变,只能改变string的引用地址,若改变次数过多,应该用StringBuffer。

      StringBuffer类中append()方法可以一直调用,并且返回一个StringBuffer类的实例。
    
public class Test {
    public static void main(String[] args) {
        StringBuffer buf = new StringBuffer();
        for(int i=0;i<50;i++){
        	buf.append("hello!");
        }
        System.out.println(buf);
    }
}
  • 6.其他需要总结的内容。

1.java中数组必须指定长度,初始化数组的时候指定,例子:
int myint[]=new int[20];
int myint[];只是声明了一个数组变量而不是定义了一个数组 ,注意二者的区别

(二)实验总结

1.评分系统:一共10个评委,满分10分,假设有5个选手,分别由评委打分,去掉一个最高分和一个最低分后的平均分为该选手得分,将选手的得分从高到低进行输出。

程序设计思路:利用二维数组依次输入选手成绩,使用求最大,最小,平均值三个方法。最后排序。people值可以更换
问题1:计算选手最终得分
解决方案:用总成绩减去最大得分减去最小得分除8

public static double[] avg(int xuanshou[][], int people) {
		double[] grade = new double[people];
		for (int j = 0; j < xuanshou.length; j++) {
			for (int i = 0; i < xuanshou[j].length; i++) {
				grade[j] += xuanshou[j][i];
			}
		}
		for (int x = 0; x < people; x++) {
			int out = max(xuanshou, x) + min(xuanshou, x);
			grade[x] = grade[x] - out;
			grade[x] = grade[x] / 8;
		}
		return grade;
	}

2.Email验证:在各种应用中,需要对用户输入的email地址进行验证,编写一个方法,判断一个email地址是否有效。

程序设计思路:输入String类型,利用string方法查找判断
问题1:“@”“.”位置问题
解决方案:indexOf方法取得位置,判断位置前后

public static boolean jundge(String in) {
		int x = in.indexOf("@");
		int y = in.indexOf(".");
		if (in.endsWith("com") || in.endsWith("cn") || in.endsWith("net")
				|| in.endsWith("gov") || in.endsWith("edu")
				|| in.endsWith("org")) {
			if (x != -1 && y != -1 && x < y && x != 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

3.统计文件:输入一个字符串,包含各种文件类型的文件名。文件名之间用“,”分隔,要求将各个文件名的首字母大写后分别输出,并统计各种类型文件的文件个数。

程序设计思路:string的分割方法分离并记录文件类型

主要代码

String s[] = filename.split(",");
		String[] flags = new String[s.length];
		for(int i=0;i<s.length;i++){
			String sb = new String(s[i]);
			String str = sb.substring(0, 1).toUpperCase() + sb.substring(1);			
			System.out.print(str+"	");			
			flags[i] = sb.substring((sb.indexOf(".")));
		}
		System.out.println();
		int jilu=0;
		for(int j=0;j<flags.length;j++){
			int F1 = 1;
			for(int k=j+1;k<flags.length;k++){
				if(flags[k].equals(flags[j])){
					F1++;
					jilu = k;
				}
			}	
			if(j==jilu){
				j++;
			}else{
				System.out.println(flags[j]+"个数为"+F1);
			}
		}
	}

4.身份证识别:公民身份证号码由十八位数字组成。从左至右依次为:六位地址码,八位出生日期码,三位顺序码和一位校验码。顺序码的奇数分配给男性,偶数分配给女性。

程序设计思路:输入身份证号码可能有x,用string类型,
问题1:身份证长度等于18
原因:不能大于不能小于
解决方案:

String ID = input.nextLine();
		if(ID.length()!=18){
			System.out.println("输入错误!");
			System.exit(0);
		}

问题2:判断地区问题
原因:地区太多不能用ifelse判断
解决方案:使用二维数组,将string的身份证转为int型。

		String[][] region1 = {
				{"北京","天津","河北","山西","内蒙"},
				{"辽宁","吉林","黑龙江"},
				{"上海","江苏","浙江","安徽","福建","江西","山东"},
				{"河南","湖北","湖南","广东","广西","海南"},
				{"重庆","四川","贵州","云南","西藏"},
				{"陕西","甘肃","青海","宁夏","新疆"}
		};
		String regionX = ID.substring(0, 1);
		String regionY = ID.substring(1, 2);
		System.out.println();
		try {
		    int x = Integer.parseInt(regionX);
		    int y = Integer.parseInt(regionY);
		    for(int j= 0;j<region1.length;j++){
		    	for(int k= 0;k<region1[j].length;k++){
		    		if((x-1)==j&&(y-1)==k){
		    			System.out.println(region1[j][k]);
		    		}
		    	}
		    }		    
		} catch (NumberFormatException e) {
		    e.printStackTrace();
		}

(三)代码托管(务必链接到你的项目)

https://git.oschina.net/hebau_cs15/hebau-cs01ZJY

原文地址:https://www.cnblogs.com/ai1045152332/p/6607551.html