今晚的月色温柔

导航

java--设计Address类和Student类

【问题描述】根据输出要求,设计Address类和Student类。类图如下:

鍥剧墖.png

【输入形式】输入1或2或3或4或5
【输出形式】

    当输入1时,输出结果为:HuBei,wuhan

    当输入2时,输出结果为:HuNan,ChangSha

    当输入3时,输出结果为:hubei,YiChang

    当输入4时,输出结果为:zhangsan:hubei,wuhan

    当输入5时,输出结果为:zhangsan:hubei,wuhan

【样例输入】
【样例输出】
【样例说明】
【评分标准】


package fuxi;
import java.util.Scanner;
import java.util.Scanner;
class Address{

public String province;
public String city;
public Address(){
province="hubei";
city="wuhan";
}
public Address(String province ,String city){
this.province=province;
this.city=city;
}
public void setProvince(String province) {
this.province=province;
}
public void setCity(String city) {
this.city=city;
}
public String toString() {
return province+","+city;
}
}

class Student{

private String n;
private Address ad;
public Student() {}
public Student(String name,Address ad) {
Address k=new Address(ad.province,ad.city);
this.n=name;
this.ad=k;
}
public String toString() {
return n+":"+ad;
}
}
public class W {
public static void main(String[] args) {
int selected;
Scanner scan=new Scanner(System.in);
selected=scan.nextInt();
Address address=new Address();
Student student=new Student("zhangsan",address);
switch(selected){
case 1:
address.setProvince("HuBei");
System.out.println(address);
break;
case 2:
address=new Address("HuNan","ChangSha");
System.out.println(address);
break;
case 3:
address.setCity("YiChang");
System.out.println(address);
break;
case 4:
address.setProvince("HuBei");
System.out.println(student);
break;
case 5:
System.out.println(student);
}
scan.close();
}
}

posted on 2020-10-10 12:22  1022  阅读(384)  评论(0编辑  收藏  举报