1、编程题

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.sdbi.ljh;
 
public class User {//设计一个用户类User,类中的变量有用户名、密码和记录用户数量的变量,
    //定义3个构造方法:无参的、为用户名赋值的、为用户名和密码赋值的,还有获取和设置密码的方法和返回类信息的方法。
    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));
         }
        }
        

  

1
2
3
4
5
6
7
8
public class Test1 {
    public static void main(String args[]){
          User user1=new User("李四","456789");
          user1.print();
          User user2=new User("刘嘉慧","123456");
          user2.print();
          new User().count();
}}

  

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()进行发牌。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.sdbi.ljh1.Card;
 
public class Poker {
    public static List<Card> allCards = new ArrayList<Card>();
    static {
        String[] sizes = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
        String[] colors = { "黑桃", "梅花", "红桃", "方块" };
        for (String size : sizes) {
            for (String color : colors) {
                Card c = new Card(size, color);
                allCards.add(c);
            }
        }
        Card c1 = new Card("", "大王");
        Card c2 = new Card("", "小王");
        Collections.addAll(allCards, c1, c2);
        System.out.println("新牌" + allCards);
    }
 
    public static void main(String[] aggs) {
        Collections.shuffle(allCards);
        System.out.println("洗牌" + allCards);
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Card {
    private String size;
    private String color;
 
    public Card() {
 
    }
 
    public Card(String size, String color) {
        this.size = size;
        this.color = color;
    }
 
    public String getsize() {
        return size;
    }
 
    public void setsize(String size) {
        this.size = size;
 
    }
 
    public String getcolor() {
        return color;
    }
 
    public void setcolor(String color) {
        this.color = color;
    }
 
    @Override
    public String toString() {
        return size + color;
    }
}