2017《Java》第二次作业

时间:2022-01-29 16:52:22

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

(一)学习总结

1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法。举例说明equals方法和==的区别。
"=="比较的是地址的数值,例如 String str1="hello",String str2=new String("hello"),str1是直接赋值str2是又新开辟的一个空间,堆内存的两个hello的地址并不相同;equals方法比较的是内容;**

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

}

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

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

2017《Java》第二次作业

Person per =new Person();其中Person();就是构造方法;只要是类就会有构造方法。构造方法构造方法的主要作用是为类中的属性初始化。
方法的重载就是方法名相同但参数的类型不同或参数的个数不同。

  public static int add(int i;intj);
public static float add(float x,float y);
public static int add(int i,int j,int k);


主函数在调用Foo obj = new 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.math.BigDecimal类,查阅JDK帮助文档或教材p378,对上述程序进行修改。

运行结果是不等于0.3。

double 和float 类型适用于不需要任何准确计算精度的数字,0.1无法准确的表示为doule类型虽然表面是0.1但传到构造方法时值不会正好为0.1。 new BigDecimal(0.1) 中的0.1是相对于精度大而且准确的数值。

在改的过程中出现因没有给0.1""而出现结果不为0.3的错误。

import java.math.BigDecimal;
public class tes {
public static void main(String args[]) {
BigDecimal a1=new BigDecimal("0.1");
BigDecimal b1=new BigDecimal("0.1");
BigDecimal c1=new BigDecimal("0.1");
if( a1.add(b1).add(c1).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;
}

2017《Java》第二次作业

该程序是给一个一维数组分配了空间

 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类,为什么?性能有差异吗?能否写出测试代码证明你的结论。(可查阅资料)。
String类字符串是常量,内容和长度都不能改变,只能用+进行连接,是通过不断修改对象的引用来实现的。
StringBuffer类可以对字符串的长度进行修改,引用append()方法,比较方便。
StringBuffer类

public class tes{
public static void main(){
StringBuffer buf=new StringBuffer();
buf.append("hello");
for(int i=0;i<100;i++){
buf.append(buf);
}
}
}

String类

public class tes{
public static void main(){
String str="hello";
for(int i=0;i<100;i++){
str+=1;
}
System.out.println(str);
}
}

(二)实验总结

对完成实验内容过程中遇到的问题、解决方案以及程序的设计思路和思考等进行归纳总结。
第一题:评委打分
程序设计思路:
利用了二维数组i代表行即五个评委,j代表列即两个参赛选手,编写Max();Min();Avg();三个方法对评委打分进行处理,得到选手最后得分,评委们给打分利用了for循环来完成的,排序利用了冒泡排序。
问题1:
因为是二维数组所以在循环时的操作i,j有点混乱。
第二题:判断email地址
程序设计思路:使用字符串然后用if语句判断输入的地址是否符合email地址的要求,利用while循环判断当@在.前边时看看结尾的标志是否正确
问题:for 语句里边一直显示未对参数类型定义运算符&&,赋值语句的左边是变量。
解决:因为public static int indexOf(String str)找不到字符串位置返回-1,所以((email.indexOf("@")!=-1) && (email.indexOf(".")!=-1))即可。在比较@和.时可以用下标位置比较

(三)代码托管http://git.oschina.net/hebau_cs15/Java-cs02QSM/tree/master

码云commit历史截图
上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

2017《Java》第二次作业