java小编程 帮帮忙作业!

java小编程 帮帮忙作业!
1.定义一个抽象类Shape,包含求面积area和周长perimeter的两个函数。定义圆Circle类,继承Shape类。定义三角形Triangle,继承Shape类。定义长方形类Square,继承Shape类。并完成以上类的测试。

2.定义一个Shape接口,包含求面积和周长的两个函数。定义一个坐标类Coordinates,包含两个成员变量X坐标和Y坐标。定义圆Circle类,继承Coordinates并实现Shape接口。定义三角形Triangle,继承Coordinates并实现Shape接口。定义长方形类Square,继承Coordinates并实现Shape接口。并完成以上类的测试。

3.编写程序,首先声明使用的包名jacky,然后创建 YMD 类,该类具有两个成员方法,功能分别为计算今年的年份以及输出一个带有年月日的字符串。

4.编写使用包jacky中YMD 类的程序,LX.java 程序功能:给定某人姓名与出生日期,计算该人年龄,并输出该人姓名,年龄,出生日期。程序使用 YMD 的计算年份的方法来计算年龄。
经过调铅握试,运行正确的
第一题
abstract class Shape
{abstract double area();
abstract double perimeter();
void print()
{System.out.println("面积是:"+area()+",周长是:"+perimeter());}
}
class Circle extends Shape
{double radius;
Circle(double radius)
{this.radius=radius;}
double area()
{return 3.14*radius*radius;}
double perimeter()
{return 2*3.14*radius;}
}
class Triangle extends Shape
{int a;
int b;
int c;
int p=(a+b+c)/棚激返2;
Triangle(int a,int b,int c)
{this.a=a;
this.b=b;
this.c=c;}
double area()
{return Math.sqrt(p*(p-a)*(p-b)*(p-c)) ;}
double perimeter()
{return a+b+c;}
}
class Square extends Shape
{double width;
double height;
Square(double width,double height)
{this.width=width;
this.height=height;}
double area()
{return width*height;}
double perimeter()
{return 2*(width+height);}
}
public class Testshape
{public static void main(String args[])
{Circle c1=new Circle(8.6);
System.out.println("圆的半径是:"+c1.radius);
c1.print();
Triangle t1=new Triangle(3,4,5);
System.out.println("三角形的边长分别是:"+t1.a+","+t1.b+","+t1.c);
t1.print();
Square s1=new Square(6.3,4.5);
System.out.println("长方形的长是:"+s1.width+",宽是:"+s1.height);
s1.print();
}
}

第3题
package jacky; // 声明链饥一个jacky包,区分大小写
import java.util.*;
public class YMD{
private int year,month,day;
public static void main(String []args)
{}
public YMD(int y, int m ,int d){
year=y;
month=(((m>=1)&(m<=12))?m:1);
day=(((d>=1) & (d<=31))?d:1); }
public YMD(){
this(0,0,0); }
public static int thisyear(){
return Calendar.getInstance().get(Calendar.YEAR); }//获得当年的年份
public int year(){
return year; }
public String toString(){
return year+"-"+month+"-"+day; //转化为字符串
}}

第4题
import jacky.YMD;
public class P3{
private String name;
private YMD birth;
public static void main(String args[]){
P3 a=new P3("张弛",1990,1,11);
a.output();}
public P3(String n1,YMD d1){
name=n1;
birth=d1; }
public P3(String n1,int y,int m,int d){
this(n1,new YMD(y,m,d)); //初始化变量与对象,调用上一个构造方法}
public int age()
{return YMD.thisyear()-birth.year(); //计算年龄 }
public void output(){
System.out.println("姓名: "+name);
System.out.println("出生年龄: "+birth.toString());
System.out.println("今年年龄: "+age());
}}
顶下,另外,这也太初级了,建议自己去机器上下试下,
← 上一篇: 下一篇: →