编写java程序,程序中定义一个抽象类Area、两个Area类的派生子类RectArea类和RoundArea类、以及实现类Impl

一道简单的java编程题。~

public class Abstractor Area{
public abstractor double area();
}
public class RoundArea{

public RoundArea(double ra){this.area=ra}
private double area;
public double area(){
//求圆的面积
}
}
public class RectArea {
private double width;
private double leng;
public RectArea(double width,double leng){this.width = width; this.leng=leng}
public double area(){
//求长方形的面积
}
}

class ImpleArea{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
area=br.read();
width= br.read();
leng = br.read();
RoundArea ra = new RoundArea(area);
double area1 = ra.area();
RectArea ra2 = new RectArea(width,leng);
double area2 = ra2.area();
}

大概写了下 参考一下吧

public abstract class Shape {public abstract double getArea();public abstract double getLength();}public class Rectangle extends Shape{private double length;private double width;public Rectangle(double length, double width) {super();this.length = length;this.width = width;}@Overridepublic double getArea() {return width*length;}@Overridepublic double getLength() {return width*2+length*2;}}public class Circle extends Shape{private double sidelength;public Circle(double sidelength) {super();this.sidelength = sidelength;}@Overridepublic double getArea() {return Math.pow(sidelength, 2);}@Overridepublic double getLength() {return sidelength*4;}}ublic class Test {public static void main(String[] args) {Rectangle rectangle = new Rectangle(5, 6);double area1=rectangle.getArea();double length2=rectangle.getLength();System.out.println("矩形的面积是:"+area1);System.out.println("矩形的周长是:"+length2);Circle circle = new Circle(7);double area = circle.getArea();double length = circle.getLength();System.out.println("正方形的面积是:"+area);System.out.println("正方形的周长是"+length);}}//结果:矩形的面积是:30.0矩形的周长是:22.0正方形的面积是:49.0正方形的周长是28.0

abstract class Area{
abstract double area();
}

//矩形面积类
class RectArea extends Area{
private double width; //宽
private double height; //高

public RectArea(){
}

public RectArea(double width, double height){
this.width = width;
this.height = height;
}

public double area() {
return this.width * this.height;
}

public double getWidth(){
return this.width;
}

public void setWidth(double width){
this.width = width;
}

public double getHeight(){
return this.height;
}

public void setHeight(double height){
this.height = height;
}
}

//圆面积类
class RoundArea extends Area{
private double radius; //半径

public RoundArea(){
}

public RoundArea(double radius){
this.radius = radius;
}

public double area() {
return Math.PI * this.radius * this.radius;
}

public double getRadius(){
return this.radius;
}

public void setRadius(double radius){
this.radius = radius;
}
}

class ImpleArea{
public void displayArea(){
Scanner scan = new Scanner(System.in);
Area a;

a = new RectArea();
System.out.print("矩形的宽:");
((RectArea)a).setWidth(scan.nextDouble());
System.out.print("矩形的高:");
((RectArea)a).setHeight(scan.nextDouble());
System.out.println("矩形的面积:" + a.area());

a = new RoundArea();
System.out.print("圆的半径:");
((RoundArea)a).setRadius(scan.nextDouble());
System.out.println("圆的面积:" + a.area());
}
}

public class Test {
public static void main(String[] args) {
ImpleArea impl = new ImpleArea();
impl.displayArea();
}
}

像这种简单的程序,你们的课本上应该有吧,如果我们帮你写出来,感觉没多大的意义哦,我记得这个求面积的例子,课本上是有的,自己好好的研究下应该就可以写出来这个程序了,java其实是个很有趣的语言,你只要认真去分析下,你会觉得java太容易了!
好好的了解下java类和接口的定义,继承类,还有java.io,要相信自己哦!~

编写一个Java程序,在程序中定义个Employee类,该类有两个私有属性:Strin...
答:class Employee{ private String name; private String address; public Employee(String name){ this.name = name; } public Employee(String name, String address){ this(name); this.address = address; } public void show(){ System.out.println("My name i...

Java作业求帮助 编写一个Java程序,在程序中定义一个PersonB类,定义一...
答:class PersonB { String name ;int age;public PersonB(){ System.out.println("PersonB()被调用");} public PersonB(String newName){ name = newName;System.out.println("PersonB(String newName)被调用");} public void introduce( ){ System.out.println("我是"+name+",今年"+age...

Java作业求帮助 题一: 编写一个Java程序,在程序中定义一个PersonA类...
答:代码1:extends PersonA 代码2:s1.setName("张三");代码3:s1.setDepartment("计算机系");代码4:System.out.println(s1.getName());代码5:System.out.println(s1.getDepartment());注意,后四句最后的分号别落下了

在java中编写程序,定义一个学生类
答:import java.util.ArrayList;import java.util.List;/ 编写一个Java应用程序,该程序包括3个类:定义一个学生类,Student有姓名,学号,选学的课程列表 定义一个课程类:课程名称,课程分数 (1)初始化一个学生,选择了三门课程,并添加到学生的属性中 (2)实现统计学生的总分功能 1.正确编写2个类(5...

1.编写一个Java程序,在程序中定义一个抽象类Shape,里面有一个求面积...
答:a*b;}}Test1.java类 import java.util.Scanner;public class Test1{public static void main(String[] args){Scanner sca = new Scanner(System.in);double aa = sca.nextDouble();double b = sca.nextDouble();Rectangle a = new Rectangle(aa,b);System.out.println(a.GetArea());} } ...

编写一个Java应用程序,要求在程序中定义一个名为“Revers
答:public class Revers { public static void main(String[] args) { String letters = "acpefgsythmd"; System.out.printf("排序前:"); System.out.println(letters); char[] c = letters.toCharArray(); Arrays.sort(c); System.out.printf("排序后:"); System.ou...

编写java程序,定义一个2*3的数组a和一个3*4的数组b,在定义一个数组c...
答:/ public class JavaApplication3 { / param args the command line arguments / static void init(int [][]p,int la,int lb){ System.out.println("请输入二维数组元素:"+la+"行 "+lb+"列");int i,j;Scanner sc = new Scanner(System.in);byte []by = new byte[10];try { for(...

编写Java应用程序,在main方法在中定义字符串
答:public static void main(String[] args) { String str = "The first snow came, how beautiful it was!";String ch ;int count = 0;for(int i = 0 ; i <str.length() ; i++) { ch = String.valueOf(str.charAt(i)); //截取当前位置的字符,转换为字符串后将引用值赋给ch ...

编写java应用程序,定义类Car,有3个变量:字符串name,表示汽车品牌,字符...
答:编写java应用程序,定义类Car,有3个变量:字符串name,表示汽车品牌,字符串color,表示颜色是publicclassTest{publicstaticvoidmain(String[]args){ List<Course>courses=newArrayList<>()。//初始化3门课程 Coursecourse1=newCourse("java",80)。Coursecourse2=newCourse("Math",60)。Coursecourse3=...

编写一个Java应用程序,要求在程序中自定义一个类,该类对象。
答:import java.util.regex.Pattern;public class test{ private static String str = "78.86,12,25";private static void calc(String str){ String temp = "";double sum = 0;str+=","; //将最后一个数字后面添加一个逗号,否则正则表达式无法取出最后一个数字 Pattern p = Pattern.compile("...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网