Java practice 创建自定义作用的类

规定 输入1为计算圆形 2 则三角形 0 则结束程序

代码示例:

package test;

import java.util.Scanner;

public class areas{
	public static void main(String[] args) {
		
	    Scanner s=new Scanner(System.in);
	    Circ sd=new Circ() ;
	    int y=0;
	    double l=0,o=0,p=0;
	    Rectangle re=new  Rectangle();
	    int h=0;
	   while(s.hasNext())
	   {
		   h=s.nextInt();
		   if(h==1)
		   {
			   y++;
			   int k=s.nextInt();
			   int n=s.nextInt();
			   String str=s.next();
			   boolean st=s.nextBoolean();
			   Circ ds=new Circ(str,st,k) ;
			   o=ds.getArea();
			   System.out.printf("Circle area is:%.6f color is:%s\n",ds.getArea(),ds.getcolor());
		   }
		   else if(h==2)
		   {
			   y++;
			   int k1=s.nextInt();
			   int n1=s.nextInt();
			   String str1=s.next();
			   boolean st1=s.nextBoolean();
			   Rectangle er=new  Rectangle(str1,st1,k1,n1);
			   p=er.getArea();
			   System.out.printf("Rectangle area is:%.6f color is:%s\n",er.getArea(),er.getcolor());
		   }
		   else 
			   break;
		   h=0;
		   l=o+p+l;
		   o=0;
		   p=0;
		  
	   }
	   System.out.println("----------");
	    System.out.printf("%d objects, total area are: %.6f",y,l);
	    
	}
}
 abstract class puji {

	private String color;
	private boolean filled;
	
	public puji()
	{
		
	}
	public puji(String yan,boolean fill)
	{
		this.color=yan;
		this.filled=fill;
	}
	public abstract double getArea();
	
	public void setcolor(String yan)
	{
		color=yan;
	}
	public void setfilled(boolean filled)
	{
		filled=filled;
	}
	public String getcolor()
	{
		return color;
	}
	public boolean getfilled()
	{
		return filled;
	}
	
}

class Circ extends puji {
	public int r;
	public Circ()
	{
		
	}
	public Circ(String yan,boolean fill,int r)
	{
		super(yan,fill);
		this.r=r;
	}
	public  double getArea()
	{
		return Math.PI*r*r;
	}
}

class Rectangle extends puji
{
	public int l,w;
	
	public Rectangle()
	{
		
	}
	
	public Rectangle(String yan,boolean fill,int l,int w)
	{
		super(yan,fill);
		this.l=l;
		this.w=w;
	}
	
	public Rectangle(int l,int w)
	{
		this.l=l;
		this.w=w;
	}
	
	public  double getArea()
	{
		return l*w;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值