1、编程题

设计一个用户类User,类中的变量有用户名、密码和记录用户数量的变量,定义3个构造方法:无参的、为用户名赋值的、为用户名和密码赋值的,还有获取和设置密码的方法和返回类信息的方法。

package com.sdbi.gy;

class User {
private String name;
private String password;
private static int count;
public User(){
count++;
}
public User(String name){
this.name=name;
count++;
}
public User(String name,String password){
this.name=name;
this.password=password;
count++;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public void print(){
System.out.println("用户名:"+name+"\t"+"口令:"+password);
}
public void count(){
System.out.println("用户个数:"+(count-1));
}
}

 

 

 

package com.sdbi.gy;

public class UserText {
public static void main(String args[]){
User user1=new User("卢浩","6666666");
user1.print();
User user2=new User("卢浩二号","26661");
user2.print();
User user3=new User("卢浩三号","299961");
user3.print();
new User().count();
}
}

 

 1 package com.sdbi.gy;
 2 
 3 class User {
 4 private String name;
 5 private String password;
 6 private static int count;
 7 public User(){
 8 count++;
 9 }
10 public User(String name){
11 this.name=name;
12 count++;
13 }
14 public User(String name,String password){
15 this.name=name;
16 this.password=password;
17 count++;
18 }
19 public String getName(){
20 return name;
21 }
22 public void setName(String name){
23 this.name=name;
24 }
25 public String getPassword(){
26 return password;
27 }
28 public void setPassword(String password){
29 this.password=password;
30 }
31 public void print(){
32 System.out.println("用户名:"+name+"\t"+"口令:"+password);
33 }
34 public void count(){
35 System.out.println("用户个数:"+(count-1));
36 }
37 }
38 
39  
40 
41  
42 
43  
44 
45 package com.sdbi.gy;
46 
47 public class UserText {
48 public static void main(String args[]){
49 User user1=new User("卢浩","6666666");
50 user1.print();
51 User user2=new User("卢浩二号","26661");
52 user2.print();
53 User user3=new User("卢浩三号","299961");
54 user3.print();
55 new User().count();
56 }
57 }

2、编程题

设计一副牌Poker的外部类和一张牌Card的内部类。

(1)Poker类中定义私有成员花色数组、点数数组以及一副牌的数组属性,提供构造方法(创建并初始化一副牌的数组)、随机洗牌方法shuffle(Math.random()获取[0,1)的随机数;获取[n,m)的随机数公式为Math.random()*(m-n)+n)和发牌方法deal。

(2)Card类中定义花色和点数属性,提供打印信息方法。

(3)定义测试类并在main()方法中创建一副牌Poker对象,并调用shufle()进行洗牌,调用deal()进行发牌。

package com.sdbi.gy;

class Poker {
public class Card
{
private String suite; 
private int face;

public Card(String suite, int face)
{
this.suite = suite;
this.face = face;
}
public String toString()
{
String faceStr = "";
switch (face)
{
case 1:
faceStr = "A";
break;
case 11:
faceStr = "J";
break;
case 12:
faceStr = "Q";
break;
case 13:
faceStr = "K";
break;
default:
faceStr = String.valueOf(face);
}
return suite + faceStr;
}
}
private static String[] suites = { "黑桃", "红桃", "梅花", "方块" };
private static int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
private Card[] cards;


public Poker()
{

cards = new Card[52];
for (int i = 0; i < suites.length; i++)
{
for (int j = 0; j < faces.length; j++)
{
cards[i * 13 + j] = new Card(suites[i], faces[j]);
}
}
}


public void shuffle()
{
int len = cards.length;
for (int i = 0; i < len; i++)
{
int index = (int) (Math.random() * len);
Card temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}


public Card getCard(int index)
{
return cards[index];
}

}

 

 

package com.sdbi.gy;


public class Text {

public static void main(String[] args) {

Poker poker=new Poker();
poker.shuffle();
Poker.Card c1 = poker.getCard(0);
Poker.Card c2 = poker.getCard(1);
System.out.println(c1);
System.out.println(c2);

}

}

 1 package com.sdbi.gy;
 2 
 3 class Poker {
 4 public class Card
 5 {
 6 private String suite; 
 7 private int face; 
 8 
 9 public Card(String suite, int face)
10 {
11 this.suite = suite;
12 this.face = face;
13 }
14 public String toString()
15 {
16 String faceStr = "";
17 switch (face)
18 {
19 case 1:
20 faceStr = "A";
21 break;
22 case 11:
23 faceStr = "J";
24 break;
25 case 12:
26 faceStr = "Q";
27 break;
28 case 13:
29 faceStr = "K";
30 break;
31 default:
32 faceStr = String.valueOf(face);
33 }
34 return suite + faceStr;
35 }
36 }
37 
38 private static String[] suites = { "黑桃", "红桃", "梅花", "方块" };
39 private static int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
40 private Card[] cards;
41 
42 
43 public Poker()
44 {
45 
46 cards = new Card[52];
47 for (int i = 0; i < suites.length; i++)
48 {
49 for (int j = 0; j < faces.length; j++)
50 {
51 cards[i * 13 + j] = new Card(suites[i], faces[j]);
52 }
53 }
54 }
55 
56 
57 public void shuffle()
58 {
59 int len = cards.length;
60 for (int i = 0; i < len; i++)
61 {
62 int index = (int) (Math.random() * len);
63 Card temp = cards[index];
64 cards[index] = cards[i];
65 cards[i] = temp;
66 }
67 }
68 
69 
70 public Card getCard(int index)
71 {
72 return cards[index];
73 }
74 
75 }
76 
77  
78 
79  
80 
81 package com.sdbi.gy;
82 
83 
84 public class Text {
85 
86 public static void main(String[] args) {
87 Poker poker=new Poker();
88 poker.shuffle();
89 Poker.Card c1 = poker.getCard(0);
90 Poker.Card c2 = poker.getCard(1);
91 System.out.println(c1);
92 System.out.println(c2);
94 }
96 }