1 抽象类计算体积
题目:1.定义一个抽象类,抽象类中含有一个计算图像体积的抽象方法
2.定义可以计算球,圆柱体,长方体体积的类并实现计算图像体积的抽象方法
Shape.java
package exercise1;
public abstract class Shape {
public abstract double volume();
}
Sqhere.java
package exercise1;
public class Sqhere extends Shape{
private double radius;
public Sqhere(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double volume() {
return (4/3)*Math.PI*Math.pow(radius,3);
}
}
Cylinder.java
package exercise1;
public class Cylinder extends Shape {
private double radius,height;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Cylinder(double radius,double height) {
this.radius = radius;
this.height = height;
}
public double volume() {
return Math.PI*Math.pow(radius, 2)*height;
}
}
Cuboid.java
package exercise1;
public class Cuboid extends Shape {
private double len,width,height;
public double getLen() {
return len;
}
public void setLen(double len) {
this.len = len;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public Cuboid(double len,double width,double height) {
this.len = len;
this.width = width;
this.height = height;
}
public double volume() {
return len*width*height;
}
}
TestVol.java
package exercise1;
public class TestVol {
public static void main(String[] args) {
Shape sh1 = new Sqhere(5.0);
Shape sh2 = new Cylinder(2,3);
System.out.println("体积为"+sh1.volume());
System.out.println("体积为"+sh2.volume());
}
}
2 输入不限个数和位数的正整数数组,将数组中的数字拼接起来排成一个数,打印所能拼接出的所有数字中最小的一个。例如输入数组{3, 32, 321},则扫描输出这3 个数字能排成的最小数字321323。
基本知识点:
- Arrays.sort()使用时,若是基本类型,需要转化为对应的对象类型(如:int转化为Integer)Arrays.sort()可以排序基本对象类型,但是不可以使用基本数据类型。Arrays.sort()默认的是升序排序,降序排序可采用Collection.sort()匿名内部类。
package test6;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/*
* String类的compareTo() 方法返回值是整型,它是先比较对应字符的大小(ASCII码顺序),
* 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,
* 如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,直至比较的字符或被比较的字符有一方结束。
* 如果参数字符串等于此字符串,则返回值 0;
* 如果此字符串小于字符串参数,则返回一个小于 0 的值;
* 如果此字符串大于字符串参数,则返回一个大于 0 的值。
*/
public class StitchNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("输入数组元素的个数:");
Scanner x = new Scanner(System.in);// 获取输入流
int[] number = null;
while (x.hasNext()) {
int m = x.nextInt();// 获取元素个数
number = new int[m];// 初始化数组
for (int i = 0; i < m; i++) {
number[i] = x.nextInt();
}
System.out.println(Arrays.toString(number));
break;
}
System.out.println(PrintMinNumber(number));
}
public static String PrintMinNumber(int[] numbers) {
if (numbers.length == 0 || numbers == null) {
return "";
}
// change int array to string数组
int len = numbers.length;
String[] str = new String[len];
for (int i = 0; i < len; i++) {
str[i] = String.valueOf(numbers[i]);
}
Arrays.sort(str, new Comparator<String>() {
public int compare(String s1, String s2) {
System.out.println("+1 "+s1+" "+s2);
String c1 = s1 + s2;
String c2 = s2 + s1;
return c1.compareTo(c2);//return int
}
});
//str数组中此时已经是正确的拼接顺序,直接追加即可
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append(str[i]);
}
return sb.toString();
}
}
3 向一个长度为10的整型数组中随机生成10个0-9的随机整数,完成:
(1)统计每个数字出现了多少次
(2)输出出现次数最多的数字
(3)输出只出现一次的数字中最小的数字
package test7;
import java.util.Random;
import java.util.TreeSet;
public class CountNumbers {
public static void main(String[] args) {
int []nums = new int[10];
int[] flag=new int[10];
int count;
int max = 0;
int maxCount = 0;
TreeSet ts = new TreeSet();
//0-随机生成数组;flag保存该位数字出现的个数
Random random = new Random();
for(int i=0;i<10;i++) {
nums[i] = random.nextInt(10);
System.out.print(nums[i]+" ");
}
System.out.println();
//1-统计每个数字出现了多少次
for(int i =0;i<=9;i++) {
count = 0;
for(int j=0;j<10;j++) {
if(i==nums[j]) {
count++;
}
}
flag[i]=count;
}
for(int i =0;i<flag.length;i++) {
if(flag[i]!=0) {
System.out.println(i+"出现的次数为:"+flag[i]);
}
}
//2-统计出现次数最多的数字
for(int i=0;i<10;i++) {
if(flag[i]>max) {
maxCount=flag[i];
max=i;
}
}
System.out.println(max+"出现的次数最多,次数:"+maxCount);
//3-输出只出现一次的数字中,最小的数字(treeSet默认从小到大排序)
for(int i=0;i<10;i++) {
if(flag[i]==1) {
ts.add(i);
}
}
System.out.println("只输出一次的数字中,最小的是 "+ts.first());
}
}