
Concept explainers
Draw a UML class diagram for the following code:
import java.util.Scanner;
public class ArrayStack {
private int maxSize;
private int top;
private int[] stackArray;
public ArrayStack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public void push(int value) {
if (isFull()) {
System.out.println("Stack is full. Cannot push.");
return;
}
stackArray[++top] = value;
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop.");
return -1;
}
return stackArray[top--];
}
public void popAll() {
while (!isEmpty()) {
pop();
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot peek.");
return -1;
}
return stackArray[top];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the stack: ");
int stackSize = scanner.nextInt();
ArrayStack stack = new ArrayStack(stackSize);
while (true) {
System.out.println("\nSelect an option:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Pop All");
System.out.println("5. Check if stack is empty");
System.out.println("6. Check if stack is full");
System.out.println("7. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a value to push: ");
int value = scanner.nextInt();
stack.push(value);
break;
case 2:
System.out.println("Popped: " + stack.pop());
break;
case 3:
System.out.println("Top element: " + stack.peek());
break;
case 4:
stack.popAll();
System.out.println("All elements popped.");
break;
case 5:
System.out.println("Is stack empty? " + stack.isEmpty());
break;
case 6:
System.out.println("Is stack full? " + stack.isFull());
break;
case 7:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice.");
}
}
}
}

Step by stepSolved in 3 steps with 1 images

- Modify the unsortedArrayAccess class by adding a method that find the smallest element and average in the array. import java.util.*; public class unsortedArrayAccess{ public static double[] arr; public int arraySize; public unsortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; } public int search(double Key) { int i = 0; while ((i < arraySize) && (arr[i] != Key) ) { i = i + 1; } if (i < arraySize) { return i; } else { System.out.println("There is no such item!"); return -1; } } public void append(double Item) { arr[arraySize] = Item; arraySize = arraySize + 1; } public double remove() { if (arraySize == 0) { System.out.println("There is no item in the array!"); return -1; } double x = arr[arraySize - 1]; arraySize = arraySize - 1; return x; } public void deletion(double Key) { int k = search(Key); if (k != -1) { for (int i = k; i…arrow_forwardConsider the following Java interface definition: public interface Mover { public int getX(); public int getY(); public void setLocation(int x, int y); } Which of the following is a correct implementation of the Mover interface? O A. public class Cartoon Character implements Mover{ private int x, y; public int getX() { public int getY() { } O B. public class Cartoon Character implements Mover{ private int x, y; public int getX(); public int getY(); public void setLocation(int x, int y); // code for method body} // code for method body} } O C. public class Cartoon Character implements Mover{ private int x, y; public int getX; public int getY; public void setLocation; } O D. public class Cartoon Character implements Mover{ private int x, y; public int getX() { // code for method body} public int getY() { // code for method body} public void setLocation(int x, int y){ // code for method body} }arrow_forwardImplement move the following way. public int[] walk(int... stepCounts) { // Implement the logic for walking the players returnnewint[0]; } public class WalkingBoardWithPlayers extends WalkingBoard{ privatePlayer[] players; privateintround; publicstaticfinalintSCORE_EACH_STEP=13; publicWalkingBoardWithPlayers(int[][] board, intplayerCount) { super(board); initPlayers(playerCount); } publicWalkingBoardWithPlayers(intsize, intplayerCount) { super(size); initPlayers(playerCount); } privatevoidinitPlayers(intplayerCount) { if(playerCount <2){ thrownewIllegalArgumentException("Player count must be at least 2"); } else { this.players=newPlayer[playerCount]; this.players[0] =newMadlyRotatingBuccaneer(); for (inti=1; i < playerCount; i++) { this.players[i] =newPlayer(); } } } package walking.game.player; import walking.game.util.Direction; public class Player{ privateintscore; protectedDirectiondirection=Direction.UP; publicPlayer() {} publicintgetScore() { return score; }…arrow_forward
- *JAVA* complete method Delete the largest valueremoveMax(); Delete the smallest valueremoveMin(); class BinarHeap<T> { int root; static int[] arr; static int size; public BinarHeap() { arr = new int[50]; size = 0; } public void insert(int val) { arr[++size] = val; bubbleUP(size); } public void bubbleUP(int i) { int parent = (i) / 2; while (i > 1 && arr[parent] > arr[i]) { int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; i = parent; } } public int retMin() { return arr[1]; } public void removeMin() { } public void removeMax() { } public void print() { for (int i = 0; i <= size; i++) { System.out.print( arr[i] + " "); } }} public class BinarH { public static void main(String[] args) { BinarHeap Heap1 = new BinarHeap();…arrow_forwardConsider the GameOfLife class public class GameOfLife { private BooleanProperty[][] cells; public GameOfLife() { cells = new BooleanProperty[10][10]; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { cells[x][y] = new SimpleBooleanProperty(); public void ensureAlive(int x, int y) { cells[x][y].set(true); public void ensureDead(int x, int y) { cells[x][y]•set(false); public boolean isAlive(int x, int y) { return cells[x][y]•get(); } a. Identify one code smell in this program. b. Identify and explain the most pertinent (problematic) design smell in this program.arrow_forwardc) Draw a UML Diagram for the following source code. (CLO1, CLO2) public class MyCircle { double MyRadius= 0.0; MyCircle() { } MyÇircle(double newRadius) { MyRadius = newRadius; } double getArea() { return MyRadius * MyRadius * 3.14159;} public static void main(String[] args) { MyCircle C1 = new MyCircle(); C1 MyRadius = 2.0; MyCircle C2 = new MyCircle(); C2.MyRadius = 4.0; System.out.println(C1.getArea()); System.out println(C2,getArea(); } }arrow_forward
- Write the implementation of the Java classes based on the following UML diagram. (a) Define the method IncreTotSpecies) to increment the data field totalSpecles (b) Define the method printColor) to print the data field color (c) Override the method printColor) in the class Falcon to print the data fields featherColor, beakColor Birds +color: String +foodHabit: String +totalSpecies: int -skeleton: String +Birds () +Birds(color: String, foodHabit: String, skeleton: String) +increTotSpecies (): void +printColor (): void Falcon -featherColor: String -beakColor: String +Falcon (featherColor: String, beakColor: String) +printColor (): voidarrow_forwardimport java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int n = scnr.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scnr.nextInt(); } for (int i = n - 1; i >= 0; i--) { System.out.print(arr[i]); if (i > 0) { System.out.print(","); } } }}arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





