JAVA实验题笔记 - 接口&初始化块实验题 抗疫期间大家都在做什么
782访客 209字under Notes of Programming tag Homework Java Published on April 24th , 2020 at 01:53 pm
问题描述
- 抗疫过程中政府、组织、企业和个人分别在做什么。要求:
- 定义接口ResistanceToDisease,接口提供doSomething方法;
- 分别定义Government、Organization、Enterprise和People类实现接口ResistanceToDisease;
- 根据需要再类中增加必要的属性和方法,并且使用初始化块进行初始化,具体要求见示例;
- 提供测试程序,在程序中提供菜单,用户可以根据菜单编辑不同类别的角色及其为抗疫做了些什么;
输出形式示例(部分)
菜单如下:
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:
选择1~4,则依次提示:
enter name:
how do:
你可以输入相应信息,如果没有输入信息则使用初始化信息
而后输出结果(下面的为选择1~4后的默认输出信息):
China:Unity is strength
WTO:Cooperation with power
Gree:Master core technology
People:Stay at home
选择5,则一次输出默认信息:
China:Unity is strength
WTO:Cooperation with power
Gree:Master core technology
People:Stay at home
选择6,退出程序
Programing Time
package cn.matce.hw9;
import java.util.Scanner;
public class Cyberpunk2020 {
Scanner sc = new Scanner(System.in);
String s1, s2;
String s01, s02;
static boolean loop = true;
public static void main(String[] args) {
Cyberpunk2020 Cy = new Cyberpunk2020();
while (loop == true) {
Cy.menu();
}
}
void menu() {
print("1.Government\n");
print("2.Organization\n");
print("3.Enterprise\n");
print("4.People\n");
print("5.All\n");
print("6.exit\n");
choice();
}
void choice() {
int c = 0;
s1 = s2 = s01 = s02 = "";
print("your choice:");
c = Integer.valueOf(sc.next());
switch (c) {
case 1:
Government G = new Government();
choice2();
if (!s1.equals("")) {
G.Name = s1;
}
if (!s2.equals("")) {
G.ToDo = s2;
}
print(G.Name + ":\t" + G.ToDo + "\n");
break;
case 2:
Organization O = new Organization();
choice2();
if (!s1.equals("")) {
O.Name = s1;
}
if (!s2.equals("")) {
O.ToDo = s2;
}
print(O.Name + ":\t" + O.ToDo + "\n");
break;
case 3:
Enterprise E = new Enterprise();
choice2();
if (!s1.equals("")) {
E.Name = s1;
}
if (!s2.equals("")) {
E.ToDo = s2;
}
print(E.Name + ":\t" + E.ToDo + "\n");
break;
case 4:
People P = new People();
choice2();
if (!s1.equals("")) {
P.Name = s1;
}
if (!s2.equals("")) {
P.ToDo = s2;
}
print(P.Name + ":\t" + P.ToDo + "\n");
break;
case 5:
Government G1 = new Government();
Organization O1 = new Organization();
Enterprise E1 = new Enterprise();
People P1 = new People();
print(G1.Name + ":\t" + G1.ToDo + "\n");
print(O1.Name + ":\t" + O1.ToDo + "\n");
print(E1.Name + ":\t" + E1.ToDo + "\n");
print(P1.Name + ":\t" + P1.ToDo + "\n");
break;
case 6:
loop = false;
break;
default:
print("Wrong Input.");
break;
}
}
void choice2() {
print("enter name:");
sc.nextLine();
s01 = sc.nextLine();
if (!s01.equals("")) {
s1 = s01;
}
print("how do:");
s02 = sc.nextLine();
if (!s02.equals("")) {
s2 = s02;
}
}
void print(String s) {
System.out.print(s);
}
}
interface ResistanceToDisease {
public void doSomething();
}
class All implements ResistanceToDisease {
String Name;
String ToDo;
@Override
public void doSomething() {
}
}
class Government extends All {
{
Name = "China";
ToDo = "Unity is strength";
}
}
class Organization extends All {
{
Name = "WTO";
ToDo = "Cooperation with power";
}
}
class Enterprise extends All {
{
Name = "Gree";
ToDo = "Master core technology";
}
}
class People extends All {
{
Name = "People";
ToDo = "Stay at home";
}
}
输出结果
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:1
enter name:Country1
how do:Do Nothing.
Country1: Do Nothing.
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:2
enter name:WWWW
how do:
WWWW: Cooperation with power
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:3
enter name:
how do:nice
Gree: nice
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:4
enter name:how do:
People: Stay at home
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:5
China: Unity is strength
WTO: Cooperation with power
Gree: Master core technology
People: Stay at home
1.Government
2.Organization
3.Enterprise
4.People
5.All
6.exit
your choice:6
本页由 matcha 整理和发布,采用 知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
文章最后更新时间为:October 28th , 2020 at 04:29 am
分享到:Twitter Weibo Facebook