小白Java编程训练三

/*
 * 设计一个读物接口,小说,杂志,期刊类实现这个接口,每种读物有相同的属性,
 * 如页数,价格等,也有不同的属性,如杂志和期刊都有出版周期,而课本有适用对象
 * 写一个测试类产生一系列读物,并输出它们的信息
 */

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

/**
 * 创建一个描述长方体的父类Cuboid,其内定义成员变量height,width,length,
 * 计算长方体的体积的方法volume().创建继承类Cuboid的子类CuboidWeight,为其添加表示
 * 密度的新成员density和计算长方体重量的方法weight()。再创建继承CuboidWeight的子类
 * CuboidValue,为其添加新成员value和计算价值的方法cost()。
 * 最后,在main()方法的创建类InfoCuboid,输出长方体的体积,重量和价值。
 */
package Liu;

import java.util.Scanner;

//描述长方体的父类Cuboid
class Cuboid {
	private double height,width,length;
	//父类构造方法
	public Cuboid(double height,double width,double length) {
		this.height = height;
		this.length = length;
		this.width = width;
	}
	//计算长方体的体积的方法volume()
	public double volume() {
		double volume;
		volume = height * length * width;
		return volume;
	}
}
//继承类Cuboid的子类CuboidWeight
class CuboidWeight extends Cuboid {
	double density;
	//子类CuboidWeight构造方法
	public CuboidWeight(double height,double width, double length, double density) {
		super(height, width, length);
		this.density = density;
	}
	//计算长方体重量的方法weight()
	public double weight() {
		double weight;
		weight = super.volume() * density;
		return weight;
	}
}
//再创建继承CuboidWeight的子类CuboidValue
class CuboidValue extends CuboidWeight {
	double value;
	//CuboidValue类构造方法
	public CuboidValue(double height,double width, double length,double density, double value) {
		super(height, width, length, density);
		this.value = value;
	}
	//计算价值的方法cost()
	public double cost() {
		double cost;
		cost = super.weight() * this.value;
		return cost;
	}
}
public class text11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		CuboidValue InfoCuboid = new CuboidValue(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		System.out.println("长方体体积:" + InfoCuboid.volume());
		System.out.println("长方体重量:" + InfoCuboid.weight());
		System.out.println("长方体价值:" + InfoCuboid.cost());
		

	}

}
//输出
3 4 5 6 7
长方体体积:60.0
长方体重量:360.0
长方体价值:2520.0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值