1、面试必备100道典型Java基本题 1.完毕数组int a = 100,40,60,87,34,11,56,0迅速排序、冒泡排序;迅速排序实当代码:public class Test001 public static void main(String args) int a = new int100,40,60,87,34,11,56,0; System.out.println(未排序之前数据是:); print(a); System.out.println(排序之后数据是:); sort(a,0,a.length-1); print(a); /打印办法 public static void
2、print(int b) for(int i=0;i= high) return;/low不大于high,则直接返回 if(high - low)=1)/如果只有两个数字,则直接比较 if(a0 a1) swap(a,0,1); return; int pivot = alow;/取第一种数作为中间数 int left = low +1; int right = high; while(leftright) /从左边开始找 while(left right & left pivot) break; left+;/左下标往右边走一点 /从右边开始找 while(left low)/如果左不不大于
3、右则始终循环 if(aright = pivot) break; right-;/右下标往左走一点 if(left right)/如果还没有找完,则互换数字 swap(a,right,left); swap(a,low,right); sort(a,low,right); sort(a,right+1,high); /调位办法 private static void swap(int array,int i,int j) int temp; temp = arrayi; arrayi = arrayj; arrayj = temp; 打印成果为:未排序之前数据是:100 40 60 87 34
4、 11 56 0 排序之后数据是:0 11 34 40 56 60 87 100 冒泡排序实当代码:public class Test002 public static void main(String args) int arr = 100,40,60,87,34,11,56,0;/定义数组 System.out.println(未排序之前数据是:); maopaoPrint(arr); System.out.println(); System.out.println(排序之后数据是:); maopaoSort(arr); /排序办法 public static void maopaoSor
5、t(int arrys) /定义暂时变量temp int temp = 0; /用j表达下标,遍历数组 for(int j=0;jarrys.length;j+) /对于每一种数组元素,从0到尚未排序最大下标,总是把最大数字放在后边 for(int k=0;karrysk+1)/判断当前数字与背面数字大小 temp = arrysk; arrysk = arrysk+1; arrysk+1 = temp; maopaoPrint(arrys);/打印输出 /打印办法 public static void maopaoPrint(int l) for(int i=0;il.length;i+)
6、System.out.print(li+ );/从小到大输出 打印成果为:未排序之前数据是:100 40 60 87 34 11 56 0 排序之后数据是:0 11 34 40 56 60 87 1002.采用折半查找算法,在数组中查询到某个数;实当代码:import java.util.Scanner;public class Test003 public static int Max = 20; / 数据数组源 public static int data = 12,16,19,22,25,32,39,39,48,55,57,58, 63,68,69,70,78,84,88,90,97 ;
7、 / 计数器 public static int count = 1; public static void main(String args) System.out.println(请输入您要查找数字:); Scanner sc = new Scanner(System.in); int KeyValue = sc.nextInt(); / 调用折半查找 if (Search(KeyValue) / 输出查找次数 System.out.println(共查找了 + count + 次); else / 输出没有找到数据 System.out.println(抱歉,数据数组源中找不到您输入数字
8、); /折半查找法 public static boolean Search(int k) int left = 0;/ 左边界变量 int right = Max - 1;/ 右边界变量 int middle;/ 中位数变量 while (left = right) middle = (left + right) / 2; if (k datamiddle) left = middle + 1;/查找后半段 else if (k = datamiddle) System.out.println(Data + middle + = + datamiddle); return true; cou
9、nt+; return false; 3.输入一种字符串,其中包括数字、特殊符号(像:¥、&、(、等)、大小写字母等,然后输出每个字符串或符号ASCII码和;例如:输入“#$%&*():|”,则打印出643。实当代码:public class Test001 public static void main(String args) System.out.println(请输入一种字符串:); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int sum = 0; for(int i=0;istr.length(
10、);i+) sum = sum+str.charAt(i); System.out.println(您输入字符串每个字节相加和为:+sum); 4. 将一种数组中值=0项去掉,将不为0值存入一种新数组例如:int a=0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5;生成新数组为:intb=1,3,4,5,6,6,5,4,7,6,7,5实当代码:import java.util.*;public class Test001 public static void main(String args) int a=0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5;
11、 List list = new ArrayList(); for(int i =0;ia.length;i+) if(ai!=0) list.add(ai); int b = new intlist.size(); for(int i = 0;ilist.size();i+) bi = list.get(i); System.out.println(原数组为:); for(int i =0;ia.length;i+) System.out.print(ai+ ); System.out.println(); System.out.println(去掉值为0项之后为:); for(int i:
12、b) System.out.print(i+ ); 5. 定义10个长度Student数组,将10个Student对象年龄所有加1,然后把10个Student对象详细信息逐行打印出来(数组和ArrayList实现)实当代码:第一种类:public class Student public String name; public String sex; public int age; public String getName() return name; public void setName(String name) this.name = name; public String getSe
13、x() return sex; public void setSex(String sex) this.sex = sex; public int getAge() return age; public void setAge(int age) this.age = age; public Student(String name,String sex,int age) super(); this.name = name; this.sex = sex; this.age = age; 第二个类:import java.util.ArrayList;import java.util.List;p
14、ublic class Test001 static Student s = new Student10; int k = 1; public static void main(String args) List li = new ArrayList(); for(int i=0;i10;i+) li.add(new Student(zhangsan+i,男,20); for(int i=0;i10;i+) (li.get(i).age)+; for(int i=0;i10;i+) System.out.println(li.get(i).getName()+ +li.get(i).getSe
15、x()+ +li.get(i).getAge(); 6. 有工人,农民,教师,科学家,服务生,其中,工人,农民,服务生只有基本工资.教师除基本工资外,尚有课酬(元/天),科学家除基本工资外,尚有年终奖,请你写出有关类,将各种类型员工全年工资打印出来实当代码:(共有7个类)第一种类:package com.softeem.zy006;/* 定义一种人接口,以供实现*/public interface People public double num();第二个类:package com.softeem.zy006;/* 工人类*/public class Worker implements Pe
16、ople private double montherSalary; public Worker(double montherSalary) super(); this.montherSalary = montherSalary; public double num() return getMontherSalary()*12; public double getMontherSalary() return montherSalary; public void setMontherSalary(double montherSalary) this.montherSalary = monther
17、Salary; 第三个类:package com.softeem.zy006;/* 农民类*/public class Peasant implements People private double montherSalary; public Peasant(double montherSalary) super(); this.montherSalary = montherSalary; public double getMontherSalary() return montherSalary; public void setMontherSalary(double montherSala
18、ry) this.montherSalary = montherSalary; public double num() return getMontherSalary()*12; 第四个类:package com.softeem.zy006;/* 教师类*/public class Teacher implements People private double montherSalary; private double daySalary; public Teacher(double montherSalary,double daySalary) super(); this.montherS
19、alary = montherSalary; this.daySalary = daySalary; public double num() return getMontherSalary()*12+getDaySalary()*365; public double getMontherSalary() return montherSalary; public void setMontherSalary(double montherSalary) this.montherSalary = montherSalary; public double getDaySalary() return da
20、ySalary; public void setDaySalary(double daySalary) this.daySalary = daySalary; 第五个类:package com.softeem.zy006;/* 科学家类*/public class Scientist implements People private double montherSalary; private double projectSalary; public Scientist(double montherSalary,double projectSalary) super(); this.month
21、erSalary = montherSalary; this.projectSalary = projectSalary; public double num() return getMontherSalary()*12+getProjectSalary(); public double getMontherSalary() return montherSalary; public void setMontherSalary(double montherSalary) this.montherSalary = montherSalary; public double getProjectSal
22、ary() return projectSalary; public void setProjectSalary(double projectSalary) this.projectSalary = projectSalary; 第六个类:package com.softeem.zy006;/* 服务员类*/public class Waiter implements People private double montherSalary; public Waiter(double montherSalary) super(); this.montherSalary = montherSala
23、ry; public double num() return getMontherSalary()*12; public double getMontherSalary() return montherSalary; public void setMontherSalary(double montherSalary) this.montherSalary = montherSalary; 第七个类:package com.softeem.zy006;/* 测试类*/public class Test public static void main(String args) Test a = n
24、ew Test(); Worker w = new Worker(1000); System.out.println(工人年薪为: + w.num()+元); Peasant p = new Peasant(2500); System.out.println(农民年薪为:+p.num()+元); Teacher t = new Teacher(4500,50); System.out.println(教师年薪为:+t.num()+元); Scientist e = new Scientist(10500,30000); System.out.println(科学家年薪为: + e.num()+
25、元); Waiter y = new Waiter(3400); System.out.println(服务生年薪为: + y.num(); 打印成果为:工人年薪为:1.0元农民年薪为:30000.0元教师年薪为:72250.0元科学家年薪为:156000.0元服务生年薪为:40800.07. 创立一种复数类complex,对复数进行数学运算,复数具备如下格式:RealPart+ImaginaryPart*I其中,I为平方根。规定如下:()运用浮点变量表达此类私有数据。提供两个构造办法,一种用于此类声明时对象初始化;一种为带默认值得无参构造办法。()提供两复数加、减、乘运算办法。()按格式(a
26、,b)打印复数。其中a为实部,b为虚部。实当代码:package com.softeem.zy007;/* 创立一种复数类complex,对复数进行数学运算,复数具备如下格式: RealPart+ImaginaryPart*I 其中,I为平方根。* 规定如下:()运用浮点变量表达此类私有数据。提供两个构造办法,一种用于此类声明时对象初始化;一种为带默认值得无参构造办法。* ()提供两复数加、减、乘运算办法。()按格式(a,b)打印复数。其中a为实部,b为虚部。*/public class ComplexNumber implements Cloneable /* 复数实部 */ private
27、 double realPart; /* 复数虚部 */ private double imaginaryPart; /* 默认构造函数 */ public ComplexNumber() this.realPart = 0.0; this.imaginaryPart = 0.0; /* * 构造函数 * * param a * 实部 * param b * 虚部 */ public ComplexNumber(double a,double b) this.realPart = a; this.imaginaryPart = b; /* * 复数加法运算。 c = a + b运算法则是: c
28、.实部 = a.实部 + b.实部;c.虚部 = a.虚部 + b.虚部 * * param aComNum * 加数 * return */ public ComplexNumber add(ComplexNumber aComNum) if (aComNum = null) System.err.println(对象不可觉得null!); return new ComplexNumber(); return new ComplexNumber(this.realPart + aComNum.getRealPart(), this.imaginaryPart + aComNum.getIma
29、ginaryPart(); /* * 复数减法运算。 c = a - b运算法则是: c.实部 = a.实部- b.实部;c.虚部 = a.虚部- b.虚部 * * param aComNum * 减数 * return */ public ComplexNumber decrease(ComplexNumber aComNum) if (aComNum = null) System.err.println(对象不可觉得null!); return new ComplexNumber(); return new ComplexNumber(this.realPart - aComNum.get
30、RealPart(), this.imaginaryPart - aComNum.getImaginaryPart(); /* * 复数乘法运算。 c = a * b运算法则是: c.实部 = a.实部 * b.实部- a.虚部 * b.虚部;c.虚部 = a.虚部 * * b.实部 + a.实部 * b.虚部; * * param aComNum * 乘数 * return */ public ComplexNumber multiply(ComplexNumber aComNum) if (aComNum = null) System.err.println(对象不可觉得null!); r
31、eturn new ComplexNumber(); double newReal = this.realPart * aComNum.realPart - this.imaginaryPart * aComNum.imaginaryPart; double newImaginary = this.realPart * aComNum.imaginaryPart + this.imaginaryPart * aComNum.realPart; ComplexNumber result = new ComplexNumber(newReal,newImaginary); return resul
32、t; /* * 复数除法运算。 c = a / b 运算法则是: c.实部 = (a.实部 * b.实部 + a.虚部 * b.虚部) / (b.实部 * *b.实部 + b.虚部 * b.虚部);c.虚部 = (a.虚部 * b.实部- a.实部 * b.虚部) / (b.实部 * b.实部 + * b.虚部 * b.虚部); * * param aComNum * 除数 * return */ public ComplexNumber divide(ComplexNumber aComNum) if (aComNum = null) System.err.println(对象不可觉得nul
33、l!); return new ComplexNumber(); if (aComNum.getRealPart() = 0) & (aComNum.getImaginaryPart() = 0) System.err.println(除数不可觉得0!); return new ComplexNumber(); double temp = aComNum.getRealPart() * aComNum.getRealPart() + aComNum.getImaginaryPart() * aComNum.getImaginaryPart(); double crealpart = (this
34、.realPart * aComNum.getRealPart() + this.imaginaryPart * aComNum.getImaginaryPart() / temp; double cimaginaryPart = (this.imaginaryPart * aComNum.getRealPart() - this.realPart * aComNum.getImaginaryPart() / temp; return new ComplexNumber(crealpart,cimaginaryPart); /* * 将一种复数显示为字符串 */ public String toString() return this.realPart + + + this.imaginaryPart + i; /* * 比较一种对象与否和这个复数对象值相等 */ public boolean equals(Object obj) if (obj = null) return false; / 一方面判断a是不是一种复数对象,instanceof核心字是用来判断对象类型。 if (obj instanceof ComplexNumber) / 如果a是复数对象,需要将它强制类型转换成复数对象,才干调用复数类提供办法。
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100