题目
阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。
[说明] 编写一个完整的JavaApplet 程序使用复数类Complex 验证两个复数1+2i 和3+4i 相加产生一个新的复数4+6i。
复数类Complex 必须满足如下要求:
(1) 复数类Complex 的属性有:
RealPart: int 型,代表复数的实数部分
ImaginPart: int 型,代表复数的虚数部分
(2) 复数类Complex 的方法有:
Complex():构造函数,将复数的实部和虚部都置0
Complex (intr,inti):构造函数,形参r为实部的初值,i为虚部的初值。
ComplexeomplexAdd (Complexa):将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者
String ToString():把当前复数对象的实部、虚部组合成s+ bi 的字符串形式,其中a和b分别为实部和虚部的数据。
importjava. applet. * ;
importjava. awt. * ;
publicclassabcextends Applet
{
Complex a, b, c;
publi cvoid init()
{
a = newComplex(1,2);
b = newComplex(3,4);
c = newComplex();
}
publievoidpaint (Graphicsg)
{
(1)
g. drawstring(“第一个复数:” + a. toString(), 10,50);
g. drawstring(“第二个复数:” + b. toString(), 10,70 );
g. drawstring(“两复之和:” + c. toString(), 10,90);
}
}
class Complex
{
int RealPart;
int ImaginPart;
Complex() { (2) }
Complex(intr , inti)
{ (3) }
ComplexeomplexAdd (Complexa)
{
Complextemp = newComplex();
temp. BealPart = RealPart + a. BealPart;
(4)
returntemp;
}
public StringtoString()
{ return(RealPart + " + " + ImaginPart + " i "); }
}
第1题
阅读以下说明和Java码,将应填入(n)处的字名写在对应栏内。
[说明] 编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
import java. io. * ;
public class abc
{
public static void main(String args [ ] )
{ int i, n = 10 , max = 0 , min = 0 , temp = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System. in) );
(1));
} catch (IOException e ) { } ;
for(i = 2 ;i <= n; i ++ ) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader (System. in) );
temp = Integer. parselnt(br. readLine() );
if (temp > max ) (2)
if (temp < min) (3)
} catch (IOExeeption e ) { } ;
System. out. println("max =" + max + "\nmin =" + min);
}
}
第2题
阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。
[说明] 打印输出10行杨晖三角形。形式如下:
杨晖三角形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
class yanghui
{
public static void main (String args [] )
{
int i, j;
(1)
int yanghui [] [];
System. out. println(“杨晖三角形:” );
yanghui = new int [ yhleve1] [];
for(i =0;i < yanghui, length; i + + )
yanghui[i] = new int [i + 1];
(2)
for((3))
{
yanghui [i] [0] = 1;
for(j = 1 ;j < yanghui[i], length - 1 ;j + + )
yanghui[i] [j] = yanghui[i - 1] [j - 1] + yanghui[i - 1] [j];
yanghui[i] [yanghui[i]. length - 1 ] = 1;
}
for (i=0; i < yanghui. length; i + + )
{
for(j =0;j < yanghui[i]. length; j + + )
(4)
System. out. println();
}
}
}
第3题
() 阅读下列说明和JAVA代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第4题
阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。
[说明] 编写一个学生类Student,要求:
(1) 学生类Student 属性有:
id: long 型,代表学号
name: String类对象,代表姓名
age: int 型,代表年龄
sex: boolen 型,代表性别(其中:true 表示男,false 表示女)
phone: String 类对象,代表联系电话
(2) 学生类Student 的方法有:
Student (long i,String n,int a,boolean s,String p)
:有参构造函数,形参表中的参数分别初始化学号、姓名、
年龄、性别和联系电话。
int getAge ():获取年龄作为方法的返回值。
boolean getSex ():获取性别作为方法的返回值。
String getPhone ():获取联系电话作为方法的返回值。
public String to String ():以姓名:性别:学号:联系电话的形式作为方法的返
import java. applet. Applet;
import java. awt.* ;
public class Student extends Applet {
long id;
String name, phone;
int age;
boolean sex;
Student(long i, String n, int a, boolean s, String p)
{
id=i;
name = n;
age = a;
sex= s;
phone = p;
{
public void paint(Graphics g)
{
Student x= new Student (5000," xiaoliu" , 89, true, " 8989898" );
(1);
(2)
g. drawstring(x. getPhone(), 140,140);
}
int getAge()
{ return age; }
boolean getsex ()
{ return sex; }
String getPhone()
{ return phone; }
String ToString()
{
(3)
}
}
第5题
阅读以下说明和Java程序,将应填入(n)处的字句写在对应栏内。
[说明]
下面程序实现十进制向其它进制的转换。
[Java程序]
ClasS Node{
int data;
Node next;
}
class Transform{
private Node top;
public void print(){
Node p;
while(top!=null){
P=top;
if(P.data>9)
System.out.print((char)(P.data+55));
else
System.out.print(p.data);
top=p.next;
}
}
public void Trans(int d,int i){//d为数字;i为进制
int m;
(1) n=false;
Node p;
while(d>0){
(2);
d=d/i;
p=new Node();
if((3) ){
p.data=m;
(4);
top=P;
n=true;
}
else{
p.data=m;
(5);
top=P;
}
}
}
}
第6题
阅读以下说明和c++码,将应填入(n)处的字名写在的对应栏内。
[说明] 以下函数完成求表达式
的值,请填空使之完成此功能。
float sum (float x )
{ float s=0.0;
int sign = 1;
(1);
for(inti=1;(2); i+ +)
{
t=t*x;
s=s+(3);
sign = - sign;
(4);
}
第7题
阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。
[说明]
以下程序为类类型的变量应用实例,通过异常处理检验了类CCircle的变量的合法性,即参数半径应为非负值。仔细阅读代码和相关注释,将程序补充完整。
[JAVA代码]
//定义自己的异常类
class CCircleException extends Exception
{
}
// 定义类 CCircle
class CCircle
{
private double radius;
public void setRadius (double r ) (1)
{
if (r<0 ) {
(2)
}
else
(3)
}
Public void show () {
System. out. println ("area="+3.14*radius*radius );
}
}
public class ciusample
{
public static void main (String args[] )
{
CCircle cir=new CCircle();
(4) {
cir. setRadius (-2.0 )
}
(5)
{
System. out. println (e+" throwed" ) ;
}
cir. show() ;
}
}
第8题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
[说明]
编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
[Java 代码]
import java. io.* ;
public class abc
{
public static void main ((1))
{int i, n=10, max=0, min=0, temp=0;
try {
BufferedReader br = new BufferedReader (
new InputStreamReader (System.in ) );
max = min =Integer. parselnt (br. readLine () );
}. (2) (IOExccption e ) {} ;
for (i=2 ; i<=n ; i++ ) {
try {
BufferedReader br = new (3) (
new InputStreamReader (System. in ) );
temp =(4) (br. readLine ());
if (temp>max ) max=temp;
if (temp<min) (5)
} catch (IOException e ) {};
}
System.out.println ("max="+max+"\nmin="+min );
}
}
第9题
阅读以下说明、Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
本程序输出10000之内的所有完全数。完全数是指等于其所有因子和(包括1,但不包括这个数本身)的数。例如:6=1×2×3,6=1+2+3,则6是一个完全数。
【程序】
public class PerfectNum
{
Public static void main(String args[])
{
int count=1;
for(int i=1; i<10000; i++)
{
int y=0;
for(int j=1; j<i; j++)
if((1))
y=(2)
if((3))
{
System.out.print((4) +String.valueOf('\t'));
(5)
If(count%3==0)
System.out.printin();
}
}
}
第10题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
下面程序完成从键盘读入一个字符串,然后采用parseInt方法将其转换为一个相应的整数。
import java.io.*;
public class testThrows{
public static (1) readString() (2) IOException{
int ch;
String r="";
boolean done=false;
while((3)){
ch=System.in.read();
if(ch<0 || ch=0xd) //处理回车符中第一个符号
done=true;
else
r=r+(char)ch;
}
return r;
}
public static void main(Stling args[]){
String str;
(4) {
str=readString();
} (5) (IOException e){
System.out.println("error");
return;
}
System.out.println("input integer: "+Integer.parselnt(str));
}
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!