編寫程序描述以下類層次,見圖1所示,其中人為一個(gè)普通類,其屬性包
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、王屋網(wǎng)站維護(hù)、網(wǎng)站推廣。括:姓名、性別、出生日期等,方法為 printInfo() 。教師包括學(xué)校和工號(hào)屬性;學(xué)生包括學(xué)校、學(xué)號(hào)和年級(jí)屬性;
編寫一個(gè)測(cè)試類 TestPerson,在 main 方法中創(chuàng)建 1 個(gè)教師和 1 個(gè)學(xué)生對(duì)象,輸出對(duì)象的所有屬性信息。
public class Person {
String name;
char sex;
String birthday;
public void printInfo() {
System.out.print("姓名:" + name +"\t"+ "性別:" + sex+"\t" + "出生日期:" + birthday); }}
public class Student extends Person{
String school;
String major;
int studentID;
int grade;
int zlass;//班級(jí)
}
public class Teacher extends Person{
String school;
int teacherID;}
測(cè)試類
public class TestPerson {
public static void main(String args[]) {
Teacher t = new Teacher();
Student s = new Student();
System.out.println("教師:");
t.name = "xxx";
t.sex = '男';
t.birthday = "2000-8-12";
t.teacherID = 234556465;
t.school = "xxx";
t.printInfo();
System.out.println("\t" + "工號(hào):" + t.teacherID + "\t" + "出生日期:" + t.birthday + "\t" + "學(xué)校:" + t.school);
System.out.println("學(xué)生:");
s.name = "xxx";
s.sex = '男';
s.birthday = "2000-8-12";
s.studentID = 234556465;
s.school = "xxx";
s.major = "計(jì)算機(jī)";
s.grade = 22;
s.zlass = 6;
s.printInfo();
System.out.println("\t" + "學(xué)號(hào):" + s.studentID + "\t" + "學(xué)校:" + s.school + "\t" + "專業(yè):" + s.major + "\t" + "年級(jí)" + s.grade + "班級(jí)" + s.zlass);
}}
定義一個(gè)父類:形狀類Shapes,里面有兩個(gè)方法,分別是求面積和周長(zhǎng)。
定義一個(gè)子類:矩形Rectangle
定義一個(gè)子類:三角形 Triagle
定義一個(gè)子類:圓 Circle
定義一個(gè)測(cè)試類:傳入圓的半徑4 輸出周長(zhǎng)和面積
傳入矩形的長(zhǎng)和寬4,5 輸出周長(zhǎng)和面積
傳入三角形三邊:3,4,5 輸出周長(zhǎng)和面積
public class Circle extends Shapes {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double Area() {
return Math.PI * radius * radius;
}
@Override
public double Perimeter() {
return (Math.PI * radius) * 2;
}}
public class Rectangle extends Shapes {
int width;
int height;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public double Area() {
return width * height;
}
@Override
public double Perimeter() {
return 2 * (width + height);}}
public abstract class Shapes {
public abstract double Area();
public abstract double Perimeter();
}
public class Triagle extends Shapes {
int a;
int b;
int c;
int d;
int p;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
@Override
public double Area() {
if ((a + b >c) && (a + c >b) && (c + b >a))
d = a + b + c;
p = d / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
@Override
public double Perimeter() {
return a + b + c;
}}
測(cè)試類
public class test {
@Test
public void Circle() {
Circle c = new Circle();
c.setRadius(4);
System.out.println("圓:");
System.out.println("面積" + c.Area());
System.out.println("周長(zhǎng)" + c.Perimeter());
}
@Test
public void Rectangle() {
Rectangle r = new Rectangle();
r.setHeight(4);
r.setWidth(5);
System.out.println("矩形:");
System.out.println("面積" + r.Area());
System.out.println("周長(zhǎng)" + r.Perimeter());
}
@Test
public void Triagle() {
Triagle t = new Triagle();
t.setA(3);
t.setB(4);
t.setC(5);
System.out.println("三角型:");
System.out.println("面積" + t.Area());
System.out.println("周長(zhǎng)" + t.Perimeter());
}}
(1)編寫一個(gè)矩形類Rect,包含:兩個(gè)屬性:矩形的寬width;矩形的高h(yuǎn)eight。
兩個(gè)構(gòu)造方法:
1.一個(gè)帶有兩個(gè)參數(shù)的構(gòu)造方法,用于將width和height屬性初化;
2.一個(gè)不帶參數(shù)的構(gòu)造方法,將矩形初始化為寬和高都為10。
兩個(gè)方法:
求矩形面積的方法area()
求矩形周長(zhǎng)的方法perimeter()
(2)通過繼承Rect類編寫一個(gè)具有確定位置的矩形類PlainRect,其確定位置用矩形的左上角坐標(biāo)來標(biāo)識(shí),包含:添加兩個(gè)屬性:矩形左上角坐標(biāo)startX和startY。
兩個(gè)構(gòu)造方法:
帶4個(gè)參數(shù)的構(gòu)造方法,用于對(duì)startX、startY、width和height屬性初始化;不帶參數(shù)的構(gòu)造方法,將矩形初始化為左上角坐標(biāo)、長(zhǎng)和寬都為0的矩形;
添加一個(gè)方法:判斷某個(gè)點(diǎn)是否在矩形內(nèi)部的方法isInside(double x,double y)。如在矩形內(nèi),返回true,?否則,返回false。
提示:點(diǎn)在矩形類是指滿足條件:x>=startX&&x<=(startX+width)&&y
(3)編寫PlainRect類的測(cè)試程序創(chuàng)建一個(gè)左上角坐標(biāo)為(10,10),長(zhǎng)為20,寬為10的矩形對(duì)象;計(jì)算并打印輸出矩形的面積和周長(zhǎng);判斷點(diǎn)(25.5,13)是否在矩形內(nèi),并打印輸出相關(guān)信息。
public class PlainRect extends Rect {
double startX;
double startY;
public double getStartY() {
return startY;
}
public void setStartY(double startY) {
this.startY = startY;
}
public double getStartX() {
return startX;
}
public void setStartX(double startX) {
this.startX = startX;
}
public PlainRect(double width, double height, double startX, double startY) {
this.width = width;
this.height = height;
this.startX = startX;
this.startY = startY;
}
public PlainRect() {
this.width = 0;
this.height = 0;
this.startX = 0;
this.startY = 0;
}
public boolean isInside(double x, double y) {
if (x >= startX && x<= (startX + width) && y< startY && y >= (startY - height)) {
return true;
} else {
return false;
}}}
public class Rect {
double width;
double height;
public Rect() {
this.width = 10;
this.height = 10;
}
public Rect(double width, double height) {
this.width = width;
this.height = height;
}
public double Area() {
return width * height;
}
public double Perimeter() {
return 2 * (width + height); }}
測(cè)試類
public class testPlainRect {
public static void main(String[] args) {
PlainRect p = new PlainRect(10, 20, 10, 10);
System.out.println("面積" + p.Area());
System.out.println("周長(zhǎng)" + p.Perimeter());
System.out.println(p.isInside(25.5, 13));}}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧