面向对象案例
1.分数的运算
package Day12_08_01;
/**
* 构建一个分数类
* @author YY
*/
public class Fraction
{
private int num;
private int den;
/**
* 构造器
* @param num 分子
* @param den 分母
*/
public Fraction(int num, int den)
{
this.num = num;
this.den = den;
this.normalize();
this.simplify();
}
/**
* 构造器:此构造器将小数转化为分数的形式
* @param val
*/
public Fraction(double val){
//在构造器里可以用this去调用其他已有的构造器,必须写在此构造器的第一行
this((int) (val*1000),1000);
}
/**
* 加法运算
* @param other 传进来的分数
* @return 返回一个进行加法运算后的新分数
*/
public Fraction add(Fraction other)
{
return new Fraction(num * other.den + other.num * den, den * other.den);
}
/**
* 减法运算
* @param other 传进来的分数
* @return 返回一个进行减法运算后的新分数
*/
public Fraction sub(Fraction other)
{
return new Fraction(num * other.den - other.num * den, den * other.den);
}
/**
* 乘法运算
* @param other 传进来的分数
* @return 返回一个进行乘法运算后的新分数
*/
public Fraction mul(Fraction other)
{
return new Fraction(num * other.num, den * other.den);
}
/**
* 除法运算
* @param other 传进来的分数
* @return 返回一个进行除法运算后的新分数
*/
public Fraction div(Fraction other)
{
return new Fraction(num * other.den, den * other.num);
}
/**
* 此方法将分子分母有负号的时候把负号约掉
*/
public void normalize()
{
if (den < 0)
{
this.num = -num;
this.den = -den;
}
}
/**
* 化简分数
*/
public void simplify()
{
if (num != 0)
{
int x = Math.abs(num);
int y = Math.abs(den);
int factor = gcd(x, y);
if (factor > 1)
{
num /= factor;
den /= factor;
}
}
}
@Override
//将结果以字符串的形式输出
public String toString()
{
if (num == 0)
{
return "0";
} else if (den == 1)
{
return "" + num;
} else
{
return num + "/" + den;
}
}
// 私有的方法一般写在类的最后
// 递归调用(自己调用自己的方法)
//找最大公约数
private int gcd(int x, int y)
{
if (x > y)
{
return gcd(y, x);
} else if (y % x != 0)
{
return gcd(y % x, x);
} else
{
return x;
}
}
}```
package Day12_08_01;
public class FractionTest
{
public static void main(String[] args)
{
Fraction fraction1=new Fraction(6, 4);
Fraction fraction2=new Fraction(-10, 5);
//如果没有tostring方法,此方式输出的是fraction1的哈希码
System.out.println(fraction1);
System.out.println(fraction2);
System.out.println(fraction1.add(fraction2));
System.out.println(fraction1.sub(fraction2));
System.out.println(fraction1.mul(fraction2));
System.out.println(fraction1.div(fraction2));
}
}
###2.模拟扑克牌游戏
- 枚举的应用:类的某个变量只有有限种形式
public enum Suite
{
SPADE,HEART,CLUB,DIAMOND,TRUMP
}
package Day12_08_01;
/**
- 一张牌
- @author YY
*/
public class Card
{
private int face;
private Suite suite;// 枚举的定义
public Card(int face, Suite suite)
{
this.face = face;
this.suite = suite;
}
@Override
public String toString()
{
String str = "";
switch (suite)
{
case SPADE:
str = "♠";
break;
case HEART:
str = "♥";
break;
case CLUB:
str = "♣";
break;
case DIAMOND:
str = "♦";
break;
case TRUMP:
str = "";
break;
}
switch (face)
{
case 1:
str += "A";
break;
case 11:
str += "J";
break;
case 12:
str += "Q";
break;
case 13:
str += "K";
break;
case 15:
str += "小王";
break;
case 16:
str += "大王";
break;
default:
str += face;
break;
}
return str;
}
}
package Day12_08_01;
/**
- 扑克
- @author YY
*/
// 描述扑克
public class Poker
{
// 创建了一个容量为54的数组
private Card[] cardsArray = new Card[54];
// 记录数组的下标
private int currentIndex;
/**
* 构造器
*/
public Poker()
{
Suite[] suites =
{ Suite.SPADE, Suite.HEART, Suite.CLUB, Suite.DIAMOND };
for (int i = 0; i < suites.length; i++)
{
for (int j = 0; j < 13; j++)
{
Card card = new Card(j + 1, suites[i]);
cardsArray[i * 13 + j] = card;
}
}
cardsArray[52] = new Card(15, Suite.TRUMP);
cardsArray[53] = new Card(16, Suite.TRUMP);
}
/**
* 发牌
*
* @return 发出去的牌
*/
// 创建一个发牌的方法
public Card deal()
{
return cardsArray[currentIndex++];
}
/**
* 判断发牌是否发完
*
* @return 是or否
*/
public boolean hasMoreCards()
{
return currentIndex < cardsArray.length;
}
/**
* 洗牌
*/
public void shuffle()
{
for (int i = 0, len = cardsArray.length; i < len; i++)
{
int randomInde = (int) (Math.random() * len);
Card temp=cardsArray[i];
cardsArray[i]=cardsArray[randomInde];
cardsArray[randomInde]=temp;
}
currentIndex=0;//洗完牌之后重新发牌
}
}
###3.Creps游戏的面向对象写法
- 构造器可直接调用游戏重置的方法(reset)来初始化变量
package Day12_08_02;
/**
- 猜数机器人
- @author YY
*/
public class Robot
{
private int answer;
private String str;
private int count;
/**
* 构造器
*/
public Robot()
{
this.reset();
}
public String getHint()
{
return str;
}
public void setHint(String hint)
{
this.str = hint;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
/**
* 判断是否猜对
*
* @param thyAnswer
* 玩家猜的数
* @return
*/
public boolean judge(int thyAnswer)
{
count++;
if (thyAnswer == answer)
{
str = "猜对了";
return true;
} else if (thyAnswer > answer)
{
str = "小一点";
} else
{
str = "大一点";
}
return false;
}
/**
* 重置游戏
*/
public void reset()
{
this.answer = (int) (Math.random() * 100 + 1);
this.count = 0;
this.str = "";
}
}
package Day12_08_02;
import java.util.Scanner;
public class GussNumberGame
{
public static void main(String[] args)
{
Robot robot = new Robot();
Scanner input = new Scanner(System.in);
boolean isCorrect = false;
do
{
System.out.println("输入你猜的数:");
int thyAnswer = input.nextInt();
isCorrect = robot.judge(thyAnswer);
System.out.println(robot.getHint());
} while (!isCorrect);
if (robot.getCount() > 7)
{
System.out.println("智商捉急!");
}
input.close();
}
}