(1.设计并编写一个分数类(Rational),并比较分数的大小。
要求:
1. 具有构造函数Rational(int numerator, int denominator),其中numberator表示分子,而denominator表示分母。
2. 实现Comparable接口,并实现比较分数大小的功能。
3. 设计并实现InverseComparator类(实现Comparator接口),比较两个分数的倒数的大小。
4. 随机产生100个分数,然后分别对分数本身和其倒数进行排序。
提示:
1. 不能用分子除与分母的结果来比较大小,这样不精确。
2. 分母为零时分数为无穷大,两个无穷大的数相等,无穷大的数大于一切其他数。
3. 正无穷大大于负无穷大,两个负无穷大的分数一样大,两个正无穷大的分数一样大。
4. 如果分子和分母同时为零,则分数没有意义,此时分数构造函数需要抛出IllegalRationalException异常。
Rationnal.java
package shiyan6_1;
public class Rational implementsComparable<Rational>{
private int numberator,denominator;
public Rational(int numberator,int denominator) {
this.numberator=numberator;
this.denominator=denominator;
}
public int getNumberator() {
return numberator;
}
public int getDenominator() {
return denominator;
}
public int GCD(int n,int m){
int max,min;
max=(n>m)?n:m;
min=(n>m)?n:m;
if(max%min==0){
return min;
}else{
returnGCD(min, max%min);
}
}
public int LCM(int a,int b){
return a*b/GCD(a,b);
}
public int compareTo(Rational o) {
if(this.denominator==0&&o.denominator!=0){
return 1;
}else if(this.denominator!=0&&o.denominator==0){
return-1;
}else if(this.denominator==0&&o.denominator==0){
return 0;
}
else{
int lcm = LCM(this.denominator, o.denominator);
int lcm1 = lcm/this.denominator;
int lcm2 = lcm/o.denominator;
int numberator1=this.numberator*lcm1;
int numberator2 =o.numberator*lcm2;
if(numberator1>numberator2){
return 1;
}else if(numberator1<numberator2){
return-1;
}else{
return 0;
}
}
}
}
InverseComparator.java
package shiyan6_1;
public class InverseComparator implements Comparable<InverseComparator>{
private int numberator,denominator;
publicInverseComparator(int numberator,int denominator) {
this.numberator=numberator;
this.denominator=denominator;
}
public int getNumberator() {
return numberator;
}
public int getDenominator() {
return denominator;
}
public int GCD(int n,int m){
int max,min;
max=(n>m)?n:m;
min=(n>m)?n:m;
if(max%min==0){
return min;
}else{
returnGCD(min, max%min);
}
}
public int LCM(int a,int b){
return a*b/GCD(a,b);
}
public int compareTo(InverseComparator o) {
if(this.denominator==0&&o.denominator!=0){
return1;
}else if(this.denominator!=0&&o.denominator==0){
return-1;
}else if(this.denominator==0&&o.denominator==0){
return0;
}
else{
int lcm = LCM(this.denominator, o.denominator);
int lcm1 = lcm/this.denominator;
int lcm2 = lcm/o.denominator;
int numberator1=this.numberator*lcm1;
int numberator2 =o.numberator*lcm2;
if(numberator1>numberator2){
return1;
}else if(numberator1<numberator2){
return-1;
}else{
return0;
}
}
}
}
Test.java
package shiyan6_1;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Randomrand = new Random();
int x = 0,y =0;
List<Rational>list = new ArrayList<Rational>();
List<Rational>list2 = new ArrayList<Rational>();
for(int i=0;i<10;i++){
try{
x=rand.nextInt(500);
y=rand.nextInt(500);
if(x==0&&y==0){
throw newIllegalThreadStateException();
}
}catch(IllegalThreadStateExceptione) {
i--;
System.out.println("分子和分母不能同时为0!");
}
list.add(newRational(x,y));
list2.add(newRational(y,x));
}
System.out.println("排序前 分子分母都是随机生成500以内的正整数(含0):");
for (Rational rational : list){
System.out.print(rational.getNumberator()+"/"+rational.getDenominator()+" ");
}
System.out.println("");
for (Rational rational : list2){
System.out.print(rational.getNumberator()+"/"+rational.getDenominator()+" ");
}
System.out.println("");
System.out.println("排序后从小到大:");
Collections.sort(list);
Collections.sort(list2);
for (Rational rational : list){
System.out.print(rational.getNumberator()+"/"+rational.getDenominator()+" ");
}
System.out.println("");
for (Rational rational : list2){
System.out.print(rational.getNumberator()+"/"+rational.getDenominator()+" ");
}
}
}
(2.编写一个数字彩票游戏(Lottery类)。
要求:
1. 输入彩票中需要的数字个数n(必须在3到9之间)。然后程序随机产生n个互不相等个位数。
2. 输入彩票中需要匹配的最小数字数m(必须在1到n之间)。
3. 通过构造函数Lottery(n, m)构造彩票游戏对象。
4. 提示用户输入n个互不相等的猜测的数字。
5. 使用addGuess()方法往游戏类中添加用户猜测的数字。
6. 使用getAward()方法来获取用户中奖的金额。
奖金的计算:
1. 如果用户所猜测的n个数和系统生成的n个数完全相同,且顺序也完全相同,则奖金为100*8n。
2. 如果用户所猜测的n个数和系统生成的n个数完全相同,但是顺序不相同,则奖金为6n。
2. 如果用户所猜测的n个数中,有p
(0<p<n)个数与系统生成的一致,则奖金为100*2p。
提示:
1. 使用equals()方法可以带顺序比较两个集合。
2. 使用containsAll()方法可以比较两个集合包含关系,不管顺序。
3. 产生n选m组合的过程比较困难,请参考附件中的Combination.java文件。其中Combination. getAllCombinations(collection,
m)方法返回含有n个元素的集合collection中选取m个元素所组成的所有组合。
Lottery.java
package shiyan6_2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.List;
public class Lottery {
private int n=3,m=1,flag=0;
public int getFlag() {
return flag;
}
public void ListFlag(int flag) {
this.flag = flag;
}
public int getN() {
return n;
}
public void ListN(int n) {
this.n = n;
}
public int getM() {
return m;
}
public void ListM(int m) {
this.m = m;
}
private List<Integer> list = new ArrayList<Integer>();
private Set<Integer> set = new HashSet<Integer>();
private List<Integer> s = new ArrayList<Integer>();
private Set<Integer> s1 = new HashSet<Integer>();
public Boolean addGuess(int e){
if(s1.add(e)){
s.add(e);
return true;
}else{
return false;
}
}
public double getAward(){
if(list.equals(s)){
System.out.println("猜对了"+this.n+"个且顺序一样,祝贺你!");
returnMath.pow(8,this.n)*100;
}else if(list.containsAll(s)){
System.out.println("猜对了"+this.n+"个但顺序不一样!");
returnMath.pow(6,this.n);
}else{
if(getAllCombinations(list)>=this.m){
returnMath.pow(2,getAllCombinations(list))*100;
}else{
System.out.println("很遗憾,你不满足最低要求个数~");
return 0;
}
}
}
private int getAllCombinations(List<Integer> list2) {
int sum=0;
int nCnt = list.size();
int nBit = 1<<nCnt;
for (int i =1; i<= nBit; i++) {
Listtemp = new ArrayList<Integer>();
for (int j = 0; j< nCnt; j++) {
if ((i<< (31 - j))>> 31 == -1) {
// System.out.print(list.get(j)+" ");
temp.add(list.get(j));
}
}
if(s.containsAll(temp)){
sum=Math.max(sum, temp.size());
}
//System.out.println("");
}
System.out.println("猜对了"+sum+"个");
return sum;
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean containsAlls(Collection<?> c) {
return s.containsAll(c);
}
public Lottery(int n,int m) {
this.m=m;
this.n=n;
Randomrand = new Random();
for(int i=0;i<n;i++){
int x = rand.nextInt(100);
if(set.add(x)){
list.add(x);
}else{
i--;
}
}
}
public void printList(){
System.out.println(list);
System.out.println(s);
}
public boolean equals(Object o) {
return list.equals(o);
}
}
Test.java
package shiyan6_2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scannerinput = new Scanner(System.in);
System.out.println("输入彩票中随机产生的数字个数n(必须在3到9之间)。");
int n = input.nextInt();
System.out.println("输入彩票中需要匹配的最小数字数m(必须在1到n之间)。");
int m = input.nextInt();
Lotterylottery = new Lottery(n, m);
//lottery.printList();
System.out.println("请用户输入"+n+"个互不相等的猜测的数字");
for(int i=0;i<n;i++){
System.out.println("第"+i+"个");
int e = input.nextInt();
if(lottery.addGuess(e)){
}else{
System.out.println("您已经猜过这个数了,请重新输入。");
i--;
}
}
System.out.println("您的中奖结果为:");
System.out.println("奖金为:"+lottery.getAward()+"RMB");
System.out.println("彩票的结果和你的结果:");
lottery.printList();
input.close();
}
}