一、基础案例
题目1
一、 有以下数据:
- 三个老师信息:
教师编号 姓名 性别 年龄 科目
t001 薛之谦 男 26 Java
t002 张碧晨 女 24 IOS
t003 张杰 男 28 Java - 存储两个科目信息:
课程编号 名称 创建时间 课程描述
s001 Java 2007-02-08 Java学科,包含JavaSE和JavaEE
s002 IOS 2007-02-09 IOS系统开发
二、 请分别定义两个类;
三、 创建MainApp类中,包含main()方法,创建相应对象,通过构造方法给成员变量赋值。
四、 打印每个对象的所有属性。
要求:每个类要按照封装的原则进行定义。并提供无参和全参的构造方法。
public class Teachers {
private String tId;
private String name;
private String sex;
private int age;
private String courseName;
public Teachers() {
}
public Teachers(String tId,String name,String sex,int age,String courseName) {
this.tId = tId;
this.name = name;
this.sex = sex;
this.age = age;
this.courseName = courseName;
}
public String gettId() {
return tId;
}
public void settId(String tId) {
this.tId = tId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public class Courses {
private String cId;
private String cName;
private String createTime;
private String description;
public Courses() {
}
public Courses(String cId,String cName,String createTime,String description) {
this.cId = cId;
this.cName = cName;
this.createTime = createTime;
this.description = description;
}
public String getcId() {
return cId;
}
public void setcId(String cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public class MainApp {
public static void main(String[] args) {
//1.1
Teachers t1 = new Teachers("t001","薛之谦","男",26,"Java");
Teachers t2 = new Teachers("t002","张碧晨","女",24,"IOS");
Teachers t3 = new Teachers("t003","张杰","男",28,"Java");
Courses c1 = new Courses("s001","Java","2007-02-08","Java学科,包含JavaSE和JavaEE");
Courses c2 = new Courses("s002","IOS","2007-02-09","IOS系统开发");
System.out.println("教师编号:"+t1.gettId()+" 姓名:"+t1.getName()+" 性别:"+t1.getSex()+" 年龄:"+t1.getAge()+" 科目:"+t1.getCourseName());
System.out.println("教师编号:"+t1.gettId()+" 姓名:"+t2.getName()+" 性别:"+t2.getSex()+" 年龄:"+t2.getAge()+" 科目:"+t2.getCourseName());
System.out.println("教师编号:"+t1.gettId()+" 姓名:"+t3.getName()+" 性别:"+t3.getSex()+" 年龄:"+t3.getAge()+" 科目:"+t3.getCourseName());
System.out.println("课程编号:"+c1.getcId()+" 名称:"+c1.getcName()+" 创建时间:"+c1.getCreateTime()+" 课程描述:"+ c1.getDescription());
System.out.println("课程编号:"+c2.getcId()+" 名称:"+c2.getcName()+" 创建时间:"+c2.getCreateTime()+" 课程描述:"+ c2.getDescription());
}
}
题目2
一、 实现从控制台接收一个学员信息,并存储到一个对象中
二、 打印这个对象的所有属性值。
public class Students {
private String sId;
private String name;
private String sex;
private double height;
private int age;
public Students() {}
public Students(String sId,String name,String sex,double height,int age) {
this.sId = sId;
this.name = name;
this.sex = sex;
this.height = height;
this.age = age;
}
public String getsId() {
return sId;
}
public void setsId(String sId) {
this.sId = sId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.util.Scanner;
public class MainApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学员编号:");
String sId = sc.nextLine();
System.out.println("请输入学员姓名:");
String name = sc.nextLine();
System.out.println("请输入学员性别:");
String sex = sc.nextLine();
System.out.println("请输入学员身高:");
Double sHeight = sc.nextDouble();
System.out.println("请输入学员年龄:");
int sAge = sc.nextInt();
Students s= new Students(sId,name,sex,sHeight,sAge);
System.out.println("学员编号:"+s.getsId()+" 姓名:"+s.getName()+" 性别:"+s.getSex()+" 身高:"+ s.getHeight()+" 年龄:" +s.getAge());
}
}
二、扩展案例
题目1
分析以下需求,并用代码实现
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 String id;
private int salary;
private int bonus;
public Manager() {}
public Manager(String name,String id,int salary,int 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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public void work() {
System.out.println("工号为"+this.getId()+"基本工资为"+this.getSalary()
+"奖金为"+this.getBonus()+"的项目经理"+this.getName()+"正在努力的做着管理工作,分配任务,检查员工提交上来的代码....");
}
}
public class Coder {
private String name;
private String id;
private int salary;
public Coder() {}
public Coder(String name,String id,int 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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void work() {
System.out.println("工号为"+this.getId()+"基本工资为"+this.getSalary()
+"的程序员"+this.getName()+"正在努力的写着代码......操作步骤描述");
}
}
public class MainApp {
public static void main(String[] args) {
Manager m = new Manager("成龙","123",15000,2000);
Coder c = new Coder();
c.setId("456");
c.setName("房祖名");
c.setSalary(10000);
m.work();
c.work();
}
}
题目2
分析以下需求,并用代码实现
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 Teacher {
private String name;
private int age;
private String content;
public Teacher() {}
public Teacher(String name,int age,String content) {
this.name = name;
this.age = age;
this.content = 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 void Lecture() {
System.out.println("年龄为"+this.getAge()+"岁的"+this.getName()+"老师正在亢奋的讲着"+this.getContent()+"的知识....");
}
public void eat() {
System.out.println("年龄为"+this.getAge()+"岁的"+this.getName()+"老师正在吃饭....");
}
}
public class Student {
private String name;
private int age;
private String content;
public Student() {}
public Student(String name,int age,String content) {
this.name = name;
this.age = age;
this.content = 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 void eat() {
System.out.println("年龄为"+this.getAge()+"岁的"+this.getName()+"同学正在吃饭....");
}
public void study() {
System.out.println("年龄为"+this.getAge()+"岁的"+this.getName()+"同学正在专心致志的听着"+this.getContent()+"的知识....");
}
}
public class MainApp {
public static void main(String[] args) {
Teacher t = new Teacher("孔子",35,"论语");
Student s = new Student();
s.setName("子路");
s.setAge(20);
s.setContent("论语");
t.eat();
t.Lecture();
s.eat();
s.study();
}
}
题目3
分析以下需求,并用代码实现
1.猫类Cat
属性:
毛的颜色color
品种breed
行为:
吃饭eat()
抓老鼠catchMouse()
狗特有行为:看家lookHome
2.狗类Dog
属性:
毛的颜色color
品种breed
行为:
吃饭()
看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用成员方法,打印格式如下:
花色的波斯猫正在吃鱼…
花色的波斯猫正在逮老鼠…
黑色的藏獒正在啃骨头…
黑色的藏獒正在看家…
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(this.getColor()+"的"+this.getBreed()+"正在吃鱼.....");
}
public void catchMouse() {
System.out.println(this.getColor()+"的"+this.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(this.getColor()+"的"+this.getBreed()+"正在啃骨头.....");
}
public void lookHome() {
System.out.println(this.getColor()+"的"+this.getBreed()+"正在看家....");
}
}
public class MainApp {
public static void main(String[] args) {
Cat cat = new Cat("蓝色","英国短毛猫");
Dog dog = new Dog();
dog.setColor("黄色");
dog.setBreed("柯基");
cat.eat();
cat.catchMouse();
dog.eat();
dog.lookHome();
}
}
题目4
一、 需求说明:创建三个图书类对象,找出价格最高的图书并打印该图书的所有信息。
二、 设计“图书类”Book,要求有以下属性:
图书编号:
书名:
价格:
public class Book {
private String id;
private String name;
private int price;
public Book() {}
public Book(String id, String name,int price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void printInfromation() {
System.out.println("图书编号:"+this.getId()+"书名为:"+this.getName()+"的价格是"+this.getPrice()+"元");
}
}
public class MainApp {
public static void main(String[] args) {
Book book1 = new Book("1","西游记",70);
Book book2 = new Book("2","论语",30);
Book book3 = new Book("3","四书",50);
if(book1.getPrice()>book2.getPrice()) {
if(book1.getPrice()>book3.getPrice()) {
book1.printInfromation();
}else {
book3.printInfromation();
}
}else {
if(book2.getPrice()>book3.getPrice()) {
book2.printInfromation();
}else {
book3.printInfromation();
}
}
}
}
题目5
分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的小米品牌的手机打电话....
正在使用价格为998元的小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....
public class Phone {
private String brand;
private int price;
public Phone() {}
public Phone(String brand,int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void call() {
System.out.println("正在使用价格为"+this.getPrice()+"元的"+this.getBrand()+"品牌的手机打电话....");
}
public void sendMessage() {
System.out.println("正在使用价格为"+this.getPrice()+"元的"+this.getBrand()+"品牌的手机发短信....");
}
public void playGame() {
System.out.println("正在使用价格为"+this.getPrice()+"元的"+this.getBrand()+"品牌的手机玩游戏....");
}
}
public class MainApp {
public static void main(String[] args) {
Phone p1 = new Phone("苹果",10000);
Phone p2 = new Phone();
p2.setBrand("华为");
p2.setPrice(10999);
p1.call();
p1.sendMessage();
p1.playGame();
p2.call();
p2.sendMessage();
p2.playGame();
}
}