1、编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编等组成
public class Address {
private String country;
private String province;
private String city;
private String street;
private String zipcode;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Address(){}
public Address(String country, String province, String city, String street, String zipcode) {
this.country = country;
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getInfo() {
return "国家:"+this.country+" 省份:"+this.province+" 城市:"+this.city +" 街道:"+this.street+" 邮编:"+this.zipcode;
}
}
public class Address01 {
public static void main(String[] args) {
Address ad=new Address("中国","陕西省","西安","太白南路","700000");
System.out.println(ad.getInfo());
}
}
国家:中国 省份:陕西省 城市:西安 街道:太白南路 邮编:700000
2、定义并测试一个代表员工的Employee类。员工属性包括编号、姓名、基本薪水、薪水增长率,还包括计算薪水增长额及计算增长后的工资总额的操作方法。
public class Employee {
private long empno;
private String ename;
private double salary;
private double rate;
public long getEmpno() {
return empno;
}
public void setEmpno(long empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public Employee() {}
public Employee(long empno, String ename, double salary, double rate) {
this.empno = empno;
this.ename = ename;
this.salary = salary;
this.rate = rate;
}
public double salaryAdd() {
return this.salary*this.rate;
}
public double salaryAllAdd() {
return this.salary*(1+this.rate);
}
@Override
public String toString() {
return "Employee [编号=" + empno + ", 姓名=" + ename + ", 薪水=" + salary + ", 薪水增长率=" + rate + "]";
}
}
public class Employee01 {
public static void main(String[] args) {
Employee em=new Employee(1236549,"李雷",1000,0.1);
System.out.println(em.toString());
System.out.println("薪水增长额"+em.salaryAdd());
System.out.println("工资总额"+em.salaryAllAdd());
}
}
Employee [编号=1236549, 姓名=李雷, 薪水=1000.0, 薪水增长率=0.1]
薪水增长额100.0
工资总额1100.0
3、设计一个Dog类,有名字、颜色、年龄、等属性,定义构造方法来初始化这些属性,定义方法输出Dog信息。
public class Dog {
private String name;
private String color;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog() {}
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
@Override
public String toString() {
return "Dog [姓名=" + name + ", 颜色=" + color + ", 年龄=" + age + "]";
}
}
public class Dog01 {
public static void main(String[] args) {
Dog d=new Dog("小花","白色",5);
System.out.println(d.toString());
}
}
Dog [姓名=小花, 颜色=白色, 年龄=5]