《JAVA程序设计》期末考试实操题库及答案

发布时间 : 星期五 文章《JAVA程序设计》期末考试实操题库及答案更新完毕开始阅读

实操考核课程试题(2)参考答案

package test2;

interface Cubage//声明接口 {

public static final double PI=3.14;//常量 public double doCubage();//抽象方法 }

class Cylinder implements Cubage

{//创建圆柱类,实现Cubage接口 double r;//圆柱底半径 double h;//圆柱高 public Cylinder(double r,double h) {//圆柱构造方法 this.r=r; this.h=h; } public double doCubage() {//重写接口的doCubage()方法,实现多态 return(Cubage.PI*r*r*h); } }

class Cone implements Cubage

{//创建圆锥类,实现Cubage接口 double r;//圆锥底半径 double h;//圆锥高 public Cone(double r, double h) {//圆锥构造方法 this.r = r; this.h = h; } public double doCubage() {//重写接口的doCubage()方法,实现多态 return(Cubage.PI*r*r*h/3); } }

public class test2 { /** * @param args */

}

public static void main(String[] args) { // TODO Auto-generated method stub }

Cylinder cylinder=new Cylinder(2.5,4);//创建圆柱对象 Cone cone=new Cone(3,4);//创建圆锥对象

System.out.println(\圆柱的体积是:\ System.out.println(\圆锥的体积是:\

实操考核课程试题(3)参考答案

package test3;

import java.util.*;

public class test3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; int sum =0; System.out.println(\请任意输入1到10000以内的整数!\ Scanner sc= new Scanner (System.in); int num = sc.nextInt(); for(i = 1; i <=num; i++) { sum += i; } System.out.println(\-1)+\ } }

实操考核课程试题(4)参考答案

package test4;

abstract class Shape//声明抽象类

{

abstract double doArea();//抽象方法 }

class Rectangle extends Shape {//创建长方形类, double a;//长方形长 double b;//长方形宽 public Rectangle(double a,double b) {//长方形构造方法 this.a=a; this.b=b; } public double doArea() {//重写接口的doArea()方法,实现多态 return(a*b); } }

class Trapezoid extends Shape {//创建梯形类 double a;//梯形上底 double b;//梯形下底 double h;//梯形高 public Trapezoid(double a, double b,double h) {//梯形构造方法 this.a = a; this.b = b; this.h = h; } public double doArea() {//重写接口的doArea()方法,实现多态 return((a+b)*h/2); } }

public class test4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Rectangle re=new Rectangle(3,4);//创建长方形对象

}

}

Trapezoid tr=new Trapezoid(3,4,5);//创建梯形对象 System.out.println(\长方形的面积是:\ System.out.println(\梯形的面积是:\

实操考核课程试题(5)参考答案

package test5;

class Person{ String name; private double weight; private int age; String address; public double getWeight() { return weight; } public void setWeight(double weight) { if(weight >50&&weight <300) { this.weight = weight; } else System.out.println(\体重不合格!!!\ } public int getAge() { return age; } public void setAge(int age) { if(age>18&&age<120){ this.age = age; } else System.out.println(\年龄不合格!!!\ } }

public class test5 {

联系合同范文客服:xxxxx#qq.com(#替换为@)