Java习题集(第三版)(7)

2019-04-02 09:09

{

上底=x; 下底=y; 高=h; }

double 计算面积() {

return (上底+下底)*高/2.0; } }

public class Class1 {

public static void main (String[] args) {

梯形 lader=new 梯形(3.0,10.0,20);

System.out.println(\的面积:\计算面积()); try {

System.in.read(); }catch(Exception e){} } }

4、写一个三角形类,能计算三角形的面积和周长。创建一个该类的对象,计算并输出其面积和周长。答:程序如下: class 三角形 {

double a,b,c;

三角形(double a,double b,double c) {

this.a=a; this.b=b; this.c=c; }

double 计算面积()

{

double p=(a+b+c)/2.0;

return(Math.sqrt(p*(p-a)*(p-b)*(p-c))); }

double 计算周长()

{

return(a+b+c); } }

public class Class1 {

public static void main (String[] args)

- 31 -

{

三角形 tra=new 三角形(3,4,5);

System.out.println(\三角形的面积是:\计算面积()); System.out.println(\三角形的周长是:\计算周长()); try {

System.in.read(); }catch(Exception e){} } }

5、编一程序,有3个文本框,在第一个文本框中输入一个圆的半径并回车之后,第二个文本框将显示这个圆的周长,第三个文本框显示这个圆的面积。

答:程序如下 import java.awt.*;

import java.awt.event.*;

class MyFrame extends Frame implements ActionListener {

TextField text1,text2,text3; MyFrame() {

text1=new TextField(10); text2=new TextField(10); text3=new TextField(10); add(text1); add(text2); add(text3);

text1.addActionListener(this); setBounds(100,100,400,200); setVisible(true); }

public void actionPerformed(ActionEvent e) {

double r,c,s;

if(e.getSource()==text1) {

r=Double.valueOf(text1.getText()).doubleValue(); c=2*r*3.14159; s=3.14159*r*r;

text2.setText(\圆的周长是:\ text3.setText(\圆的面积是:\ } } }

public class Class1 {

public static void main (String[] args) {

- 32 -

new MyFrame(); } }

5、编一程序,有3个文本框,在第一个文本框中输入一个整数并回车之后,第二个文本框将显示这个数的平方值,第三个文本框显示这个数的立方值。

答:程序如下 import java.awt.*;

import java.awt.event.*;

class F1 extends Frame implements ActionListener {

TextField text1,text2,text3; F1() {

text1=new TextField(10); text2=new TextField(10); text3=new TextField(10); add(text1); add(text2); add(text3);

text1.addActionListener(this); setBounds(100,100,400,200); setVisible(true); }

public void actionPerformed(ActionEvent e) {

long n1,n2,n3;

if(e.getSource()==text1) {

n1=Long.parseLong(text1.getText()); n2=n1*n1; n3=n1*n1*n1;

text2.setText(n1+\的平方是:\ text3.setText(n1+\的立方是:\ } } }

public class Class1 {

public static void main (String[] args) {

new F1(); } }

6、有两个文本区,在第一个文本区中输入若干个数时,另一个文本区同时对输入的数从小到大排序。也就是说随着输入的变化,另一个文本区不断地更新排序。

答:程序如下

import java.util.*; //因使用字符分析器,所以使用该包

- 33 -

import java.awt.*;

import java.awt.event.*;

class F2 extends Frame implements TextListener {

TextArea text1,text2; F2() {

text1=new TextArea(\ text2=new TextArea(\ add(text1); add(text2);

text2.setEditable(false); text1.addTextListener(this); setBounds(100,100,400,200); setVisible(true); }

public void textValueChanged(TextEvent e) {

if(e.getSource()==text1) {

String s=text1.getText(); double a[]=new double[50];

StringTokenizer fenxi=new StringTokenizer(s,\

//用空格逗号和回车做分隔符

int n=fenxi.countTokens(); for(int i=0;i<=n-1;i++) {

String temp=fenxi.nextToken();

a[i]=Double.valueOf(temp).doubleValue(); }

for(int i=0;i<=n-2;i++) {

for(int j=i+1;j<=n-1;j++) {

if(a[j]<=a[i]) {

double t=a[j];a[j]=a[i];a[i]=t; } } }

text2.setText(null);

for(int i=0;i<=n-1;i++)

text2.append(\ } } }

- 34 -

public class Class1 {

public static void main (String[] args) {

new F2(); } }

7、设计4个按钮,分别命名为“加”、“减”、“乘”、“除”,3个文本框,当在前两个文本框输入操作数并按某一按钮后,第三个文本框就会显示相应的运算结果。 import java.awt.*;

import java.awt.event.*;

class F3 extends Frame implements ActionListener {

TextField text1,text2,text3;

Button button1,button2,button3,button4; F3() {

text1=new TextField(10); text2=new TextField(10); text3=new TextField(10); add(text1); add(text2); add(text3);

button1=new Button(\加\ button2=new Button(\减\ button3=new Button(\乘\ button4=new Button(\除\ add(button1); add(button2); add(button3); add(button4);

button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setBounds(100,100,400,200); setVisible(true); }

public void actionPerformed(ActionEvent e) {

double n,n1,n2;

n1=Double.valueOf(text1.getText()).doubleValue();

n2=Double.valueOf(text2.getText()).doubleValue(); if(e.getSource()==button1) {

n=n1+n2;

text3.setText(String.valueOf(n));

- 35 -


Java习题集(第三版)(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:中职内宿生养成生活卫生习惯教育的实践与思考

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: