第一题:分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并使用set方式给属性赋值(价格:998,品牌:小米)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的手机打电话....
正在使用小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....

 

package com.lw.www;
//创建测试类
public class test {
 public static void main(String[] args) {
  Phone phone = new Phone("小米",998);
//  phone.setBrand("小米");
//  phone.setPrice(998);    
  phone.call();
  phone.sendMessage();
  phone.playGame();
 }
}

package com.lw.www;
//创建手机类
public class Phone {
 private String brand;
 private double price;
 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 Phone() {
 }
}

 


第二题:分析以下需求,并用代码实现
1.猫类Cat
属性:
毛的颜色color
品种breed
行为:
吃饭eat()
抓老鼠catchMouse()
2.狗类Dog
属性:
毛的颜色color
品种breed
行为:
吃饭()
看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建每个类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用每个对象的成员方法,打印格式如下:
花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....

package com.lw.www;
public class Cat {
 private String color;
 private String breed;
 public Cat(String color,  String breed) {
  this.color = color;
  this.breed = breed;
 
 System.out.println("毛的颜色:" + color + ",品种:" + breed);
}
 /*吃饭eat()
 抓老鼠catchMouse()*/
 public void eat(){
  System.out.println(color+"的"+breed+"正在吃鱼.....");
 }
public void catchMouse(){
 System.out.println(color+"的"+breed+"正在逮老鼠.....");
 }
}
package com.lw.www;
public class Dog {
 private String color;
 private String breed;
 public Dog(String color,  String breed) {
  this.color = color;
  this.breed = breed;
 
 System.out.println("毛的颜色:" + color + ",品种:" + breed);
}
 /*吃饭eat()
 抓老鼠catchMouse()*/
 public void eat(){
  System.out.println(color+"的"+breed+"正在啃骨头.....");
 }
public void lookHome(){
 System.out.println(color+"的"+breed+"正在看家.....");
 }
}
package com.lw.www;
import com.hp.ryy.Phone;
public class test1 {
 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(),调用方法打印 ***电脑正在玩王者荣耀s
   
 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();
    }
}

 

 posted on 2020-10-16 12:53  经得起折腾的李富贵  阅读(89)  评论(0)    收藏  举报