JAVA程序设计实验指导书(3)

2019-01-18 21:05

4.定义类头时可以使用的访问控制符是(CD)。 A) private B) protected

C) public D) 缺省的,即没有访问控制修饰符 5.有一个类A,对于其构造函数的声明正确的是( B )。 A)void A(int x){...} B)A(int x){...} C)A A(int x){...} D)int A(int x){...}

6.设X为已定义的类名,下列声明对象x1的语句中正确的是(ABD)。 A) static X x1; B) private X x1=new X( ); C) abstract X x1; D) final X x1=new X( );

7.设类B是类C的父类,下列声明对象x1的语句中不正确的是(D)。 A)B x1= new B( ); B) B x1=new C( ); C)C x1=new C( ); D) C x1=new B( ); 8. 编译运行下面的程序,结果是(A)。 public class A { public static void main (String[] args){ B b=new B( ); this.test(); } public void test() { System.out.print (\ } }

class B extends A{ void test() { super.test(); System.out.println(\ } }

A)产生编译错误,因为类B覆盖类A的方法test()时,降低了其访问控制的级别。 B)代码可以编译运行,并输出结果:AB。 C)代码可以编译运行,但没有输出。 D)代码可以编译运行,并输出结果:A。 9.下面的程序编译运行的结果是(A)。 public class A implements B{ public static void main (String[] args){ int m,n; A t= new A(); m= t.k; n= B.k; System.out.println(m+\ } }

interface B { int k = 5;}

A)5,5 B)0,5 C) 0,0 D)编译程序产生编译错误 10.为了使包abc中的所有类在当前程序中可见,可以使用的语句是(A)。 A)import abc.*; B)package abc.*; C)import abc; D) package abc;

二. 本章上机实验

上机实验五

1.定义一个名为MyRectangle的矩形类,类中有4个私有的整型域,分别是矩形的左上角

坐标(xUp,yUp)和右下角坐标(xDown,yDown);类中定义没有参数的构造方法和有4个int参数的构造方法,用来初始化类对象。类中还有以下方法: getW( )——计算矩形的宽度;getH( )——计算矩形的高度;area( )——计算矩形的面积;toString( )——把矩形的宽、高和面积等信息作为为字符串返回。编写应用程序使用MyRectangle类。

2.设计一个Dog类,它有名字、颜色、年龄等属性,定义构造方法用来初始化类的这些属性,定义方法输出Dog 的信息。编写应用程序使用Dog类。

3.设计一个长方体类MyCube,该类包含第1题中的MyRectangle类对象作为类的域,表示长方体的底面;此外还包含一个整型变量d,表示长方体的高。类中定义构造方法初始化类对象、定义求体积和表面积的方法。编写应用程序测试MyCube类。

4.设计一个表示用户的User类,类中的变量有用户名、口令(私有的)和记录用户个数的变量(静态的),定义类的3个构造方法(没有参数、有一个参数给用户名赋值、有两个参数给用户名和口令赋值)、获取和设置口令的方法、返回字符串表示的类信息的方法(包括用户名、口令)。编写应用程序测试User类。

上机实验六

1.定义一个抽象基类Shape,它包含一个抽象方法getArea(),从Shape类派生出Rectangle和Circle类,这两个类都用getArea()方法计算对象的面积。编写编写应用程序使用Rectangle类和Circle类。

2.定义一个接口ClassName,接口中只有一个抽象方法getClassName()。设计一个类Horse,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名。编写应用程序使用Horse类。

3.定义接口myItfc,接口中只有一个名为area的方法,该方法有一个double类型参数,返回类型也为double。编写一个应用程序,并实现接口myItfc,接口中area方法的实现是返回参数的立方值;在应用程序中调用该方法并输出其结果。

4.编写小程序,在小程序窗口中显示字符串“java程序设计”,按下键盘上的箭头键,可按照箭头方向移动(提示:得到键盘键代码的方法是e.getKeyCode(),上、下、左、右键的编码分别用常量VK_UP 、VK_DOWN 、VK_LEFT 、VK_RIGHT表示)。

本章上机拓展练习

1.调式下面的程序,使之能编译通过并正确运行。 //Test.java

import javax.swing.JOptionPane;

public class Test {

public static void main( String args[] ) {

String firstName = JOptionPane.showInputDialog( \ String lastName = JOptionPane.showInputDialog( \ String color2 = JOptionPane.showInputDialog( \ String miles = JOptionPane.showInputDialog( \ \

double miles2 = Double.parseDouble( miles );

Miles mile = new Miles(); Color color = new Color();

Person person = new Person( firstName , lastName );

mile.setMile( miles2 ); color.setColor( color2 );

String output = person.getFirstName()+ \ \ \ \

JOptionPane.showMessageDialog( null, output, \ JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); } }

//Person..java

public class Person extends Object { private String firstName; private String lastName;

public Person( String firstName, String lastName ) {

lastName = getLastname(); firstName = getFirstName(); }

public String getFirstName() {

return firstName; }

public String getLastName() {

return lastName; } }

// Miles.java

import java.text.DecimalFormat;

public class Miles extends Object { private final double miles;

public Miles() {

setMile( 0.0 ); }

public void setMile( double m ) {

miles = ( ( m >= 0.0 && m <= 200000 ) ? m : 0 ); }

public String toMilesString() {

DecimalFormatformatMile = new DecimalFormat( \ return formatMile.format( miles ); } }

// Color.java

public class Color extends Object { private String color;

public Color() {

return setColor( \ }

public void setColor() {

color = \ return color; }

public void setColor( String m ) {

color = m; return color; }

public String toColorString() {

return color; }

}

2.下面的程序Complex.java定义一个复数类,ComplexTest.java测试该复数类。根据/**/中的注释将下面的程序补充完整,使之编译通过并能正确运行。 ============================ //ComplexTest.java

// Test the Complex number class ============================ import javax.swing.*;

public class ComplexTest {

public static void main( String args[] ) {

Complex a, b;

a = new Complex( 9.9, 7.7 ); b = new Complex( 1.4, 3.1 );

String result = \ result += \

result += \ result += \

JOptionPane.showMessageDialog( null, result, \ JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } }

========================= //Complex.java

// Definition of class Complex =========================

public class Complex { private double real;

private double imaginary;

// Initialize both parts to 0

/* Write header for a no-argument constructor. */ {

/* Write code here that calls the Complex constructor that takes 2 arguments and initializes both parts to 0 */ }

// Initialize real part to r and imaginary part to i

/* Write header for constructor that takes two arguments梤eal part r and imaginary part i. */ {

/* Write line of code that sets real part to r. */

/* Write line of code that sets imaginary part to i. */ }

// Add two Complex numbers

public Complex add( Complex right ) {

/* Write code here that returns a Complex number in which the real part is the sum of the real part of this Complex object and the real part of the Complex object passed to the method; and the imaginary part is the sum of the imaginary part of this Complex object and the imaginary part of the Complex object passed to the method. */ }

// Subtract two Complex numbers

public Complex subtract( Complex right ) {

/* Write code here that returns a Complex number in which the real part is the difference between the real part of this Complex object and the real part of the Complex object passed to the method; and the imaginary part is the difference between the imaginary part of this Complex object and the imaginary part of the Complex object passed to the method. */ }

// Return String representation of a Complex number public String toComplexString() {

return \ }

} // end class Complex

3. 下面的程序定义了父类Employee和它的三个子类:CommissionEmployee类、HourlyEmployee类、SalariedEmployee类都继承自Employee类。根据/**/中的注释将下面的程序补充完整,使之编译通过并能正确运行。 ============================ // EmployeeTest.java


JAVA程序设计实验指导书(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:江苏省张家港市2015届九年级上学期期末考试物理试题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: