展开全部
下面一道一道的完成:
第一题:
1)快速排序
package src;
/*
* 有几个关键点
* 1 中间值到底取什么
* 2 i和62616964757a686964616fe78988e69d8331333239303864j的比较什么时候需要是<=
* 3 递归调用的left和right值如何确定
*/
public class QuickSort { //快速排序
public static void main(String[] args) {
int[] a = {100,40, 60, 87, 34, 11, 56, 0};
int left = 0 ;
int right = a.length - 1;
quickSort(a , left , right);
//循环显示数组
for(int i = 0 ; i < a.length ; i++){
System.out.print(a[i] + " ");
}
}
public static void quickSort(int[] a , int left , int right ){
int middle , temp ;
int i = left ;
int j = right ;
middle = a[left];
while(i < j){
while((a[i] < middle) && (i < right)){
i++;
}
while((a[j] > middle) && (j > left)){
j--;
}
if(i <= j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
if(left < j){
quickSort(a , left , j);
}
if(right > i){
quickSort(a , i , right);
}
}
}
2)冒泡排序
package src;
public class PopSort {
public static void main(String[] args) {
int[] a = {100,40, 60, 87, 34, 11, 56, 0};
for(int i = 0 ; i < a.length ; i++)
for(int j = 0 ; j < a.length - i -1 ; j++){
if(a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int k = 0 ; k < a.length ; k++){
System.out.print(a[k] + " ");
}
}
}
第二题
package src;
public class BinarySearch {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
int[] datas = { 1, 2, 3, 4, 5, 6 ,7, 8, 9, 10}; //二分法必须应用在有序数组
int key = 9;
System.out.println("the result is " + bSearch(datas , key));
}
public static int bSearch(int[] datas , int key){
int index = -1;
int low = 0;
int high = datas.length - 1;
while (low <= high) {
int middle = (low + high) >> 1;
if (key == datas[middle]) {
index = middle;
break;
} else if (key > datas[middle]) {
low = middle + 1;
} else if (key < datas[middle]) {
high = middle - 1;
}
}
return index;
}
}
第三题
package src;
public class CharCode {
public static void main(String[] args){
String str = "hello world";
char[] strToChar = str.toCharArray();
int charSum = 0;
for(int i = 0 ; i < strToChar.length ; i++){
charSum = charSum + strToChar[i];
}
System.out.print("the charArray's Sum is " + charSum);
}
}
第四题
package src;
public class NewArray {
/**
* @param args
*/
public static void main(String[] args) {
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int[] b = new int[a.length];
int j = 0;
for(int i = 0 ; i < a.length ; i++){
if(a[i] != 0){
b[j] = a[i];
j++;
}
}
for(int k = 0 ; k < j ; k++){
System.out.print(b[k] + " ");
}
}
}
第五题:
ArrayList实现
package src;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class StudentList {
/**
* @param args
*/
public String name;
public int age;
public String grade;
public String school;
public static List stuList = null;
public StudentList(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}
public static void main(String[] args) {
stuList = new ArrayList();
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));
for(Iterator it = stuList.listIterator() ; it.hasNext(); ){
StudentList stu = (StudentList)it.next();
System.out.println(stu.name + " ," +stu.age + " ," + stu.grade + " ," + stu.school );
}
}
}
数组实现:
package src;
public class StudentArray {
/**
* @param args
*/
public String name;
public int age;
public String grade;
public String school;
public StudentArray(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}
public static void main(String[] args) {
StudentArray[] stua = {new StudentArray("aaa" ,13 ,"4" ,"beijing") , new StudentArray("bbb" ,14 ,"4" ,"dalian") ,new StudentArray("ddd" ,13 ,"4" ,"beijing") ,new StudentArray("ccc" ,13 ,"4" ,"tianjin") ,new StudentArray("eee" ,20 ,"8" ,"beijing") ,new StudentArray("fff" ,23 ,"10" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("kkk" ,13 ,"4" ,"beijing") ,new StudentArray("www" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") };
for(int i = 0 ; i < stua.length ; i++){
System.out.println(stua[i].name + " , " + stua[i].age + " , " +stua[i].grade + " , " +stua[i].school);
}
}
}
第六题:
package job;
public abstract class Job { //这个是父类抽象类,表示职业类
public double baseWage; //基本工资
public Job(double baseWage) {
super();
this.baseWage = baseWage;
}
public double getBaseWage() {
return baseWage;
}
public void setBaseWage(double baseWage) {
this.baseWage = baseWage;
}
public double getYearWage(){ //一年的工资
return baseWage * 12 ;
}
}
package job;
public class Farmer extends Job {
public Farmer(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}
public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}
}
package job;
public class Scientist extends Job {
public String yearendBonus; //年终奖
public Scientist(double baseWage) {
super(baseWage);
//
}
public String getYearendBonus() {
return yearendBonus;
}
public void setYearendBonus(String yearendBonus) {
this.yearendBonus = yearendBonus;
}
public double getYearWage() {
//
return super.getYearWage();
}
}
package job;
public class Teacher extends Job {
public double reward; //每天的课酬
public Teacher(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}
public double getReward() {
return reward;
}
public void setReward(double reward) {
this.reward = reward;
}
public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}
}
package job;
public class Waiter extends Job {
public Waiter(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}
public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}
}
package job;
public class Worker extends Job {
public Worker(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}
public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}
}
package job;
public class Manager { //这是一个管理各种职业工资的类,这是一个含有具体业务的类
/**
* @param args
*/
public static void main(String[] args) {
Teacher tjob = new Teacher(5000); //教师的工资是5000美金一个月
printSalary(tjob);
Scientist sjob = new Scientist(8000);
printSalary(sjob);
Farmer fjob = new Farmer(4000);
printSalary(fjob);
Waiter wjob = new Waiter(3500);
printSalary(wjob);
Worker workjob = new Worker(4500);
printSalary(workjob);
}
public static void printSalary(Job job) {
System.out.println(job.getYearWage());
}
}
第七题:
package src;
public class Writer {
/**
* @param args
*/
public static void main(String[] args) {
int i = 911;
float j = 911.911f;
String str = "hello world";
write(i);
write(j);
write(str);
}
public static void write(int s) {
System.out.print("int: " + s + "\n");
}
public static void write(float s) {
System.out.print("float:" + s + "\n");
}
public static void write(String s) {
System.out.print("String:" + s + "\n");
}
}
好困啊,睡觉了
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起