1、定义手机类,手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能。
请定义出手机类,类中要有空参、有参构造方法,set/get方法。
定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。
调用对象的两个功能,打印效果如下:
正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
public class phone {
private String brand;
private String color;
private int price;
public phone(String brand, String color, int price) {
this.brand = brand;
this.color = color;
this.price = price;
}
public phone() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void call(){
System.out.println("打电话...");
}
public void sendMessage(){
System.out.println("发短信...");
}
public void show(){
System.out.print("正在使用价格为"+price+"元"+color+"的"+brand);
}
}
public class phoneText {
public static void main(String[] args) {
phone p = new phone("小米手机","黑色",3988);
p.show();
p.call();
p.show();
p.sendMessage();
}
}
2、定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,
然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
我女朋友叫XX,身高170.0厘米,体重110.0斤
女朋友帮我洗衣服
女朋友给我做饭
public class girlFriend {
private String name;
private double high;
private double weight;
public girlFriend() {
}
public girlFriend(String name, double high, double weight) {
this.name = name;
this.high = high;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void wash(){
System.out.println(this.name+"帮我洗衣服");
}
public void cook(){
System.out.println(this.name+"给我做饭");
}
public void show(){
System.out.println("我女朋友叫"+name+",身高"+high+"厘米,体重"+weight+"斤");
}
}
public class girlFriendText {
public static void main(String[] args) {
girlFriend g = new girlFriend("以枚",170.0,110.0);
g.show();
g.wash();
g.cook();
System.out.println();
System.out.println("遇到这样的,赶紧娶了吧");
}
3、定义项目经理类Manager。属性:姓名name,工号id,工资salary,奖金bonus。行为:工作work()
定义程序员类Coder。属性:姓名name,工号id,工资salary。行为:工作work()
要求:
1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造,set和get方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法)
3.调用成员方法,打印格式如下:
工号为123基本工资为15000奖金为6000的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....
工号为135基本工资为10000的程序员正在努力的写着代码......
public class coder {
private String name;
private int id;
private double salary;
public void work(){
System.out.println("正在努力的写着代码......");
}
public void show(){
System.out.print("工号为"+id+"基本工资为"+salary+"的"+name+"程序员");
}
public coder() {
}
public coder(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public class manager {
private String name;
private int id;
private double salary;
private double bonus;
public void work(){
System.out.println("正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
public void show(){
System.out.print("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的项目经理");
}
public manager() {
}
public manager(String name, int id, double salary, double bonus) {
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public static void main(String[] args) {
manager m = new manager();
m.setBonus(5000.0);
m.setId(150170);
m.setName("赵四");
m.setSalary(15000.0);
m.show();
m.work();
coder c = new coder();
c.setID(124);
c.setName("王五");
c.setSalary(12000);
c.show();
c.work();
}