大学Java实验一,素数,哥德巴赫猜想,简单的日期类,复数类四则运算
1、尝试两人合作编写程序,加深对方法的接口与实现的理解。其中一人负责编写方法isPrime(),用于判断一个给定的正整数是否素数,另一个人负责编写主方法main(),利用isPrime()验证著名的哥德巴赫猜想:任何比2大的偶数都可表示为两个素数之和。(提示:验证必须有上界,程序未调试成功之前应先选择一个较小的上界,调试成功后再换成较大的上界。上界可由用户输入,否则应定义为一个符号常量。)
2、设计一个简单的日期类Date,然后编写一个主程序演示该类的用法。(提示:一个日期至少具有年、月、日等属性,其行为有初始化、修改日期、返回描述日期的字符串、返回该日期的年、月、日等。)
3、试设计一个带有四则运算的复数类,并编写主程序演示其用法。(提示:一个复数Complex由两部分组成:实部realPart和虚部imaginaryPart,两个复数可进行加、减、乘、除四则运算。)
PrimeNumber.java
package shiyan1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class PrimeNumber {
/**
* @param args
*/
private int prime;
boolean flag = true;
int[] gold;
public PrimeNumber() {
super();
System.out.print("/t/t--------------来自构造函数的消息:祝您每天都有好心情----------/n/n");
}
protected int setPrime() {
Scanner _prime = null;
System.out.print("/n/t请输入要验证的正整数:");
_prime = new Scanner(System.in);
prime = _prime.nextInt();
if (prime <= 0) {
System.out.print("/t"+prime + "不是正整数,素数只能是正整数的,请再来一次!/n");
setPrime();// 递归调用
}
return prime;
}
protected void isPrime() {
int mark = 0;
for (int i = 1; i <= prime; i++) {
if (prime % i == 0) {
mark++;
}
}
if (mark > 2) {
System.out.print("/t" + prime + "不是素数!/n/n");
} else {
System.out.print("/t" + prime + "是素数!/n/n");
}
}
public void showTips() {
System.out
.print("/t*****************************************************************/n/n");
System.out.print("/t/t/t#/t GO----继续验证/t #/n");
System.out.print("/t/t/t#/t EXIT--退出程序/t #/n/n");
System.out.print("/t/t/t#/t GO,EXIT不区分大小写/t #/n/n");
System.out
.print("/t*****************************************************************/n");
}
protected boolean isExit() {
String isexit = null;
showTips();
System.out.print("/t请选择您的下一步操作:");
Scanner _isexit = new Scanner(System.in);
isexit = _isexit.next();
if (isexit.equalsIgnoreCase("EXIT")) {
flag = false;
System.out.print("/n/n/t程序已正常退出,拜拜");
return flag;
} else if (isexit.equalsIgnoreCase("GO")) {
flag = true;
return flag;
} else {
flag = true;
System.out.print("/t" + isexit + "不是程序的命令,请重新来过!/n/n");
isExit(); // 递归调用
return flag;
}
}
public static void main(String[] args) {
PrimeNumber primenumber = new PrimeNumber();
while (primenumber.flag) {
try {
primenumber.setPrime();
primenumber.isPrime();
primenumber.isExit();
} catch (InputMismatchException ims) {
System.out.print("/t您的输入与要求不一致,请确定后重新来过!/n/n");
}
}
}
}
Goldbach.java
package shiyan1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Goldbach {
/**
* @param args
* private int prime 成员变量 ,用于判断的素数,由命令行输入 boolean flag 成员变量
* ,用于判断程序是否该退出了,true不是不退出,false表示该退出了
*
*/
private int prime;
boolean flag = true;// flag用于判断程序是否该退出了,true不是不退出,false表示该退出了
public Goldbach() {
super();
System.out.print("/t/t--------------来自构造函数的消息:祝您每天都有好心情----------/n/n");
}
protected int setPrime() {
Scanner _prime = null;
System.out.print("/n/t请输入要验证的正整数:");
_prime = new Scanner(System.in);
prime = _prime.nextInt();
if (prime <= 0) {
System.out.print("/t"+prime + "不是正整数,素数只能是正整数的,请再来一次!/n");
setPrime();// 递归调用
}
return prime;
}
/*
* protected void isPrime() {
*
* int mark = 0; for (int i = 1; i <= prime; i++) { if(prime%2!=0){
* System.out.println(prime+"不是偶数,这里需要输入偶数,请再来一次!/n"); isPrime(); } else if
* (prime % i == 0) { mark++; } } if (mark > 2) { System.out.print(prime +
* "不是素数!/n/n"); // return false; } else { System.out.print(prime +
* "是素数!/n/n"); // return true; } }
*/
protected void twoPrime() {
// 确保输入的是偶数,题目要求的
if (prime % 2 != 0) {
System.out.println("/t"+prime + "不是偶数,这里需要输入偶数,请再来一次!/n");
setPrime();
}
// i为两个素数中的第一个,
int i = 1;
// num用于统计有多少组是符合题意的
int num = 0;
while (i <= prime / 2) {
// mark1,mark2如果放在while语句外面就会出问题,此处要研究
int mark1 = 0;
int mark2 = 0;
// 判断此时的i是不是素数
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
mark1++;
}
}
// 判断prime-i是不是素数
for (int m = 1; m <= (prime - i); m++) {
if ((prime - i) % m == 0) {
mark2++;
}
}
// 如果i和prime-i都为素数,打印出来,
if (mark1 == 2 & mark2 == 2) {
num++;
System.out.print("/t" + prime + " = " + i + " + " + (prime - i)
+ "/n");
}
// 因为i是从1开始的,又偶数不可能为素数,所以i每次都加2,即i每次都是奇数
i += 2;
}
// 最后打印总数
System.out.print("/n/t/t共有" + num + "组/n");
}
protected boolean isExit() {
// isexit等于输入的字符串,用于与“EXIT”,“GO”做比较
String isexit = null;
System.out.print("/n/t输入EXIT退出程序,输入GO继续验证(都不区分大小写)/n");
System.out.print("/t请选择您的下一步操作:");
Scanner _isexit = new Scanner(System.in);
isexit = _isexit.next();
if (isexit.equalsIgnoreCase("EXIT")) {
flag = false;// flag = false,程序退出
System.out.print("/n/n/t程序已正常退出,拜拜");
return flag;
} else if (isexit.equalsIgnoreCase("GO")) {
flag = true;// flag = true,程序不退出
return flag;
} else {
flag = true;// flag = true,程序不退出
System.out.print("/n/t" + isexit + "不是程序的命令,请重新来过!/n");
isExit(); // 递归调用
return flag;
}
}
public static void main(String[] args) {
Goldbach goldbach = new Goldbach();
while (goldbach.flag) {
try {
goldbach.setPrime();
goldbach.twoPrime();
goldbach.isExit();
} catch (InputMismatchException ime) {
System.out.print("/n/t您的输入与要求不一致,请确定后重新来过!/n/n");
}
}
}
}
MyDate .java
package shiyan1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MyDate {
int year;
int month;
int day;
int num_order;// 接受来自命令行的输入,int类型,只要负责showTips()的命令代号和日期的修改
boolean flag = true;// 控制程序的退出,当flag = false时,退出
public MyDate() {
year = 2008;
month = 8;
day = 8;
}
// 这个构造函数未用
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
// showTips()用于提示用户相关操作代号
public void showTips() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1--修改年份/t #/n");
System.out.print("/t/t/t#/t 2--修改月份/t #/n");
System.out.print("/t/t/t#/t 3--修改日份/t #/n");
System.out.print("/t/t/t#/t 4--修改全部/t #/n");
System.out.print("/t/t/t#/t 5--退出修改/t #/n");
System.out
.print("/t*****************************************************************/n");
}
public void changeDate() {
Scanner _order = new Scanner(System.in);
System.out.print("/t请输入您的命令:");
// 下面的数字异常没有捕获
num_order = _order.nextInt();
if (num_order == 1) {
System.out.print("/t请输入新的年份:");
year = _order.nextInt();
System.out.println();
} else if (num_order == 2) {
System.out.print("/t请输入新的月份:");
month = _order.nextInt();
System.out.println();
} else if (num_order == 3) {
System.out.print("/t请输入新的日份:");
day = _order.nextInt();
System.out.println();
} else if (num_order == 4) {
System.out.print("/t请输入新的年份:");
year = _order.nextInt();
System.out.print("/t请输入新的月份:");
month = _order.nextInt();
System.out.print("/t请输入新的日份:");
day = _order.nextInt();
System.out.println();
} else if (num_order == 5) {
flag = false;
System.out.print("/n/n/t程序已正常退出,拜拜!");
}
}
public void displayDate() {
if (month >= 0 && month <= 12 && day >= 1 && day <= 31) {
System.out.println("/t目前的日期为:" + year + "年" + month + "月" + day
+ "日/n/n");
} else {
System.out.print("/t您的输入与设计情况不相符,请确认后重新来过!/n/n");
}
showTips();
changeDate();
}
/**
* @param args
*/
public static void main(String[] args) {
MyDate mydate = new MyDate();
while (mydate.flag) {
try {
mydate.displayDate();
} catch (InputMismatchException ime) {
System.out.print("/t您的输入与要求不一致,请确定后重新来过!/n/n");
mydate.changeDate();
}
}
}
}
Complex .java
package shiyan1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Complex {
double a1;
double b1;
double a2;
double b2;
boolean flag = true;
Scanner _param = new Scanner(System.in);
double param;
public Complex() {
super();
System.out.print("/t/t------------来自构造函数的消息:祝您每天都有好心情----------/n/n");
System.out
.print("/t要对复数进行四则运算,首先要构造出两个复数,两个复数的形式如下:/n"
+ "/n/t/t/t(1) comp1 = a1 + b1*i/n"
+ "/n/t/t/t(2) comp2 = a2 + b2*i/t(a1,a2,b1,b2∈R)/n/n/n");
System.out.print("/t下面要求输入四个参数(a1,b1,b1,b2)的值:/n/n");
System.out.print("/t输入a1的值:");
a1 = _param.nextDouble();
System.out.print("/t输入b1的值:");
b1 = _param.nextDouble();
System.out.print("/t输入a2的值:");
a2 = _param.nextDouble();
System.out.print("/t输入b2的值:");
b2 = _param.nextDouble();
System.out.print("/n/t构造出两个复数的形式如下:"
+ "/n/n/t/t/t(1) comp1 = " + a1 + " + " + b1 + "*i/n"
+ "/n/t/t/t(2) comp2 = " + a2 + " + " + b2
+ "*i/n/n/n");
}
/**
* @param args
*/
public void showTips() {
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---加法运算/t #/n");
System.out.print("/t/t/t#/t 2---减法运算/t #/n");
System.out.print("/t/t/t#/t 3---乘法运算/t #/n");
System.out.print("/t/t/t#/t 4---除法运算/t #/n");
System.out.print("/t/t/t#/t 5---退出程序/t #/n");
System.out
.print("/t*****************************************************************/n");
}
public void yunsuan() {
Scanner _param = null;
showTips();
_param = new Scanner(System.in);
System.out.print("/t请选择您要进行的操作:");
param = _param.nextInt();
if (param == 1) {
add();
} else if (param == 2) {
subtract();
} else if (param == 3) {
multiply();
} else if (param == 4) {
divide();
} else if (param == 5) {
System.out.print("/n/n/t程序已正常退出,拜拜");
flag = false;
} else {
System.out.print("/n/t您输入的代号不是程序的代号,请确定后重新来过!/n/n");
yunsuan();
}
}
public void add() {
System.out.print("/n/t对两复数comp1和comp2作加法运算后的结果为:/n");
System.out.print("/t/tcomp = comp1 + comp2 = (a1 + a2)+ (b1+b2)*i = ("
+ a1 + " + " + a2 + ")+" + "(" + b1 + " + " + b2 + ")*i/n");
System.out.print("/t所以,加法运算的结果为:comp = " + (a1 + a2) + " + "
+ (b1 + b2) + "*i/n/n");
System.out.print("/t此次的加法运算到此已完成!/n/n");
}
public void subtract() {
System.out.print("/n/t对两复数comp1和comp2作减法运算后的结果为:/n");
System.out
.print("/t/tcomp = comp1 - comp2 = (a1 - a2)+ (b1 - b2)*i = ("
+ a1 + " - " + a2 + ")+" + "(" + b1 + " - " + b2
+ ")*i/n");
System.out.print("/t所以,减法运算的结果为:comp = " + (a1 - a2) + " + "
+ (b1 - b2) + "*i/n/n");
System.out.print("/t此次的减法运算到此已完成!/n/n");
}
public void multiply() {
System.out.print("/n/t对两复数comp1和comp2作乘法运算后的结果为:/n");
System.out
.print("/t/tcomp = comp1 * comp2 = (a1*a2 - b1*b2)+ (a1*b2 + a2*b1)*i = ("
+ a1 + "+" + a2 + ")+" + "(" + b1 + "+" + b2 + ")*i/n");
System.out.print("/t所以,加法运算的结果为:comp = " + (a1 * a2 - b1 * b2) + " + "
+ (a1 * b2 + a2 * b1) + "*i/n/n");
System.out.print("/t此次的乘法运算到此已完成!/n/n");
}
public void divide() {
System.out.print("/n/t是comp1除以comp2还是comp2除以comp1,请做出选择!/n/n");
System.out
.print("/t*****************************************************************/n");
System.out.print("/t/t/t#/t 1---comp1除以comp2/t #/n");
System.out.print("/t/t/t#/t 2---comp2除以comp1/t #/n");
System.out
.print("/t*****************************************************************/n/n");
System.out.print("/t请输入操作的代号:");
param = _param.nextInt();
if (param == 1) {
System.out.print("/n/t对两复数comp1和comp2作除法运算后的结果为:/n");
System.out
.print("/t/tcomp = comp1 * comp2 = (a1*a2 + b1*b2)/(a2*a2 + b2*b2)+ ((a2*b1 - a1*b2)/(a2*a2 + b2*b2))*i = ("
+ (a1 * a2 + b1 * b2)
/ (a2 * a2 + b2 * b2)
+ ")+"
+ "("
+ (a2 * b1 - a1 * b2)
/ (a2 * a2 + b2 * b2)
+ ")*i/n");
System.out.print("/t所以,除法运算的结果为:comp = " + (a1 * a2 - b1 * b2)
/ (a2 * a2 + b2 * b2) + " + " + (a2 * b1 - a1 * b2)
/ (a2 * a2 + b2 * b2) + "*i/n/n");
System.out.print("/t此次的除法运算到此已完成!/n/n");
} else if (param == 2) {
System.out.print("/n/t对两复数comp2和comp1作加除运算后的结果为:/n");
System.out
.print("/t/tcomp = comp1 * comp2 = (a1*a2 + b1*b2)/(a1*a1 + b1*b1)+ ((a1*b2 - a2*b1 )/(a1*a1 + b1*b1))*i = ("
+ (a1 * a2 + b1 * b2)
/ (a1 * a1 + b1 * b1)
+ ") + "
+ "("
+ (a1 * b2 - a2 * b1)
/ (a1 * a1 + b1 * b1) + ")*i/n");
System.out.print("/t所以,加法运算的结果为:comp = " + (a1 * a2 - b1 * b2)
/ (a2 * a2 + b2 * b2) + " + " + (a2 * b1 - a1 * b2)
/ (a2 * a2 + b2 * b2) + "*i/n/n");
System.out.print("/t此次的除法运算到此已完成!/n/n");
}
}
public static void main(String[] args) {
Complex complex = new Complex();
while (complex.flag) {
try {
complex.yunsuan();
} catch (InputMismatchException ime) {
System.out.print("/n/t您的输入与要求不一致,请确定后重新来过!/n/n");
}
}
}
}