1、第二次1. 上机运行以下程序,写出程序运行结果。public class UsePhoneCardpublic static void main(String args)PhoneCard myCard = new PhoneCard(12345678,1234,50.0,300);System.out.println(myCard.toString();class PhoneCardlong cardNumber;private int password;double balance;String connectNumber;boolean connected;PhoneCard(long
2、cn, int pw, double b,String s)cardNumber = cn;password = pw;if ( b 0 )balance = b;elseSystem.exit(1);connectNumber = s;connected = false;boolean performConnection(long cn,int pw)if(cn = cardNumber & pw = password )connected = true;return true;elseconnected = false;return false;double getBalance()if(
3、connected)return balance;elsereturn -1;void performDial()if(connected)balance -= 0.5;public String toString()String s = 电话卡接入号码:+connectNumber+n电话卡卡号:+cardNumber+n电话卡密码:+password+n剩余金额:+balance;if(connected)return (s + n电话已接通。);elsereturn (s + n电话未接通。);运行结果:电话卡接入号码:300电话卡卡号:12345678电话卡密码:1234剩余金额:50
4、.0电话未接通。2. 上机运行以下程序,写出程序运行结果。public class TestStaticpublic static void main(String args)PhoneCard200 my200_1 = new PhoneCard200();PhoneCard200 my200_2 = new PhoneCard200();System.out.println(第一张200卡的卡号:+my200_1.cardNumber);System.out.println(第二张200卡的卡号:+my200_2.cardNumber);class PhoneCard200static l
5、ong nextCardNumber;static String connectNumber = 200;static double additoryFee;long cardNumber ;int password;boolean connected;double balance;staticnextCardNumber = 2001800001;PhoneCard200()cardNumber = nextCardNumber+;运行结果:第一张200卡的卡号:2001800001第二张200卡的卡号:20018000023. 上机运行以下程序,写出程序运行结果。public class
6、TestAbstractpublic static void main(String args)PhoneCard200 my200 = new PhoneCard200(50.0);IC_Card myIC = new IC_Card(50.0);System.out.println(200卡可以拨打 +my200.TimeLeft()+ 次电话。);System.out.println(IC 卡可以拨打 +myIC.TimeLeft()+ 次电话。);abstract class PhoneCarddouble balance;abstract void performDial();dou
7、ble TimeLeft()double current = balance;int times = 0;doperformDial();times+;while(balance=0);balance = current;return times-1;class PhoneCard200 extends PhoneCardstatic long nextCardNumber;static final String connectNumber = 200;static double additoryFee;long cardNumber ;int password;boolean connect
8、ed;staticnextCardNumber = 2001800001;additoryFee = 0.1;PhoneCard200(double ib)cardNumber = nextCardNumber+;balance = ib;void performDial()balance -= 0.5+additoryFee;class IC_Card extends PhoneCardIC_Card(double ib)balance = ib;void performDial()balance -= 0.9;运行结果:200卡可以拨打 83.0 次电话。IC 卡可以拨打 55.0 次电话
9、。4. 以下程序的功能是比较三个整数并输出其中的最大值,请将程序补充完整。import java.applet.*;import java.awt.*;import java.awt.event.*;public class FindMax extends Applet implements ActionListenerLabel result;TextField in1,in2,in3;Button btn;int a=0,b=0,c=0,max;public void init() result = new Label(请先输入三个待比较的整数);in1 = new TextField(5
10、);in2 = new TextField(5);in3 = new TextField(5);btn = new Button(比较);add(in1);add(in2);add(in3);add(btn);add(result);btn.addActionListener(this);public void actionPerformed(ActionEvent e)a = Integer.parseInt(in1.getText();b = Integer.parseInt(in2.getText();c = Integer.parseInt(in3.getText();/请填入语句re
11、sult.setText(三数中最大值是:+ max );运行结果: if(ab) if(ac) max = a; else max = c; else if(bc) max = b; else max = c;5. 阅读以下程序,写出程序功能。import java.applet.*;import java.awt.*;import java.awt.event.*;public class UseArithmetic extends Applet implements ActionListenerLabel prompt;TextField input1,input2;Button btn
12、;int a=0,b=1;public void init()prompt = new Label(请输入两个整型数据:);input1 = new TextField(5);input2 = new TextField(5);btn = new Button(计算);add(prompt);add(input1);add(input2);add(btn);btn.addActionListener(this);public void paint(Graphics g)g.drawString(a + + + b + = + (a+b),10,50);g.drawString(a + - +
13、b + = + (a-b),10,70);g.drawString(a + * + b + = + (a*b),10,90);g.drawString(a + / + b + = + (a/b),10,110);g.drawString(a + % + b + = + (a%b),10,130);public void actionPerformed(ActionEvent e)a = Integer.parseInt(input1.getText();b = Integer.parseInt(input2.getText();repaint();运行结果:输入两个整数,输出两个整数的加,减,乘、除和求余数的结果。