黑马程序员---入学测试题

来源:互联网 发布:淘宝卖家如何设置佣金 编辑:程序博客网 时间:2024/05/21 10:20

-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 

2、 编写一个延迟加载的单例设计模式。

新建类Test2

代码如下:

懒汉式单例模式是延迟加载的单例设计模式

public class Test2{

//静态私有的成员变量
private static test2 instance = null;

// 私有的构造方法
private Test2() {}

// 如果不加synchronized会导致对线程的访问不安全
// 双重锁定检查
public static Test2 getInstance() {
    if (instance == null) {
        synchronized (Test2.class) {
        if (null == instance) {
            instance = new Test2();
        }
    }

}
return instance;

}

 

 

 

 

 

 

 

 

 

 

 

 

新建一个类Test3

3、 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,

例如:键盘输入6,打印6秒;

键盘输入60,打印1分;

键盘输入66,打印16秒;

键盘输入666,打印116秒;

键盘输入3601,打印1小时1

 

 

 

 

 

import java.util.Scanner;

public class Test3 {

public static void main(String[] args) {
int time = getInt();
outTime(time);
}

/**
获取从控制台输入数字
* @param name
* @return
*/
private static int getInt() {
boolean flog = true;
int result = 0;
while (flog){
Scanner sc = new Scanner(System.in);
System.out.println("输入一个大于0的整数:");
try{
result = Integer.parseInt(sc.nextLine());
if (result < 0) {
throw new Exception("输入的值必须大于等于0");
}
flog = false;
} catch(Exception e) {
System.out.println("输入值非法,请输入大于0的整数!");
}
}
return result;
}

private static void outTime(int time){
int s = 0, m = 0, h = 0, d = 0;
if (time < 0) return ;
if (time < 60) {
s = time;
} else if (time >= 60 && time < 60*60 ){
m = time/60;
s = time%60;
} else if (time >= 60*60 && time < 24*60*60){
h = time/(60*60);
m = time%(60*60)/60;
s = time%(60*60)%60;
} else {
d = time/(24*60*60);
h = time%(24*60*60)/(60*60);
m = time%(24*60*60)%(60*60)/60;
s = time%(24*60*60)%(60*60)%60; 
}
out(d, h, m, s);
}

private static void out(int d, int h, int m, int s) {
String str = "";
if (d != 0) {
str += d + "";
}
if (h != 0) {
str += h + "小时";
}
if (m != 0) {
str += m + "分钟";
}
if (s != 0) {
str += s + "";
}
System.out.println(str);
}
}

 

 

 

 

 

新建类Test4

4、 编程计算38等于几,什么方法效率更高?

public class Test4 {

public static void main(String args[]){
  //采用移位运算输出结果
  //System.out.println((2<<3)+(2<<2));本来想把8拆分成2的整数,不过反而麻烦
  System.out.println(3<<3);// 左移相当于乘. 左移一位相当于乘2(2^1);左移两位相当于乘4(2^2);左移三位相当于乘8(2^3)
}

}

5  数组去重复,例如原始数组是{4,2,4,6,1,2,4,7,8},得到结果{4,2,6,1,7,8}

新建 Test5

import java.util.ArrayList;

import java.util.List;

 

public class Test5 {

    public static void main(String[] args) {

        int[] arr = { 4, 2, 4, 6, 1, 2, 4, 7, 8 };

 

        List data = new ArrayList();

 

        for (int num : arr) {

            if (!data.contains(num)) {

                data.add(num);

            }

        }

 

        System.out.println(data);

    }

}

 

 

6、 声明类Test6,包含3个成员变量:nameagescore,要求可以通过 new Test6("张三", 22, 95) 的方式创建对象,并可以通过setget方法访问成员变量 

新建Test6

public class Test6 {
 private String name;
 private Integer age;
private Integer score;
public Test6(String name, Integer age, Integer score) {
   super();

   this.name = name;
    this.age = age;
     this.score = score;

}
public String getName() {
        return name;
}

        public void setName(String name) {
           this.name = name;
  }
public Integer getAge() {
   return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
/**
* @param args
*/
public static void main(String[] args) {
//1、通过“通过 new Test6("张三", 22, 95) 的方式创建对象”
Test6 test6 = new Test6("张三",22,95);
System.out.println(test6.getName());

System.out.println(test6.getAge());
System.out.println(test6.getScore());
System.out.println("=============================");
//2、通过“可以通过set和get方法访问成员变量” 设置变量并且访问变量
test6.setName("李四");
test6.setAge(23);
test6.setScore(96);
//3、通过get访问
System.out.println(test6.getName());
System.out.println(test6.getAge());
System.out.println(test6.getScore());
}
}

 

 

7、 声明类Test7,包含2个成员变量:name、age。定义函数sayHello(),调用时输出:我叫***,今年***岁了。声明类Chinese继承Test7。

新建Test7

public class Test7{
    private String name;
    private String age;
    //getter and setter (eclipse 可以生成)
    public Test7(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String sayHello(){
        System.out.println("我叫"+name+",今年"+age+"岁了。
")
    }
}

public class Chinese extends Test7{
    public static void main(String[] ages){
        Test7 per = new Test7("Jim","12");
        per.sayHello();

}  

8、 分析运行结果,说明原理。(没有分析结果不得分)

 

        class A {

                void fun1() {

                      System.out.println(fun2());

                }

 

                int fun2() {

                        return 123;

                }

        }

 

         public class B extends A {

                int fun2() {

                         return 456;

                 }

 

                public static void main(String args[]) {

                        B b = new B();

                        b.fun1();

                        A a = b;

                        a.fun1();

                }

        }

 

这个你运行分析一下

 

 

9、 定义一个静态方法,该方法可以接收一个List<Integer>,方法内对List进行排序

新建类Test9

import java.util.ArrayList;

import java.util.List;

public class Test9 {

 public static void sort(List<Integer> list) {

 int size = list.size();

 int[] store = new int[size];

                for (int i = 0; i < size; i++) {

                        store[i] = list.get(i);

                }

                for (int i = 0; i < size; i++) {   //对数据进行从小到大排序

                        for (int j = i; j <size; j++) {

                                if (store[i] > store[j]) {

                                        int temp = store[j];

                                       store[j] = store[i];

                                       store[i] = temp;

                                }

                        }

                }

                for(int i  = 0 ; i <size ; i++)

                {

                        list.set(i, store[i]);

                }

        }

 

        public static void main(String[] args) {

 

                List<Integer> list = newArrayList<Integer>();

                for (int i = 9; i > 0; i--) {//产生数据

                        list.add(i);

                }

                System.out.println(list);

                Test9.sort(list);

                System.out.println(list);

 

        }

 

}



-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 

0 0