云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 编程题有答案

编程题有答案

  • 62 次阅读
  • 3 次下载
  • 2025/5/5 23:05:48

编程题

按指定的要求编写程序段,

1.编写一个程序,把六个按钮分别标识为‘A’至‘F’,并排列成一行。 参考程序:

import java.awt.*; public class MyClass {

public static void main(String args[]) {

String[] labels = {\ Window win = new Frame();

win.setLayout(new GridLayout(1,6)); for(int i=0;i < labels.length;i++) win.add(new Button(labels[i])); win.pack(); win.setVisible(true); } }

【解析】该题目的知识点在于图形用户界面设计中的布局设计,只能采用GridLayout的方

式,不能采用默认的FlowLayout布局方式 2.设计一个applet,加载和显示图象“student.gif”。图像文件与包含applet的HTML文件在同一个目录下。 参考程序:

import java.awt.*; import java.applet.*;

public class Mypicture extends java.applet.Applet{ Image image;

public void init (){

image=getImage(getDocumentBase(),\ }

public void paint(Graphics g){

g.drawImage(image,0,0, this ); } }

【解析】该题要求掌握如何在applet中显示图像文件的编程能力,具体可参照参考教材第十章。

3. 编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数。删除哪个字符由随机数决定。如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。

removeRandChar()方法必须包括调用removeSingChar()方法,该方法定义在test4_1应用程序中。

例如执行完整的test4_1应用程序两次后,产生如下的输出:

C:\\> java test4_1

Remove 3 random characters from INTERESTING: INEETING Remove 6 random characters from INTERESTING: EESTN C:\\> java test4_1

Remove 3 random characters from INTERESTING: ITERETIN Remove 6 random characters from INTERESTING: IEESG

下面是不包括removeRandChar()方法的test4_1应用程序:

public class test4_1{

public static void main(String[] args) {

String word1 = removeRandChar(\

System.out.println(\word1 = removeRandChar(\

System.out.println(\} /**

This method removes the character at position: indexNum, from the String: str, and returns the resulting String. */

private static String removeSingChar(String str, int indexNum) { return str.substring(0,indexNum) + str.substring(indexNum+1); }

……

}

参考程序: 页:2

private static String removeRandChar(String str, int howMany){ if (str.length() <= howMany) return \ int randNo;

for(int j=0; j

randNo = (int) (Math.random() * str.length()); str = removeSingChar(str, randNo); }

return str; }

【解析】该题要求掌握编写方法和调用方法的能力,要注意形参的使用,以及有返回值和没有返回值的不同编写形式。

4.仔细阅读下面student类的定义,该类的定义可以存放学生的姓名(name)、测验分数(testMark)和考试分数(examMark)。

public class Student { private String name; private int testMark; private int examMark;

public Student(String theName) {

name = theName; }

public Student(String theName, int test, int exam) {

name = theName; testMark = test; examMark = exam; }

public void setExamMark(int exam) {

examMark = exam;

System.out.println(name + \}

public int getTestMark() {

return testMark; }

public void displayInfo() {

System.out.println(name + \

the test and \

}

public void compareTo(Student other) {

if (examMark > other.examMark)

System.out.println(name + \else

System.out.println(name + \

} }

要求完成test4_3的编程,它将创建两个Student对象并调用相应的方法,产生的输出如下所示:

C:\\> java test4_3

张楠 got 70 in the test and 85 in the exam 李浩 got 80 in the test and 90 in the exam 李浩's exam mark changed to 40 李浩 did worse than 张楠

注意不能使用任何System.out.print() 或 System.out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。

public class test4_3 {

public static void main(String[] args) {

Student student1; Student student2;

……

}

}

参考程序: 页:4

student1 = new Student(\张楠\ student2 = new Student(\李浩\ student1.displayInfo(); student2.displayInfo();

student2.setExamMark(40); student2.compareTo(student1);

【解析】该题考核的重点是面向对象程序设计的基本应用:对象的声明与创建,以及根据需

求调用实例方法。

5.编写change( ) 方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。

例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:

import java.io.*; public class Test4_1{

public static void main(String args[]) throws IOException { int money; String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in));

System.out.print(\

搜索更多关于: 编程题有答案 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

编程题 按指定的要求编写程序段, 1.编写一个程序,把六个按钮分别标识为‘A’至‘F’,并排列成一行。 参考程序: import java.awt.*; public class MyClass { public static void main(String args[]) { String[] labels = {\ Window win = new Frame(); win.setLayout(new GridLayout(1,6)); for(int i=0;i < labels.length;i++) win.add(new Button(labels[i])); win.pack(); win

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com