Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Exp 2.2 Java WS

The document describes an experiment to create a Card class that implements the Comparable interface and contains symbol and number data members. A HashSet is declared to hold Card objects. The program takes input from the user to create 8 Card objects, adds them to the HashSet, and displays the first occurrences of four card values in the set. The Card and Main classes are provided, with the Card class implementing methods like compareTo, hashCode, and equals for the Comparable interface. The learning outcomes include understanding HashSet operations and the Comparable interface.

Uploaded by

madhu jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Exp 2.2 Java WS

The document describes an experiment to create a Card class that implements the Comparable interface and contains symbol and number data members. A HashSet is declared to hold Card objects. The program takes input from the user to create 8 Card objects, adds them to the HashSet, and displays the first occurrences of four card values in the set. The Card and Main classes are provided, with the Card class implementing methods like compareTo, hashCode, and equals for the Comparable interface. The learning outcomes include understanding HashSet operations and the Comparable interface.

Uploaded by

madhu jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.2

Student Name: Madhu Kumar Jha UID: 20BCS2877


Branch: CSE Section/Group: 20BCS_WM-903_A
Semester: 5 Date of Performance: 28/9/2022
Subject Name: PBLJ Lab Subject Code: 20CSP-321

1. Aim/Overview of the practical:

2. Software/Hardware Requirements:

o Laptop
o Eclipse IDE

3. Algorithm/pseudo code:

Step 1- Start

Step 2- Create a class Card which implements Comparable interface

Step 3- Class contains symbol and number data members, getter and
setter functions.

Step 4- Declare a HashSet which take values of type Card.


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 5- Take 8 input of symbols and numbers from user and create
objects and using constructor initialize their values and add object in
Set.

Step 6- Display the first occurrences of four cards values in the set

Step 7- Exit

4.Steps for experiment/practical/Code:

Card.java
package com;
public class Card implements Comparable<Card> {
private char symbol;
private int number;

public Card() {}

public Card(char symbol, int number) {


super();
this.symbol = symbol;
this.number = number;
}

public char getSymbol() {


return symbol;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public void setSymbol(char symbol) {


this.symbol = symbol;
}

public int getNumber() {


return number;
}

public void setNumber(int number) {


this.number = number;
}

@Override
public String toString() {
return "Card [symbol=" + symbol + ", number=" + number + "]";
}

@Override
public int compareTo(Card o) {
if (this.symbol < o.symbol) return -1;
else if (this.symbol > o.symbol) return 1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else return 1;
}

@Override
public int hashCode() {
return String.valueOf(symbol).hashCode();
}

@Override
public boolean equals(Object obj){
if (obj instanceof Card) {
Card card = (Card) obj;
return (card.symbol == this.symbol);
} else {
return false;
}
}
}
Main.java
package com;
import java.util.*;
import java.lang.*;
import java.io.*;
import com.Card;
public class Main {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
Set<Card> set = new HashSet<>();

for (int i = 0; i < 8; i++) {


System.out.println("Enter a card:");
Card card = new Card();

card.setSymbol(sc.nextLine().charAt(0));
card.setNumber(sc.nextInt());
sc.nextLine();

set.add(card);
}
System.out.println("Four symbols gathered in
eight cards.");
System.out.println("Cards in Set are:");

for (Card card : set)


System.out.println(card.getSymbol() + " "
+ card.getNumber());

sc.close();
}

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6.Learning outcomes (What I have learnt):

1. Learnt about use of collections, HashSet

2. Learnt about various operation of HashSet.

3. Learnt about Comparable interface

You might also like