java lesson15Homework

package lesson15;

/**

 * 1. 使用ArrayList存储整型元素,并对元素进行升序输出

 */

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

 

public class Homework01 {

       public void listSort(){

              //泛型为整形

              List<Integer> list =new ArrayList<Integer>();

             

              //对其中的值随机赋值

              Random rd=new Random();

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

              list.add(rd.nextInt(20));

              }

              //遍历一次输出值

              Iterator iterator=list.iterator();

              System.out.println("未排序前的数字,第一种方式输出:");

              while(iterator.hasNext()){

                     System.out.print(iterator.next()+" ");

              }

              System.out.println();

              System.out.println("未排序前的数字,第二种方式输出:");

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

                     System.out.print(list.get(i)+" ");

              }

              //第一种方式将List接口转化为数组

              Integer arr[]=list.toArray(new Integer[list.size()]);

              //利用冒泡排序法进行排序

              for(int i=list.size();i>0;i--){

                     for(int j=0;j<i-1;j++){

                            if(arr[j]>arr[j+1]){

                                   Integer temp=arr[j];

                                   arr[j]=arr[j+1];

                                   arr[j+1]=temp;

                            }

                           

                     }

             

              }

             

              System.out.println();

              System.out.println("输出排序后的数字:");

              //加强for循输出。。

              for (Integer intg:arr ){

                     System.out.print(intg+" ");

              }

       }            

              public static void main(String[]args){

                     Homework01 hw=new Homework01();

                     hw.listSort();

              }

             

 

}

package lesson15;

 

import java.util.ArrayList;

import java.util.Iterator;

 

/**

 * 2. 创建一个类,用于向类中的ArrayList添加元素的同时进行降序排列

class T {

   ArrayList alist = new ArrayList();

 

   public void addAndSort(int element) {

      //1. 添加元素到alist

      //2. 将alist中的元素降序排列

}

}

 

 * @author LENOVO

 *

 */

 

public class T {

   //alist中只能放整形类

   ArrayList<Integer> alist=new ArrayList<Integer>();

   public void addAndSort(int element){

      //向对象alist中添加元素

      System.out.println("未排序前输出数字:");

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

         alist.add(i);

      }

      Iterator iterator=alist.iterator();

      while(iterator.hasNext()){

         System.out.print(iterator.next()+" ");

      }

     

      Integer arr[]=alist.toArray(new Integer[element]);

      //冒泡排序法降序输出

      for (int i=element;i>0;i--){

         for(int j=0;j<i-1;j++){

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

                Integer temp=arr[j];

                arr[j]=arr[j+1];

                arr[j+1]=temp;

            }

         }

      }

      System.out.println();

      System.out.println("排序后输出数字:");

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

         System.out.print(arr[i]+" ");

      }

     

   }

   public static void main(String[]args){

      T t=new T();

      t.addAndSort(10);

   }

 

}

package lesson15;

 

import java.util.ArrayList;

 

/**

 * 3. 创建平面坐标类(Point,设置两个成员变量x和y,分别表示该点的两个坐标)

   创建ArrayList用于保存Point对象,并完成如下功能

  (1)将ArrayList中的Point按照x坐标升序输出

  (2)将ArrayList中的Point按照y坐标降序输出

  (3)将ArrayList中的Point按照与原点的距离大小,升序输出。

 

 * @author LENOVO

 *

 */

 

public class Point {

   double  x;

   double y;

   //构造方法

   public Point(double x,double y){

      this.x=x;

      this.y=y;

     

   }

   public double getX() {

      return x;

   }

   public void setX(double x) {

      this.x = x;

   }

   public double getY() {

      return y;

   }

   public void setY(double y) {

      this.y = y;

   }

   public double getDistance(double x,double y){

      return Math.sqrt(x*x+y*y);

   }

  

}

   class Test{

      public static void main(String[]args){

         ArrayList<Point> alist=new ArrayList<Point>();

         alist.add(new Point(8,3));

         alist.add(new Point(4,5));

         alist.add(new Point(10,6));

         alist.add(new Point(7,8));

         //按照x坐标升序排序

         for (int i=alist.size();i>0;i--){

            for(int j=0;j<i-1;j++){

                if(alist.get(j).getX()>alist.get(j+1).getX()){

                   Point P=alist.get(j);

                   alist.set(j, alist.get(j+1));

                   alist.set(j+1, P);

                }

            }

         }

         System.out.println("按X升序排序结果:");

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

         System.out.print("x="+alist.get(i).getX()+"y="+alist.get(i).getY());

         }

         //按照y坐标降序排序

         for (int i=alist.size();i>0;i--){

            for(int j=0;j<i-1;j++){

                if(alist.get(j).getY()<alist.get(j+1).getY()){

                   Point P=alist.get(j);

                   alist.set(j, alist.get(j+1));

                   alist.set(j+1, P);

                }

            }

         }

         System.out.println("按y降序排序结果:");

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

            System.out.print("x="+alist.get(i).getX()+"y="+alist.get(i).getY());

         }

         //按照与原点的距离大小,升续

         for(int i=alist.size();i>0;i--){

            for(int j=0;j<i-1;j++){

               if(alist.get(j).getDistance(alist.get(j).getX(),alist.get(j).getY())>

                alist.get(j+1).getDistance(alist.get(j+1).getX(), alist.get(j+1).getY())){

                   Point P=alist.get(j);

                   alist.set(j, alist.get(j+1));

                   alist.set(j+1, P);

                }

            }

         }

         System.out.println("按照与原点的距离大小,升续:");

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

            System.out.print("x="+alist.get(i).getX()+"y="+alist.get(i).getY());

      }

     

   }

}

package lesson15;

 

import java.util.ArrayList;

import java.util.Scanner;

 

/**

 * 准备工作:创建一个Computer类

包含成员变量如下:

(1)编号(即计算机的唯一标识)

(2)CPU型号

(3)主板型号

(4)硬盘大小

(5)内存大小

(6)显卡型号

(7)价格

包含方法如下:

(1)输出当前计算机的所有属性

(2)设置计算机CPU型号

(3)设置计算机主板型号

(4)设置计算机硬盘大小

(5)设置计算机内存大小

(6)设置计算机显卡型号

(7)设置计算机价格

(8)获取计算机价格

 

题目:

1)要求使用ArrayList存储计算机对象,

形如:ArrayList<Computer> cList = new ArrayList<Computer> ();

 

2)为ArrayList对象(例如:上面的cList)添加3个元素。

 

3)模拟用户输入(Scanner对象)获取对应的Computer信息,并且输出具体的参数。

 

4)遍历输出ArrayList中的Computer,同时输出Computer的详细信息。

 

 * @author LENOVO

 *

 */

 

public class Computer {

   int count;

   int CPUSize;

   int mainboardSize;

   double hardboardSize;

   double rom;

   String graphicsCard;

   double prize;

   public Computer(int count,int CPUSize,int mainboardSize,double hardboardSize,double rom

         ,  String graphicsCard,double prize){

      this.count=count;

      this.CPUSize=CPUSize;

      this.mainboardSize= mainboardSize;

      this.hardboardSize=hardboardSize;

      this.rom=rom;

      this.graphicsCard=graphicsCard;

      this.prize=prize;

           

     

   }

   public int getCount() {

      return count;

   }

   public void setCount(int count) {

      this.count = count;

   }

   public int getCPUSize() {

      return CPUSize;

   }

   public void setCPUSize(int cPUSize) {

      CPUSize = cPUSize;

   }

   public int getMainboardSize() {

      return mainboardSize;

   }

   public void setMainboardSize(int mainboardSize) {

      this.mainboardSize = mainboardSize;

   }

   public double getHardboardSize() {

      return hardboardSize;

   }

   public void setHardboardSize(double hardboardSize) {

      this.hardboardSize = hardboardSize;

   }

   public double getRom() {

      return rom;

   }

   public void setRom(double rom) {

      this.rom = rom;

   }

   public String getGraphicsCard() {

      return graphicsCard;

   }

   public void setGraphicsCard(String graphicsCard) {

      this.graphicsCard = graphicsCard;

   }

   public double getPrize() {

      return prize;

   }

   public void setPrize(double prize) {

      this.prize = prize;

   }

   public static void main(String[]args){

      ArrayList<Computer> alist=new ArrayList<Computer>();

      alist.add(new Computer(1,3,5,3.2,99.6,"ab",20.9));

      alist.add(new Computer(2,3,5,3.2,99.6,"abc",20.9));

      alist.add(new Computer(3,3,5,3.2,99.6,"abcd",20.9));

      System.out.println("请输入您想了解的编号:");

      Scanner scanner=new Scanner(System.in);

      int a=scanner.nextInt();

      System.out.println("输出第"+a+"个电脑的信息!!");

      System.out.println("编号是:"+alist.get(a).getCount());

      System.out.println("CPU型号是:"+alist.get(a).getCPUSize());

      System.out.println("主板型号是:"+alist.get(a).getMainboardSize());

      System.out.println("硬盘大小是:"+alist.get(a).getHardboardSize());

      System.out.println("内存大小是:"+alist.get(a).getRom());

      System.out.println("显卡型号是:"+alist.get(a).getGraphicsCard());

      System.out.println("价格是:"+alist.get(a).getPrize());

      System.out.println("\n\n遍历输出ArrayList中的Computer,同时输出Computer的详细信息");

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

         System.out.println("输出第"+(i+1)+"个电脑的信息!!");

         System.out.println("编号是:"+alist.get(a).getCount());

         System.out.println("CPU型号是:"+alist.get(a).getCPUSize());

         System.out.println("主板型号是:"+alist.get(a).getMainboardSize());

         System.out.println("硬盘大小是:"+alist.get(a).getHardboardSize());

         System.out.println("内存大小是:"+alist.get(a).getRom());

         System.out.println("显卡型号是:"+alist.get(a).getGraphicsCard());

         System.out.println("价格是:"+alist.get(a).getPrize());

         System.out.println("编号是:"+alist.get(a).getCount());

         System.out.println("CPU型号是:"+alist.get(a).getCPUSize());

         System.out.println("主板型号是:"+alist.get(a).getMainboardSize());

         System.out.println("硬盘大小是:"+alist.get(a).getHardboardSize());

         System.out.println("内存大小是:"+alist.get(a).getRom());

         System.out.println("显卡型号是:"+alist.get(a).getGraphicsCard());

         System.out.println("价格是:"+alist.get(a).getPrize());

      }

     

   }

}

 

posted @ 2017-08-07 12:37  木&子  阅读(564)  评论(0编辑  收藏  举报