二、实验项目内容(实验题目)
编写代码,掌握javabean的用法。【参考课本 上机实验 5.5.1 】
三、源代码以及执行结果截图:
源代码:
Fraction.java
package sea.water;
public class Fraction {
public double number;
public long numerator;
public long denominator;
public double getNumber() {
String numberString = String.valueOf(number);
String xiaoshuPart = numberString.substring(numberString.indexOf(".")+1);
return Double.parseDouble("0."+xiaoshuPart);
}
public long getNumerator() {
return numerator;
}
public long getDenominator() {
return denominator;
}
public void setNumber(double number) {
this.number = number;
String numerString = String.valueOf(number);
String xiaoshuPart = numerString.substring(numerString.indexOf(".")+1);
int m = xiaoshuPart.length();
numerator = Long.parseLong(xiaoshuPart);
denominator = (long)Math.pow(10, m);
long greatCommonDivisor = f(numerator,denominator);
numerator = numerator/greatCommonDivisor;
denominator = denominator/greatCommonDivisor;
}
private long f(long a, long b) {
if(a==0) return 1;
if(a<b) {
long c = a;
a = b;
b = c;
}
long r = a % b;
while(r!=0) {
a = b;
b = r;
r = a%b;
}
return b;
}
}
getFraction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="cyan">
<p style="font-family: 宋体;font-size: 36;color: red;">
<jsp:useBean id="fraction" class="sea.water.Fraction" scope="request" />
<jsp:setProperty name="fraction" property="number" param="number"/>
<jsp:getProperty name="fraction" property="number"/>
表示成分数是:<br>
<jsp:getProperty name="fraction" property="numerator"/>/
<jsp:getProperty name="fraction" property="denominator"/>
</p>
</body>
</html>
inputNumber.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="cyan">
<p style="font-family: 宋体;font-size: 36;color: red;">
<jsp:useBean id="fraction" class="sea.water.Fraction" scope="request" />
<jsp:setProperty name="fraction" property="number" param="number"/>
<jsp:getProperty name="fraction" property="number"/>
表示成分数是:<br>
<jsp:getProperty name="fraction" property="numerator"/>/
<jsp:getProperty name="fraction" property="denominator"/>
</p>
</body>
</html>
效果图: