这是我从网上看到的习题,拿来做了做,四个题目基本上都是一样的,就是建立一个类,调用其中的方法或者属性。代码我自己写的,写的不是很好,以后有时间再来优化吧。
目录
题目1
- 定义手机类,手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能。
请定义出手机类,类中要有空参、有参构造方法,set/get方法。
定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。
调用对象的两个功能,打印效果如下:
正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
代码如下:
public class Phone {
private String brand;
private int price;
private String color;
public Phone(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public Phone(){
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void call(){
System.out.println("正在使用价格为"+getPrice()+"元"+getColor()+"的"+getBrand()+"手机打电话....");
}
public void sendMessage(){
System.out.println("正在使用价格为"+getPrice()+"元"+getColor()+"的"+getBrand()+"手机发短信....");
}
}
public class Test01 {
public static void main(String[] args) {
Phone phone = new Phone("小米",3998,"黑色");
phone.call();
phone.sendMessage();
}
}
题目2
- 定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
我女朋友叫凤姐,身高155.0厘米,体重130.0斤
女朋友帮我洗衣服
女朋友给我做饭
代码如下:
public class GrilFriend {
private String name;
private double height;
private double weight;
public GrilFriend(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void show(){
System.out.println("我的女朋友叫"+getName()+",身高"+getHeight()+"厘米"+",体重"+getWeight()+"斤");
}
public void wash(){
System.out.println("女朋友帮我洗衣服");
}
public void cook(){
System.out.println("女朋友帮我做饭");
}
}
题目3
- 定义项目经理类Manager。属性:姓名name,工号id,工资salary,奖金bonus。行为:工作work()
定义程序员类Coder。属性:姓名name,工号id,工资salary。行为:工作work()
打印格式如下
工号为123基本工资为15000奖金为6000的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....
工号为135基本工资为10000的程序员正在努力的写着代码......
代码如下
public class Manager {
private String name;
private String id;
private double salary;
private double bonus;
public Manager(){
}
public Manager(String name, String id, double salary, double bonus) {
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work(){
System.out.println("工号为"+getId()+"基本工资为"+getSalary()+"奖金为"+getBonus()+"的"+getName()+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码....");
}
}
public class Coder {
private String name;
private String id;
private double salary;
public Coder(){
}
public Coder(String name, String 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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work(){
System.out.println("工号为"+getId()+"基本工资为"+getSalary()+"的"+getName()+"正在努力的写着代码....");
}
}
public class Test03 {
public static void main(String[] args) {
Manager manager = new Manager("项目经理","123",15000,6000);
manager.work();
Coder coder = new Coder("程序员","135",10000);
coder.work();
}
}
题目4
- 定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse()
定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家lookHome()
调用成员方法,打印格式如下:
花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....
代码如下:
public class Cat {
private String color;
private String breed;
public Cat() {
}
public Cat(String color, String breed) {
this.color = color;
this.breed = breed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void eat(){
System.out.println(getColor()+"的"+getBreed()+"正在吃鱼.....");
}
public void catchMouse(){
System.out.println(getColor()+"的"+getBreed()+"正在逮老鼠.....");
}
}
public class Dog {
private String color;
private String breed;
public Dog() {
}
public Dog(String color, String breed) {
this.color = color;
this.breed = breed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void eat(){
System.out.println(getColor()+"的"+getBreed()+"正在啃骨头.....");
}
public void lookHome(){
System.out.println(getColor()+"的"+getBreed()+"正在看家.....");
}
}
public class Test04 {
public static void main(String[] args) {
Cat cat = new Cat("花色","波斯猫");
cat.eat();
cat.catchMouse();
Dog dog = new Dog("黑色","藏獒");
dog.eat();
dog.lookHome();
}
}