[Pulsating Spirograph (Processing)](https://www.ktbyte.com/projects/project/93961/pulsating-spirograph?ref=20181212daniweb2)

Member Avatar
Member Avatar
-1 forum 1

[[Depths (Made with Processing)](https://www.ktbyte.com/projects/project/94051/depths?ref=20181210daniweb)](null)

Member Avatar
+1 forum 0

Here's an interesting little scenario. You get challenged to a game of coin toss. Both players pick a sequence of coin toss results (heads or tails). The coin is repeatedly tossed until the sequence for one of the players occurs. The loser pays the winner a dollar. You'd assume that you each have an equal chance of winning. However, using two little "tricks" you can tip the odds in your favour. The first trick is to always choose second. If you can manage this then your strategy is as follows: Your first choice should be the opposite of your opponent's …

Member Avatar
Member Avatar
+0 forum 36

This is a little discussion/example of design for modularity and re-use, inspired by javaAddict and Orlando Augusto posts in their thread “Dynamic Multidimensional Array Printing” https://www.daniweb.com/programming/software-development/code/305580/dynamic-multidimensional-array-printing That thread has 3 solutions to printing the contents of a multi-dimensional array of arbitrary dimensions - although in fact all the solutions will work for any structure of arrays and scalars within arrays, regardless of shape or size. But really there are two things going on here 1. recursively traverse all the elements within the arbitrarily nested arrays or scalars 2. print the contents with nice layout/indentation. I think it’s worth the extra …

Member Avatar
Member Avatar
+1 forum 5

Hi All. This is my first code snippet. I don't know how practical it is, but I came up with the idea, so I wanted to do it. Assuming you have an array. You use a for loop to display its data. For 2D arrays you use 2 for loops. But that is not dynamic. Every time an extra dimension is added, you need to write an extra loop. How about a method that takes as argument an Object, that is actually an array of unknown dimensions (NxD). By using recursion the program will "generate" dynamically the right amount of …

Member Avatar
Member Avatar
+0 forum 6

This class is intended as a small and simple alternative to java.util.Scanner for people in the early stages of learning Java. Unlike Scanner it has built-in error handling and retry, it throws no checked exceptions, is as tolerant as possible of variations in the format of user input, and avoids “gotchas” like Scanner’s nextInt/nextLine. Using it is as simple as UserInput ui = new UserInput(); String name = iu.getString(“Enter name:”); int age = ui.getInt(“Enter age:”); double salary = ui.getDouble(“Enter salary:”); The `isYes(String prompt)` method returns a boolean suitable for yes/no questions, e.g. do { … while (ui.isYes(“enter more data?”)); An …

Member Avatar
Member Avatar
+3 forum 1

i have to write a code that has a random number generated between 1-100 and the user has the choice to keep the number, discard the number and quit the program. and when the user quits the program displays the numbers kept and discarded, but i dont know how to use the character input for the user to chose. this is what i have `Inline Code Example import java.util.*; public class Lab1A{ public static void main(String [] args){ int input=0; int K = 0; int D = 0; char Q = 0; for (int i=0; i<100; ){ double random = …

Member Avatar
Member Avatar
+0 forum 1

Hi everyone this is my first code snippet so be gentle.. I have written a basic client/server chat program that uses multithreading for listening and sending. I welcome any improvements or discussion... Thanks James

Member Avatar
Member Avatar
+2 forum 1

The code simply captures your working window as jpeg image and stores it in the same directory as where your java class after compiling by javac is being stored

Member Avatar
Member Avatar
+0 forum 12
Code Snippet Non-static method

here where am doing mistake and program is showing error non static method cannot be refferenced to static context

Member Avatar
Member Avatar
+0 forum 2

Similarly, you can add more fields to an enum and simply provide accessor methods to retrieve them. For example you could change it to have an extra field indicating an output directory to write specific types of data to, and a getter method to return that directory.

Member Avatar
Member Avatar
+0 forum 2
Code Snippet Homework: Pyramid

**This is intented for learning purposes, feel free to study the code** Create a drawing that looks like this (pyramid) The number should be incremented with the modulus.

Member Avatar
Member Avatar
-1 forum 7

Hey everybody, Lately I have written the game Hangman in many different languages (C, JavaScript, Java and PHP so far). Here is the code snippet for Java! It uses a words file, on the bottom of this post you will see a small example of a few words. The source code for other languages can be found here on DaniWeb and on my new website: [URL="http://www.hangman.symbolwebdesign.nl/"]http://www.hangman.symbolwebdesign.nl/[/URL]. Due to the fact it is not possible (in web apllications) to open a words file using Java, it is required to directly put the words string separated by a | into the string …

Member Avatar
Member Avatar
+2 forum 16
Code Snippet MineSweeper

I made this minesweeper game earlier this summer. It shows many things, including: array use, 1D array to 2D array, gridlayout, changing the way a JButton works depending on mouse button used, floodfill, loops, GUI. slightly long, 382 lines, can probably be condensed. There is one known bug that i cannot seem to fix: when a floodfill causes a win, and the OK button is clicked on the win dialog, it will automatically click a button on the new game, not a problem usually, but in games that are set to be large with a small amount of bombs, it …

Member Avatar
Member Avatar
+2 forum 49
Code Snippet Basic Web Server

The server only does the basics. TODOs: The header method needs expanding. The file send method can't handle too large files. Maybe write custom buffer class since some buffering is done. Keep cache of recent files.

Member Avatar
Member Avatar
+0 forum 11

I have recently been interested in Java Swing and Game development, so naturally I began creating many different 2D games. During them I found myself having to rewrite much code, but eventually I decided to write some classes that would help me whenever I wanted to make a game. Here is the basics for a *single threaded game loop* with multiple user input (i.e player moves with W, S, A and D and player 2 with UP, LEFT, RIGHT, and DOWN. Player1 can shoot with SPACE and player 2 wuth ENTER) Basically there is an abstract class `GameLoop` which require …

Member Avatar
Member Avatar
+3 forum 10

Hello This is a demo showing Java interacting with Daniweb's new API ( http://www.daniweb.com/api/documentation ) You introduce a member's name and it should show you the total number of posts he has made. Demo is a proof of concept: No bug checking, error checking, etc is in this code. In order to make this work you need the Jersey library. Download here: http://jersey.java.net/nonav/documentation/latest/chapter_deps.html#core_client Any comments, bugs, glitches, etc, please say and Ill look into it.

Member Avatar
Member Avatar
+4 forum 11

I have seen many questions on autocompeletion for *java text components* like `JTextField` `JTextArea` `JTextEditorPane` etc. There are not many options either: 1) 3rd party library (like SwingX) 2) DIY (i.e using `DocumentListener`, `JWindow` with `JLabel` etc and a few requestFocusInWindow calls) I chose number 2 and put the code up here for others to have a working foundation that can be improved to your needs. Basically you would just make sure the `AutoSuggestor` class is within package hierarchy than you would do something like: //create JTextComponent that we want to make as AutoSuggestor //JTextField f = new JTextField(10); JTextArea …

Member Avatar
Member Avatar
+1 forum 2

A temperature converter GUI. Converts from Celsius to Fahrenheit and vise versa. Tests JLabels, JButtons and JTextFields. *Two separate programs, Converter and Thermometer (Thermometer Class)*

Member Avatar
Member Avatar
+0 forum 4

The above code computes the value of a mathematical expression supplied at command line by using the concept of converting infix to postfix,then evaluating the postfix expression. Any comments are appreciated.

Member Avatar
Member Avatar
+0 forum 3

Jython is a version of Python that can use the extensive Java library. Jython uses Python syntax and avoids the rather ugly syntax (at least for Pythonions) of Java. Here we apply it to bring up an image from a file.

Member Avatar
+4 forum 0
Code Snippet Long UUID

I was trying to set a UniqueIdentifier based on Long. The objective was to give a certain custom Object a unique identifier in order to allow me to store them in a hashmap. I saw someone trying to use the most significant bits. So I realized that the addition of least and most significant bits will make it work. Here it is for anyone looking for a long UUID.

Member Avatar
Member Avatar
+1 forum 3

Often we're faced with the need to represent primitive data types as readable/writable bytes that can be sent through a data stream. In Java, there are classes such as DataOutputStream/DataInputStream that can do the work for us, but often a programmer wants to manage the conversions himself (or herself, to be politically correct). So, how does one convert primitive data types to and from [binary] byte arrays? The answer is this: by working with Binary Bitwise Operators. Here are some methods used to convert primitive types, and arrays of primitive types, to and from byte arrays. Order of primitive types …

Member Avatar
Member Avatar
+1 forum 4
Code Snippet Armstrong Numbers

Eg.407 4*4*4=64 0*0*0=0 7*7*7=343 now 64+0+343=407. Any number which satisfies the above condition is called as Armstrong

Member Avatar
Member Avatar
+1 forum 5
Code Snippet Sorting Strings

This class will sort strings in alphabetical order.

Member Avatar
Member Avatar
+0 forum 11

Hello everyone, this is something I wrote a couple months back. It is used as a universal class/object loader from an XML file. For the handling of XML I use the JDOM library. The story basically goes like this: I have a load of classes in a game I'm working on, going from enemies to items to zones, you name it. That data is written in xml files. Instead of writing a different method for every class and every constructor, what these methods do is: you start with for instance loadItems(File aFile), which takes the xml file for Items as …

Member Avatar
Member Avatar
+1 forum 5

Since I keep seeing people asking how to read the Integers from a File, I figured this simple snippet might be useful. What it does is it creates a new Scanner Object based on the File Object "test.txt" then it reads all of the Integers from that file, skipping over anything else that was found in the file, and adding the Integers to an ArrayList. It then sorts the ArrayList and prints out the sorted numbers. The file must exist otherwise this example will crash.

Member Avatar
Member Avatar
+0 forum 6

Simple code to see if array of primitives has duplicates. The aim is to only check if there are any duplicates, but not to check what element is repeated, or its position etc. PS: Do you know some handy snippet? Don't way, post it!

Member Avatar
Member Avatar
+4 forum 5
Code Snippet Infix to Postfix

I have seen several posters asking for help on programs converting an Infix expression to a Postfix one. I have written a program to convert a simple Infix expression to a Postfix one so that people here can be directed towards some sample code from which they get the idea to writing one. I have also provided comments describing what the code part is supposed to do in that block, the idea here is to discourage blind copying and to encourage the understanding of the program. Once the idea behind such a program is undestood, even a beginner can write …

Member Avatar
Member Avatar
+1 forum 4

I have seen many people struggling with Reading files and writing files in java, also a great problem is how to edit a file too. So in this code snippet I have put all these problems into one small program in hopes to help others. So this code snippet will ask for the name of a file, it will then read it in fully, after reading it will ask you to type in the contents of the line you would like to edit, it will then ask you for the replacement text. This will then be edited with new data …

Member Avatar
Member Avatar
+5 forum 13

The End.