Java基础测试题1(含答案)

1.编程打印下面的图形:

*****
****
***
**
*

public class Test1 {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5-i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

2.编写一个方法求s=a+aa+aaa+a...a,其中a是一个数,比如a=2;那么求s=2+22+222+2222...的值。该方法需要两个参数,第一个参数控制a,第二个参数控制有多少个数。

public class Test2 {
	public static void main(String[] args) {
		int s = sum(2,4);
		System.out.println(s);
	}
	
	/**
	 * 求s=a+aa+aaa+a...a
	 * @param a 基数
	 * @param i 数的个数
	 */
	public static int sum(int a,int i){
		int sum = 0;
		int n = a;
		for (int j = 0; j < i-1; j++) {
			n = n*10 + a;
			sum = sum + n ;
		}
		return sum + a;
	}
}

3.给出一组学生的成绩int[] score={80,45,60,100,89,92,93,...}请求出这组成绩中,100分,90-99,80-89的学生人数。

public class Test3 {
	public static void main(String[] args) {
		int[] score={80,45,60,100,89,92,93,70,90,60,20};
		group(score);
	}
	
	/**
	 * 分数统计
	 * @param arr 所传递的数组的首地址
	 */
	public static void group(int[] arr){
		int a=0,b=0,c=0;
		for (int i : arr) {
			if(i==100)a++;
			else if (i>=90&&i<=99)b++;
			else if (i>=80&&i<=89)c++;
		}
		System.out.println("100:"+a);
		System.out.println("90-99:"+b);
		System.out.println("80-89:"+c);
		
	}
}

4.String类中有一个方法叫toCharArray();该方法在string对象上面调用,用于将一个字符串变成char的数组,方法签名为:char[] toCharArray();请编写一个方法int lastIndexOf(String s,char c),计算出一个字符在string中第一次出现的位置(倒着数),如果在string中没有这个字符,则返回-1;

public class Test4 {
	public static void main(String[] args) {
		String str = "hello,world";
		int i = lastIndextOf(str, 'o');
		System.out.println(i);
	}
	
	/**
	 * 计算出一个字符在string中第一次出现的位置
	 * @param s 字符串
	 * @param c 所需定位的字符
	 * @return 若存在返回首次出现的数组下标,若不存在返回-1
	 */
	public static int lastIndextOf(String s,char c){
		char[] cs = s.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			if(cs[i]==c)return i;
			
		}
		return -1;
	}
}

5.在System类中有静态方法public static void arraycopy(
Object src,  
int srcPos,  
Object dest, 
int destPos,
int length):
可以用于数组src从第srcPos项元素开始的length个元素拷贝到目标数组dest从destPos开始的length个元素。请自己实现这个方法。

public class Test5 {
	public static void main(String[] args) {
		int[] arr = {1,3,4,5,6,3,7,8,8};
		int[] dest = new int[10];
		arrayCopy(arr,2,dest,2,4);
		for (int i : dest) {
			System.out.print(i+" ");
		}
	}
	
	/**
	 * 实现数组拷贝功能
	 * @param src 源数组
	 * @param srcPos 拷贝位置
	 * @param dest 目标数组
	 * @param destPos 目标数组位置
	 * @param length 拷贝长度
	 */
	public static void arrayCopy(int[] src,int srcPos,int[] dest,int destPos,int length){
		for (int i = 0; i < length; i++) {
			dest[destPos++] = src[srcPos++];
		}
	}
}

6.各种类型的几何图形都有自己的面积计算公式,但在一个CAD(计算机辅助设计)软件中,图形的种类非常多,如果程序需要计算出任何一种图形的面积,最简单 的想法是使用条件判断语句根据图形类型来选择相应的计算公式。然而,这种方法不太好,可能会导致嵌套层次很深的条件判断语句。另外,如果软件要扩充功能以 支持新的图形种类,则又不得不修改调用不同公式计算图形面积的代码。在学习了面向对象技术之后,你能对上述问题给出一个较好解决方案来吗?请以计算圆形、矩形和三角形三种几何图形的面积为例,列出关键代码。

/**
 * 抽象基类:图形
 * @author well
 *
 */
abstract class Graph{
	/**
	 * 求图形的周长
	 * @return 图形的周长
	 */
	public abstract double getCircumference();
	/**
	 * 求图形的面积
	 * @return 图形的面积
	 */
	public abstract double getAera();
}

/**
 * 圆形
 * @author well
 *
 */
class Circle extends Graph{
	private double r;//半径
	
	public Circle(double r) {
		super();
		this.r = r;
	}

	@Override
	public double getCircumference() {
		return 2*r*Math.PI;
	}

	@Override
	public double getAera() {
		return Math.PI*r*r;
	}
}

/**
 * 矩形
 * @author well
 *
 */
class Rectangle extends Graph{
	private double length;
	private double width;
	
	public Rectangle(double length, double width) {
		super();
		this.length = length;
		this.width = width;
	}

	@Override
	public double getCircumference() {
		return 2*(length + width);
	}

	@Override
	public double getAera() {
		return length * width;
	}
	
}

/**
 * 题目:定义抽象类,求不同图形的周长
 * @author well
 *
 */
public class AbstractDemo {
	public static void main(String[] args) {
		Circle c = new Circle(2);
		double cc = c.getCircumference();
		System.out.println(cc);//12.566370614359172
		System.out.println(c.getAera());//12.566370614359172
		
		Rectangle r = new Rectangle(2, 3);
		double rr = r.getCircumference();
		System.out.println(rr);//10.0
		System.out.println(r.getAera());//6.0
	}
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值