john
john
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
if (restructureNodeOnRemove != null) {
//update heights
updateHeights(restructureNodeOnRemove);
while (restructureNodeOnRemove != null) {
//restructure until tree is balanced
if (!isBalanced(restructureNodeOnRemove)) {
AVLNode z = restructureNodeOnRemove;
AVLNode y = (z.left != null && (z.right == null || z.left.height > z.right.height)) ? z.left : z.right;
AVLNode x = (y.left != null && (y.right == null || y.left.height > y.right.height)) ? y.left : y.right;
restructure(x, y, z);
}
restructureNodeOnRemove = restructureNodeOnRemove.parent;
if(restructureNodeOnRemove!=null){
updateHeights(restructureNodeOnRemove);
}
}

}
if (restructureNodeOnRemove != null) {
//update heights
updateHeights(restructureNodeOnRemove);
while (restructureNodeOnRemove != null) {
//restructure until tree is balanced
if (!isBalanced(restructureNodeOnRemove)) {
AVLNode z = restructureNodeOnRemove;
AVLNode y = (z.left != null && (z.right == null || z.left.height > z.right.height)) ? z.left : z.right;
AVLNode x = (y.left != null && (y.right == null || y.left.height > y.right.height)) ? y.left : y.right;
restructure(x, y, z);
}
restructureNodeOnRemove = restructureNodeOnRemove.parent;
if(restructureNodeOnRemove!=null){
updateHeights(restructureNodeOnRemove);
}
}

}
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
updateHeights(restructureNodeOnRemove);
if(restructureNodeOnRemove.parent!=null){
updateHeights(restructureNodeOnRemove.parent);
}
if(restructureNodeOnRemove.parent!=null){
updateHeights(restructureNodeOnRemove.parent);
}
195 replies
JCHJava Community | Help. Code. Learn.
Created by SummerPsycho on 11/15/2024 in #java-help
Flow chart to Java.
""" //name this file program2 or it will not work import java.util.Scanner;
public class program2 {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);

// Prompt the user for an input
System.out.print("Enter an amount of change (0-99): ");
int amount = scanner.nextInt();

// Calculate number of quarters
int quarters = amount / 25;
amount %= 25; // quarters is worth 25

// Calculate number of dimes
int dimes = amount / 10;
amount %= 10; // dimes is worth 10

// Calculate number of nickels
int nickels = amount / 5;
amount %= 5; // nickel is worth 5

//there may be remaining amount, here I add a nickel if there is
if (amount != 0){
nickels += 1;
}

// Display the result
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
}
}
public class program2 {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);

// Prompt the user for an input
System.out.print("Enter an amount of change (0-99): ");
int amount = scanner.nextInt();

// Calculate number of quarters
int quarters = amount / 25;
amount %= 25; // quarters is worth 25

// Calculate number of dimes
int dimes = amount / 10;
amount %= 10; // dimes is worth 10

// Calculate number of nickels
int nickels = amount / 5;
amount %= 5; // nickel is worth 5

//there may be remaining amount, here I add a nickel if there is
if (amount != 0){
nickels += 1;
}

// Display the result
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
}
}
"""
8 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
public void restructure(AVLNode x, AVLNode y, AVLNode z){
NodeGroup g = getComponents(x,y,z);
//Getting a NodeGroup object g that for x,y,z returns a,b,c node and subtrees t0,t1,t2 and t3
g.b.left = g.a;
g.b.right = g.c;

g.a.left = g.t0;
g.a.right = g.t1;

g.c.left = g.t2;
g.c.right = g.t3;

if (z.parent != null) {
if (z.parent.left == z) {
z.parent.left = g.b;
} else {
z.parent.right = g.b;
}
} else {
root = g.b;
}
g.b.parent = z.parent;

g.a.parent = g.b;
g.c.parent = g.b;



updateHeights(g.a);
updateHeights(g.c);
updateHeights(g.b);
}
public void restructure(AVLNode x, AVLNode y, AVLNode z){
NodeGroup g = getComponents(x,y,z);
//Getting a NodeGroup object g that for x,y,z returns a,b,c node and subtrees t0,t1,t2 and t3
g.b.left = g.a;
g.b.right = g.c;

g.a.left = g.t0;
g.a.right = g.t1;

g.c.left = g.t2;
g.c.right = g.t3;

if (z.parent != null) {
if (z.parent.left == z) {
z.parent.left = g.b;
} else {
z.parent.right = g.b;
}
} else {
root = g.b;
}
g.b.parent = z.parent;

g.a.parent = g.b;
g.c.parent = g.b;



updateHeights(g.a);
updateHeights(g.c);
updateHeights(g.b);
}
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
public boolean insertNode(int key, float value) throws IllegalArgumentException {
if(value < 0 || findByKey(key) != -1f) {
//returning false if node has a key that already exists or value is < 0
return false;
}
AVLNode newNode;
if (root == null){
root = new AVLNode(key, value);
updateHeights(root);
size++;
return true;
}else {
AVLNode current = root;
while (true) {
int cmp = Integer.compare(current.key, key);
if (cmp == 0)
return false;
else if (cmp < 0) {
if (current.right != null)
current = current.right;
else {
newNode = new AVLNode(key, value);
setRight(current,newNode);
updateHeights(newNode);
size++;
break;
}
} else {
if (current.left != null)
current = current.left;
else {
newNode = new AVLNode(key, value);
setLeft(current, newNode); updateHeights(newNode);
size++;
break;
}}}}
AVLNode n = newNode;

while (n != null) {
AVLNode x = n;
if (x.parent != null && x.parent.parent != null && !isBalanced(x.parent.parent)) {
AVLNode z = x.parent.parent;
AVLNode y = x.parent;
restructure(x, y, z);
updateHeights(n);
}else {
n = n.parent;
}
}
return true;
}
public boolean insertNode(int key, float value) throws IllegalArgumentException {
if(value < 0 || findByKey(key) != -1f) {
//returning false if node has a key that already exists or value is < 0
return false;
}
AVLNode newNode;
if (root == null){
root = new AVLNode(key, value);
updateHeights(root);
size++;
return true;
}else {
AVLNode current = root;
while (true) {
int cmp = Integer.compare(current.key, key);
if (cmp == 0)
return false;
else if (cmp < 0) {
if (current.right != null)
current = current.right;
else {
newNode = new AVLNode(key, value);
setRight(current,newNode);
updateHeights(newNode);
size++;
break;
}
} else {
if (current.left != null)
current = current.left;
else {
newNode = new AVLNode(key, value);
setLeft(current, newNode); updateHeights(newNode);
size++;
break;
}}}}
AVLNode n = newNode;

while (n != null) {
AVLNode x = n;
if (x.parent != null && x.parent.parent != null && !isBalanced(x.parent.parent)) {
AVLNode z = x.parent.parent;
AVLNode y = x.parent;
restructure(x, y, z);
updateHeights(n);
}else {
n = n.parent;
}
}
return true;
}
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
if (current.height != oldHeight && current.parent != null) {
updateHeights(current.parent);
}
if (current.height != oldHeight && current.parent != null) {
updateHeights(current.parent);
}
195 replies
JCHJava Community | Help. Code. Learn.
Created by Edmon on 11/14/2024 in #java-help
Testing Framework in Java
JUnit
10 replies
JCHJava Community | Help. Code. Learn.
Created by StremesJ on 11/11/2024 in #java-help
Need Help with java ASCII art - can anyone try this and then talk me through it. Please and Thanks:D
Program 4. Seven Segment Display (20%) In the file SevenSegment.jsh, write a method with the signature: void display(int n) that, given a number, displays ASCII art of that number in the style of an electronic “seven segment display”. For example: display(28); -- -- | | | -- -- | | | -- -- You may assume the number is a non-negative Java int. Hints:  Use n % 10 to get the right-most digit of a number. For example, 123456 % 10 == 6.  Use n / 10 to chop off the right-most digit of a number. For example, 123456 / 10 == 12345. To help your program draw the ASCII art, you should use the following method that, given a digit d and a line number n (from 1 to 5), returns a String representing line n of digit d.
String ssd(int d, int n) {
switch ((d*10)+n) {
case 1: case 5: case 21: case 23: case 25: case 31: case 33: case 35:
case 43: case 51: case 53: case 55: case 61: case 63: case 65: case 71:
case 81: case 83: case 85: case 91: case 93: case 95:
return " -- ";
case 24: case 52: case 62:
return "| ";
case 12: case 14: case 22: case 32: case 34: case 44: case 54: case 72:
case 74: case 94:
return " | ";
case 2: case 4: case 42: case 64: case 82: case 84: case 92:
return "| | ";
default: return " ";
}
}
String ssd(int d, int n) {
switch ((d*10)+n) {
case 1: case 5: case 21: case 23: case 25: case 31: case 33: case 35:
case 43: case 51: case 53: case 55: case 61: case 63: case 65: case 71:
case 81: case 83: case 85: case 91: case 93: case 95:
return " -- ";
case 24: case 52: case 62:
return "| ";
case 12: case 14: case 22: case 32: case 34: case 44: case 54: case 72:
case 74: case 94:
return " | ";
case 2: case 4: case 42: case 64: case 82: case 84: case 92:
return "| | ";
default: return " ";
}
}
29 replies
JCHJava Community | Help. Code. Learn.
Created by Akif on 10/30/2024 in #java-help
Module Not Found, when doing "mvn clean install"
open module use.guiFX {

requires javafx.graphics;
requires javafx.fxml;
requires use.core;
requires javafx.controls;
requires javafx.swing;
requires javafx.web;
requires org.kordamp.desktoppanefx.core;
requires use.gui;

exports org.tzi.use.mainFX;
exports org.tzi.use.gui.mainFX;
}
open module use.guiFX {

requires javafx.graphics;
requires javafx.fxml;
requires use.core;
requires javafx.controls;
requires javafx.swing;
requires javafx.web;
requires org.kordamp.desktoppanefx.core;
requires use.gui;

exports org.tzi.use.mainFX;
exports org.tzi.use.gui.mainFX;
}
1275 replies
JCHJava Community | Help. Code. Learn.
Created by Akif on 10/30/2024 in #java-help
Module Not Found, when doing "mvn clean install"
So when i add it like this: :runApp mvn -pl use-guiFX exec:java -Dexec.mainClass=org.tzi.use.mainFX.MainFX -Dexec.jvmArgs="%VMARGS%" -Dexec.args="%*" I get following: java.lang.NullPointerException: in (MainWindowFX.java:159) which is this:
if (fSession != null && fSession.hasSystem()) {
fModelBrowser.setModel(fSession.system().model());
}
if (fSession != null && fSession.hasSystem()) {
fModelBrowser.setModel(fSession.system().model());
}
inside MainWindowFX
1275 replies
JCHJava Community | Help. Code. Learn.
Created by Akif on 10/30/2024 in #java-help
Module Not Found, when doing "mvn clean install"
before it was using this methode
public static String getIconPath(String iconName) {
return "/images/" + iconName;
}
public static String getIconPath(String iconName) {
return "/images/" + iconName;
}
1275 replies
JCHJava Community | Help. Code. Learn.
Created by userexit on 10/30/2024 in #java-help
How to create custom id generator for primary key in springboot
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) {

}
public Serializable generate(SharedSessionContractImplementor session, Object object) {

}
302 replies
JCHJava Community | Help. Code. Learn.
Created by Dosage on 10/12/2024 in #java-help
what code I'll write?
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] grades = new int[7];


System.out.print("Enter first grade: ");
grades[0] = scanner.nextInt();
System.out.print("Enter second grade: ");
grades[1] = scanner.nextInt();
System.out.print("Enter third grade: ");
grades[2] = scanner.nextInt();
System.out.print("Enter fourth grade: ");
grades[3] = scanner.nextInt();
System.out.print("Enter fifth grade: ");
grades[4] = scanner.nextInt();
System.out.print("Enter sixth grade: ");
grades[5] = scanner.nextInt();
System.out.print("Enter seventh grade: ");
grades[6] = scanner.nextInt();


int smallestGrade = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] < smallestGrade) {
smallestGrade = grades[i];
}
}


System.out.println("The smallest grade is " + smallestGrade + ".");

scanner.close();
}
}
public class Grades {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] grades = new int[7];


System.out.print("Enter first grade: ");
grades[0] = scanner.nextInt();
System.out.print("Enter second grade: ");
grades[1] = scanner.nextInt();
System.out.print("Enter third grade: ");
grades[2] = scanner.nextInt();
System.out.print("Enter fourth grade: ");
grades[3] = scanner.nextInt();
System.out.print("Enter fifth grade: ");
grades[4] = scanner.nextInt();
System.out.print("Enter sixth grade: ");
grades[5] = scanner.nextInt();
System.out.print("Enter seventh grade: ");
grades[6] = scanner.nextInt();


int smallestGrade = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] < smallestGrade) {
smallestGrade = grades[i];
}
}


System.out.println("The smallest grade is " + smallestGrade + ".");

scanner.close();
}
}
5 replies
JCHJava Community | Help. Code. Learn.
Created by Yarden on 10/10/2024 in #java-help
I've created 1 client server, but everytime I'm exiting the server I can't connect anymore
try {
socket = new Socket("localhost", 1234);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

Scanner scanner = new Scanner(System.in);

while(true){
String msgToSend = scanner.nextLine();
bufferedWriter.write(msgToSend);
bufferedWriter.newLine();

System.out.println("Server: " + bufferedReader.readLine());

if(msgToSend.equalsIgnoreCase("BYE"))
break;

}
try {
socket = new Socket("localhost", 1234);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

Scanner scanner = new Scanner(System.in);

while(true){
String msgToSend = scanner.nextLine();
bufferedWriter.write(msgToSend);
bufferedWriter.newLine();

System.out.println("Server: " + bufferedReader.readLine());

if(msgToSend.equalsIgnoreCase("BYE"))
break;

}
76 replies
JCHJava Community | Help. Code. Learn.
Created by Yarden on 10/10/2024 in #java-help
I've created 1 client server, but everytime I'm exiting the server I can't connect anymore
socket.close(); inputStreamReader.close(); outputStreamWriter.close(); bufferedReader.close(); bufferedWriter.close(); System.out.println("Socket closed: " + socket.isClosed());
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
76 replies
JCHJava Community | Help. Code. Learn.
Created by Yarden on 10/10/2024 in #java-help
I've created 1 client server, but everytime I'm exiting the server I can't connect anymore
public class Server {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;

ServerSocket serverSocket = null;

serverSocket = new ServerSocket(1234);


while(true){

try {

socket = serverSocket.accept();

inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

while(true) {

String msgFromClient = bufferedReader.readLine();

System.out.println("Client: " + msgFromClient);

bufferedWriter.write("MSG Received");
bufferedWriter.newLine();
bufferedWriter.flush();



if (msgFromClient.equalsIgnoreCase("BYE")) {

bufferedWriter.flush();
socket.shutdownOutput();
break;
}
}
public class Server {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;

ServerSocket serverSocket = null;

serverSocket = new ServerSocket(1234);


while(true){

try {

socket = serverSocket.accept();

inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

while(true) {

String msgFromClient = bufferedReader.readLine();

System.out.println("Client: " + msgFromClient);

bufferedWriter.write("MSG Received");
bufferedWriter.newLine();
bufferedWriter.flush();



if (msgFromClient.equalsIgnoreCase("BYE")) {

bufferedWriter.flush();
socket.shutdownOutput();
break;
}
}
76 replies
JCHJava Community | Help. Code. Learn.
Created by ! Slayer on 9/27/2024 in #java-help
I am getting an error when on my workspace in eclipse
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = 'https://maven.minecraftforge.net/' }
maven {
name = 'MinecraftForge'
url = 'https://maven.minecraftforge.net/'
}
}
}

plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
}
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = 'https://maven.minecraftforge.net/' }
maven {
name = 'MinecraftForge'
url = 'https://maven.minecraftforge.net/'
}
}
}

plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
}
221 replies
JCHJava Community | Help. Code. Learn.
Created by ! Slayer on 9/27/2024 in #java-help
I am getting an error when on my workspace in eclipse
Modify Player Capabilities: Create methods to handle the player’s ability to walk on water and climb walls. @SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
// Check if the ability is active and modify player's behavior
if (isAbilityActive) {
if (event.player.isInWater()) {
event.player.setMotion(event.player.getMotion().x, 0, event.player.getMotion().z); // Prevent sinking
}
// Add wall climbing logic here
}
}
}
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
// Check if the ability is active and modify player's behavior
if (isAbilityActive) {
if (event.player.isInWater()) {
event.player.setMotion(event.player.getMotion().x, 0, event.player.getMotion().z); // Prevent sinking
}
// Add wall climbing logic here
}
}
}
221 replies
JCHJava Community | Help. Code. Learn.
Created by ! Slayer on 9/27/2024 in #java-help
I am getting an error when on my workspace in eclipse
Register a Key Binding: Use the ClientRegistry to create a key binding for the "K" key. import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.event.InputEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraft.client.options.KeyBinding; @EventBusSubscriber(modid = "walkonwater", value = Dist.CLIENT)
public class KeyInputHandler {
public static KeyBinding toggleAbility;

public static void registerKeyBindings() {
toggleAbility = new KeyBinding("Toggle Walk on Water", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_K, "key.categories.misc");
ClientRegistry.registerKeyBinding(toggleAbility);
}

@SubscribeEvent
public static void onKeyInput(InputEvent.KeyInputEvent event) {
if (toggleAbility.isPressed()) {
// Toggle walk on water and wall climbing ability
}
}
}
public class KeyInputHandler {
public static KeyBinding toggleAbility;

public static void registerKeyBindings() {
toggleAbility = new KeyBinding("Toggle Walk on Water", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_K, "key.categories.misc");
ClientRegistry.registerKeyBinding(toggleAbility);
}

@SubscribeEvent
public static void onKeyInput(InputEvent.KeyInputEvent event) {
if (toggleAbility.isPressed()) {
// Toggle walk on water and wall climbing ability
}
}
}
221 replies