**编写Person类 ** 属性:String name, int id; 方法:必要的构造方法,属性name的getter/setter方法,覆盖Object中继承的toString方法(返回的字符串格式:id=id的值 name='name的值')
ObjectGenerator的getObject可以根据给定的种子随机生成String、Integer、Person对象。
class ObjectGenerator{ private Random random; public ObjectGenerator(int seed) { random = new Random(seed); } public Object getObject(){ int choice = random.nextInt(3); int x = random.nextInt(26); switch (choice) { case 0: char z = (char)(97+x); return new String(""+z); case 1: return Integer.valueOf(x); case 2: char name = (char)(65+x); return new Person(x, name+""); } return null; } }
你提交的代码中应该包含: Person类,ObjectGenerator类,Main类
调用ObjectGenerator类时需要引入 java.util.Random包
Main类中main的代码: 根据种子seed初始化ObjectGenerator 根据数量n,调用ObjectGenerator的getObject方法随机生n个对象放入数组 输出数组。注意:尽量不要自己拼接否则容易出错。 找到数组中的Person类,输出他们的name
输入格式:
种子seed 生成的对象数量n
输出格式:
数组中的内容,格式如输出样例第一行所示 数组中的Person对象的信息,每行一个,格式如输出样例第二行所示
输入样例:
3 10
输出样例:
[id=12 name='M', h, u, g, 23, y, id=2 name='C', m, h, 19]
Name of the person is M
Name of the person is C
第一种 数组方法:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int seed =sc.nextInt();
int n=sc.nextInt();
Object a[] = new Object[n];
//根据种子seed初始化ObjectGenerator
ObjectGenerator b= new ObjectGenerator(seed);
for(int i=0;i<n;i++){
a[i]=b.getObject();
}
System.out.println(Arrays.toString(a));
for(int i=0;i<n;i++){
if(a[i] instanceof Person){
System.out.println("Name of the person is "+((Person) a[i]).getName());
}
}
}
}
class Person{
String name;
int id;
Person(int id,String name){
this.name=name;
this.id=id;
}
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;
}
@Override
public String toString() {
return "id=" + id +
" name=" + "'"+name+"'"
;
}
}
class ObjectGenerator{
private Random random;
public ObjectGenerator(int seed) {
random = new Random(seed);
}
public Object getObject(){
int choice = random.nextInt(3);
int x = random.nextInt(26);
switch (choice) {
case 0:
char z = (char)(97+x);
return new String(""+z);
case 1:
return Integer.valueOf(x);
case 2:
char name = (char)(65+x);
return new Person(x, name+"");
}
return null;
}
}
第二种? 集合方法:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Object> list = new ArrayList<>();
int seed = sc.nextInt();
int n=sc.nextInt();
//根据种子seed初始化ObjectGenerator
ObjectGenerator a= new ObjectGenerator(seed);
//根据数量n,调用ObjectGenerator的getObject方法随机生n个对象放入数组
for(int i=0;i<n;i++){
list.add(a.getObject());
}
//输出数组
System.out.println(list);
//找到数组中的Person类,输出他们的name
for(int i=0;i<n;i++){
if(list.get(i) instanceof Person){
System.out.println("Name of the person is "+((Person) list.get(i)).getName());
}
}
}
}
class Person{
String name;
int id;
Person(int id,String name){
this.name = name;
this.id = id;
}
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;
}
@Override
public String toString() {
return
"id=" + id +
" name=" + "'"+name+"'"
;
}
}
class ObjectGenerator{
private Random random;
public ObjectGenerator(int seed) {
random = new Random(seed);
}
public Object getObject(){
int choice = random.nextInt(3);
int x = random.nextInt(26);
switch (choice) {
case 0:
char z = (char)(97+x);
return new String(""+z);
case 1:
return Integer.valueOf(x);
case 2:
char name = (char)(65+x);
return new Person(x, name+"");
}
return null;
}
}
两种都可以!
|