写一个正八边形类Octagon,要求从课本的GeometricObject类继承,并实现 Comparable和 Cloneable接口。
写一个测试类,测试Octagon类的所有可用方法。
实现Octagon类,从GeometricObject类中继承,并覆盖Clone和Compareto方法。在主类中测试clone和comparato方法。
GeometricObject类
package com.company;
import java.util.Date;
public abstract class GeometricObject {
private String color = "white";
private boolean filled = false;
private Date dateCreated;
protected GeometricObject()
{
this.dateCreated = new Date();
}
protected GeometricObject(String color, boolean filled)
{
this.dateCreated = new Date();
this.color = color;
this.filled = filled;
}
public String getColor()
{
return this.color;
}
public void setColor(String color)
{
this.color = color;
}
public boolean isFilled()
{
return this.filled;
}
public void setFilled(boolean filled)
{
this.filled = filled;
}
public Date getDateCreated()
{
return this.dateCreated;
}
@Override
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
Octagon类
package com.company;
public class Octagon extends GeometricObject implements Cloneable,Comparable<Octagon>{
double side;
public Octagon(double side)
{
this.side = side;
}
public Octagon(double side, String color, boolean filled)
{
this.side = side;
setColor(color);
setFilled(filled);
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double getArea() {
return (2 + 4 / Math.sqrt(2)) * side * side;
}
@Override
public double getPerimeter() {
return 8 * side;
}
@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
@Override
public int compareTo(Octagon o)
{
if(this.side > o.side)
return 1;
else if(this.side < o.side)
return -1;
else
return 0;
}
}
测试类
package com.company;
public class Main {
public static void main(String[] args) {
Octagon o = new Octagon(3);
System.out.println("Area: " + o.getArea());
System.out.println("o's Perimeter: " + o.getPerimeter());
Octagon o1 = new Octagon(2);
try
{
o1 = (Octagon)o.clone();
System.out.println("o1's Perimiter : "+o1.getPerimeter());
}
catch(CloneNotSupportedException ex)
{
System.out.println("CloneNotSupportedException");
}
System.out.println(o.compareTo(o1));
}
}
改写课本Listing 13.13的分数类Rational,将分子和分母从类型long改为BigInteger,新版分数类名为MyRational。
写一个测试类,测试MyRational。
源代码中的long类型数据全部换成BigInteger,使用BigInteger的静态方法进行四则运算,按照错误提示按部就班修改代码,熟悉了BigInterger的使用方法,加深了对继承和接口的理解。
MyRational类
package com.company;
import java.math.BigInteger;
public class MyRational extends Number implements Comparable {
private BigInteger numerator=new BigInteger("0");
private BigInteger denominator=new BigInteger("1");
public MyRational()
{
this(new BigInteger("0"), new BigInteger("1"));
}
public MyRational(BigInteger numerator, BigInteger denominator)
{
BigInteger gcd=gcd(numerator,denominator);
this.numerator = ((new BigInteger(denominator.compareTo(BigInteger.ZERO) + "")).multiply(numerator)).divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}
private static BigInteger gcd(BigInteger n, BigInteger d)
{
BigInteger n1=n.abs();
BigInteger n2=d.abs();
BigInteger gcd=new BigInteger("1");
for(BigInteger k = BigInteger.ONE; k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0; k = k.add(BigInteger.ONE))
{
if(n1.mod(k).compareTo(BigInteger.ZERO) == 0 && n2.mod(k).compareTo(BigInteger.ZERO) == 0)
gcd = k;
}
return gcd;
}
public BigInteger getNumerator()
{
return numerator;
}
public BigInteger getDenominator()
{
return denominator;
}
public MyRational add(MyRational secondMyRational)
{
BigInteger n = numerator.multiply(secondMyRational.getDenominator()).add(
denominator.multiply(secondMyRational.getNumerator()));
BigInteger d = denominator.multiply(secondMyRational.getDenominator());
return new MyRational(n, d);
}
public MyRational subtract(MyRational secondMyRational)
{
BigInteger n = numerator.multiply(secondMyRational.getDenominator()).subtract(
denominator.multiply(secondMyRational.getNumerator()));
BigInteger d = denominator.multiply(secondMyRational.getDenominator());
return new MyRational(n, d);
}
public MyRational multiply(MyRational secondMyRational)
{
BigInteger n = numerator.multiply(secondMyRational.getNumerator());
BigInteger d = denominator.multiply(secondMyRational.getDenominator());
return new MyRational(n, d);
}
public MyRational divide(MyRational secondMyRational)
{
BigInteger n = numerator.multiply(secondMyRational.getDenominator());
BigInteger d = denominator.multiply(secondMyRational.getNumerator());
return new MyRational(n, d);
}
public String toString()
{
if(denominator.compareTo(BigInteger.ONE) == 0)
{
return numerator+"";
}else
return numerator+"/"+denominator;
}
public boolean equals(Object parm1)
{
if((this.subtract((MyRational)parm1)).getNumerator().equals(0))
return true;
else
return false;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if((this.subtract((MyRational)o)).getNumerator().compareTo(BigInteger.ZERO) == 1)
return 1;
else if((this.subtract((MyRational)o)).getNumerator().compareTo(BigInteger.ZERO) == -1)
return -1;
else
return 0;
}
@Override
public int intValue() {
// TODO Auto-generated method stub
return (int)doubleValue();
}
@Override
public long longValue() {
// TODO Auto-generated method stub
return (long)doubleValue();
}
@Override
public float floatValue() {
// TODO Auto-generated method stub
return (float)doubleValue();
}
@Override
public double doubleValue() {
// TODO Auto-generated method stub
return numerator.doubleValue() * 1.0 / denominator.doubleValue();
}
}
Test类
package com.company;
import com.sun.jmx.snmp.SnmpTooBigException;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// write your code here
MyRational r1=new MyRational(new BigInteger("4"),new BigInteger("2"));
MyRational r2=new MyRational(new BigInteger("2"),new BigInteger("3"));
System.out.println(r1+"+"+r2+"="+r1.add(r2));
System.out.println(r1+"-"+r2+"="+r1.subtract(r2));
System.out.println(r1+"*"+r2+"="+r1.multiply(r2));
System.out.println(r1+"/"+r2+"="+r1.divide(r2));
System.out.println(r2+" is "+r2.doubleValue());
}
}
编程实现程序每次启动后,在界面上随机显示54张扑克牌中的4张,并在其下方显示花色名(如图)。扑克牌图片已打包到cards.jar文件中,命名规则见下一页。
扑克牌命名规则
友情赠送:从jar中读取指定图片的方法
import java.util.jar.;
import java.io.;
import javafx.scene.image.*;
public static ImageView readImageFromJar(String jarname, String picname) {
ImageView imageView = null;
try {
JarFile jarFile = new JarFile(jarname);
JarEntry entry = jarFile.getJarEntry(picname);
InputStream in = jarFile.getInputStream(entry);
imageView = new ImageView(new Image(in));
in.close();
jarFile.close();
} catch (IOException e) {
System.err.println(“read file error.”);
}
return imageView;
}
读方块3图片的使用示例:
ImageView imageView = readImageFromJar(“cards.jar”,“cards/29.png”);
生成四个随机数,读取jar里面的图片内容,使用getName方法获取扑克牌的名字。采用GridPane放置扑克牌。
//package Conv.company;
//
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import java.util.HashMap;
import java.util.jar.*;
import java.io.*;
import javafx.scene.image.*;
import javafx.scene.text.Text;
public class Card extends Application {
public static ImageView readImageFromJar(String jarname, String picname) {
ImageView imageView = null;
try {
JarFile jarFile = new JarFile(jarname);
JarEntry entry = jarFile.getJarEntry(picname);
InputStream in = jarFile.getInputStream(entry);
imageView = new ImageView(new Image(in));
in.close();
jarFile.close();
} catch (IOException e) {
System.err.println("read file error.");
}
return imageView;
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane pane = new GridPane();
pane.setPadding(new Insets(5,5,5,5));
int r1=(int)((Math.random()*100)%54);
int r2=(int)((Math.random()*100)%54);
int r3=(int)((Math.random()*100)%54);
int r4=(int)((Math.random()*100)%54);
String card1=new String("cards/"+String.valueOf(r1)+".png");
String card2=new String("cards/"+String.valueOf(r2)+".png");
String card3=new String("cards/"+String.valueOf(r3)+".png");
String card4=new String("cards/"+String.valueOf(r4)+".png");
ImageView imageView1 = readImageFromJar("cards.jar",card1);
ImageView imageView2 = readImageFromJar("cards.jar",card2);
ImageView imageView3 = readImageFromJar("cards.jar",card3);
ImageView imageView4 = readImageFromJar("cards.jar",card4);
String name1=getName(r1);
String name2=getName(r2);
String name3=getName(r3);
String name4=getName(r4);
Text t1=new Text(name1);
Text t2=new Text(name2);
Text t3=new Text(name3);
Text t4=new Text(name4);
pane.add(imageView1,0,0);
pane.add(t1,0,1);
pane.add(imageView2,1,0);
pane.add(t2,1,1);
pane.add(imageView3,2,0);
pane.add(t3,2,1);
pane.add(imageView4,3,0);
pane.add(t4,3,1);
Scene scene=new Scene(pane);
primaryStage.setTitle("Mycard");
primaryStage.setScene(scene);
primaryStage.show();
}
public String getName(int r){
String name ="";
if(r>=1 && r<=13)
name+="黑桃";
else if(r>=14 && r<=26)
name+="红桃";
else if(r>=27 && r<=39)
name+="方块";
else if(r>=40 && r<=52)
name+="梅花";
else if(r==53 || r==54) {
name += "大王";
return name;
}
HashMap<Integer,String> map = new HashMap<>();
map.put(1, "A"); map.put(2, "2");
map.put(3, "3"); map.put(4, "4");
map.put(5, "5"); map.put(6, "6");
map.put(7, "7"); map.put(8, "8");
map.put(9, "9"); map.put(10, "10");
map.put(11, "J"); map.put(12, "Q");
map.put(0, "K");
name+=map.get(r%13);
return name;
}
}
编程设计如图所示的界面。要求最后的界面布局尽可能接近效果图,不需要实现按钮功能。
注意有几个按钮的背景色有所不同。
将窗口进行分块布局,总体采用边界窗格BorderPane,上边是文字部分,下边按钮采用网格窗格GridPane。
package Conv.company;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class calculator extends Application {
@Override
public void start(Stage primaryStage)
{
BorderPane pane = new BorderPane();
TextField textField = new TextField("0");
textField.setAlignment(Pos.BOTTOM_RIGHT);
textField.setStyle("-fx-font: 22 Consolas;");
pane.setTop(textField);
pane.setBottom(getGrid());
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("计算器");
primaryStage.show();
}
private GridPane getGrid()
{
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
pane.add(GetButton("MC", 40, 20), 0, 0);
pane.add(GetButton("MR", 40, 20), 1, 0);
pane.add(GetButton("MS", 40, 20), 2, 0);
pane.add(GetButton("M+", 40, 20), 3, 0);
pane.add(GetButton("M-", 40, 20), 4, 0);
pane.add(GetButton("←", 40, 20), 0, 1);
pane.add(GetButton("CE", 40, 20), 1, 1);
pane.add(GetButton("C", 40, 20), 2, 1);
pane.add(GetButton("±", 40, 20), 3, 1);
pane.add(GetButton("√", 40, 20), 4, 1);
Button b7 = GetButton("7", 40, 20);
b7.setStyle("-fx-base: #bbffff");
pane.add(b7, 0, 2);
Button b8 = GetButton("8", 40, 20);
b8.setStyle("-fx-base: #bbffff");
pane.add(b8, 1, 2);
Button b9 = GetButton("9", 40, 20);
b9.setStyle("-fx-base: #bbffff");
pane.add(b9, 2, 2);
Button b4 = GetButton("4", 40, 20);
b4.setStyle("-fx-base: #bbffff");
pane.add(b4, 0, 3);
Button b5 = GetButton("5", 40, 20);
b5.setStyle("-fx-base: #bbffff");
pane.add(b5, 1, 3);
Button b6 = GetButton("6", 40, 20);
b6.setStyle("-fx-base: #bbffff");
pane.add(b6, 2, 3);
Button b1 = GetButton("1", 40, 20);
b1.setStyle("-fx-base: #bbffff");
pane.add(b1, 0, 4);
Button b2 = GetButton("2", 40, 20);
b2.setStyle("-fx-base: #bbffff");
pane.add(b2, 1, 4);
Button b3 = GetButton("3", 40, 20);
b3.setStyle("-fx-base: #bbffff");
pane.add(b3, 2, 4);
Button b0 = GetButton("0", 85, 20);
b0.setStyle("-fx-base: #bbffff");
GridPane.setValignment(b0, VPos.TOP);
pane.add(b0, 0, 5, 2, 5);
pane.add(GetButton("/", 40, 20), 3, 2);
pane.add(GetButton("%", 40, 20), 4, 2);
pane.add(GetButton("*", 40, 20), 3, 3);
pane.add(GetButton("1/x", 40, 20), 4, 3);
pane.add(GetButton("-", 40, 20), 3, 4);
pane.add(GetButton("=", 40, 65), 4, 4, 4, 5);
pane.add(GetButton(".", 40, 20), 2, 5);
pane.add(GetButton("+", 40, 20), 3, 5);
return pane;
}
private Button GetButton(String text,int width,int height){
Button b = new Button(text);
b.setPrefSize(width, height);
return b;
}
}