Java第二次作业——数组和String类

(一)学习总结

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

我个人理解,equals比较的是2个对象的内容而"=="比较的是2个对象的地址,例如:

public class Test{
	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("str1 equals str3-->"+(str1.equals(str3)));
		System.out.println("str2 equals str3-->"+(str2.equals(str3)));
	}
}	

运行结果:

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

运行结果:

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

因为Java进行数学计算时有一个原则,就是double和float用来做科学计算,商业计算中(也就是日常使用的时候)最好使用java.math.BigDecimal

修改如下:

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(add(add(a,b),c) == 0.3){
        	System.out.println("等于0.3");
    	}else {
        	System.out.println("不等于0.3");
    	}
	}    
	public static double add(double d1,double d2){
    	BigDecimal b1=new BigDecimal(Double.toString(d1));
    	BigDecimal b2=new BigDecimal(Double.toString(d2));
    	return b1.add(b2).doubleValue();
	}
}

运行结果:

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[3];
	        for(int i=0;i<arr.length;i++){
	            arr[i]=new MyClass();
	        }
	        arr[1].value=100;
	    }
	}
	class MyClass{
	    public int value=1;
	}

5.在一个10000次的循环中,需要进行字符串的连接操作,那么,应该使用String类还是StringBuffer类,为什么?性能有差异吗?能否写出测试代码证明你的结论。(可查阅资料)

应该使用StringBuffer类,因为Strin类表示字符串常量且内容和长度不能修改而StringBuffer类表示字符变量且内容和长度可以被修改,效率更高。

//使用String类
public class test {
	    public static void main(String args[]) {
	        String str =new String("lalala");
	        for (int i=0; i<10000;i++) {
	            str =  str+",lalala!";
	        }
	         System.out.println(str);
	    }
}

//使用StringBuffer类
import java.lang.StringBuilder;
public class test {
	public static void main(String args[]) { 
    	StringBuffer str=new StringBuffer("lalala");
    	for(int i=0;i<10000;i++){
        	str.append=(",lalala!");
    	}
    	System.out.println(str);
 	}        
} 

6.其他:

还是不熟练的问题,一些新的语法无法合上书自己写出来,总是忘掉几个字母或者忘记使用规则,还需要勤加练习。

(二)实验总结

实验内容:

1.评分系统:

程序设计思路:创建五个整形数组分别存放五位选手的十个成绩,随后依次求出五个数组的最大值和最小值并删除;创建一个双精度数组,然后分别求出五个数组数据的平均分,存放在双精度数组里,最后对双精度数组的数据进行冒泡排序(降序),输出。

问题:不能正确降序输出双精度数组里的五个数据。

原因:冒泡排序方法出现逻辑错误,没有正确的将最小值逐步沉淀到数组的最后。

解决方案:重新梳理了一下冒泡排序方法的逻辑,改进了二重循环的逻辑。

2.Email验证:

程序设计思路:使用Scanner类实现对字符的输入,然后构造方法根据条件判断是否符合规则,符合返回1,否则返回0,最后根据返回值输出Email地址是否正确。

问题:判断方法无论Email地址正确与否都返回0.

原因:判断方法出现逻辑错误,没有处理好四个条件之间的关系。

解决方案:将判断方法里面的else if统统换成if,并把最后的if从否定判断变为肯定变换。

3.统计文件:

程序设计思路:先按照要求输入字符串,然后创建一个字符数组,用split方法截取字符串,存放在先前创建的字符数组中,最后逐个将字符数组的元素首字母大写输出,并统计个数。

问题:文件名首字母没有大写输出。

原因:toUpperCase()方法没有调用正确。

解决方案:重新正确调用了toUpperCase()方法。

(三)代码托管

码云commit历史截图

链接:

http://git.oschina.net/hebau_cs15/hebau-cs02cmy/tree/master

posted on 2017-03-27 22:55  绝版de初夏  阅读(183)  评论(1编辑  收藏  举报