//filename:app8_2.java 抽象类的说明 abstract class Shape {
protected String name; public Shape(String xm) {
name=xm;
System.out.print(\名称:\ }
abstract public double getArea(); }
class Circle extends Shape {
private final double PI=3.14; private double radius;
public Circle(String shapeName,double r) {
super(shapeName); radius=r; }
public double getArea() {
return PI*radius*radius; } }
class Rectangle extends Shape {
private double width; private double height;
public Rectangle(String shapeName,double width,double height) {
super(shapeName); this.width=width; this.height=height; }
public double getArea() {
return width*height; } }
public class E8_2 {
public static void main(String[] args) {
Shape rect =new Rectangle(\长方形\ System.out.println(\;面积=\ Shape circle=new Circle(\圆\
System.out.println(\;面积=\ } }
10、编写一个“Student”类,该类拥有属性:校名、学号、性别、出生日期。方法包含构造方法和输出方法。再编写“Student”类的子类:Undergraduate(大学生)。Undergraduate类除拥有父类属性和方法外,还有其自己的属性和方法:附加属性包括系(department)、专业(major);方法包含构造方法和输出方法。 class Student { String name; int sNum; String sex; String birth; String sname; int Score;
public Student(String name1,int sNum1, String sex1,String birth1, String sname1,int Score1) { name=name1; sNum=sNum1; sex=sex1; birth=birth1; sname=sname1; Score=Score1; }
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; public Undergraduate(String name1,int sNum1, String sex1,String birth1, String sname1,int Score1,String department1,String major1) { super(name1,sNum1,sex1, birth1, sname1, Score1); department=department1; major=major1; }
void show1(){ super.show();
System.out.println(\系部:\ System.out.println(\专业:\ } }
public class aa{
public static void main(String arg[]) {
Undergraduate B=new Undergraduate(\湖南****学院\男\许翼\信息工程系\计算机网络\ B.show1(); } }
11、按以下要求编写程序
(1) 编写Animal接口,接口中声明run() 方法 (2) 定义Bird类和Fish类实现Animal接口
(3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法 Bird类的run()方法输出\鸟儿在飞...\Fish类的run()方法输出\鱼儿在游...\ 解答:
public interface Animal { void run(); }
class Bird implements Animal { public void run() {
System.out.println(\鸟儿在飞...\); } }
class Fish implements Animal { public void run() {
System.out.println(\鱼儿在游...\); } }
public class TestAnimal {
public static void main(String[] args) { Bird bird = new Bird(); bird.run();
Fish fish = new Fish(); fish.run(); } }
12、
按以下要求编写程序
(1) 创建一个Rectangle类,添加width和height两个成员变量 (2) 在Rectangle中添加两种方法分别计算矩形的周长和面积 (3) 编程利用Rectangle输出一个矩形的周长和面积 public class Rectangle { float width, height;
public Rectangle(float width, float height) { this.width = width; this.height = height; }
public float getLength(){
return (this.width + this.height) * 2; }
public float getArea(){
return this.width * this.height; }
public static void main(String [] args) { Rectangle rect = new Rectangle(10, 20);
System.out.println(\周长是:\ System.out.println(\面积是:\ } }
13、由person类派生出student类和teacher类 import java.io.*; import java.util.*;
class person{
private String name,sex,birth;
public person(String n1, String s1, String b1) { name=n1; sex=s1; birth=b1; }
void display()
{ System.out.println(\ } }
class student extends person{
private String cl; private int no;
private float total;
public student(String c,int num,float to,String n2,String s2,String b2) { super(n2,s2,b2); cl=c; no=num; total=to; }
void displays()
{ System.out.println(\ display();
System.out.println(\ } }
class teacher extends person{ private String dp; private float wage;
public teacher(String d,float w,String n2,String s2,String b2) { super(n2,s2,b2); dp=d; wage=w; }
void displayt()
{ System.out.println(\ display();
System.out.println(\ } }
public class sttry
{ public static void main(String[] args) throws IOException {
String n2,s2,b2,c,d; int num,ch; float to,w;
Scanner reader=new Scanner(System.in); BufferedReader buf;
buf=new BufferedReader(new InputStreamReader(System.in)); while(true){
System.out.println(\student\\n2.Input teacher\\n3.Exit\\nEnter:\ ch=reader.nextInt();
switch(ch){ case 1:{
System.out.println(\ System.out.print(\ //n2=reader.next();
n2=buf.readLine();