第一题:分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()

要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并使用set方式给属性赋值(价格:998,品牌:小米)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的手机打电话....
正在使用小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....
public class Phone {
private String brand;
private double price;
public Phone(){
}
public Phone(String brand,double price){
this.brand=brand;
this.price=price;
System.out.println("品牌:"+brand+",价格:"+price);
}
public void call(){
System.out.println("正在使用价格为"+price+"元的手机打电话....");
}
public void sendMessage(){
System.out.println("正在使用"+brand+"品牌的手机发短信....");
}
public void playGame(){
System.out.println("正在使用价格为"+price+"元的小米品牌的手机玩游戏....");
}

public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Test {
public static void main(String[] args) {
Phone phone=new Phone("小米",998);
//Phone phone=new Phone();
//phone.setBrand("小米");
//phone.setPrice(998);
phone.call();
phone.sendMessage();
phone.playGame();
}
}


第二题:分析以下需求,并用代码实现
1.猫类Cat
属性:
毛的颜色color
品种breed
行为:
吃饭eat()
抓老鼠catchMouse()
2.狗类Dog
属性:
毛的颜色color
品种breed
行为:
吃饭()
看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用每个对象的成员方法,打印格式如下:
花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....
public class Cat {
private String color;
private String breed;
public void eat(){
System.out.println(color+"的"+breed+"正在吃鱼.....");
}
public void catchMouse(){
System.out.println(color+"的"+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 Cat(){
}
public Cat(String color,String breed){
this.color=color;
this.breed=breed;
}
}
public class Dog {
private String color;
private String breed;
public void eat(){
System.out.println(color+"的"+breed+"正在啃骨头.....");
}
public void lookHome(){
System.out.println(color+"的"+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 Dog(){
}
public Dog(String color,String breed){
this.color=color;
this.breed=breed;
}
}
public class Test {
public static void main(String[] args) {
Cat cat=new Cat("花色", "波斯猫");
cat.eat();
cat.catchMouse();

Dog dog=new Dog("黑色", "藏獒");
dog.eat();
dog.lookHome();
}
}

第三题:分析以下需求,并用代码实现
1.老师类Teacher
属性:
姓名name
年龄age
讲课内容content
行为:
吃饭
讲课
2.学生类Student
属性:
姓名name
年龄age
学习内容content
行为:
吃饭eat()
学习study()
要求:
1.按照以上要求定义Teacher类和Student类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用每个对象的成员方法,打印格式如下:
年龄为30的周志鹏老师正在吃饭....
年龄为30的周志鹏老师正在亢奋的讲着Java基础中面向对象的知识........("Java基础中面向对象"代表老师讲课的内容)
年龄为18的韩光同学正在吃饭....
年龄为18的韩光同学正在专心致志的听着面向对象的知识....("面向对象"代表学生学习的内容)
public class Student {
private String name;
private int age;
private String content;
public void eat(){
System.out.println("年龄为"+age+"的"+name+"同学正在吃饭....");
}
public void study(){
System.out.println("年龄为"+age+"的"+name+"同学正在专心致志的听着"+content+"的知识....");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}

public Student(){
}
public Student(String name,int age,String content){
this.name=name;
this.age=age;
this.content=content;
}
}
public class Teacher {
private String name;
private int age;
private String content;
public void eat(){
System.out.println("年龄为"+age+"的"+name+"老师正在吃饭....");
}
public void jk(){
System.out.println("年龄为"+age+"的"+name+"老师正在亢奋的讲着"+content+"的知识........");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}

public Teacher(){
}
public Teacher(String name,int age,String content){
this.name=name;
this.age=age;
this.content=content;
}
}
public class Test {
public static void main(String[] args) {
Teacher teacher=new Teacher("周志鹏", 30, "Java基础中面向对象");
teacher.eat();
teacher.jk();
Student student=new Student("韩光", 18, "面向对象");
student.eat();
student.study();
}
}

第四题:分析以下需求,并用代码实现
定义人类Person,包含以下成员:
成员属性:
姓名 name( String类型)
年龄 age(double类型)

1.按照以上要求定义Person,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类:根据如下需求创建多个对象(使用满参构造创建,即有参构造).
老王-35 小芳-23
3.通过两个对象,比较谁的年龄大,并打印出来.
例: 老王年龄比较大
public class Person {
private String name;
private double age;
public Person(){
}
public Person(String name,double age){
this.name=name;
this.age=age;
System.out.println(name+"-"+age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
Person person=new Person("老王",35);
Person person2=new Person("小芳",23);
if (person.getAge()>person2.getAge()){
System.out.println(person.getName()+"年龄比较大");
}else {
System.out.println(person2.getName()+"年龄比较大");
}
}
}

练习题:
------------------------------------------------------------------

1.定义“电脑类”Computer,包含以下成员:
成员属性:
品牌brand( String类型)
价格 price(double类型)
成员方法:
编码coding(), 调用方法打印 ***电脑正在使用Java语言编程
玩游戏,playGame(),调用方法打印 ***电脑正在玩王者荣耀

1.按照以上要求定义Computer,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,a.创建一个电脑对象,设置品牌为ThinkPad,价格为7399,调用方法coding
b.创建一个电脑对象,设置品牌为Acer,价格为5399,调用方法playGame
public class Computer {
private String brand;
private double price;
public Computer(){
}
public Computer(String brand,double price){
this.brand=brand;
this.price=price;
}
public void coding(){
System.out.println(brand+"电脑正在使用Java语言编程");
}
public void playGame(){
System.out.println(brand+"电脑正在玩王者荣耀");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Test {
public static void main(String[] args) {
Computer computer=new Computer("ThinkPad",7399);
computer.coding();
Computer computer1=new Computer("Acer",5399);
computer1.playGame();
}
}

2.定义汽车类Car,包含以下成员:
成员属性:
品牌 brand( String类型)
电量 power(double类型)
成员方法:
报警 warning() 调用方法,可以检验当前电量是否低于10,如果低于10,就打印"电量不足". 如果不低于10,就打印"电量充足"

1.按照以上要求定义Car,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类:根据如下需求创建多个对象,调用warning()方法.
特斯拉-50 比亚迪-9
public class Car {
private String brand;
private double power;
public Car(){
}
public Car(String brand,double power){
this.brand=brand;
this.power=power;
System.out.println(brand+"-"+power);
}
public void warning(){
if (power<10){
System.out.println("电量不足");
}else {
System.out.println("电量充足");
}
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPower() {
return power;
}
public void setPower(double power) {
this.power = power;
}
}
public class Test {
public static void main(String[] args) {
Car car=new Car("特斯拉",50);
car.warning();
Car car1=new Car("比亚迪",9);
car1.warning();
}
}

3. 1.项目经理类Manager
属性:
姓名name
工号id
工资salary
奖金bonus
行为:
工作work()
2.程序员类Coder
属性:
姓名name
工号id
工资salary
行为:
工作work()
要求:
1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用每个对象的成员方法,打印格式如下:
工号为123基本工资为15000奖金为6000的项目经理周扒皮正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....
工号为135基本工资为10000的程序员杨白劳正在努力的写着代码......
public class Manager {
private String name;
private int id;
private double salary;
private double 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 void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"奖金为"+bonus+"的项目经理"+name+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
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 class Coder {
private String name;
private int id;
private double salary;
public Coder(){
}
public Coder(String name, int id, double salary){
this.name=name;
this.id=id;
this.salary=salary;
}
public void work(){
System.out.println("工号为"+id+"基本工资为"+salary+"的程序员"+name+"正在努力的写着代码......");
}
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 Test {
public static void main(String[] args) {
Manager manager=new Manager("周扒皮",123,15000,6000);
manager.work();
Coder coder=new Coder("杨白劳",135,10000);
coder.work();
}

原文地址:https://www.cnblogs.com/1121cyy/p/13837709.html