代码实现
题目1
编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出。
//正常操作
public class Student {
private String name;
private String gender;
private int age;
private int id;
private double score;
public String say(){
return "姓名:" + getName() +"性别:" + getGender()+"年龄:" + getAge() + "学号" + getId()+ "成绩"+ getScore();
}
public Student() {
}
public Student(String name, String gender, int age, int id, double score) {
this.name = name;
this.gender = gender;
this.age = age;
this.id = id;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
//正常操作
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("唐伯虎","男",18,9527,100);
System.out.println(student.say());
//姓名:唐伯虎性别:男年龄:18学号9527成绩100.0
}
}
题目2
定义一个丈夫Husband类,有姓名、年龄、妻子属性
定义一个妻子Wife类,有姓名、年龄、丈夫属性
丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
定义一个测试类,创建妻子和丈夫对象,然后测试
public class Husband {
private String name;//丈夫姓名
private int age;//丈夫年龄
//此构造器为了获得丈夫的姓名和年龄
public Husband(String name, int age) {
this.name = name;
this.age = age;
}
//getInfo方法参数Wife wife 为了获得 wife的姓名和年龄
public void getInfo(Wife wife){
System.out.println(getName()+"的年龄是" + getAge());
System.out.println(getName()+"的妻子"+wife.getName()+"的年龄是" + wife.getAge());
}
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 class Wife {
private String name;//妻子的姓名
private int age;//妻子的年龄
//此构造器为了获得妻子的姓名和年龄
public Wife(String name, int age) {
this.name = name;
this.age = age;
}
//getInfo方法参数Husband husband 为了获得 wife的姓名和年龄
public void getInfo(Husband husband){
System.out.println(getName()+"的年龄是" + getAge());
System.out.println(getName()+"的丈夫"+husband.getName()+"的年龄是" + husband.getAge());
}
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 class Test1 {
public static void main(String[] args) {
Husband husband = new Husband("邓超",26);
Wife wife = new Wife("孙俪",24);
//妻子和丈夫互相获取
wife.getInfo(husband);
husband.getInfo(wife);
//执行结果
/*
孙俪的年龄是24
孙俪的丈夫邓超的年龄是26
邓超的年龄是26
邓超的妻子孙俪的年龄是24
*/
}
}
题目3
定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer
银行账户类Account有方法:
(1)getInfo(),返回String类型,返回卡的详细信息
(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false
其中Customer类有姓名、身份证号、联系电话、家庭地址等属性
Customer类有方法say(),返回String类型,返回他的个人信息。
在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息
public class Account {
private int cid;
private double balance;
//获得卡号ID,余额
public Account(int cid, double balance) {
this.cid = cid;
this.balance = balance;
}
//getInfo(),返回String类型,返回卡的详细信息
public String getInfo(Customer customer){
return "用户:" + customer +" 卡号:" + getCid() + " 余额为:"+ getBalance();
}
//取钱方法withdraw(),如果取钱成功返回true,失败返回false,参数为取钱金额
public boolean withdraw(double amt){
if (amt>balance){
return false;
}else{
balance = balance - amt;
return true;
}
}
//存钱方法save(),如果存钱成功返回true,失败返回false 参数为存钱金额
public boolean save(double amt){
if(amt>0){
balance = balance+amt;
return true;
}
return false;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
public class Customer {
private String name;
private int id;
private String phone;
private String address;
//返回他的个人信息。
public String say(){
return "姓名:" + getName() +"身份证号:" + getId()+"联系电话:" + getPhone() + "家庭地址" + getAddress();
}
//在main方法中获得个人信息
public Customer(String name, int id, String phone, String address) {
this.name = name;
this.id = id;
this.phone = phone;
this.address = address;
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class Bank {
public static void main(String[] args) {
Account account = new Account(9527, 1000.00);
Customer customer = new Customer("唐伯虎",10086,"13366666666","江南");
account.save(2000);
account.withdraw(4000);
System.out.println(account.getInfo(customer));
System.out.println(customer.say());
}
}
OVER~