Java考试测试题目

题目一:

创建一个Student类,要求有2个实例变量和2个实例函数


public class Test1 {
class Student{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
			
	}

}

题目二:

创建一个Flower类 ,要求有2个实例变量和2个实例函数


public class Test2 {
class Flower{
	private String FlowerName;
	private int petals;   //petals 意为花瓣
	public String getFlowerName() {
		return FlowerName;
	}
	public void setFlowerName(String flowerName) {
		FlowerName = flowerName;
	}
	public int getPetals() {
		return petals;
	}
	public void setPetals(int petals) {
		this.petals = petals;
	}
	
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根

	}

}

题目三:

创建一个Circle类 ,要求有2个实例变量和2个实例函数。


public class Test3 {
public static class  Circle{
	private double r;
	private double pi = 3.14159;
	public Circle(double r) {
		this.r = r;
	}
	public double getC() {
		return 2*pi*r;
	}
	public double getS() {
		return pi*r*r;
	}
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
   Circle a = new Circle(5.0);
   System.out.println("周长是:"+a.getC());
   System.out.println("面积是:"+a.getS());
	}

}

题目四:

编写一个函数Sort(int a[]),实现对数组a的元素从小到大排序并输出结果。

import java.awt.print.Printable;

public class Test4 {
public static void Sort(int []a) {
	int temp;
	for(int i=0;i<a.length;i++) {
		for(int j = i+1;j<a.length;j++) {
			if(a[j]<a[i]) {
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
}
public static void printshuzu(int []a) {
	for(int i =0;i<a.length;i++) {
		System.out.print(a[i]+"  ");
	}
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
int[] arr = {5,2,0,1,3,1,4};
Sort(arr);
printshuzu(arr);
	

}
}

题目五:

编写一个函数ReverseOrder(int a[]),实现对数组a的元素逆序输出。


public class Test5 {
public static void ReverseOrder(int a[]) {
	int temp;
	int m = a.length-1;
	for(int i=0;i<a.length/2;i++) {
		temp = a[m];
		a[m] = a[i];
		a[i] = temp;
		m--;
	}
}
public static void printshuzu(int[] a) {
	for(int i=0;i<a.length;i++) {
		System.out.print(a[i]+"   ");
	}
}

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
int[] arr = {1,2,3,4,5,6,7,8,9};
ReverseOrder(arr);
printshuzu(arr);
	}

}

题目六:

编写一个函数SumofEven (int a[]),实现对数组a的所有索引为偶数的元素求和。


public class Test6 {
public static  int SumofEven (int a[]) {
	int sum = 0;
	for(int i =0;i<a.length;i++) {
		if(i%2==0) {
			sum+=a[i];
		}
	}
	return sum;
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
int[] arr = {0,1,2,3,4,5,6,7,8};
System.out.println("数组中偶数索引的总和为:"+SumofEven(arr));
	}

}

题目七:

编写一个函数MyRandom( int n )随机生成5个不超过n且互不相同的整数,并输出结果。

package 生成不大于n的随机数;

import java.util.ArrayList;
import java.util.Collections;



public class Test {
	public static void MyRandom( int n ) {
		ArrayList<Integer>list = new ArrayList<Integer>();
		for(int i=0;i<n;i++) {
			list.add(i);
		}
		Collections.shuffle(list);
		for(int i=0;i<5;i++) {
			System.out.print(" "+list.get(i));
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
MyRandom(60);
	}

}

题目八:

小明20岁开始投资,初始资金位10000元,假设年化复合收益率分别为r%请设计函数capitl(int  n, double r) 打印出n年后的本利之和。

import java.util.Scanner;
public class Test8 {
public static double capitl(int n,double r) {import java.util.Scanner;
public class Test8 {
public static double capitl(int n,double r) {
	double Money;
	Money  = 10000*Math.pow(1+r/100,n);
	return Money;
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		System.out.println("请输入存款年限:");
		Scanner a = new Scanner(System.in);
		int n = a.nextInt();
		System.out.println("输入年利率,输入格式为r%,只输入r即可");
		double r = a.nextDouble();

        System.out.println(capitl(n, r));
	}

}

	double Money;
	Money  = 10000*Math.pow(1+r/100,n);
	return Money;
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		System.out.println("请输入存款年限:");
		Scanner a = new Scanner(System.in);
		int n = a.nextInt();
		System.out.println("输入年利率,输入格式为r%,只输入r即可");
		double r = a.nextDouble();

        System.out.println(capitl(n, r));
	}

}

题目九:

编写一个Java程序,设计矩阵类,至少包含三个属性:行数,列数,存储数据的二维数组;至少两个构造函数,实现矩阵的加法以及两个矩阵的乘法。



class Matrix {
	int rows;
	int cols;
	double data[][];
	private static Matrix res1;

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getCols() {
		return cols;
	}

	public void setCols(int cols) {
		this.cols = cols;
	}

	public Matrix() {

	}

	public Matrix(int rows, int cols) {
		this.rows = rows;
		this.cols = rows;
		this.data = new double[rows][cols];
	}

	public Matrix(double data[][]) {
		this.rows = data.length;
		this.cols = data[0].length;
		this.data = new double[rows][cols];
		for (int i = 0; i < this.rows; i++) {
			for (int j = 0; j < this.cols; j++) {
				this.data[i][j] = data[i][j];
			}
		 }
	}

	public double getElement(int i, int j) {
		return data[i][j];
	}

	public void setElement(int i, int j, double data) {
		this.data[i][j] = data;
	}

	public static Matrix MatrixAdd(Matrix ma, Matrix mb) {
		Matrix res = null;
		if (ma.getRows() != mb.getRows() || ma.getCols() != mb.getCols()) {
			System.out.println("无法相加");
			return res;
		}
		res = new Matrix(ma.getRows(), ma.getCols());
		for (int i = 0; i < ma.getRows(); i++) {
			for (int j = 0; j < ma.getCols(); j++) {
				res.setElement(i, j, ma.getElement(i, j) + mb.getElement(i, j));
			}
		}
		return res;
	}

	public static Matrix Chengfa(Matrix m, Matrix n) {
		res1 = null;
		if (m.getCols() != n.getRows()) {
			System.out.println("无法计算");
			return res1;
		}
		res1 = new Matrix(m.getRows(), n.getCols());
		for (int i = 0; i < m.getRows(); i++) {
			for (int j = 0; j < n.getCols(); j++) {
				double sum = 0;
				for (int k = 0; k < n.getRows(); k++) {
					sum += m.getElement(i, k) * n.getElement(k, j);
				}
				res1.setElement(i, j, sum);
			}
		}
		return res1;
	}

	public static Matrix Shucheng(int a, Matrix mc) {
		Matrix res2 = new Matrix(mc.getRows(), mc.getCols());
		for (int i = 0; i < mc.getRows(); i++) {
			for (int j = 0; j < mc.getCols(); j++) {
				res2.setElement(i, j, a * mc.getElement(i, j));
			}
		}
		return res2;
	}

	public void Printdata() {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				System.out.print("  " + data[i][j]);
			}
			System.out.println();
		}
	}
}

public class Test9 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		double d1[][] = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
		double d2[][] = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
		Matrix ma = new Matrix(d1);
		Matrix mb = new Matrix(d2);
		System.out.println("打印A矩阵");
		ma.Printdata();
		System.out.println("打印B矩阵");
		mb.Printdata();
		System.out.println("打印A+B");
		Matrix mc = Matrix.MatrixAdd(ma, mb);
		mc.Printdata();
		System.out.println("打印A*B");
		Matrix md = Matrix.Chengfa(ma, mb);
		md.Printdata();
		System.out.println("打印K*B");
		Matrix me = Matrix.Shucheng(4, ma);
		me.Printdata();
	}

}

题目十:

见上题目九,添加修改就可以。此处不在写出。

题目十一:

设计函数读取文件MyDream.txt,并在控制台输出读取的内容.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test11 {
public static void readFile(File filename) {
	FileInputStream fi = null;
	byte buffer[] = new byte[1024];
	try {
		fi = new FileInputStream(filename);
		int count;
		while((count = fi.read(buffer))!=-1) {
			System.out.println("文本的原内容为");
			System.out.println(new String(buffer,0,count,"UTF-8"));
		}
	} catch (FileNotFoundException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}catch (IOException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}

	
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
readFile(new File("D:/tools/MyDream.txt"));
	}

}

题目十三:

设计函数将文件MyDream.txt拷贝到E盘根目录

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test13 {
public static void copyFile(File a,File b) {
	byte buffer[] = new byte[1024];
	FileInputStream fi = null;
	FileOutputStream fo = null;
	int count =0;
	try {
		fi = new FileInputStream(a);
		fo = new FileOutputStream(b);
		while((count=fi.read(buffer))!=-1) {
			fo.write(buffer, 0, count);
		}
	} catch (FileNotFoundException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}catch (IOException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}

	
}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
copyFile(new File("D:/tools/MyDream.txt"), new File("D:/tools/new MyDream"));
//这里我没有拷贝到E盘根目录 根据需要修改第二个即可
	}

}

补充:十二题由于文件后缀问题还在思考,十四题由于Mysql文件的安装失败暂时没有给出代码。

后序更新。

如有错误,欢迎指正 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值