2015JAVA上机考试题(1)

更新时间:2023-03-09 18:47:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

11代码

public class Test {

public static int method(int n) {

int num=0,i=0; while(i

return num; }

public static void main(String[] args) { System.out.println(method(100)); } }

11.用java语言设计一个程序,计算1+2+3+??+100;

要求:1)类名 Test

2)在主方法外写一个计算连加的方法,方法名为method且要通过传一

整形参数,及方法声明时用的的语句的一部分为 int method(int n);

3)在主方法中调用2)中写的方法,并在控制台输出值。

public class Test {

public static int method(int n){

//补充代码 }

public static void main(String[] args) { System.out.println(method(100)); } }

12. 下列程序的功能是:在文本行对象input中输入一个姓名,当input文本行

输入动作完成, 在文本行对象output中输出: “姓名Welcome you!”,;当点击按钮对象btnn后,退出应用程序(方法为:system.exit(0))。其中,姓名是input中输入的姓名.例如,输入“张三“,则输出:“张三Welcome you !”.请把程序补充完整

import java,awt,*; import java.awt.event.*; public class welcomenYou{

 public static void main(String args[])  {

 new FrameInOut();

 } }

class FrameInOut extends Frame implements ActionListener {

 Label prompt;  TextField input,output;  Button btnn; void FramInOut() {

 prompt=new Label(\ input=new TextField(10);  output=new TextField(25);  btnn=new Button(\ setLayout (new FlowLayout());  add(prompt);  add(input);  add(output);  add(btnn);

 input.addActionListener(this);  btnn.addActionListener(this);  setSize(300.200);  show(); }

public void actionperformed(ActionEvent e) { // 补充代码  

} }

21, 设计按钮,要求:标题“按钮示例”实现2个按钮,“First”与“Second”,

位置(180,160),窗口大小(300,300),类名ButtonApp,(提示需要导入的: java.awt.*; javax.swing.*;)

程序代码如下。请补充完整。 import java.awt.Button;

import java.awt.FlowLayout; import javax.swing.JFrame;

public class ButtonApp extends Jframe

{

Button button1 = new Button(\Button button2 = new Button(\public ButtonApp() { //如下位置补充代码

}

public static void main(String[] args) {

buttonApp = new ButtonApp(); buttonApp.setVisible(true); } }

22.最小公倍数是小学数学中的一个重点,为方便计算,现要求你用java语

言写一个求两个大于0的整数的最小公倍数的程序以解决所有求两个整数最小公倍数的问题; 要求:1)类名 TestApp

2)在主方法外写一个求最小公倍数的方法,方法名为method且要求通

过传两个整形参数,及方法声明时的语句的一部分为 int method(int a , int b );

3)在主方法中调用2)中写的方法,并用一组数据3和11进行测试,即求出3和11的最小公倍数,根据控制台的输出判断程序的正确性。 程序代码如下。请补充完整。 public class TestApp {

public static int method(int a, int b) { //如下位置补充代码 } public static void main(String[] args) { System.out.println(method(3, 11)); } }

31 一个加法运算器的简易运算软件,实现三个文本行对象

(TextField:text1,text2,result),一个标签(Label:lab),一个按纽

(Button:btn),程序运行时,在前两个文本行中输入两个整数,点击按纽时,将前两文本行中的两个数相加,结果显示在第三个文本行result中。 以下是要求你编写的actionPerformed(ActionEvent e)方法的部分代码。

public void actionPerformed(ActionEvent e) //实现接口,完成动作消息处理方法:

//actionPerformed,注意:参数必须有事件:ActionEvent 和 事件对象e { }

32. 设计学生类student,其数据域至少包括:学号(ID)姓名(name)性别(sex)

英语等三门课程成绩(score)3门功课总成绩(sum)3门功课平均成绩(average)。成员方法至少包括:构造方法、计算总成绩方法getSun()、计算平均成绩方法getAve()。

public class Student {

private int ID;

private String name; private String sex; private int score[3]; private int sum; private int average;

public Student( )

{ ID=0; name=null; sex=0; }

public Student(int ID, String name, String sex, int s1, int s2,int s3) { // 添加代码,实现成员变量的初始化

}

public int getSun( )

{ sum=english+score[0]+score[1]+score[2]; return sum; }

public int getAve( ) { average=sum/3; return average; } }

41.设计按钮,要求:标题“按钮示例”实现2个按钮,“First”与“Second”,

位置(180,160),

窗口大小(300,300),类名ButtonApp,(提示需要导入的: java.awt.*; javax.swing.*;)

import java.awt.Button;

import java.awt.FlowLayout; import javax.swing.JFrame;

public class ButtonApp extends JFrame { Button button1 = new Button(\Button button2 = new Button(\public ButtonApp() {

super(\按钮示例\

this.setLayout(new FlowLayout()); this.setLocation(180, 160); this.setSize(300, 300); this.add(button1); this.add(button2); }

public static void main(String[] args)

{ //补充相应代码,使程序运行后,能按题意要求正常显示窗体 }

42、用java语言设计一个程序,计算阶乘50!

要求:1)类名 Test

2)在主方法外写一个计算连加的方法,方法名为method且要通过传一整形参数,

及方法声明时用的的语句的一部分为 double method(int n); 3)在主方法中调用2)中写的方法,并在控制台输出值5050。

public class Test {

public static double method(int n){

//按题意补充代码 }

public static void main(String[] args) {

System.out.println(method(100)); } }

本文来源:https://www.bwwdw.com/article/zm8a.html

Top