/*
* 设计一个读物接口,小说,杂志,期刊类实现这个接口,每种读物有相同的属性,
* 如页数,价格等,也有不同的属性,如杂志和期刊都有出版周期,而课本有适用对象
* 写一个测试类产生一系列读物,并输出它们的信息
*/
package Liu;
//读物接口
interface Reading {
public void Info();
}
//小说类
class Fiction implements Reading{
String applicable_object;
int price,pages;
//Fiction类构造方法
public Fiction(int pages,int price,String applicable_object) {
this.pages = pages;
this.price = price;
this.applicable_object = applicable_object;
}
//实现接口方法Info()
public void Info() {
System.out.println("This is a fiction, which has " + pages + " pages,"+" costs " + price + " yuan, and applicable for " + applicable_object +"." );
}
}
//杂志类
class Magazine implements Reading {
int price,pages;
String publish_time;
//构造方法
public Magazine(int price,int pages,String publish_time) {
this.pages = pages;
this.price = price;
this.publish_time = publish_time;
}
//实现接口方法Info()
public void Info() {
System.out.println("This is a magazine, which has " + pages + " pages,"+" costs " + price + " yuan, and publish once every " + publish_time +"." );
}
}
public class text7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fiction f = new Fiction(199, 12, "high school students");
f.Info();
Magazine m = new Magazine(3, 30, "6 months");
m.Info();
}
}
This is a fiction, which has 199 pages, costs 12 yuan, and applicable for high school students.
This is a magazine, which has 30 pages, costs 3 yuan, and publish once every 6 months.
/*
* 要求创建 A4Paper,A6Paper两个类,实现Paper接口,
* 并实现GetName方法在main方法中创建Printer对象,
* 并调用Print()方法两次,分别传入A4,A6类的对象,根据已有代码完成程序
*/
package Liu;
import java.util.Scanner;
//Paper接口
interface Paper {
public String GetName();
}
class Printer {
public void Print(Paper p) {
System.out.println(p.GetName());
}
}
class A4Paper implements Paper {
String PaperName;
//构造方法
public A4Paper(String PaperName) {
this.PaperName = PaperName;
}
//实现接口的GetName()方法
public String GetName() {
return this.PaperName;
}
}
class A6PAper implements Paper {
String PaperName;
//构造方法
public A6PAper(String PaperName) {
this.PaperName = PaperName;
}
//实现接口的GetName()方法
public String GetName() {
return this.PaperName;
}
}
//测试类
public class text8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
A4Paper a4Paper = new A4Paper(scanner.next());
A6PAper a6pAper = new A6PAper(scanner.next());
Printer aPrinter = new Printer();
aPrinter.Print(a4Paper);
Printer bPrinter = new Printer();
bPrinter.Print(a6pAper);
}
}
//输出
A4 A6
A4
A6
/**
* 要求完成一个接口ShowMessage,TV类调用接口“I am TV”。
* 输入:
*owen
*输出:
*I am TV owen
*/
package Liu;
import java.util.Scanner;
//接口ShowMessage
interface ShowMessage {
public void Show();
}
//TV类
class TV implements ShowMessage {
private String name;
//带参数构造方法
public TV(String name) {
this.name = name;
}
//实现接口抽象方法
public void Show() {
System.out.println("I am TV " + this.name);
}
}
public class text9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
ShowMessage tv = new TV(scanner.next());
tv.Show();
}
}
/**
* 已知一个抽象类Shape,该类有两个抽象方法,分用来计算面积和周长。
* 在此基础上派生出Rectangle和Circle类,分别实现GetArea方法
* 和GetPerimeter方法,用来计算矩形和圆的面积以及周长。
*/
package Liu;
import java.util.Scanner;
abstract class Shape {
abstract public void GetArea(); //抽象方法
abstract public void GetPerimeter(); //抽象方法
}
//派生类Rectangle
class Rectangle extends Shape {
private double length,width; //定义私有属性
//Rectangle类构造方法
public Rectangle(double length,double width) {
this.length = length;
this.width = width;
}
//实现抽象类GetArea()方法
public void GetArea() {
double area;
area = length * width;
System.out.println(area);
}
//实现抽象类GetPerimeter()方法
public void GetPerimeter() {
double perimeter;
perimeter = 2 * (length + width);
System.out.println(perimeter);
}
}
//派生类Circle
class Circle extends Shape {
double radius;
//带参数构造方法
public Circle(double radius) {
this.radius = radius;
}
//实现抽象类GetArea()方法
public void GetArea() {
double area;
final double PI = 3.14;
area = radius * radius * PI;
System.out.println(area);
}
//实现抽象类GetPerimeter()方法
public void GetPerimeter() {
double perimeter;
final double PI = 3.14;
perimeter = 2 * PI * radius;
System.out.println(perimeter);
}
}
public class text10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Rectangle r = new Rectangle(scanner.nextDouble(), scanner.nextDouble());
Circle c = new Circle(scanner.nextDouble());
r.GetArea();
r.GetPerimeter();
c.GetArea();
c.GetPerimeter();
}
}
输入输出说明:
输入:
4 5 6
输出:
20.0
18.0
113.04
37.68