(1) 创建一个1×100的一维数组,对数组中的元素进行随机赋值(使用Math.random()生成0-1之间的浮点数)。通过算法找到该数组中最大的5个数,要求从大到小输出。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:采用面向对象思想,设计两个数据结构,一个为主数组,用来存储n=100个数,同时再维护一个结果数组,将保存最大(最小的前m个)
package Hw2;
import java.util.Arrays;
import java.util.Collections;
//结果数组类
class ResArray {
private Double[] resArray;
private int resArraySize;
public ResArray(int resArraySize) {
this.resArraySize = resArraySize;
resArray = new Double[resArraySize + 1];//多分配一位
for (int i = 0; i < resArraySize; i++) {
resArray[i] = 0.0;
}
}
//维护结果数组的方法
public void insertDouble(boolean sortIncreasing, double element) {
resArray[resArraySize] = element;
if (sortIncreasing == true) {
Arrays.sort(resArray);
} else {
Arrays.sort(resArray, Collections.reverseOrder());
}
}
//打印前resArraySize位
public String toString() {
String temp = "";
for (int i = 0; i < resArraySize; i++) {
temp += resArray[i] + ", ";
}
return temp;
}
}
//Buffer类
class BufferN {
private double[] buffer; //主数组
private ResArray resArray; //维护一个结果数组
private boolean sortIncreasing; //升序或者降序
public BufferN(int bufferSize, int resArraySize, boolean sortIncreasing) {
buffer = new double[bufferSize];
resArray = new ResArray(resArraySize);
this.sortIncreasing = sortIncreasing;
for (int i = 0; i < buffer.length; i++) {
buffer[i] = Math.random();
resArray.insertDouble(this.sortIncreasing, buffer[i]);
}
}
//输出结果
public String diplayResult() {
return resArray.toString();
}
public String toString() {
String temp = "";
for (double b : buffer) {
temp += b + ", ";
}
return temp;
}
}
public class BiggestN {
public static void main(String[] args) {
int bufferSize = 100;
int resArraySize = 5;
boolean sortIncreasing = false;
BufferN bufferN = new BufferN(bufferSize, resArraySize, sortIncreasing);
System.out.println("主数组:");
System.out.println(bufferN);
System.out.println("结果数组:");
System.out.println(bufferN.diplayResult());
}
}
(2) 某省居民电价分三个“阶梯”:月用电量50度以内的,电价为0.538元/度;用电量在51度至200度之间的,电价为0.568元/度,用电量超过200度的,电价为0.638元/度。编写程序,用户从键盘输入用电量,程序输出用户应缴纳的电费。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:封装一个电量类,将每段电费设计为变量,进行三次判断,利用数学关系得出结果。并在此基础上添加对输入用电量的非负判断
package Hw2;
import java.util.Scanner;
class PriceCaculater {
private double price1; //第一段价格
private double price2; //第二段价格
private double price3; //第三段价格
private double input; //输入
public PriceCaculater(double price1, double price2, double price3, double input) {
this.price1 = price1;
this.price2 = price2;
this.price3 = price3;
this.input = input;
}
//输出
public String toString() {
String temp = "";
if (input < 0) {
temp = "您输入的用电量有误";
} else if (input >= 0 && input < 50) {
temp = "" + input * price1;
} else if (input >= 50 && input < 200) {
temp = "" + (50 * price1 + (input - 50) * price2);
} else {
temp = "" + (50 * price1 + 150 * price2 + (input - 200) * price3);
}
return temp;
}
}
public class ElectricityPrice {
public static void main(String[] args) {
double price1 = 0.538;
double price2 = 0.568;
double price3 = 0.638;
double input;
Scanner sc = new Scanner(System.in);
input = sc.nextDouble();
PriceCaculater priceCaculater = new PriceCaculater(price1, price2, price3, input);
System.out.println("您输入的用电量是:");
System.out.println(input);
System.out.println("应缴纳的电费为:");
System.out.println(priceCaculater);
}
}
(3) 编写程序:用户从键盘输入一个两位以上的正整数,程序逆序打印出其各位数字。 (例如:用户输入34567,程序输出76543)在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:封装一个功能类,专门用于翻转数字。
package Hw2;
import java.util.Scanner;
class Reverser {
private int inputNum;
private int outputNum;
//构造并反转数字
public Reverser(int inputNum) {
this.inputNum = inputNum;
int temp = inputNum;
int res = 0;
while (temp != 0) { //每次取最后一位
res = res * 10 + temp % 10;
temp /= 10;
}
outputNum = res;
}
public String toString() {
String temp = "" + outputNum;
return temp;
}
}
public class ReverseNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
Reverser reverser = new Reverser(num);
System.out.println("翻转前的数字为:");
System.out.println(num);
System.out.println("翻转后的数字为:");
System.out.println(reverser);
}
}
(4) 编写程序:计算100-500之间有多少个素数,并输出所有素数。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:同样封装一个素数类,将范围设置为变量,采用埃拉托斯特尼筛子法,筛掉所有的合数。
package Hw2;
import java.util.Scanner;
import static java.lang.Math.sqrt;
class PrimeFinder {
private int start;
private int end;
int[] re;
int cnt;
public PrimeFinder(int start, int end) {
this.start = start;
this.end = end;
cnt = 0;
re = new int[end+1];
int count1 = 0;//质数个数
re[0] = 2;
int k = 0;
//埃拉托斯特尼筛子法
for (int i = 2; ; i++) {
if (count1 == end) {
break;
}
int flag = 0;//用作标记,如果是质数就为0,不是质数就为1-
for (int j = 0; j <= k; j++) {
if (re[j] > sqrt(i)) {//只去小于等于该数字本身平方根的数字
break;
}
if (i % re[j] == 0) {//只对质数取余
flag = 1;//不是质数,改为1
break;//既然已经知道这个数不是质数,那么就可以结束对这个数字的判断
}
}
if (flag == 0) {
re[++k] = i;
count1++;
}
}
for (int i = 0; i < end; i++) {
if(re[i]>=start&&re[i]<=end){
cnt++;
}
}
}
public int getCnt(){
return cnt;
}
public String toString() {
String temp = "";
for (int i = 0; i < end; i++) {
if(re[i]>=start&&re[i]<=end){
temp += re[i] + " ";
}
}
return temp;
}
}
public class PrimeNum {
public static void main(String[] args) {
int start;
int end;
Scanner sc = new Scanner(System.in);
System.out.println("请输入区间:");
start = sc.nextInt();
end = sc.nextInt();
PrimeFinder primeFinder = new PrimeFinder(start, end);
System.out.println("素数个数为:");
System.out.println(primeFinder.getCnt());
System.out.println("素数列出:");
System.out.println(primeFinder);
}
}
(5) 编写程序:有一序列:2/2,3/4,4/8,5/16,6/32,...求出这个数列的前15项之和。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:封装一个工具类,将序列变为可变长的。
package Hw2;
import java.util.Scanner;
class ArrAdder {
private double sum;
private int length;
public ArrAdder(int length) {
this.length = length;
sum = 0;
int tempFz = 2;
int tempFm = 2;
for (int i = 0; i < length; i++) {
sum+= tempFz/(double)tempFm;
tempFz+=1;
tempFm*=2;
}
}
public String toString(){
String temp = "";
temp = "和为:" + sum;
return temp;
}
}
public class ArrSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入序列长度:");
int cnt = sc.nextInt();
ArrAdder arrAdder = new ArrAdder(cnt);
System.out.println(arrAdder);
}
}