0% found this document useful (0 votes)
4 views18 pages

Java Programs for Data Structures

The document contains two Java programming assignments. The first assignment involves creating a 3D array, displaying its elements, and searching for a user-input number within it. The second assignment involves implementing a linked list, inserting and deleting elements at specified positions, and displaying the updated list.

Uploaded by

hindunation69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Java Programs for Data Structures

The document contains two Java programming assignments. The first assignment involves creating a 3D array, displaying its elements, and searching for a user-input number within it. The second assignment involves implementing a linked list, inserting and deleting elements at specified positions, and displaying the updated list.

Uploaded by

hindunation69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Java - Assignment 3‬

‭Program 1:‬‭Write a program‬‭in JAVA to create a 3D array (containing integer‬


‭ alues) and display it on the console. Then input a number from the user and‬
v
‭check whether it is present.‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ S
‬canner‬
;‬

public‬‭
‭ class‬‭
ThreeDArraySearch‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

// Initialize a 3D array with integer values‬

int‬
‭ [][][]‬‭
‭ array‬‭
=‬‭
{‬
{‬

{‭
‭1‬‭
,
‬‬‭
2‭
,
‬‬‭
3‬},‬

{‭
‭4‬‭
,
‬‬‭
5‭
,
‬‬‭
6‬}‬

},‬

{‬

{‭
‭7‬‭
,
‬‬‭
8‭
,
‬‬‭
9‬},‬

{‭
‭1‬0‬
,‬‭
‭ 11‬
,‬‭
‭ 12‬
}‬

}‬

};‬

// Display the 3D array‬



System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ println‬
‭ (‭
‭"‬3D Array Elements:"‬
);‬

for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
i‬‭
<‬‭
array‬
.‭
‭l‬ength‬
;‬‭
‭ i‭
+‬+‬
) {‬

for‬‭
‭ (‭
i‬nt‬‭
j‬‭
=‬‭
0‭
;‬‬‭
j‬‭
<‬‭
array‬
[‭
‭i‬‭
]
‬.‬
length‬
‭ ;‬‭
‭ j‭
+‬+‬
) {‬

for‬‭
‭ (‭
i
‬nt‬‭
k‬‭
=‬‭
0‬;‬‭
‭ k‬‭
<‬‭
array‬
[‭
‭i‬‭
]
‬[‬
j‬
‭ ].‬
‭ length‬
‭ ;‬‭
‭ k‭
+‬+‬
) {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭a‬rray‬
[‭
‭i‬‭
]
‬[‬
j‭
‭ ]
‬[‬
k‭
‭]‬‬‭
+‬‭
" "‬
);‬

}‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
();‬

}‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
();‬

}‬

// Take input from the user‬



Scanner‬‭
‭ scanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Enter a number to search: "‬
);‬

int‬‭
‭ searchNumber‬‭
=‬‭
scanner‬
.‬
‭ nextInt‬
‭ ();‬

scanner‬
‭ .‭
‭c‬lose‬
();‬

// Search for the number in the 3D array‬



boolean‬‭
‭ found‬‭
=‬‭
false‬
;‬

for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
i‬‭
<‬‭
array‬
.‭
‭l‬ength‬
;‬‭
‭ i‭
+‬+‬
) {‬

for‬‭
‭ (‭
i‬nt‬‭
j‬‭
=‬‭
0‭
;‬‬‭
j‬‭
<‬‭
array‬
[‭
‭i‬‭
]
‬.‬
length‬
‭ ;‬‭
‭ j‭
+‬+‬
) {‬

for‬‭
‭ (‭
i
‬nt‬‭
k‬‭
=‬‭
0‬;‬‭
‭ k‬‭
<‬‭
array‬
[‭
‭i‬‭
]
‬[‬
j‬
‭ ].‬
‭ length‬
‭ ;‬‭
‭ k‭
+‬+‬
) {‬

if‬‭
‭ (‬array‬
‭ [‬
‭ i‭
‭]‬[‬
j‭
‭]
‬[‬
k‭
‭ ]
‬‬‭
==‬‭
searchNumber‬
) {‬

found‬‭
‭ =‬‭
true‬
;‬

break‬
‭ ;‬

}‬

}‬

}‬

‭Java - Assignment 3‬
}‬

// Display result‬

if‬‭
‭ (‭
f
‬ound‬
) {‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ s
‬earchNumber‬‭
+‬‭
" is present in the 3D array."‬
);‬

}‬‭
‭ else‬‭
{‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ s
‬earchNumber‬‭
+‬‭
" is not found in the 3D array."‬
);‬

}‬

}‬

}‬

‭Flowchart:‬

‭Results:‬
‭Java - Assignment 3‬

‭ rogram 2: Write a program in Java to implement a linked list containing 5 numbers‬


P
‭<11, 22, 6, 89, 99> and then perform the following: Then insert a number <50> in the‬
‭third position of the linked list and print the new linked list <11, 22, 50, 6, 89, 99>.‬
‭Delete the 2nd element of the linked list and print the remaining linked list <11, 50, 6,‬
‭89, 99>. Delete the 1st element of the linked list and print the remaining linked list <50,‬
‭6, 89, 99>. Delete the last element of the linked list and print the remaining linked list‬
‭<50,6,89>‬
class‬‭
‭ Node‬‭
{‬
int‬‭
‭ data‬
;‬

Node‬‭
‭ next‬
;‬

Node‬
‭ (‬
‭ int‬‭
‭ data‬
) {‬

this‬
‭ .‭
‭d
‬ata‬‭
=‬‭
data‬
;‬

this‬
‭ .‭
‭n
‬ext‬‭
=‬‭
null‬
;‬

}‬

}‬

public‬‭
‭ class‬‭
LinkedListOperations‬‭
{‬
Node‬‭
‭ head‬
;‬

// Method to add a node at the end‬



void‬‭
‭ add‬
(‭
‭i
‬nt‬‭
data‬
) {‬

Node‬‭
‭ newNode‬‭
=‬‭
new‬‭
Node‬
(‭
‭ d
‬ata‬
);‬

if‬‭
‭ (‭
h
‬ead‬‭
==‬‭
null‬
) {‬

head‬‭
‭ =‬‭
newNode‬
;‬

return‬
‭ ;‬

}‬

Node‬‭
‭ temp‬‭
=‬‭
head‬
;‬

while‬‭
‭ (‭
t‬emp‬
.‭
‭ n
‬ext‬‭
!=‬‭
null‬
) {‬

temp‬‭
‭ =‬‭
temp‬
.‬
‭ next‬
‭ ;‬

‭Java - Assignment 3‬
}‬

temp‬
‭ .‭
‭n
‬ext‬‭
=‬‭
newNode‬
;‬

}‬

// Method to insert at a specific position‬



void‬‭
‭ insertAt‬
(‭
‭i‬nt‬‭
index‬
,‬‭
‭ int‬‭
data‬
) {‬

Node‬‭
‭ newNode‬‭
=‬‭
new‬‭
Node‬
(‭
‭ d
‬ata‬
);‬

if‬‭
‭ (‭
i
‬ndex‬‭
==‬‭
0‭
)‬ {‬
newNode‬
‭ .‭
‭ n
‬ext‬‭
=‬‭
head‬
;‬

head‬‭
‭ =‬‭
newNode‬
;‬

return‬
‭ ;‬

}‬

Node‬‭
‭ temp‬‭
=‬‭
head‬
;‬

for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
temp‬‭
!=‬‭
null‬‭
&&‬‭
i‬‭
<‬‭
index‬‭
-‬‭
1‭
;‬‬‭
i‬++‬
‭ ) {‬

temp‬‭
‭ =‬‭
temp‬
.‬
‭ next‬
‭ ;‬

}‬

if‬‭
‭ (‭
t
‬emp‬‭
==‬‭
null‬
)‬‭
‭ return‬
;‬

newNode‬
‭ .‭
‭n‬ext‬‭
=‬‭
temp‬
.‭
‭ n
‬ext‬
;‬

temp‬
‭ .‭
‭n
‬ext‬‭
=‬‭
newNode‬
;‬

}‬

// Method to delete node at a specific position‬



void‬‭
‭ deleteAt‬
(‭
‭i‬nt‬‭
index‬
) {‬

if‬‭
‭ (‭
h
‬ead‬‭
==‬‭
null‬
)‬‭
‭ return‬
;‬

if‬‭
‭ (‭
i
‬ndex‬‭
==‬‭
0‭
)‬ {‬
head‬‭
‭ =‬‭
head‬
.‬
‭ next‬
‭ ;‬

return‬
‭ ;‬

}‬

Node‬‭
‭ temp‬‭
=‬‭
head‬
;‬

for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
temp‬‭
!=‬‭
null‬‭
&&‬‭
i‬‭
<‬‭
index‬‭
-‬‭
1‭
;‬‬‭
i‬++‬
‭ ) {‬

temp‬‭
‭ =‬‭
temp‬
.‬
‭ next‬
‭ ;‬

}‬

if‬‭
‭ (‭
t
‬emp‬‭
==‬‭
null‬‭
||‬‭
temp‬
.‭
‭ n
‬ext‬‭
==‬‭
null‬
)‬‭
‭ return‬
;‬

temp‬
‭ .‭
‭n
‬ext‬‭
=‬‭
temp‬
.‭
‭ n
‬ext‬
.‬
‭ next‬
‭ ;‬

}‬

// Method to display the linked list‬



void‬‭
‭ display‬
() {‬

Node‬‭
‭ temp‬‭
=‬‭
head‬
;‬

while‬‭
‭ (‭
t‬emp‬‭
!=‬‭
null‬
) {‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rint‬
(‭
‭t‬emp‬
.‭
‭ d
‬ata‬‭
+‬‭
" -> "‬
);‬

temp‬‭
‭ =‬‭
temp‬
.‬
‭ next‬
‭ ;‬

}‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ println‬
‭ (‭
‭"‬null"‬
);‬

}‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

LinkedListOperations‬‭
‭ list‬‭
=‬‭
new‬‭
LinkedListOperations‬
();‬

// Step 1: Create linked list with initial values‬



‭Java - Assignment 3‬
list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 1
‬1‬
);‬

list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 2
‬2‬
);‬

list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 6
‬‭
)‬;‬
list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 8
‬9‬
);‬

list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 9
‬9‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Original Linked List: "‬
);‬

list‬
‭ .‭
‭d
‬isplay‬
();‬

// Step 2: Insert 50 at the third position (index 2)‬



list‬
‭ .‭
‭i
‬nsertAt‬
(‭
‭ 2
‬‭
,‬‬‭
50‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬After inserting 50 at position 3: "‬
);‬

list‬
‭ .‭
‭d
‬isplay‬
();‬

// Step 3: Delete the 2nd element (index 1)‬



list‬
‭ .‭
‭d
‬eleteAt‬
(‭
‭ 1
‬‭
)‬;‬
System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬After deleting 2nd element: "‬
);‬

list‬
‭ .‭
‭d
‬isplay‬
();‬

// Step 4: Delete the 1st element (index 0)‬



list‬
‭ .‭
‭d
‬eleteAt‬
(‭
‭ 0
‬‭
)‬;‬
System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬After deleting 1st element: "‬
);‬

list‬
‭ .‭
‭d
‬isplay‬
();‬

// Step 5: Delete the last element‬



list‬
‭ .‭
‭d
‬eleteAt‬
(‭
‭ 3
‬‭
)‬;‬
System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬After deleting last element: "‬
);‬

list‬
‭ .‭
‭d
‬isplay‬
();‬

}‬

}‬

‭Flowchart:‬
‭Java - Assignment 3‬

‭Results:‬
‭Java - Assignment 3‬

‭ rogram 3: Write a program in Java to find duplicate characters in an user defined‬


P
‭input sting‬
import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ H
‬ashMap‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ M
‬ap‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ S
‬canner‬
;‬

public‬‭
‭ class‬‭
FindDuplicateCharacters‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

// Taking input from the user‬


Scanner‬‭
‭ scanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Enter a string: "‬
);‬

String‬‭
‭ input‬‭
=‬‭
scanner‬
.‭
‭ n
‬extLine‬
();‬

scanner‬
‭ .‭
‭c‬lose‬
();‬

// Create a map to store character frequencies‬


Map‬
‭ <‭
‭C
‬haracter‬
,‬‭
‭ Integer‬
>‬‭
‭ charCount‬‭
=‬‭
new‬‭
HashMap‬
<>();‬

// Loop through each character of the string‬


for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
i‬‭
<‬‭
input‬
.‭
‭l‬ength‬
();‬‭
‭ i‭
+‬+‬
) {‬

char‬‭
‭ c‬‭
=‬‭
input‬
.‭
‭c‬harAt‬
(‭
‭i
‬‬);‬

charCount‬
‭ .‭
‭ p
‬ut‬
(‭
‭ c
‬‭
,‬‬‭
charCount‬
.‭
‭ g
‬etOrDefault‬
(‭
‭ c
‬‭
,‬‬‭
0‬)‬‭
‭ +‬‭
1‭
)‬;‬

}‬

// Print duplicate characters‬


System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ println‬
‭ (‭
‭"‬Duplicate characters in the string are:"‬
);‬

boolean‬‭
‭ found‬‭
=‬‭
false‬
;‬

for‬‭
‭ (‭
M
‬ap‬
.‭
‭ E
‬ntry‬
<‭
‭C‬haracter‬
,‬‭
‭ Integer‬
>‬‭
‭ entry‬‭
:‬‭
charCount‬
.‭
‭e‬ntrySet‬
())‬‭
‭ {‬

if‬‭
‭ (‭
e
‬ntry‬
.‭
‭g‬etValue‬
()‬‭
‭ >‬‭
1‭
)‬ {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
(‬
‭ entry‬
‭ .‭
‭g‬etKey‬
()‬‭
‭ +‬‭
" appears "‬‭
+‬‭
entry‬
.‬
‭ getValue‬
‭ ()‬

+‬‭
‭ " times"‬
);‬

‭Java - Assignment 3‬
found‬‭
‭ =‬‭
true‬
;‬

}‬

}‬

if‬‭
‭ (‭
!
‬‭
f‬ound‬
) {‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬No duplicate characters found."‬
);‬

}‬

}‬

}‬

‭Flowchart:‬
‭Java - Assignment 3‬

‭Results:‬
‭Java - Assignment 3‬

‭Problem 4:‬‭Write a program in Java to check palindrome string using data types‬
‭queue and stack‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ L
‬inkedList‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ Q
‬ueue‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ S
‬tack‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ S
‬canner‬
;‬

public‬‭
‭ class‬‭
PalindromeUsingQueueStack‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

// Taking input from the user‬

Scanner‬‭
‭ scanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Enter a string: "‬
);‬

String‬‭
‭ input‬‭
=‬‭
scanner‬
.‭
‭ n
‬extLine‬
();‬

scanner‬
‭ .‭
‭c‬lose‬
();‬

// Remove spaces and convert to lowercase to handle cases like "A man a‬

plan"‬

input‬‭
‭ =‬‭
input‬
.‭
‭r‬eplaceAll‬
(‭
‭"‬‭
\
‬\‬
s"‬
‭ ,‬‭
‭ ""‬
).‬
‭ toLowerCase‬
‭ ();‬

// Using Stack for checking palindrome‬



Stack‬
‭ <‬
‭ Character‬
‭ >‬‭
‭ stack‬‭
=‬‭
new‬‭
Stack‬
<>();‬

Queue‬
‭ <‬
‭ Character‬
‭ >‬‭
‭ queue‬‭
=‬‭
new‬‭
LinkedList‬
<>();‬

// Push characters to stack and enqueue to queue‬



for‬‭
‭ (‭
i
‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
i‬‭
<‬‭
input‬
.‭
‭l‬ength‬
();‬‭
‭ i‭
+‬+‬
) {‬

‭Java - Assignment 3‬
char‬‭
‭ c‬‭
=‬‭
input‬
.‭
‭c‬harAt‬
(‭
‭i
‬‬);‬

stack‬
‭ .‭
‭p‬ush‬
(‭
‭ c
‬‬);‬

queue‬
‭ .‭
‭a‬dd‬
(‭
‭ c
‬‭
)‬;‬
}‬

// Check if the string is a palindrome by comparing stack and queue‬



boolean‬‭
‭ isPalindrome‬‭
=‬‭
true‬
;‬

while‬‭
‭ (‭
!‬‭
s
‬tack‬
.‭
‭i‬sEmpty‬
()) {‬

if‬‭
‭ (‭
s
‬tack‬
.‭
‭p‬op‬
()‬‭
‭ !=‬‭
queue‬
.‭
‭p‬oll‬
()) {‬

isPalindrome‬‭
‭ =‬‭
false‬
;‬

break‬
‭ ;‬

}‬

}‬

// Print result‬

if‬‭
‭ (‭
i
‬sPalindrome‬
) {‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬The string is a palindrome."‬
);‬

}‬‭
‭ else‬‭
{‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬The string is not a palindrome."‬
);‬

}‬

}‬

}‬

‭Results:‬

‭Program 5: Write a program in Java to check two strings are anagram or not‬
import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ A
‬rrays‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ S
‬canner‬
;‬

‭Java - Assignment 3‬

public‬‭
‭ class‬‭
AnagramCheck‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

// Taking input from the user‬


Scanner‬‭
‭ scanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Enter the first string: "‬
);‬

String‬‭
‭ str1‬‭
=‬‭
scanner‬
.‬
‭ nextLine‬
‭ ();‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ print‬
‭ (‭
‭"‬Enter the second string: "‬
);‬

String‬‭
‭ str2‬‭
=‬‭
scanner‬
.‬
‭ nextLine‬
‭ ();‬

scanner‬
‭ .‭
‭c‬lose‬
();‬

// Remove spaces and convert to lowercase for comparison‬


str1‬‭
‭ =‬‭
str1‬
.‭
‭ r
‬eplaceAll‬
(‭
‭"‬‭
\
‬\‬
s"‬
‭ ,‬‭
‭ ""‬
).‬
‭ toLowerCase‬
‭ ();‬

str2‬‭
‭ =‬‭
str2‬
.‭
‭ r
‬eplaceAll‬
(‭
‭"‬‭
\
‬\‬
s"‬
‭ ,‬‭
‭ ""‬
).‬
‭ toLowerCase‬
‭ ();‬

// Check if lengths are different‬


if‬‭
‭ (‭
s
‬tr1‬
.‭
‭ l
‬ength‬
()‬‭
‭ !=‬‭
str2‬
.‭
‭ l
‬ength‬
()) {‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬The strings are not anagrams."‬
);‬

return‬
‭ ;‬

}‬

// Convert strings to character arrays‬


char‬
‭ []‬‭
‭ arr1‬‭
=‬‭
str1‬
.‭
‭ t
‬oCharArray‬
();‬

char‬
‭ []‬‭
‭ arr2‬‭
=‬‭
str2‬
.‭
‭ t
‬oCharArray‬
();‬

// Sort the arrays‬


Arrays‬
‭ .‭
‭ s
‬ort‬
(‭
‭ a
‬rr1‬
);‬

Arrays‬
‭ .‭
‭ s
‬ort‬
(‭
‭ a
‬rr2‬
);‬

// Compare sorted arrays‬


if‬‭
‭ (‭
A
‬rrays‬
.‬
‭ equals‬
‭ (‭
‭a‬rr1‬
,‬‭
‭ arr2‬
)) {‬

‭Java - Assignment 3‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬The strings are anagrams."‬
);‬

}‬‭
‭ else‬‭
{‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ "
‬The strings are not anagrams."‬
);‬

}‬

}‬

}‬

‭Results:‬

‭ rogram 6: You are asked to write a discount system for a beauty salon, which‬
P
‭provides services and sells beauty products. It offers 3 types of memberships:‬
‭Premium, Gold and Silver. Premium, gold and silver members receive a discount of‬
‭20%, 15%, and 10%, respectively, for all services provided. Customers without‬
‭membership receive no discount. All members receive a flat 10% discount on‬
‭products purchased (this might change in future). Your system shall consist of three‬
‭classes: Customer, Discount and Visit, as shown in the class diagram. It shall‬
‭compute the total bill if a customer purchases $x of products and $y of services, for a‬
‭visit. Also write a test program to exercise all the classes.‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ D
‬ate‬
;‬

class‬‭
‭ Customer‬‭
{‬

private‬‭
‭ String‬‭
name‬
;‬

private‬‭
‭ boolean‬‭
member‬‭
=‬‭
false‬
;‬

‭Java - Assignment 3‬
private‬‭
‭ String‬‭
memberType‬
;‬

public‬‭
‭ Customer‬
(‭
‭ S
‬tring‬‭
name‬
) {‬

this‬
‭ .‭
‭n
‬ame‬‭
=‬‭
name‬
;‬

}‬

public‬‭
‭ String‬‭
getName‬
() {‬

return‬‭
‭ name‬
;‬

}‬

public‬‭
‭ boolean‬‭
isMember‬
() {‬

return‬‭
‭ member‬
;‬

}‬

public‬‭
‭ void‬‭
setMember‬
(‭
‭ b
‬oolean‬‭
member‬
) {‬

this‬
‭ .‭
‭m
‬ember‬‭
=‬‭
member‬
;‬

}‬

public‬‭
‭ String‬‭
getMemberType‬
() {‬

return‬‭
‭ memberType‬
;‬

}‬

public‬‭
‭ void‬‭
setMemberType‬
(‬
‭ String‬‭
‭ type‬
) {‬

this‬
‭ .‭
‭m
‬emberType‬‭
=‬‭
type‬
;‬

}‬

@‭
‭O‬verride‬

public‬‭
‭ String‬‭
toString‬
() {‬

return‬‭
‭ "Customer[name="‬‭
+‬‭
name‬‭
+‬‭
", member="‬‭
+‬‭
member‬‭
+‬‭
", memberType="‬‭
+‬
memberType‬‭
‭ +‬‭
"]"‬
;‬

}‬

‭Java - Assignment 3‬
}‬

class‬‭
‭ DiscountRate‬‭
{‬

private‬‭
‭ static‬‭
final‬‭
double‬‭
serviceDiscountPremium‬‭
=‬‭
0.2‬
;‬

private‬‭
‭ static‬‭
final‬‭
double‬‭
serviceDiscountGold‬‭
=‬‭
0.15‬
;‬

private‬‭
‭ static‬‭
final‬‭
double‬‭
serviceDiscountSilver‬‭
=‬‭
0.1‬
;‬

private‬‭
‭ static‬‭
final‬‭
double‬‭
productDiscount‬‭
=‬‭
0.1‬
;‬

public‬‭
‭ static‬‭
double‬‭
getServiceDiscountRate‬
(‭
‭S‬tring‬‭
type‬
) {‬

switch‬‭
‭ (‭
t
‬ype‬
) {‬

case‬‭
‭ "Premium"‬
:‬‭
‭ return‬‭
serviceDiscountPremium‬
;‬

case‬‭
‭ "Gold"‬
:‬‭
‭ return‬‭
serviceDiscountGold‬
;‬

case‬‭
‭ "Silver"‬
:‬‭
‭ return‬‭
serviceDiscountSilver‬
;‬

default:‬‭
‭ return‬‭
0.0‬
;‬

}‬

}‬

public‬‭
‭ static‬‭
double‬‭
getProductDiscountRate‬
() {‬

return‬‭
‭ productDiscount‬
;‬

}‬

}‬

class‬‭
‭ Visit‬‭
{‬

private‬‭
‭ Customer‬‭
customer‬
;‬

private‬‭
‭ Date‬‭
date‬
;‬

private‬‭
‭ double‬‭
serviceExpense‬
;‬

private‬‭
‭ double‬‭
productExpense‬
;‬

public‬‭
‭ Visit‬
(‭
‭C‬ustomer‬‭
customer‬
,‬‭
‭ Date‬‭
date‬
) {‬

this‬
‭ .‭
‭c
‬ustomer‬‭
=‬‭
customer‬
;‬

this‬
‭ .‭
‭d
‬ate‬‭
=‬‭
date‬
;‬

‭Java - Assignment 3‬
}‬

public‬‭
‭ String‬‭
getName‬
() {‬

return‬‭
‭ customer‬
.‬
‭ getName‬
‭ ();‬

}‬

public‬‭
‭ double‬‭
getServiceExpense‬
() {‬

return‬‭
‭ serviceExpense‬
;‬

}‬

public‬‭
‭ void‬‭
setServiceExpense‬
(‭
‭d
‬ouble‬‭
ex‬
) {‬

this‬
‭ .‭
‭s
‬erviceExpense‬‭
=‬‭
ex‬
;‬

}‬

public‬‭
‭ double‬‭
getProductExpense‬
() {‬

return‬‭
‭ productExpense‬
;‬

}‬

public‬‭
‭ void‬‭
setProductExpense‬
(‭
‭d
‬ouble‬‭
ex‬
) {‬

this‬
‭ .‭
‭p
‬roductExpense‬‭
=‬‭
ex‬
;‬

}‬

public‬‭
‭ double‬‭
getTotalExpense‬
() {‬

double‬‭
‭ serviceDiscount‬‭
=‬‭
customer‬
.‭
‭ i
‬sMember‬
()‬‭
‭ ?‬

DiscountRate‬
‭ .‭
‭g‬etServiceDiscountRate‬
(‭
‭ c
‬ustomer‬
.‭
‭g‬etMemberType‬
())‬‭
‭ :‬‭
0‭
;‬‬

double‬‭
‭ productDiscount‬‭
=‬‭
customer‬
.‭
‭ i
‬sMember‬
()‬‭
‭ ?‬

DiscountRate‬
‭ .‭
‭g‬etProductDiscountRate‬
()‬‭
‭ :‬‭
0‭
;‬‬

return‬‭
‭ (‭
s
‬erviceExpense‬‭
*‬‭
(‭
1
‬‬‭
-‬‭
serviceDiscount‬
))‬‭
‭ +‬‭
(‭
p‬roductExpense‬‭
*‬‭
(‭
1‬‬‭
-‬
productDiscount‬
‭ ));‬

}‬

‭Java - Assignment 3‬

@‭
‭O‬verride‬

public‬‭
‭ String‬‭
toString‬
() {‬

return‬‭
‭ "Visit[customer="‬‭
+‬‭
customer‬‭
+‬‭
", date="‬‭
+‬‭
date‬‭
+‬‭
",‬
serviceExpense="‬‭
‭ +‬‭
serviceExpense‬‭
+‬‭
", productExpense="‬‭
+‬‭
productExpense‬‭
+‬‭
"]"‬
;‬

}‬

}‬

public‬‭
‭ class‬‭
TestSalon‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬
[]‬‭
‭ args‬
) {‬

Customer‬‭
‭ customer‬‭
=‬‭
new‬‭
Customer‬
(‭
‭ "
‬Alice"‬
);‬

customer‬
‭ .‭
‭s‬etMember‬
(‭
‭ t
‬rue‬
);‬

customer‬
‭ .‭
‭s‬etMemberType‬
(‭
‭ "
‬Gold"‬
);‬

Visit‬‭
‭ visit‬‭
=‬‭
new‬‭
Visit‬
(‭
‭c‬ustomer‬
,‬‭
‭ new‬‭
Date‬
());‬

visit‬
‭ .‬
‭ setServiceExpense‬
‭ (‭
‭1‬00‬
);‬

visit‬
‭ .‬
‭ setProductExpense‬
‭ (‭
‭5‬0‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ println‬
‭ (‭
‭v‬isit‬
);‬

System‬
‭ .‭
‭ o
‬ut‬
.‬
‭ println‬
‭ (‭
‭"‬Total expense: "‬‭
+‬‭
visit‬
.‭
‭g‬etTotalExpense‬
());‬

}‬

}‬

‭Results:‬
‭Java - Assignment 3‬

You might also like