JAVA期末试题及答案 下载本文

100

四、程序设计

1.编写一个“Student”类,该类拥有属性:校名、学号、性别、出生日期。方法包含设置姓名和成绩(setName(),setScore())。再编写“Student”类的子类:Undergraduate(大学生)。Undergraduate类除拥有父类属性和方法外,还有其自己的属性和方法:附加属性包括系(department)、专业(major);方法包含设置系别和专业。(setDepartment(),setMajor())。 1. class Student {

String name=\湖南****学院\ int sNum=888888; String sex=\男\

String birth=\ String sname; int Score;

void setName(String a){ sname=a; }

void setScore(int b){ Score=b; }

void show(){

System.out.println(\所在学校:\ System.out.println(\学号:\ System.out.println(\性别:\ System.out.println(\生日:\ System.out.println(\姓名:\ System.out.println(\成绩:\ } }

class Undergraduate extends Student{ String department; String major;

void setDeparment(String c){ department=c; }

void setMajor(String d){ major=d; }

void show1(){ super.show();

System.out.println(\系部:\ System.out.println(\专业:\

} }

class Student{

public static void main(String arg[]){ Student A=new Student();

Undergraduate B=new Undergraduate(); B.setName(\许翼\ B.setScore(95);

B.setDeparment(\信息工程系\ B.setMajor(\计算机网络\ B.show1(); } }

2.从键盘或者命令行输入3个数,求这三个数的最大值。 2. import javax.swing.JOptionPane; public class MaxNum {

public static String sum(int a,int b,int c,int d){ if(a>b){ d=a; } if(b>d){ d=b; } if(c>d){ d=c; }

return \你输入的三个数字中,最大的数是\ }

public static void main(String[] args) { int number1,number2,number3; try{

number1=Integer.parseInt(JOptionPane.showInputDialog(\ number2=Integer.parseInt(JOptionPane.showInputDialog(\ number3=Integer.parseInt(JOptionPane.showInputDialog(\ System.out.println(sum(number1,number2,number3,0)); }catch(NumberFormatException ne){

System.out.println(ne.toString()); }

System.exit(0); }

the first Number: \the second Number: \the third Number: \ }

3. import java.awt.*;

public class TestCenterPanel {

public static void main(String args[]) { new MyFrame3(300,300,600,400,Color.BLUE); } }

class MyFrame3 extends Frame{ //private Panel p;

MyFrame3(int x,int y,int w,int h,Color c){ super(\ setLayout(null); setBounds(x,y,w,h); setBackground(c);

Panel p = new Panel(null); p.setBounds(w/4,h/4,w/2,h/2); p.setBackground(Color.pink); add(p);

setVisible(true); } }