zoukankan      html  css  js  c++  java
  • 《Java技术》第二次作业

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

    2.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?

    public class Test {
    	 public static void main(String args[]) { 
     	  Foo obj = new Foo();       
    	}     
    }
    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,输出的(a+b+c)为0.30000000000000004

    为了处理精度损失的问题,可以使用java.math.BigDecimal类,查阅JDK帮助文档或教材p378,对上述程序进行修改。

    程序修改为:

    import java.math.BigDecimal;
    
    public class Math {
    public static void main(String args[]) {
    	
    	BigDecimal b1=new BigDecimal("0.1");
    	BigDecimal b2=new BigDecimal("0.1");
    	BigDecimal b3=new BigDecimal("0.1");
    	
        double s;
        s=b1.add(b2).add(b3).doubleValue();
        System.out.println(s);
        if(s == 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;
    }
    

    结果为:

    Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:6)
    

    这个调用使用类名称进行调用,则必须将其声明为static属性。

    应为:

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

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

    应该使用StringBuffer类。
    

    String类表示的字符串是常量,一旦创建则内容和长度都不能改变。StringBuffer类表示字符容器,内容和长度可以随时修改。在操作字符串时,如果该字符串仅用于表示数据类型,则使用String类型,如果需要对字符串进行增删操作,则使用StringBuffer类。

    String类:

    public class Test {
     public static void main(String[] args){
        String str = "Hello";
        for (int i=0; i<10000;i++) {
            str =  str+"World";
        }
         System.out.println(str);
                
     }
     } 
    

    StringBuffer类:

    public class Test {
     public static void main(String[] args){
        StringBuffer str = new StringBuffer("Hello");
        for(int i=0;i<10000;i++){
            str.append("world");
        }
        System.out.println(str);         
    }
    }
    

    (二)实验总结

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

    程序设计思路:先用双重循环,在内循环依次输入每个选手10个评委所得的分,赋值给数组,然后在对每个选手的分求总和,然后去掉最大值和最小值,再求平均值,然后将平均值赋给另一个数组,五个选手重复上述数输入,最后再将平均值的数组从高到低排序,最后循环输出数组。

    问题1:

    int y=in.nextInt();
    a[j]=y;
    

    原因:不知道应该怎么输入数组

    解决方案:后来问了同学知道输入变量然后将变量赋值给数组

    问题2:

    d=max(a,d);
    x=min(a,x);
    System.out.println(sum);
    System.out.println("第"+(i+1)+"个人的最高分"+d);
    System.out.println("第"+(i+1)+"个人的最低分"+x);
    sum=sum-d-x;
    

    原因:不会调用函数

    解决方案:通过看书,从网上查资料及同学帮助修改,实参与形参个数相等 ,类型一致。使

    d=max(a,d);
    x=min(a,x);
    

    完成了函数调用。

    2.Email验证:在各种应用中,需要对用户输入的email地址进行验证,编写一个方法,判断一个email地址是否有效。(判断条件:A:@和.同时存在 B: @在.之前 C: 不能@开头 D: 以com|cn|net|gov|edu|org结尾 )

    程序设计思路:让用户输入一个邮箱,然后对用户输入的邮箱进行判断,'@'和'.'同时存在为下标都不为'-1','@'在'.'之前为'@'的下标小于'.'的下标, 不能'@'开头,用以。。开头字符来写, 以'com|cn|net|gov|edu|org'结尾用以。。字符结尾来写,这些条件都满足邮箱有效,否则无效。
    问题1:

    if((str.indexOf("@"))<(str.indexOf("."))) &&(str.indexOf("@")!=-1)&&(str.indexOf(".")!=-1))
    原因:不知道同时存在和。。在。。之前应该怎么写

    解决方案:通过问同学,知道可以用下标大小来表示在。。之前,下标不为'-1'来表示存在。

    问题2:

    if((!str.startsWith("@"))&&((str.endsWith("com"))||(str.endsWith("cn"))||(str.endsWith("net"))
    			||(str.endsWith("gov"))||(str.endsWith("edu"))||(str.endsWith("org")))
    

    原因:输出总是有错

    解决方案:原来是这样写的

    (str.endsWith("com||cn||net||gov||edu||org")
    

    后来总是错误就把这些分开写,为

    ((str.endsWith("com"))||(str.endsWith("cn"))||(str.endsWith("net"))||(str.endsWith("gov"))||(str.endsWith("edu"))||(str.endsWith("org")))
    

    然后就正确了。

    (三)代码托管

    码云commit历史截图

  • 相关阅读:
    【洛谷P4838】P哥破解密码【矩阵乘法】
    2019年暑假 纪中培训总结
    Objective-C中的@property和@synthesize用法
    转载: C#: Left outer joins with LINQ
    SQL 查询某字段id为空(不为空)
    聚类算法(五)--层次聚类(系统聚类)及超易懂实例分析
    聚类算法(四)--DBSCAN
    聚类算法(二)--BIRCH
    聚类算法(二)--Kmeans++、elkan K-Means、Mini Batch K-Means、Sequential Leader Clustering
    聚类算法(一)--Kmeans
  • 原文地址:https://www.cnblogs.com/yang-yonghui/p/6625582.html
Copyright © 2011-2022 走看看