代码改变世界

java第二次作业

2017-03-28 14:07  空白格DE  阅读(172)  评论(1)    收藏  举报

1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法。举例说明equals方法和==的区别
(1)Eclipse关联jdk并截图



(2)
==进行比较时,比较的是他们在内存中存放的地址;equals是比较值是否相等
例如1:

public class StringDemo04{
public static void main(String[],args){
String str1="hello";
String str2=new String("hello");
String str3=str2;
System.out.println("str1==str2->"+(str1==str2));
System.out.println("str1==str3->"+(str1==str3));
System.out,println("str2==str3->"(str2==str3));
}
}
public class StringDemo05{
public static void main(String[] args){
String str1="hello";
String str2=new String("hello");
String str3=str2;
System,out,println("str1 equals str2->"(str1.equals(str2)));
System,out,println("str2 equals str3->"(str2.equals(str3)));
System,out,println("str1 equals str3->"(str1.equals(str3)));
}
}

2.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?
(1)构造方法:为类中的属性初始化,是一种特殊的方法,与一般的方法不同是:构造方法的名称必须与类名称一样;构造方法的声明处不能有任何返回值类型的声明;不能在构造方法中使用return返回一个值。
(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方法中没有参数传递过去。
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");
        }
    }     
}

结果是不为0.3

原因:double类型的精度超出精度范围,导致计算出来的数不准。
修改之后

import java.math.BigDecimal;

public class Test {
    public static void main(String args[]) { 
        System.out.println("加法运算"+MyMath.round(MyMath.add(0.1,0.1,0.1), 1));
    }
}
class MyMath{
	public static double add(double d1,double d2,double d3){

		BigDecimal b1=new BigDecimal(d1);
		BigDecimal b2=new BigDecimal(d2);
		BigDecimal b3=new BigDecimal(d3);
		return b1.add(b2).add(b3).doubleValue();
	}
	          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();
	          }
}


4.运行下列程序,结果是什么?分析原因,应如何修改.

    public static void main(String[] args) {
        MyClass[] arr=new MyClass[3];
        arr[1].value=100;
    }
}
class MyClass{
    public int value=1;
}

运行结果:含有空指针

改为

public class lianxi {
    public static void main(String[] args) {
        MyClass[] arr={new MyClass()};
        arr[1].value=100;
    }


    }
    class MyClass{
        public int value=1;
}

5.在一个10000次的循环中,需要进行字符串的连接操作,那么,应该使用String类还是StringBuffer类,为什么?性能有差异吗?能否写出测试代码证明你的结论。(可查阅资料)
是一个变量和常量的关系。StringBuffer对象的内容可以修改;而String对象一旦产生后就不可以被修改,,所以在进行字符串操作时用stringbuffer类比较好。
6总结:感觉这次的实验内容对我来说有点难,做了好长时间才写出程序的,在写的过程中,有的程序还运行不出来,通过询问同学和老师才把程序写出来,但是我写的程序还是不够好,第三题和第四题我没有写出来,我写了第四题,但是运行的时候有点问题,还没有能够及时的解决,所以这次就先不交了,我再私底下再继续改进。我希望还是在接下来的时间里我可以多多的练习,来不断地完善。

二实验
1.评分系统:一共10个评委,满分10分,假设有5个选手,分别由评委打分,去掉一个最高分和一个最低分后的平均分为该选手得分,将选手的得分从高到低进行输出。定义适当的方法。
设计思想:定义数组,通过手动输入评委的评分,求出总和得分,再用总和得分减去最大值和最小值,求出平均成绩
问题:只能一次一次的输入
解决方法:定义的数组和循环

 for (int i = 1; i < scores.length; i++) {
        sum+=scores[i];
    }
    int[] score =new int[10] ;
	int avg = (sum-getmax(score)-getmin(score))/8; 
    System.out.println("最后的平均分是:"+avg);

2.Email验证:在各种应用中,需要对用户输入的email地址进行验证,编写一个方法,判断一个email地址是否有效。(判断条件:A:@和.同时存在 B: @在.之前 C: 不能@开头 D: 以com|cn|net|gov|edu|org结尾 )
设计思想:i通过定义数组,以及if的判断语句来判断是否符合条件,用到布尔类型,用ture和false来判断

	public static boolean right(String email) {
		String str = "[0-9]";
		int index1 = email.indexOf("@");
		int index2 = email.indexOf(".");
		if (index1 > index2) {
			if (str.startsWith("[0-9]")) {
				System.out.println("([0-9]以0-9数字开头)");
			}
			if (str.endsWith("com")) {
				System.out.println("地址正确");
			}
			if (str.endsWith("cn")) {
				System.out.println("地址正确");
			}
			if (str.endsWith("net")) {
				System.out.println("地址正确");
			}
			if (str.endsWith("gov")) {
				System.out.println("地址正确");
			}
			if (str.endsWith("edu")) {
				System.out.println("地址正确");
			}
			if (str.endsWith("org")) {
				System.out.println("地址正确");
			}
		} else if (index1 < index2) {
			System.out.println("地址错误");
		}
		return false;
	}
}

代码托管:https://git.oschina.net/jin1/jr.git

1