Java第二次作业

Java第二次作业

(一)学习总结

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

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定义为有参的构造函数

public class Test {
    public static void main(String args[]) { 
       Foo obj = new Foo(100);       
    }     
}
class Foo{
    int value;
    public Foo(int intValue){
        value = intValue;
    }
}

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

精度丢失
0.1 无法准确地表示为float与double类型

import java.math.BigDecimal;
public class test {
public static void main(String args[]) { 
              BigDecimal a = new BigDecimal ("0.1");
              BigDecimal b = new BigDecimal ("0.1");
              BigDecimal c = new BigDecimal ("0.1");     
              if(a.add(b).add(c).doubleValue()==0.3){
                 System.out.println("等于0.3");
              }else {
                  System.out.println("不等于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();
    arr.value=100;
}
}
class MyClass{
    public int value=1;
}

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

应使用StringBuffer类。

String类表示的字符串是常量,创建后内容和长度不能改变;StringBuffer类表示字符容器,内容和长度可以随时修改,应使用StringBuilder方法。

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

(二)实验总结

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

程序设计思路

声明一个整形数组用来存放评委的十个评分,声明一个双精度型的数组存放5个选手的最终得分,计算每个选手的得分存入double数组中,将double数组中的值进行排序,再逆序输出。

问题1.如何存放5位选手的得分

双重循环输入得分,每输入10个数(1位选手得分)进行一次计算,求得最终得分,存入double数组中。

......
for(int i=0;i<5;i++){
			int score[]=new int[10];
			System.out.printf("请10位评委输入%d号选手得分
",i+1);
			for(int j=0;j<10;j++){
			score[j]=in.nextInt();
			}
			marks[i]=(double)(sumScore(score)-minScore(score)-maxScore(score))/8.0;
		}
......

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

程序设计思路

利用indexOf,判断‘@’‘.’的位置,如果indexOf("@")返回-1,代表不存在@,如果indexOf("@")返回1,代表@位置在首,,如果indexOf("@")值小于indexOf(",")的值,代表‘@’在‘,’的后面;利用email.endsWith()判断字符串结尾是否为com|cn|net|gov|edu|org

问题1. 如何判断

利用indexOf(),email.endsWith()方法,进行判断(判断条件:A:@和.同时存在 B: @在.之前 C: 不能@开头 D: 以com|cn|net|gov|edu|org结尾 )

......

if(email.indexOf("@")!=-1&&email.indexOf(".")!=-1&&email.indexOf("@")<email.indexOf(".")&&email.indexOf("@")!=0){
			if (email.endsWith("com")||email.endsWith("cn")||email.endsWith("net")||email.endsWith("gov")||
			email.endsWith("edu")||email.endsWith("org")){
				return true;
			}	
			else {
				return false;
			}
......

(三)代码托管

http://git.oschina.net/hebau_cs15/Java-CS02hc

原文地址:https://www.cnblogs.com/hc395071675/p/6616544.html