《Java技术》 第二次作业

java第二次作业

(一)学习总结

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

在Eclipse上的菜单栏选择项目-----属性-----左侧菜单栏Java构建路径-----库-----JRE系统库-----rt.jar-----源代码连接-----编辑-----外部文件-----设置外部文件的路径为JDK安装路径下的src.zip。

在程序的equals处按住ctrl单击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;
}
}

不可以通过编译,在主函数,构造的方法Foo()中,这是一个有参的构造函数,但是并没有int型参数。

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.math.BigDecimal类,查阅JDK帮助文档或教材p378,对上述程序进行修改。

程序修改:

import java.math.BigDecimal;
public class Panba {
public static void main(String args[]) { 
    double a = 0.1;
    double b = 0.1;
    double c = 0.1;
    double sum=0;
    sum=Mymath.round(Mymath.add(a,b,c),1);
    System.out.println("a+b+c="+sum);
    }
}
class Mymath{
public static double round(double a,int b){
  BigDecimal b1=new BigDecimal(a);
  BigDecimal b2=new BigDecimal(1);
  return b1.divide(b2,b,BigDecimal.ROUND_HALF_UP).doubleValue();
         }
	public static double add(double p,double l,double m){
  BigDecimal a=new BigDecimal(p);
  BigDecimal b=new BigDecimal(l);
  BigDecimal c=new BigDecimal(m);
  return a.add(b).add(c).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 Panba {

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


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

String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象,每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象。 而如果是使用 StringBuffer 类则结果就不一样了,每次结果都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。

代码:

	public class Panba {
    public static void main(String[] args) {
    	String S1 = "abc"; 
    	
    	for(int i = 0 ; i < 10000 ; i ++) { 
    	S1 += "def"; 
    	S1 = "abc"; 
    	}
	}
   }

6.其他需要总结的内容。

(二)实验总结

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

代码

package pingfen;

import java.util.Scanner;

public class Ping {
	public static void main(String[] args){
		
		Scanner input = new Scanner(System.in);
		double p=0;
		double o[]=new double[6];
		
		
		for(int x=1;x<6;x++){
		double sum=0;
		System.out.println("请为第"+x+"位选手打分");
		
		
		double a[]=new double[10];
		for(int i=0;i<10;i++){
			int num=input.nextInt();
			a[i]=num;
			
		}
		
		for(int i=0;i<10;i++){
		    sum=sum+(double)a[i];
		    }
		System.out.println("选手的总分是:"+sum);
		
		
		for(int b=0;b<a.length;b++){
			for(int c=0;c<a.length;c++){
				if(a[b]<a[c]){
				   double temp=(double)a[b];
					a[b]=a[c];
					a[c]=temp;
				}
			}
	}
		
		System.out.println("评委给的最高分是:"+a[9]);
		sum=sum-(double)a[9];
		System.out.println("去掉一个最高分得分:"+sum);
		System.out.println("评委给的最低分是:"+a[0]);
		sum=sum-(double)a[0];	
		System.out.println("去掉一个最低分得分:"+sum);
		p=sum/8;
		System.out.println("选手最终得分是"+p);
		  o[x]=p;
	}
		
		for(int p1=0;p1<o.length;p1++){
			for(int p2=0;p2<o.length;p2++){
				if(o[p1]<o[p2]){
					 double temp=(double) o[p1];
						o[p1]=o[p2];
					    o[p2]=temp;
				}
			}
		}
		
		for(int x=1;x<6;x++){
			System.out.println(o[x]);
		}	
}
}

程序设计思路:定义一个空间为10的数组,存放10个评委给出的得分;再定义一个空间为5的数组,存放5名选手的最终得分。其中利用冒泡排序对评委的评分和选手的成绩进行排序。

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

代码

import java.util.Scanner;
public class Pan {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.println("请输入一串字符串:");
	String str=input.next();
	System.out.println("邮箱:"+str);
	int a = str.indexOf("@");
	int b = str.indexOf(".");
	
	if(a>0&&b>0){
	    if(a<b){
		    if(str.endsWith("com")||str.endsWith("cn")||str.endsWith("net")||str.endsWith("gov")||str.endsWith("edu")||str.endsWith("org")){
				System.out.println("输入正确!");
		        }else{
			        System.out.println("输入错误!");
			     }
	        }else{
		        System.out.println("输入错误!");
		     }
	     }else{
	        System.out.println("输入错误!");
	     }
	  }
	}

程序设计思路:输入一串字符,利用str.indexOf语句查找到"@"与".";并将它们所在位置的下标赋给int型的a,b;用来比较"@"与"."的先后,利用str.endsWith语句判断字符串的结尾。

问题1:

原因:str.indexOf没明白该语句的返回值就是下标

解决方案:认真查看课本分析

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

代码

import java.util.Scanner;


public class Wen {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	
	String str2="java";
	String str3="c";
	String str4="exe";
	
	System.out.println("请输入一串字符串:");
	String str=input.next();
	System.out.println("字符串:
"+str);
	System.out.println();
	String a[]=str.split(",");
	
	int o=0,w=0,e=0;
	for(int i=0;i<a.length;i++){
	int q=a[i].indexOf(".");
		String str1=a[i].substring(q+1);
	    
		System.out.println();
	
		   if(str2.equals(str1)){
			  o++;
		  }
		   if(str3.equals(str1)){
			  w++;
		   }
		   if(str4.equals(str1)){
				  e++;
			  }
	}
	 System.out.println("java文件有"+o+"个");
	 System.out.println("c文件有"+w+"个");
	 System.out.println("exe文件有"+e+"个");
	
	System.out.println("文件个数是:
"+a.length);
	
	System.out.println("文件首字母大写:");
	for(int i=0;i<a.length;i++){
	String str1=a[i].substring(0,1);
	System.out.println(str1.toUpperCase());

    }
}
}

程序设计思路:利用str.split语句将字符串分隔开;利用str.substring语句截取字符串首字母;利用str.toUpperCase语句转换大小写。

4.身份证识别:公民身份证号码由十八位数字组成。从左至右依次为:六位地址码,八位出生日期码,三位顺序码和一位校验码。顺序码的奇数分配给男性,偶数分配给女性。编写一个识别身份证基本信息的小工具,输入一个居民身份证号,则输出居民所属省份(直辖市、自治区),生日(格式:xxxx-xx-xx)和性别信息。

代码

import java.util.Scanner;


public class Yan {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.println("请输入身份证号:");
	String str=input.next();
	System.out.println("身份证号:"+str);
	
	
	
	String str1=str.substring(0,2);
	String str2=str.substring(6,14);
	String str6=str.substring(14,17);
	
	
	String str3=str2.substring(0,4);
	String str4=str2.substring(4,6);
	String str5=str2.substring(6,8);
	
	System.out.println();
	
	
	System.out.println("省份:");
	if(str.startsWith("11")){
		System.out.println("北京市");
	}
	if(str.startsWith("12")){
		System.out.println("天津市");
	}
	if(str.startsWith("13")){
		System.out.println("	"+"河北省");
	}
	if(str.startsWith("14")){
		System.out.println("山西省");
	}
	if(str.startsWith("15")){
		System.out.println("内蒙古自治区");
	}
	
	
	if(str.startsWith("21")){
		System.out.println("辽宁省");
	}
	if(str.startsWith("22")){
		System.out.println("吉林省");
	}
	if(str.startsWith("23")){
		System.out.println("黑龙江省");
	}
	
	
	if(str.startsWith("31")){
		System.out.println("上海市");
	}
	if(str.startsWith("32")){
		System.out.println("江苏省");
	}
	if(str.startsWith("33")){
		System.out.println("浙江省");
	}
	if(str.startsWith("34")){
		System.out.println("安徽省");
	}
	if(str.startsWith("35")){
		System.out.println("福建省");
	}
	if(str.startsWith("36")){
		System.out.println("江西省");
	}
	if(str.startsWith("37")){
		System.out.println("山东省");
	}
	
	
	if(str.startsWith("41")){
		System.out.println("河南省");
	}
	if(str.startsWith("42")){
		System.out.println("湖北省");
	}
	if(str.startsWith("43")){
		System.out.println("湖南省");
	}
	if(str.startsWith("44")){
		System.out.println("广东省");
	}
	if(str.startsWith("45")){
		System.out.println("广西壮族自治区省");
	}
	if(str.startsWith("46")){
		System.out.println("海南省");
	}
	
	
	if(str.startsWith("50")){
		System.out.println("重庆市");
	}
	if(str.startsWith("51")){
		System.out.println("四川省");
	}
	if(str.startsWith("52")){
		System.out.println("贵州省");
	}
	if(str.startsWith("53")){
		System.out.println("云南省");
	}
	if(str.startsWith("54")){
		System.out.println("西藏自治区");
	}
	
	
	if(str.startsWith("61")){
		System.out.println("陕西省");
	}
	if(str.startsWith("62")){
		System.out.println("甘肃省");
	}
	if(str.startsWith("63")){
		System.out.println("青海省");
	}
	if(str.startsWith("64")){
		System.out.println("宁夏回族自治区");
	}
	if(str.startsWith("65")){
		System.out.println("新疆维吾尔自治区");
	}
	System.out.println("生日:");
	System.out.println("	"+str3+"-"+str4+"-"+str5);
	System.out.println("性别:");
	if(str6.endsWith("1")||str6.endsWith("3")||str6.endsWith("5")||str6.endsWith("7")||str6.endsWith("9")){
		System.out.println("	"+"男");
	}else{
		System.out.println("	"+"女");
		}
	}
}

程序设计思路:利用str.substring语句进行字符串的截取,利用str.startsWith,str.endsWith语句判断。

作业链接

(三)代码托管

原文地址:https://www.cnblogs.com/renxiuxing/p/6633305.html