Java Day20

例题

多类合作
package com.a.work;
/**
 * 轮胎类
 * 
 * @author homework
 *
 */
public class Wheel {
	private String type;
	private int size;
	
	public Wheel() {}
	
	public Wheel(String type, int size) {
		this.type = type;
		this.size = size;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
}

package com.a.work;
/**
 * 引擎类
 * 
 * @author homework
 *
 */
public class Engine {
	private String name;
	private String type;
	
	public Engine() {}
	
	public Engine(String name, String type) {
		super();
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
package com.a.work;
/**
 * 汽车类
 * 
 * @author homework
 *
 */
public class Car {
	private Engine engine;
	private Wheel wheel;
	
	public Car() {
		super();
	}
	
	public Car(Engine engine, Wheel wheel) {
		super();
		this.engine = engine;
		this.wheel = wheel;
	}

	public Engine getEngine() {
		return engine;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	public Wheel getWheel() {
		return wheel;
	}

	public void setWheel(Wheel wheel) {
		this.wheel = wheel;
	}
	
	public void show( ) {
		System.out.println("引擎名字:" + engine.getName()  + "引擎型号:" + engine.getType());
        System.out.println("轮胎型号" + wheel.getType() + "轮胎尺寸" + wheel.getSize());
	}
}
package com.a.work;
/**
 * 汽车类
 * 
 * @author homework
 *
 */
public class Car {
	private Engine engine;
	private Wheel wheel;
	
	public Car() {
		super();
	}
	
	public Car(Engine engine, Wheel wheel) {
		super();
		this.engine = engine;
		this.wheel = wheel;
	}

	public Engine getEngine() {
		return engine;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	public Wheel getWheel() {
		return wheel;
	}

	public void setWheel(Wheel wheel) {
		this.wheel = wheel;
	}
	
	public void show( ) {
		System.out.println("引擎名字:" + engine.getName()  + "引擎型号:" + engine.getType());
        System.out.println("轮胎型号" + wheel.getType() + "轮胎尺寸" + wheel.getSize());
	}
}
把两个类对象,作为Car类的成员变量。在整个方法中,获取数据都是通过类对象的方式来获取的,全部是按照面向对象思想来完成的 在代码中要注意方法的调用者是谁。
```.
循环

```java
选择排序算法推导
一 找出数组中最大值,和下标为0的元素互换位置;
 int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
                                         
   找出数组中的最大值下标位置                         
                                          
int index = 0;                              
for (int i = 1; i < arr.length; i++) {      
	if (arr[index] < arr[i]) {              
		index = i;                          
	}                                       
}                                           
                                                                                    
if (index != 0) {                           
	int temp = arr[0];                      
	arr[0] = arr[index];                    
	arr[index] = temp;                      
}                       
二 接上一题,  找出数组中剩余数据最大值,和下标为1的元素互换位置;
int index = 1;                                       
                                               
  从下标为1的位置开始找出最大值的下标位置                       
    【数组中0下标元素在题目一中已经是最大值,所以从1开始】                                         
for (int i = 2; i < arr.length; i++) {        
	if (arr[index] < arr[i]) {                   
		index = i;                               
	}                                            
}                                                
                                             
if (index != 1) {                                
	int temp = arr[1];                           
	arr[1] = arr[index];                         
	arr[index] = temp;                           
} 
三 接上一题,  找出数组中最大值,和下标为2的元素互换位置
【直到第九题,找最大值和下标8换位置,(此时数组中只剩下两个数还未排序)此时就完成了将数组中十个数从大到小顺序排列】*/
实现代码
/**
 * 选择排序算法
 * 
 * @author Anonymous
 *
 */
public class XvanZePaiXv {
	public static void main(String[] args) {
		int[] arr = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
		selectSort(arr);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
	/**
	 * 选择排序算法
	 * 
	 * @param arr
	 */
	public static void selectSort(int[] arr) {
		// 外层循环,控制需要进行核心操作的次数,次数是数据量 - 1
		for (int i = 0; i < arr.length - 1; i++) {
			
			int index = i;        /*i = 0;从数组第一位查询; 
			                        i = 1;从第二位开始查询比较
			                       (因为第一位已经查询出来是最大值了)
				         可以看出规律 int index = i*/
			
			for (int j = i + 1; j < arr.length; j++) {	//用for比较找出最大值
				//找出最大值下标赋给index
				// if 之后是选择排序排序算法的核心
				if (arr[index] < arr[j]) {
					index = j;
					
				}
			}
			if (index != i) { 
index如果不等于i,再执行以下操作,否则不用,以此减少工作量                //错误
				int temp = arr[i];        //int temp = arr[inddex]
				arr[i] = arr[index];      //arr[i] = temp;
				arr[index] = temp;     //arr[index] = arr[i];
			}
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值