在线等大佬解JAVA题
发布网友
发布时间:2024-03-13 21:54
我来回答
共3个回答
热心网友
时间:2024-06-22 14:40
第一题:
public class Employee {
private String id;
private String name;
private double pay;
public Employee(String id, String name, double pay) {
super();
this.id = id;
this.name = name;
this.pay = pay;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", pay=" + pay + "]";
}
}
public class Emploer extends Employee {
private double bonus;
public Emploer(String id, String name, double pay, double bonus) {
super(id, name, pay);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return "Emploer [id=" + super.getId() + ", name=" + super.getName() + ", pay=" +
super.getPay() + ", bonus=" + bonus + "]";
}
}
第二题:
public abstract class Shap {
public abstract double getArea();
}
public class Rectangle extends Shap {
private double i; // 长
private double k; // 宽
public Rectangle(double i, double k) {
super();
this.i = i;
this.k = k;
}
@Override
public double getArea() {
return i*k;
}
}
public class Triangle extends Shap {
private double i; // 一条边长
private double k; // 相应的高
public Triangle(double i, double k) {
super();
this.i = i;
this.k = k;
}
@Override
public double getArea() {
return i*k;
}
}
public class Circle extends Shap {
private double r; //半径
public Circle(double r) {
super();
this.r = r;
}
@Override
public double getArea() {
//Math.PI = 3.141592653589793 为java.lang包中的一个类的常量参数
return Math.PI*r*r;
}
}
public class Pillar {
private Shap bottom; // 底为可实例化的抽象父类
private double height; // 高
private double volume; // 体积
public Pillar(Shap bottom, double height) {
super();
this.bottom = bottom;
this.height = height;
}
public void calvolume(){
this.volume = bottom.getArea() * height;
}
public void print(){
System.out.println("这个柱体的体积为:"+ volume);
}
}
第三题:
public interface Advertisement {
public void getName();
public void getAdvert();
}
public class Advert implements Advertisement{
private boolean flag; // 权限 true为有权限,false为无权限。
private String name; // 公司名称
private String advert; // 公司广告词
private int mode; // 展示方式 (可自行定义方式,仅提供一个方案)
public Advert() {
super();
}
public Advert(boolean flag, String name,String advert,int mode) {
super();
this.flag = flag;
this.name = name;
this.advert = advert;
this.mode = mode;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void setName(String name) {
this.name = name;
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public void setAdvert(String advert) {
this.advert = advert;
}
@Override
public void getName() {
if(flag){
System.out.println("公司名称:"+name);
}
else{
System.out.println("您没查看的权限");
}
}
@Override
public void getAdvert() {
if(flag){
System.out.println("公司广告词为:"+advert);
}
else{
System.out.println("您没查看的权限");
}
}
public void show(){
System.out.println(name);
if(mode > 0){
// 模拟不同公司展示公司广告词的方式
switch (mode) {
case 1:
System.out.println("------------------------");
System.out.println(advert);
System.out.println("------------------------");
break;
case 2:
System.out.println("************************");
System.out.println(advert);
System.out.println("************************");
break;
case 3:
System.out.println("++++++++++++++++++++++++");
System.out.println(advert);
System.out.println("++++++++++++++++++++++++");
break;
case 4:
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println(advert);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
break;
}
}
}
}
public class AdvertTest {
public static void main(String[] args) {
Advert AdvertisementA = new Advert(true,"XX公司","让明天更美好!",2);
Advert AdvertisementB = new Advert(true,"YY公司","我有我的方式!",3);
AdvertisementA.show();
AdvertisementB.show();
AdvertisementA.setFlag(false);
AdvertisementA.getName();
AdvertisementA.getAdvert();
AdvertisementB.getName();
AdvertisementB.getAdvert();
}
}
热心网友
时间:2024-06-22 14:46
要是真的就马上有答案了
热心网友
时间:2024-06-22 14:40
//形状抽象类
public abstract class Shape {
public abstract double getArea();
}
//矩形类
public class Rectangle extends Shape {
@Override
public double getArea() {
return 0;
}
/**
* S=length*width
* S代表面积,length代表长,width代表宽
* @param length
* @param width
*/
public double getArea(double length,double width) {
return length*width;
}
}
//三角形类
public class Triangle extends Shape {
@Override
public double getArea() {
return 0;
}
/**
* S=(bottom*high)/2
* S代表面积,bottom代表底,high代表高
* @param bottom
* @param high
*/
public double getArea(double bottom,double high) {
return (bottom*high)/2;
}
}
//圆类
public class Circle extends Shape {
@Override
public double getArea() {
return 0;
}
/**
* S=Math.PI*r*r
* S代表面积,r代表半径,Math.PI代表圆周率π
* @param r
*/
public double getArea(double r) {
return Math.PI*(r*r);
}
}