要求:Trangle 类具有类型为double 的三个边,以及属性,Trangle 类具有返回周长、面积以及修改三个边的功能。另外,Trangle 类还具有一个boolean 型的属性,该属性用来判断三个属能否构成一个三角形。Lader 类具有类型double 的上底、下底、高、面积属性,具有返回面积的功能。Circle类具有类型为double的半径、周长和面积属性、返回周长、面积的功能。
//三角形类
class Trangle {
private double sideA;
private double sideB;
private double sideC;
private double girth;
private double area;
private boolean isTrangle;
public Trangle(double sideA, double sideB, double sideC) {
this.setSideA(sideA);
this.setSideB(sideB);
this.setSideC(sideC);
}
//进行判断是否构成三角形
public boolean isTrangle() {
if (sideA + sideB > sideC && sideB + sideC > sideA && sideA + sideC > sideB) {
return true;
} else {
return false;
}
}
//设置信息
public void setSideA(double a) {
this.sideA = a;
}
public void setSideB(double b) {
this.sideB = b;
}
public void setSideC(double c) {
this.sideC = c;
}
//获取信息
public double getSideA() {
return sideA;
}
public double getSideB() {
return sideB;
}
public double getSideC() {
return sideC;
}
public double getGirth() {
isTrangle = isTrangle();
if (isTrangle) {
girth = sideA + sideB + sideC;
return girth;
} else {
System.out.println("不能形成三角形");
return 0;
}
}
public double getArea() {
isTrangle = isTrangle();
if (isTrangle) {
double p = (sideA + sideB + sideC) /2.0;
area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
return area;
} else {
System.out.println("不能形成三角形");
return 0;
}
}
}
//梯形类
class Lader{
private double shang,xia,height,area;
public Lader(double shang,double xia,double height){
this.setShang(shang);
this.setXia(xia);
this.setHeight(height);
}
//设置信息
public void setShang(double a){
shang = a;
}
public void setXia(double b) {
xia = b;
}
public void setHeight(double c){
this.height = c;
}
//获取信息
public double getShang() {
return shang;
}
public double getXia() {
return xia;
}
public double getHeight() {
return height;
}
public double getArea() {
area = (shang+xia)*height/2.0;
return area;
}
}
//圆类
class Circle{
private double r,girth,area;
public Circle(double r){
this.setR(r);
}
public void setR(double r) {
this.r = r;
}
public double getR() {
return r;
}
public double getGirth(){
girth = Math.PI*r*2.0;
return girth;
}
public double getArea() {
area = Math.PI*r*r;
return area;
}
}
//测试
public class Test_9 {
public static void main(String[] args) {
double area,girth;
Trangle trangle = new Trangle(6, 8, 10);
Lader lader = new Lader(7, 10, 4);
Circle circle = new Circle(7);
girth = trangle.getGirth();
area = trangle.getArea();
System.out.println("三角形的周长为:" + girth);
System.out.println("三角形的面积为:" + area);
trangle.setSideA(3);
trangle.setSideB(4);
trangle.setSideC(5);
System.out.println("修改后:a= "+trangle.getSideA()+",b="+trangle.getSideB()+",c="+trangle.getSideC());
area = lader.getArea();
System.out.println("梯形的面积为:"+area);
girth = circle.getGirth();
area = circle.getArea();
System.out.println("圆的周长为:"+girth);
System.out.println("圆的面积为:"+area);
}
}
要求:利用构造方法完成设置信息,其中有四个重载的构造方法
单参:只传递员工号,则员工姓名:无名氏。薪水:0,部门:未定。双参:传递员工号,姓名,则员工薪水为 1000,部门:后勤
4 参:传递员工号,姓名,部门,薪水
无参:则均为空值
class Staff{
private int sno;
private String name;
private int salary;
private String department;
//无参
public Staff(){}
public void tell0(){
System.out.println("员工号:"+getSno()+",姓名:"+getName()+",薪水:"+getSalary()+",部门:"+getDepartment());
}
//单参
public Staff(int sno){
this.setSno(sno);
this.setName("无名氏");
this.setSalary(0);
this.setDepartment("后勤");
}
public void tell1(){
System.out.println("员工号:"+getSno()+",姓名:"+getName()+",薪水:"+getSalary()+",部门:"+getDepartment());
}
//双参
public Staff(int sno,int salary){
this.setSno(sno);
this.setName(name);
this.setSalary(1000);
this.setDepartment("未定");
}
public void tell2(){
System.out.println("员工号:"+getSno()+",姓名:"+getName()+",薪水:"+getSalary()+",部门:"+getDepartment());
}
//四参
public Staff(int sno,String name,int salary,String department){
this.setSno(sno);
this.setName(name);
this.setSalary(salary);
this.setDepartment(department);
}
public void tell3(){
System.out.println("员工号:"+getSno()+",姓名:"+getName()+",薪水:"+getSalary()+",部门:"+getDepartment());
}
public void setSno(int n){
sno = n;
}
public void setName(String a){
name = a;
}
public void setSalary(int s){
salary = s;
}
public void setDepartment(String d){
department = d;
}
public int getSno(){
return sno;
}
public String getName(){
return name;
}
public int getSalary() {
return salary;
}
public String getDepartment(){
return department;
}
}
public class Test4 {
public static void main(String[] args){
Staff p0 = new Staff();
System.out.println("无参:");
p0.tell0();
Staff p1 = new Staff(189023);
System.out.println("单参:");
p1.tell1();
Staff p2 = new Staff(189023,100000);
System.out.println("双参:");
p2.tell2();
Staff p3 = new Staff(189023,"小明",10000,"管理部门");
System.out.println("四参:");
p3.tell3();
}
}
要求:1、完成以上各类中的setter 和getter 接口
2、测试以上各类,并写出每个测试用例
class Book{
private String name;
private String ISBN;
private String number;
public Book(String name,String ISBN,String number){
this.setName(name);
this.setISBN(ISBN);
this.setNumber(number);
}
//方法调用
public void print(){
System.out.println("书名:"+getName()+",书号:"+getISBN()+",册数:"+getNumber());
}
//设置图书信息
public void setName(String name){
this.name = name;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public void setNumber(String number) {
this.number = number;
}
//取得图书信息
public String getName(){
return name;
}
public String getISBN(){
return ISBN;
}
public String getNumber(){
return number;
}
}
class Role{
private String user;
private String password;
private String type;
public Role(String user,String password,String type){
this.setUser(user);
this.setPassword(password);
this.setType(type);
}
//方法调用
public void print(){
System.out.println("用户:"+getUser()+",密码:"+getPassword()+",角色类型:"+getType());
}
//设置用户信息
public void setUser(String user){
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setType(String type) {
this.type = type;
}
//取得信息
public String getUser(){
return user;
}
public String getPassword() {
return password;
}
public String getType() {
return type;
}
}
public class Test_9 {
public static void main(String[] args){
Student student = new Student(2020189000,"小李","男","大数据","2022级大数据本科班");
student.print();
Book book = new Book("唐诗宋词","83294792","9");
book.print();
Role role = new Role("用户名","*****","射手C位");
role.print();
}
}
public class Test_9 {
public static void main(String[] args){
String str = "Java 技术学习班 20121205";
System.out.print("提取:");
System.out.println(str.substring(11));
String s = "JavaEE";
System.out.print("替换:");
System.out.println(str.replace("Java","JavaEE"));
System.out.print("取出第八个字符:");
System.out.println(str.substring(7,8));
System.out.println("清除所有的0:");
System.out.println(str.replace("0",""));
String str2 = "411422200009202345";
System.out.println("提取出生日期:");
System.out.println(str2.substring(6,14));
}
}
正在上传…重新上传取消正在上传…重新上传取消 |