In the most common use of the term, a class can be seen as a blueprint used to create objects. In other words, object(s) are created from a class in Java.
<h3>Creating a Class</h3><h3>Code:</h3>
import java.util.*;
class Card{
private String suit;
private int value;
public Card(String s,int v){
suit = s;
value = v;
}
public String getSuit(){
return suit;
}
public int getValue(){
return value;
}
public void display(){
System.out.println(value+"("+suit+")");
}
}
class Deck{
private ArrayList<Card> deck;
private ArrayList<Card> drawn;
public Deck(){
deck = new ArrayList<Card>();
drawn = new ArrayList<Card>();
String[] arr = {"Club","Spade","Heart","Diamond"};
for(String s : arr){
for(int i=1;i<=13;i++){
deck.add(new Card(s,i));
}
}
}
public void draw(){
drawn.add(deck.get(0));
deck.remove(0);
}
public void draw(int N){
if(N > deck.size()){
N = deck.size();
}
for(int i=0;i<N;i++){
draw();
}
}
public void showDrawn(){
for(Card c : drawn){
c.display();
}
}
public void shuffle(){
Collections.shuffle(deck);
}
public void restore(){
restore(drawn.size());
}
public void restore(int N){
if(N > drawn.size()){
N = drawn.size();
}
for(int i=0;i<N;i++){
deck.add(drawn.get(0));
drawn.remove(0);
}
}
public void showDeck(){
for(Card c : deck){
c.display();
}
}
}
class Test{
public static void main(String[] args){
Deck d = new Deck();
System.out.println("Original deck\n");
d.showDeck();
d.draw(8);
System.out.println("\nDeck after drawing 8 cards\n");
d.showDeck();
d.shuffle();
System.out.println("\nDeck after shuffling\n");
d.showDeck();
d.restore();
System.out.println("\nDeck after restoring cards\n");
d.showDeck();
}
}
<h3>Test Output:</h3>
C:\Users\hp\Desktop>java Test
Original deck
1(Club)
2(Club)
3(Club)
4(Club)
5(Club)
6(Club)
7(Club)
8(Club)
9(Club)
10(Club)
11(Club)
12(Club)
13(Club)
1(Spade)
2(Spade)
3(Spade)
4(Spade)
5(Spade)
6(Spade)
7(Spade)
8(Spade)
9(Spade)
10(Spade)
11(Spade)
12(Spade)
13(Spade)
1(Heart)
2(Heart)
3(Heart)
4(Heart)
5(Heart)
6(Heart)
7(Heart)
8(Heart)
9(Heart)
10(Heart)
11(Heart)
12(Heart)
13(Heart)
1(Diamond)
2(Diamond)
3(Diamond)
4(Diamond)
5(Diamond)
6(Diamond)
7(Diamond)
8(Diamond)
9(Diamond)
10(Diamond)
11(Diamond)
12(Diamond)
13(Diamond)
<h3>Draw action:</h3>
9(Club)
10(Club)
11(Club)
12(Club)
13(Club)
1(Spade)
2(Spade)
3(Spade)
4(Spade)
5(Spade)
6(Spade)
7(Spade)
8(Spade)
9(Spade)
10(Spade)
11(Spade)
12(Spade)
13(Spade)
1(Heart)
2(Heart)
3(Heart)
4(Heart)
5(Heart)
6(Heart)
7(Heart)
8(Heart)
9(Heart)
10(Heart)
11(Heart)
12(Heart)
13(Heart)
1(Diamond)
2(Diamond)
3(Diamond)
4(Diamond)
5(Diamond)
6(Diamond)
7(Diamond)
8(Diamond)
9(Diamond)
10(Diamond)
11(Diamond)
12(Diamond)
13(Diamond)
<h3 /><h3>Shuffling action:</h3>
12(Diamond)
8(Spade)
4(Spade)
6(Diamond)
10(Spade)
13(Club)
7(Heart)
13(Heart)
7(Diamond)
9(Heart)
11(Diamond)
5(Heart)
8(Diamond)
9(Diamond)
3(Spade)
13(Spade)
4(Heart)
2(Heart)
3(Heart)
5(Diamond)
11(Heart)
11(Club)
10(Club)
9(Club)
3(Diamond)
4(Diamond)
6(Spade)
9(Spade)
10(Heart)
11(Spade)
5(Spade)
12(Spade)
1(Spade)
6(Heart)
12(Heart)
13(Diamond)
1(Heart)
2(Diamond)
1(Diamond)
7(Spade)
12(Club)
10(Diamond)
2(Spade)
8(Heart)
12(Diamond)
8(Spade)
4(Spade)
6(Diamond)
10(Spade)
13(Club)
7(Heart)
13(Heart)
7(Diamond)
9(Heart)
11(Diamond)
5(Heart)
8(Diamond)
9(Diamond)
3(Spade)
13(Spade)
4(Heart)
2(Heart)
3(Heart)
5(Diamond)
11(Heart)
11(Club)
10(Club)
9(Club)
3(Diamond)
4(Diamond)
6(Spade)
9(Spade)
10(Heart)
11(Spade)
5(Spade)
12(Spade)
1(Spade)
6(Heart)
12(Heart)
13(Diamond)
1(Heart)
2(Diamond)
1(Diamond)
7(Spade)
12(Club)
10(Diamond)
2(Spade)
8(Heart)
1(Club)
2(Club)
3(Club)
4(Club)
5(Club)
6(Club)
7(Club)
8(Club)
You can learn more about creating a class here brainly.com/question/10410845