点击返回标题->23年Java期末复习-CSDN博客
首先设计一个学生抽象类Student,其数据成员有name(姓名)、age(年龄)和degree(学位),以及一个抽象方法show()。然后由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类Undergraduate增加成员specialty(专业),研究生类增加成员direction(研究方向)。并且每个类都有show()方法,用于输出数据成员信息。请定义对象,并打印输出下列信息:
public class Main {
public static void main(String[] args) {
Undergraduate u1 = new Undergraduate("张三", 20, "本科", "计算机科学");
Undergraduate u2 = new Undergraduate("李四", 21, "本科", "物联网");
Graduate g1 = new Graduate("王五", 25, "硕士", "软件工程");
Graduate g2 = new Graduate("刘六", 36, "博士", "通信工程");
u1.show();
u2.show();
g1.show();
g2.show();
}
}
abstract class Student{
String name, degree;
int age;
abstract void show();//定义为抽象方法,Student也必须为抽象类
}
class Undergraduate extends Student{
String specialty;
Undergraduate(String name, int age, String degree, String specialty){//有参构造
this.name = name;
this.age = age;
this.degree = degree;
this.specialty = specialty;
}
@Override
void show() {//实现父类抽象方法
//格式化输出建议使用printf
System.out.printf("%s:%d,%s,%s\n",
this.name, this.age, this.degree, this.specialty);
}
}
class Graduate extends Student{
String direction;
Graduate(String name, int age, String degree, String direction){//有参构造
this.name = name;
this.age = age;
this.degree = degree;
this.direction = direction;
}
@Override
void show() {//实现父类抽象方法
//格式化输出建议使用printf
System.out.printf("%s:%d,%s,%s\n",
this.name, this.age, this.degree, this.direction);
}
}
设计一个抽象类Graphics,它具有一个String类型参数name和两个抽象方法parameter()、area(),name用来存储图形的名称,parameter()方法用于输出图形的名称和其它属性特征,area()方法用于输出图形的面积。请用该类派生的子类实现输出一个形状为长方形、长为3宽为2和它面积以及输出一个形状为圆形、颜色为红色、半径为4和它面积。
public class Main {
public static void main(String[] args) {
Rectangle rec = new Rectangle("长方形", 3, 4);
Circle cir = new Circle("圆形", "红色", 2);
rec.parameter();
rec.area();
cir.parameter();
cir.area();
}
}
abstract class Graphics{
String name;
abstract void parameter();//用于输出图形的名称和其它属性特征
abstract void area();//用于输出图形面积
}
class Rectangle extends Graphics{
double length, width;
Rectangle(String name, double length, double width){
this.name = name;
this.length = length;
this.width = width;
}
@Override
void parameter() {
System.out.printf("这是一个长方形,它的长为%.2f,宽为%.2f\n", this.length, this.width);
}
@Override
void area() {
System.out.printf("长为%.2f,宽为%.2f的长方形的面积为%.2f\n", this.length, this.width, this.length*this.width);
}
}
class Circle extends Graphics{
double r;
String color;
Circle(String name, String color, double r){
this.name = name;
this.r = r;
this.color = color;
}
@Override
void parameter() {
System.out.printf("这是一个圆形,它的颜色为%s,它的半径为%.2f\n", this.color, this.r);
}
@Override
void area() {
System.out.printf("取PI为3.14,则半径为%.2f的圆的面积为%.2f\n", this.r, 3.14*this.r*this.r);
}
}
设计一个接口circleInterface,要求接口中有一个定义PI的常量以及一个计算圆面积的空方法circleArea()。然后设计一个类circleClass实现该接口,通过构造函数circleClass(double r)定义圆半径,并增加一个显示圆面积的方法。最后,通过上述类生成两个半径分别为3.5、5.0的圆对象circle1、circle2进行测试。
interface circleInterface{
//注意,接口中的变量默认是public static final修饰的,方法默认是public abstract修饰的
double PI = 3.14;
double circleArea();
}
public class Main {
public static void main(String[] args) {
circleClass c1 = new circleClass(3.5);
circleClass c2 = new circleClass(5.0);
c1.show_area();
c2.show_area();
}
}
class circleClass implements circleInterface{
double r;
circleClass(double r){//有参构造设置圆半径
this.r = r;
}
public double circleArea() {//父类的方法由public修饰,子类的权限不得小于public
return this.PI * this.r * this.r;
}
void show_area() {
System.out.println(this.circleArea());
}
}
设计一个Shape接口和它的两个实现类Square和Circle,要求如下:1)Shape接口中有一个抽象方法area(),方法接收一个double类型的参数,返回一个double类型的结果。2)Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形的面积并返回。在测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的园面积
interface Shape{
double PI = 3.14;
//注意,接口中的变量默认是public static final修饰的,方法默认是public abstract修饰的
double area(double para);
}
public class Main {
public static void main(String[] args) {
//没有写构造方法的类,默认存在一个无参构造,
//根据题目的意思,area()方法需要接收一个参数,因此我们直接利用这个参数给Square类对象和Circle类对象设置边长和半径
//但这种方法必须要先调用area()方法给对象初始化,存在一定局限性
Square squ = new Square();
double squ_s = squ.area(2);
System.out.printf("边长为%.2f的正方形的面积为%.2f\n", squ.l, squ_s);
Circle cir = new Circle();
double cir_s = cir.area(3);
System.out.printf("半径为%.2f的圆的面积为%.2f\n", cir.r, cir_s);
}
}
class Square implements Shape{
double l;
@Override
public double area(double para) {
this.l = para;
return this.l * this.l;
}
}
class Circle implements Shape{
double r;
@Override
public double area(double para) {
this.r = para;
return this.PI * this.r * this.r;
}
}
定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:
1.接口名字为USB,里面包括两个抽象方法:
void work();描述可以工作
void stop(); 描述停止工作
2.完成类Mouse,实现接口USB,实现两个方法:
work方法输出“我点点点”;
stop方法输出 “我不能点了”;
3.完成类UPan,实现接口USB,实现两个方法:
work方法输出“我存存存”;
stop方法输出 “我走了”;
4测试类Main中,main方法中
定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法
定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。
interface USB {//定义接口(习惯是接口定义在主类上面,其它类在主类下面)
void work();
void stop();
}
public class Main {
public static void main(String[] args) {
Mouse usb1 = new Mouse();
usb1.work();
usb1.stop();
//接口创建数组和基本及引用数据类型创建数组的语法格式是一模一样的!
USB[] usbs = new USB[2];//为什么用接口来定义数组?因为这个数组要同时存放两个不同实现类的实例化对象u和m(如果用Upan类定义数组,那么Mouse类对象就无法存放进该数组,反之同理)。
Upan u = new Upan();
usbs[0] = u;//
Mouse m = new Mouse();
usbs[1] = m;
for (int i = 0; i < 2; i++) {
usbs[i].work();
usbs[i].stop();
}
}
}
//定义两个子类,实现USB接口,并重写work和stop方法。
class Mouse implements USB {
public void work() {
System.out.println("我点点点");
}
public void stop() {
System.out.println("我不能点了");
}
}
class Upan implements USB {
public void work() {
System.out.println("我存存存");
}
public void stop() {
System.out.println("我走了");
}
}
设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。
数据成员定义:
Person [ID,姓名,生日]
Student [专业,成绩]
Teacher [职称,工资]
带参构造方法分别为:
Person(int id,String name, int bir)
Student(int id,String name, int bir, String major,double score)
Teacher(int id,String name, int bir, String title, double salary)
toString方法(Eclipse自动生成)输入格式:
第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。
输出格式:
按行输出具体对象的信息。
输入样例:
在这里给出一组输入。例如:
5 0 10101 Peter 20010121 Computer 87.5 1 20110014 Crystal 19900103 AI 7000 0 10102 Rose 20010715 E-Commerce 90 1 20120019 David 19781218 Prof 12000 0 10103 Semon 20000405 Computer 88
输出样例:
在这里给出相应的输出。例如:
Student [id=10101, name=Peter, bir=20010121, major=Computer, score=87.5] Teacher [id=20110014, name=Crystal, bir=19900103, title=AI, salary=7000.0] Student [id=10102, name=Rose, bir=20010715, major=E-Commerce, score=90.0] Teacher [id=20120019, name=David, bir=19781218, title=Prof, salary=12000.0] Student [id=10103, name=Semon, bir=20000405, major=Computer, score=88.0]
这里讲一下怎么用eclipse自动生成类的常用方法,这个技巧可以有效降低重复代码的编写,在考场中很有优势。
就拿本题的Student类作为例子。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int teac_or_stu = sc.nextInt();
if (teac_or_stu == 0) {
String id = sc.next(), name = sc.next(), birth = sc.next(), major = sc.next();
double score = sc.nextDouble();
Student s = new Student(id, name, birth, major, score);
System.out.println(s.toString());
} else {
String id = sc.next(), name = sc.next(), birth = sc.next(), title = sc.next();
double salary = sc.nextDouble();
Teacher t = new Teacher(id, name, birth, title, salary);
System.out.println(t.toString());
}
}
}
}
abstract class Person {//定义抽象类,按题目要求定义成员变量,我认为String类型都可以解决。
String id, name, birth;
}
//子类继承抽象类,直接用编辑器生成构造方法和toString方法(但自动生成的toString方法返回的字符串格式不一定符合题目要求,所以要手动改一下细节)。
class Student extends Person {
String major;
double score;
Student(String id, String name, String birth, String major, double score) {
this.id = id;
this.name = name;
this.birth = birth;
this.major = major;
this.score = score;
}
@Override
public String toString() {
return "Student [" +
"id=" + id +
", name=" + name +
", bir=" + birth +
", major=" + major +
", score=" + score +
']';
}
}
class Teacher extends Person {
String title;
double salary;
Teacher(String id, String name, String birth, String title, double salary) {
this.id = id;
this.name = name;
this.birth = birth;
this.title = title;
this.salary = salary;
}
@Override
public String toString() {
return "Teacher [" +
"id=" + id +
", name=" + name +
", bir=" + birth +
", title=" + title +
", salary=" + salary +
']';
}
}