Java homework6

来源:互联网 发布:淘宝图片锁有用么 编辑:程序博客网 时间:2024/06/03 14:34
1:需求:请设计一个方法,可以实现获取任意范围内的随机数。
import java.util.Scanner;
public class Test {
  public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入随机数的最小值:");
int start=sc.nextInt();
System.out.println("请输入随机数的最大值:");
int end=sc.nextInt();
int num=getNum(start,end);
System.out.println("输出的随机数为:"+num);
}
  public static int getNum(int start,int end) {
 int number=(int)(Math.random()*(end-start+1)+start);
 return number;
  }
}

2:下面代码执行的结果是:
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.print(s1 == s2);
System.out.print(",");
System.out.println(s1.equals(s2));
}
}
结果:false,true
3:下面代码执行的结果是:
public static void main(String arg[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a + "," + b);
}
static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}
结果:AB,B
4、下列代码的执行结果是:
String str1 = "This is a test!";
StringBuffer str2 =new StringBuffer( "This is a test!");
str1 = str1+"Hi";
str2.append("Hi");
System.out.println("str1 == " + str1);
System.out.println("str2 == " + str2);
结果:str1 == This is a test!Hi
      str2 == This is a test!Hi
5、下面代码能最后打印的值是?(C)
    public class TestValue {
private static int a;
public static void main(String[] args) {
modify(a);
System.out.println(a);
}
public static void modify(int a) {
a++;
}
}
A)编译错误 B)null C)0         D)1
6、编程题
1.集合的嵌套遍历
  需求:
  我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>
  但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。
  而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?
import java.util.ArrayList;
import java.util.Collection;
public class CollectionTest {
public static void main(String[] args) {
//创建2个集合对象
Collection c=new ArrayList();
Collection c1=new ArrayList();
//创建6个学生对象
Student s1 = new Student("张三",25);
Student s2 = new Student("李四",22);
Student s3 = new Student("刘一博",22);
Student s4 = new Student("李丹",23);
Student s5 = new Student("夏天",22);
Student s6 = new Student("张凯",22);
//给集合中添加元素
c.add(s1) ;
c.add(s2) ;
c.add(s3) ;
c1.add(s4) ;
c1.add(s5) ;
c1.add(s6) ;
//将集合转换成数组
Object[] objs = c.toArray();
Object[] objs1 = c1.toArray();
for(int x = 0 ; x < objs.length ; x ++){
Student s = (Student)objs[x];
objs[x]= (Student) objs1[x];
System.out.println(s.getName()+"---"+s.getAge());
System.out.println(s1.getName()+"---"+s1.getAge());
}
}
}
 class Student{
private String name ;
private int age ;

public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2.获取10个1-20之间的随机数,要求不能重复
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Number {
       public static void main(String[] args)
       {
               int arr[] = new int[10];
               Random rand=new Random();
               HashSet set=new HashSet();               
               while(set.size()<=10)
               {                 
                   set.add((int)(Math.random()*21));
               }
               System.out.println(set);
}
}

3.使用ArrayList集合存储自定义对象并遍历(三种方式去实现)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test{
    public static void main(String[] args) {
        //创建一个学生类
        //创建集合对象
        Collection c=new ArrayList();
        //创建3个学生对象
        Student s1 = new Student("张三",22);
        Student s2 = new Student("李四",22);
        Student s3 = new Student("张丽",23);
        //给集合中添加元素
        c.add(s1) ;
        c.add(s2) ;
        c.add(s3) ;
        //获取迭代器对象
        Iterator it = c.iterator() ;
        //遍历
        while(it.hasNext()){
            Student s = (Student)it.next() ;
            System.out.println(s.getName()+"---"+s.getAge());         
        }
        System.out.println("-----------------");
        for(Iterator it2 =c.iterator();it2.hasNext();){
            Student s = (Student) it2.next() ;
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("-----------------");
        Object[] objs = c.toArray();
for(int x = 0 ; x < objs.length ; x ++){
   System.out.println(objs[x]);
    }
}
}
class Student {
    private String name ;
    private int age ;
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name+ ", age=" + age + "]";
    }

}