public class Test {
創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、網(wǎng)站建設、外貿營銷網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務集寧,10余年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108
public static void main(String[] args) {
MyRectangle rec = new MyRectangle(3, 5);
MyRectangle square = new MySquare(4);
System.out.println(rec.toString());
System.out.println(square.toString());
}
}
class MyRectangle{
protected double width;
protected double length;
public MyRectangle(double length, double width){
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea(){
return this.width * this.length;
}
public String toString(){
return "長方形的長為:" + length + ", 寬: " + width + ", 面積為:" + getArea();
}
}
class MySquare extends MyRectangle{
public MySquare(double length){
super(length, length);
}
public double getArea(){
return Math.pow(super.width, 2);
}
public String toString(){
return "正方形邊長為: " + super.length + ", 面積為: " + getArea();
}
}
----------測試
長方形的長為:3.0, 寬: 5.0, 面積為:15.0
正方形邊長為: 4.0, 面積為: 16.0
答案放在下面地址:
順便給我踩下空間^-^
本小牛聽了那六級大牛的建議后做了如下修改
第1題:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class wangquanshu extends Applet implements ActionListener
{
int aa[]={6,28,496,8128,130816,2096128,33550336},bb[]={0,0,0,0,0,0,0};
//6=1+2+3
//28=1+2+4+7+14
//496=1+2+4+8+16+31+62+124+248
//8128=1+2+4+8+16+32+64+127+254+508+1016+2032+4064
//在1到40000000這么多數(shù)里,只有七個完全數(shù),它們是:6,28,496,8128,130816,2096128,33550336
//java里int 32位 -2^31~2^31-1(大概20億,應夠了)
int j,q;
Label lab1;
TextField text1;
Label lab2;
TextField text2;
Button but;
public void init()
{ lab1=new Label("請輸入上下限:");
text1=new TextField(8);
lab2=new Label(" - ");
text2=new TextField(8);
but=new Button("確定");
add(lab1);
add(text1);
add(lab2);
add(text2);
add(but);
but.addActionListener(this);
}
public void paint(Graphics g)
{int m=1; //用于控制輸出自由數(shù)的位置
for(int n=0;n7;n++)
{g.drawString(" "+bb[n],10,50+m*20);
m++;
}
}
public void actionPerformed(ActionEvent e)
{int s=0;
int a=Integer.parseInt(text1.getText());
int b=Integer.parseInt(text2.getText());
for(j=0;j7;j++)
{if(aa[j]aaa[j]b)
bb[j]=aa[j];
}
repaint();
}
}
第2題:
import java.io.*;
import java.text.DecimalFormat;
public class e
{public static void main(String args[])
{double ee=0;
for(int i=0;i=11;i++)
{ee=ee+1/re(i);
}
DecimalFormat a=new DecimalFormat("0.00000000");
System.out.println(a.format(ee));
}
static double re(int n)
{if(n==0||n==1) return 1;
else return n*re(n-1);
}
}
下面是WINDOW界面的代碼,用JAVA SWING寫的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlusWhile {
//主面板
private JFrame mainTop = null;
private JPanel inPanel = null; //輸入面板
private JPanel outPanel = null;//輸出面板
private JButton count = null; //按鈕
private JTextField inText = null; // 輸入文本框
private JTextField result = null;//結果文本框
private JLabel resultOut = null;//輸出提示
private JLabel remindIn = null;//輸入提示
//初始化方法
private void init() {
//組件實例化
mainTop = new JFrame("階乘計算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("計算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("計算的結果:");
remindIn = new JLabel("請輸入要計算的正整數(shù):");
//添加輸入組件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加輸出組件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//設置布局樣式
mainTop.setLayout(new BorderLayout());
//主面板添加組件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//設置監(jiān)聽
count.addActionListener(new ButtonListener());
//設置屏顯位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//設置關閉事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//監(jiān)聽類
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若輸入為非數(shù)字,提示錯誤
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//輸入數(shù)值小于0,提示錯誤
if(inputNum 0) {
inText.setText("請輸入正確的數(shù)值!");
result.setText("");
}
else if(inputNum == 0) {//輸入值為0處理
result.setText("1");
}
else {//輸入值為正整數(shù),計算階乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的繼續(xù)補充,呵呵。 加點兒分不。