# 1、编写输出球体、立方体、圆柱体体积 java 源程序:

有一个抽象类 ObjectVolume,如下所示:

abstract class ObjectVolume

{

abstract double getVolume (); // 返回物体的体积

}

(1)编写球体类,立方体类和圆柱体类,它们是抽象类 ObjectVolume 的子类。

(2)编写一个公共类,其中包含 main 方法和 static void get (ObjectVolume obj) 方法。

(3)在 main 方法中,从键盘上输入 1(表示球体)、2(表示立方体)、3(表示圆柱体)时,调用 get 方法可以分别得到球体、立方体、圆柱体的体积。

程序的运行结果如下图所示(供参考,其中,圆周率的值设置的不同,结果会有所不同):

image-20201102135325174

package javawork;
import java.util.*;
abstract class ObjectVolume
{
    abstract double getVolume(); // 返回物体的体积
}
class Sphere extends ObjectVolume // 球体子类
{
	double Radius;
	public double getVolume() 
	{
		return 4*Math.PI*Radius*Radius*Radius/3;
	}
	public Sphere(double r)
	{
		this.Radius=r;
	}
}
class Cube extends ObjectVolume  // 立方体子类
{
	double cubelength;
	public double getVolume() 
	{
		return cubelength*cubelength*cubelength;
	}
	public Cube(double l)
	{
		this.cubelength=l;
	}
}
class Cylinder extends ObjectVolume // 圆柱体子类
{
	double h;
	double r;
	double getVolume() {
		return Math.PI*r*r*h;
	}
	public Cylinder(double r2, double h2) 
	{
		this.h=h2;
		this.r=r2;
	}
}
public class Volume {
	static void get(ObjectVolume obj)
	{
		double v=obj.getVolume();
		System.out.printf("体积为:%.2f",v);
	}
	static void outSphereVolume()  // 输出球的体积
	{
		Scanner sca=new Scanner(System.in);
		System.out.println("请输入球半径:");
		double r1=sca.nextDouble();
		Sphere obj= new Sphere(r1);
		get(obj);
	}
	static void outCubeVolume()  // 输出立方体的体积
	{
		Scanner sca2=new Scanner(System.in);
		System.out.println("请输入立方体边长:");
		double l1=sca2.nextDouble();
		Cube obj= new Cube(l1);
		get(obj);
	}
	static void outCylinderVolume()  // 输出圆柱的体积
	{
		Scanner sca3=new Scanner(System.in);
		System.out.println("请输入圆柱的底面半径和高:");
		double r2=sca3.nextDouble();
		double h2=sca3.nextDouble();
		Cylinder obj= new Cylinder(r2,h2);
		get(obj);
	}
	public static void main(String[] args) {
		System.out.println("请输入整数(1表示球体,2表示立方体,3表示圆柱体):");
		Scanner sc=new Scanner(System.in);
		int num=sc.nextInt();
		switch(num)
		{
		case 1:
			outSphereVolume(); break;
		case 2:
			outCubeVolume();break;
		case 3:
			outCylinderVolume();break;
		}
	}
}

# 2、计算商品销售总额 java 源程序:

有一个接口 UnitPrice,如下所示:

interface UnitPrice

{

public double unitprice ( ); // 返回商品的单价

}

(1)编写电视机类、计算机类和手机类来实现接口 UnitPrice。

(2)编写一个公共类,其中包含 main 方法和 static double get (UnitPrice u) 方法。

(3)在 main 方法中,从键盘上输入电视、计算机和手机的销售个数,调用 get 方法可以分别得到商店各个商品的单价,之后计算商品的总销售额。

程序的运行结果如下图所示(供参考):

image-20201102135601721

package javawork;
import java.util.*;
interface UnitPrice
{
   public double unitprice( ); // 返回商品的单价
}
class TV implements UnitPrice  // 电视类
{
	public double unitprice() {
		return 5000;
	}
}
class PC implements UnitPrice  // 计算机类
{
	public double unitprice() {
		return 6000;
	}
}
class Phone implements UnitPrice  // 手机类
{
	public double unitprice() {
		return 3000;
	}
}
public class Price {  // 主类
	static double get(UnitPrice u)  // 获得单价
	{
		double pric=u.unitprice();
		return pric;
	}
	
	public static void main(String[] args) {
		System.out.println("请分别输入本月电视、计算机、手机的销售个数:");
		Scanner sc=new Scanner(System.in);
		int tvnum=sc.nextInt();
		int pcnum=sc.nextInt();
		int phonenum=sc.nextInt();
		TV tv=new TV();
		PC pc=new PC();
		Phone phone=new Phone();
		double sumprice=tvnum*get(tv)+pcnum*get(pc)+phonenum*get(phone);
		System.out.println("总销售额:"+sumprice);
	}
}

# 程序仅作参考

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Lavender 微信支付

微信支付

Lavender 支付宝

支付宝