1. 训练案例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()方法,创建相应对象,通过构造方法给成员变量赋值。
- 四、 打印每个对象的所有属性。
要求:每个类要按照封装的原则进行定义。并提供无参和全参的构造方法。
1.2. 操作步骤描述
设计类:Teacher(教师类)和Course(课程类)
为每个类设计“成员属性”
定义两个类
定义MainApp类,包含main()方法,分别创建对象存储数据。
public class Teacher {
private String number;
private String name;
private char sex;
private int age;
private String subject;
public Teacher() {
super();
}
public Teacher(String number, String name, char sex, int age, String subject) {
super();
this.number = number;
this.name = name;
this.sex = sex;
this.age = age;
this.subject = subject;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
public class Subject {
private String snum;
private String sname;
private String stime;
private String describe;
public Subject() {
super();
}
public Subject(String snum, String sname, String stime, String describe) {
super();
this.snum = snum;
this.sname = sname;
this.stime = stime;
this.describe = describe;
}
public String getSnum() {
return snum;
}
public void setSnum(String snum) {
this.snum = snum;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getStime() {
return stime;
}
public void setStime(String stime) {
this.stime = stime;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
public class MainApp {
public static void main(String[] args) {
Teacher tea1 = new Teacher();
Teacher tea2 = new Teacher();
Teacher tea3 = new Teacher();
tea1.setNumber("t001");
tea1.setName("薛之谦");
tea1.setSex('男');
tea1.setAge(26);
tea1.setSubject("Java");
tea2.setNumber("t002");
tea2.setName("张碧晨");
tea2.setSex('女');
tea2.setAge(24);
tea2.setSubject("IOS");
tea3.setNumber("t003");
tea3.setName("张杰");
tea3.setSex('男');
tea3.setAge(28);
tea3.setSubject("Java");
System.out.print("教师编号"+" "+"姓名"+" "+"性别"+" "+"年龄"+" "+"科目");
System.out.println();
System.out.print(tea1.getSubject()+" "+tea1.getName()+" "+tea1.getSex()+" "+tea1.getAge()+" "+tea1.getSubject());
System.out.println();
System.out.print(tea2.getSubject()+" "+tea2.getName()+" "+tea2.getSex()+" "+tea2.getAge()+" "+tea2.getSubject());
System.out.println();
System.out.println();
Subject sub1 = new Subject();
Subject sub2 = new Subject();
sub1.setSnum("s001");
sub1.setSname("Java");
sub1.setStime("2007-02-08");
sub1.setDescribe("Java学科,包含JavaSE和JavaEE");
sub2.setSnum("s002");
sub2.setSname("IOS");
sub2.setStime("2007-02-09");
sub2.setDescribe("IOS系统开发");
/*课程编号 名称 创建时间 课程描述
s001 Java 2007-02-08 Java学科,包含JavaSE和JavaEE
s002 IOS 2007-02-09 IOS系统开发*/
System.out.print("课程编号"+" "+"名称"+" "+"创建时间"+" "+"课程描述");
System.out.println();
System.out.print(sub1.getSnum()+" "+sub1.getSname()+" "+sub1.getStime()+" "+sub1.getDescribe());
System.out.println();
System.out.print(sub2.getSnum()+" "+sub2.getSname()+" "+sub2.getStime()+" "+sub2.getDescribe());
}
}
2. 训练案例2
2.1. 训练描述:【Scanner,类和对象】
一、 实现从控制台接收一个学员信息,并存储到一个对象中
二、 打印这个对象的所有属性值。
2.2. 操作步骤描述
设计,并定义一个学员类:Student,要求有以下属性:
学员编号(String)
姓名(String)
性别(String)
身高(double)
年龄(int)
使用封装的原则,并提供无参和全参的构造方法。
定义MainApp类,并包含main()方法。
程序启动后,应分别提示用户输入学员编号、姓名等信息。
例如控制台显示:
C:>请输入学员编号:
C:>…
C:>请输入学员姓名:
C:>…
…
数据接收到程序中,并定义局部变量存储;
创建Student对象,通过构造方法将所有数据存储到Student对象中;
打印对象中的每个属性值。
public class Student {
private String snum;
private String sname;
private double sheight;
private int sage;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String snum, String sname, double sheight, int sage) {
super();
this.snum = snum;
this.sname = sname;
this.sheight = sheight;
this.sage = sage;
}
public String getSnum() {
return snum;
}
public void setSnum(String snum) {
this.snum = snum;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public double getSheight() {
return sheight;
}
public void setSheight(double sheight) {
this.sheight = sheight;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
}
import java.util.Scanner;
public class Mainapp2 {
public static void main(String[] args) {
Student s = new Student();
Scanner input = new Scanner(System.in);
System.out.println("输入学员编号:");
String snum = input.next();
s.setSnum(snum);
System.out.println("输入学员姓名:");
String sname = input.next();
s.setSname(sname);
System.out.println("输入学员身高:");
double sheight = input.nextDouble();
s.setSheight(sheight);
System.out.println("输入学员年龄:");
int sage = input.nextInt();
s.setSage(sage);
System.out.println("学员编号:"+s.getSnum());
System.out.println("学员姓名:"+s.getSname());
System.out.println("学员身高:"+s.getSheight());
System.out.println("学员年龄:"+s.getSage());
}
}
3. 训练案例
3
3.1. 训练描述
分析以下需求,并用代码实现
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 void eat(){
System.out.println("正在吃鱼.....");
}
public void catchMouse(){
System.out.println("正在逮老鼠....");
}
public Cat() {
super();
}
public Cat(String color, String breed) {
super();
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 class Dog {
private String color;
private String breed;
public void eat(){
System.out.println("正在啃骨头.....");
}
public void lookHome(){
System.out.println("正在看家.....");
}
public Dog() {
super();
}
public Dog(String color, String breed) {
super();
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 class MainApp3 {
public static void main(String[] args) {
Cat cat1 = new Cat("花色的","波斯猫");
Dog dog1 = new Dog("黑色的","藏獒");
System.out.print(cat1.getColor()+cat1.getBreed());
cat1.eat();
System.out.print(cat1.getColor()+cat1.getBreed());
cat1.catchMouse();
System.out.println();
System.out.print(dog1.getColor()+dog1.getBreed());
dog1.eat();
System.out.print(dog1.getColor()+dog1.getBreed());
dog1.lookHome();
}
}
4. 训练案例4
4.1 训练描述:
一、 需求说明:创建三个图书类对象,找出价格最高的图书并打印该图书的所有信息。
二、 设计“图书类”Book,要求有以下属性:
图书编号:
书名:
价格:
public class Book {
private String num;
private String name;
private int price;
public Book(String i, String name, int price) {
super();
this.num = i;
this.name = name;
this.price = price;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
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 class MainApp04 {
public static void main(String[] args) {
Book b1 =new Book("0001","葵花宝典",85);
Book b2 =new Book("0002","九阴真经",64);
Book b3 =new Book("0001","九阳神功",99);
int max =0;
Book a;
int[] arr ={b1.getPrice(),b2.getPrice(),b3.getPrice()};
for (int i = 0; i < arr.length; i++) {
max = max>= arr[i]?max:arr[i];
}
if(max == b1.getPrice()){
a=b1;
}else if(max == b2.getPrice()){
a=b2;
}else{
a=b3;
}
System.out.println("最贵的图书为:");
System.out.println(a.getNum()+" "+a.getName()+" "+a.getPrice());
}
}
5. 训练案例5
5.1. 训练描述:
分析以下需求,并用代码实现
手机类Phone
属性:
品牌brand
价格price
行为:
打电话call()
发短信sendMessage()
玩游戏playGame()
要求:
1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用三个成员方法,打印格式如下:
正在使用价格为998元的小米品牌的手机打电话....
正在使用价格为998元的小米品牌的手机发短信....
正在使用价格为998元的小米品牌的手机玩游戏....
public class Phone {
private String brand;
private String price;
public void call() {
System.out.println("打电话....");
}
public void sendMessage() {
System.out.println("发短信....");
}
public void playGame() {
System.out.println("玩游戏....");
}
public Phone() {
super();
// TODO Auto-generated constructor stub
}
public Phone(String brand, String price) {
super();
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
public class MainApp05 {
public static void main(String[] args) {
Phone xiaomi = new Phone("小米品牌的手机","998元的");
System.out.print("正在使用价格为"+xiaomi.getPrice()+xiaomi.getBrand());
xiaomi.call();
System.out.print("正在使用价格为"+xiaomi.getPrice()+xiaomi.getBrand());
xiaomi.sendMessage();
System.out.print("正在使用价格为"+xiaomi.getPrice()+xiaomi.getBrand());
xiaomi.playGame();
}
}