曾梦垚

导航

二、编程题

1 将数组 int [] a={9,5,7,8,1,3,2,6,4} 进行冒泡排序

Public class test {

Public static void main(String[] args){

      int [] a={9,5,7,8,1,3,2,6,4};

      for(int i=1;i<a.length;i++){

          for(int j=0;j<a.length-i;j++){

             if(a[j]<a[j+1]){

               int temp=a[j];

               a[j]=a[j+1];

               a[j+1]=temp;

}

}

 

2 写出单例设计模式

Public class Singleton{

   Private Singleton(){}

   Private static Singleton instance=null;

   Public static Singleton getInstance(){

      If(instance==null){

        Instance=new Singleton();

}

      return instance;

}

}

3 键盘输入十行数据,直到输入quit就退出程序,将输入的数据保存在D:\temp\info.txt中,该文件不存在就创建该文件。

Public class Test{

    static String path=” D:\\temp\\info.txt”;

Public static void main(String[] args){

   File f=new File(path);

   If( ! f.exists()  ){

       f.createNewFile();

}

   BufferedReader br=new BufferedReader(New InputStreamReader(System.in)); 

  BufferedWriter bw=new BufferedWriter(new FileWriter(f));

   String line=null;

   While((line=br.readLine())!=null &&!(line.equals(“quit”))){

       bw.write(line);

       bw.newLine();

       bw.flush();

}

   br.close();

   bw.close();

 

 

}

 

}

4 模拟生成1个班级,该班级有9个学生对象,用集合模拟下列操作。

  A.班级再增加一位学生

  B.将学号是S001的姓名修改为”张恒”

  C.统计所有学生的平均分

  D.将学生的所有信息保存到文件student.txt中

}

for(int k:a){

  system.out.print(k+”   ”);

}

 

Public class Student{

   Private String sno;

   Private String name;

   Private int score;

   Public String toSTring(){

  Return “Student [sno=”+sno+”, name=”+name+”, score=”+score+”]”;

}

   Public Student(String sno,String name,int score){

       This.sno=sno;

       This.name=name;

       This.score=score;

}

Public void setScore(int score){

    This.score=score;

}

Public int getScore(){

Return score;

}

Public void setSno(String sno){

    This.sno=sno;

}

Public String getSno(){

    Return sno;

}

Public void setName(String name){

   This.name=name;

}

   Public String getName(){

       Return name;

}

 

   Public static void main(String[] args){

      List<Student> class="new" ArrayList<Student>();

      Student students={

New Student(“s001”,”张1”,65);

New Student(“s001”,”张2”,66);

New Student(“s001”,”张3”,67);

New Student(“s001”,”张4”,68);

New Student(“s001”,”张5”,44);

New Student(“s001”,”张6”,55);

New Student(“s001”,”张7”,66);

};

      For(Student student :students){

 

            Class.add(student);

}

       Class.add(new Student(“s008”,”张8”,88));

 

 

       For(Student student :students){

 

            If(student.sno.equals(“s001”)){

                 Student.setName(“张恒”);

                 Break;

}

}

       Int total=0,i=0;

       For(Student student :students){

          Total+=student.getsScore();

i++;           

}

       System.out.print(“平均分”+total/i);

 

File f=new File(“f: \\student.txt”);

File f=new File(path);

   If( ! f.exists()  ){

       f.createNewFile();

}

 

 

BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));

 

       For(Student student :students){

      

           Bw.write(student.toString());

            

}

     Bw.close();

 

}

 

}

5 模拟四个线程对同一个数据I进行操作

    1号和3号线程做加一

    2号和4号线程做减一

}

 

Public class Date{

   Static int index=0;

Public synchronized void  add(){

    Index++;

}

Public synchronized void sub(){

    Index--;

}

}

Public class A  implements Runnable{

    Date date;

Public A(Date date){

   This.date=date;

}

Public void run(){

   For(int  i=0;i<20;i++){

         date.add();

        Thread.sleep(20);

}

}

 

}

Public class B  implements Runnable{

    Date date;

 Public B(Date date){

   This.date=date;

}

 

    

Public void run(){

   For(int  i=0;i<20;i++){

         date.sub();

        Thread.sleep(30);

}

}

 

}

Public class Test{

     Public static void main(String[] args){

       Date date =new Date();

        A a=new A(date);

        B b=new B(date);

 

        Thread t1=new Thread(a,”1号”);

        Thread t3=new Thread(a,”3号”);

 

        Thread t1=new Thread(b,”2号”);

        Thread t1=new Thread(b,”4号”);

 

 

        t1.start();

        t2.start();

        t3.start();

        t4.start();

 

        

}

 

}

 

posted on 2017-02-16 19:46  曾梦垚  阅读(381)  评论(0编辑  收藏  举报