|
Post by Admin on Jun 29, 2015 12:36:28 GMT
Write your own Java programs and applications.
|
|
|
Post by handsomepredator on Jun 29, 2015 12:40:15 GMT
alpha sort [java]
alpha sort [java] Authors Comments: sunjesters alpha sort
Quote public static void alphaSort(Video[] array,int length) {
Video temp;
int i=0,j=0;
for(i=0;i<length;i++) {
for(j=0;j<i;j++) {
if(array.getVideoTitle().compareTo(array[j].getVideoTitle()) < 0) {
temp = array;//swap
array = array[j];
array[j] = temp;
}
}
}
System.out.println("Video TitlettStock Number|");
System.out.println("------------------------------------'");
for(int count=0;count<array.length;count++) {
System.out.print(array[count].getVideoTitle());
System.out.print("ttt" + array[count].getVideoStockNum());
System.out.println();//make a new line
}
System.out.println("------------------------------------.");
};//end alphaSsort
|
|
|
Post by handsomepredator on Jun 29, 2015 12:40:32 GMT
Binary Search [java]
Binary Search [java] Authors Comments: Binary Search algorithm where the data is already defined
Quote public class BinarySearch {
public static void main(String args[ ]) {
int []number= new int [10];
int NumberOfItems=10;
String output="";
number[0]=1;number[1]=2;number[2]=3; // populate array
number[3]=4;number[4]=5;number[5]=4;
number[6]=7;number[7]=8;number[8]=9;
number[9]=10;
int low=1;
int high=number.length;
//do processing
boolean found = false;
int input= Keyboard.getIntInRange(1,10,"enter number from 1-10" );
while(found==false){
int middle= getMiddle(low, high);
if (number[middle]==input){
found =true;
output="Found at Position " + middle;
}//end if
else{
if(number[middle]<input){
low=middle+1;
}//end if
else{
high=middle-1;
{
}//end else
}// end if
}//end for
}//end while
Screen.display(output,"Binary Search");
System.exit(0);
} // end of main
public static int getMiddle(int low, int high){
int middle = (low+high)/2;
return middle;
}//end getMiddle
} // end of class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:40:57 GMT
Bouncing Ball Application
Bouncing Ball Application Authors Comments: Java sucks.
Quote import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Ball implements Runnable {
public double _diameter=10, _xDir, _yDir, _x, _y;
public final BallContainer _ballContainer;
public final Color _color;
public boolean alive = true;
public Ball(int _x, int _y, int _xDir, int _yDir, int _diameter, BallContainer _ballContainer) {
this._x = _x;
this._y = _y;
this._xDir = _xDir;
this._yDir = _yDir;
this._diameter = _diameter;
this._ballContainer = _ballContainer;
Random _gen = new Random();
_color = new Color(_gen.nextInt(128)+128, _gen.nextInt(128)+128, _gen.nextInt(128)+128, _gen.nextInt(128)+128);
new Thread(this).start();
}
public void act() {
_x += _xDir;
_y += _yDir;
if((_x <= 0 && _xDir < 0) || (_x + _diameter >= _ballContainer.getWidth() && _xDir > 0)) {
_xDir = _xDir / -2; //turn around and cut your speed in half
}
if((_y <= 0 && _yDir < 0) || (_y + _diameter >= _ballContainer.getHeight() && _yDir > 0)) {
_yDir = _yDir / -2;
}
}
public void run() {
while(alive) {
try {
act();
Thread.sleep(50);
} catch(InterruptedException exc) {}
}
}
public void paint(Graphics g) {
g.setColor(_color);
g.fillOval((int)_x, (int)_y, (int)_diameter, (int)_diameter);
}
}
// end Ball.java
// ------------------
/**
* BallContainer.java
*/
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
public class BallContainer extends javax.swing.JPanel {
public final ArrayList<Ball> _balls = new ArrayList<Ball>();
private ImageIcon bg;
public synchronized void add(Ball _ball) {
_balls.add(_ball);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(bg != null) {
g.drawImage(bg.getImage(), 0, 0, bg.getImageObserver());
}
for(Ball _ball : _balls) {
_ball.paint(g);
}
Thread.yield();
repaint();
}
/** Creates new form BallContainer */
public BallContainer() {
initComponents();
bg = new ImageIcon(getClass().getResource("/resources/pikachu.jpg"));
}
public synchronized void clear() {
for(Ball _ball : _balls) {
_ball.alive = false;
}
_balls.clear();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
setBackground(new java.awt.Color(255, 255, 255));
setBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 51, 51), 2, true));
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 396, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 296, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
if (evt.getButton()== evt.BUTTON1){
Random _gen = new Random();
add(new Ball(evt.getX(), evt.getY(), _gen.nextInt(10)+1, _gen.nextInt(10)+1,_gen.nextInt(30)+5, this));
} else {
clear();
}
}//GEN-LAST:event_formMouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
// end BallContainer.java
// ------------------
/**
* BallBouncer.java
*/
import javax.swing.*;
public class BallBouncer extends JFrame {
public BallBouncer() {
super("Ball Bounce Program");
BallContainer _ballContainer = new BallContainer();
add(_ballContainer);
pack();
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Left click to add a ballnRight click to clear the screen", "About", JOptionPane.INFORMATION_MESSAGE);
Thread _thread = new Thread(new Runnable() {
public void run() {
new BallBouncer().setVisible(true);
}
});
_thread.start();
}
}
// end BallBouncer.java
|
|
|
Post by handsomepredator on Jun 29, 2015 12:41:09 GMT
Bubble Sort [java]
Bubble Sort [java] Authors Comments: A bubble sort
Quote public class BubbleSort {
public static void main(String args[ ]) {
int NumberOfItems=10;
int [] number= new int [NumberOfItems];
String output="";
// populate array
number[0]=7;number[1]=4;number[2]=1;
number[3]=2;number[4]=5;number[5]=8;
number[6]=3;number[7]=6;number[8]=10;
number[9]=9;
//do processing
for (int outer=9; outer>=0; outer--){
for (int inner=0; inner<9; inner++){
Compare (number,inner);
} // end for inner
} // end for outer
for (int i = 0; i<10 ; i++){
output += number + "n";
}//end for
Screen.display(output,"Sort");
System.exit(0);
} // end of main
public static void Compare (int [] number,int inner){
if (number[inner]>number[inner+1]){
int temp=number[inner];
number[inner]=number[inner+1];
number[inner+1]=temp;
} // end if
} // end Compare
} // end of class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:41:20 GMT
BufferedReader [java]
BufferedReader [java] BufferedReader
Quote import java.io.*;
public class BufferedReaderDemo {
public static void main (String[] args) throws IOException {
BufferedReader bufferedReader =
new BufferedReader(
new InputStreamReader(system.in));
System.out.print("pls input a line of words, including space: ");
String text = bufferedReader.redLine();
System.out.println("your input words: +text);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:41:29 GMT
Convert a hostname to the equivalent IP address [java]
Convert a hostname to the equivalent IP address [java] Authors Comments: there is a mozilla addon that gives directly the ip of the website but i am giving an alternative in java code.
Quote import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIP {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getByName("enter the website's url here");
} catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() + "="
+ address.getHostAddress());
System.exit(0);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:47:50 GMT
Coversion using case [java]
Coversion using case [java] Authors Comments: simple converting show case use.
Quote import java.util.*;
public class convert {
public static void main (String[] args)
{
int select;
Integer num = new Integer(0);
Scanner scan = new Scanner (System.in);
System.out.println("Select Program or enter quit to quit");
System.out.println("1. Number to hex");
System.out.println("2. Number to binary");
System.out.println("3. Number to octal");
select = scan.nextInt();
System.out.println("Enter number to convert");
num = scan.nextInt();
switch(select)
{
case 1:
System.out.println("number in hex: "+Integer.toHexString(num));
break;
case 2:
System.out.println("number in binary: "+Integer.toBinaryString(num));
break;
case 3:
System.out.println("number in octal: "+Integer.toOctalString(num));
break;
default: //if other than 1 2 3
System.out.println("congrats you cant enter numbers right");
}
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:47:53 GMT
Decimal-Binary Converter (Simple) [java]
Decimal/Binary Converter (Simple) [java] Authors Comments: This is a small snippet of code I wrote from scratch (as I do all my code). It takes keyboard input, and stores it as an Integer, where a small equation converts the decimal number into a "base 2" number (Binary). I'm using a similar code snippet for a much larger program I'm working on.
Quote import java.util.Scanner;
public class Binary
{
public static void main(String[] args)
{
int binaryOutput;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
binaryOutput = keyboard.nextInt();
System.out.println( Integer.toString( binaryOutput, 2 ));
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:48:12 GMT
EnumDemo-java
EnumDemo.java
Quote public class EnumDemo1 {
private enum InnerAction {TURN_LEFT, TURN_RIGHT, SHOOT};
public static vid main(String[] args) {
doAction(InnerAction.TURN_RIGHT);
]
public static void doAction(InnerAction action)[
switch(action) {
case TURN_LEFT:
system.out.println("turn left");
break;
case TURN_RIGHT:
System.out.println("TURN RIGHT");
break;
case SHOOT:
System.out.println("shoot");
break;
}
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:48:27 GMT
File Editor [java]
File Editor [java] Authors Comments: This program lets the user edit short text files in a window. A "File" menu provides the following commands:
New -- Clears all text from the window. Open -- Let's the user select a file and loads up to 100 lines of text form that file into the window. The previous contents of the window are lost. Save -- Let's the user specify an ouput file and saves the contents of the window in that file. Quit -- Closes the window and ends the program.
This class uses the non-standard classes TextReader and MessageDialog.
Quote
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class TrivialEdit extends Frame implements ActionListener {
public static void main(String[] args) {
// The main program just opens a window belonging to this
// TrivialEdit class. Then the window takes care of itself
// until the program is ended with the Quit command.
new TrivialEdit();
}
private TextArea text; // Holds that text that is displayed in the window.
public TrivialEdit() {
// Add a menu bar and a TextArea to the window, and show it
// on the screen. The first line of this routine calls the
// constructor from the superclass to specify a title for the
// window. The pack() command sets the size of the window to
// be just large enough to hold its contents.
super("A Trivial Editor");
setMenuBar(makeMenus());
text = new TextArea(20,60);
add(text, BorderLayout.CENTER);
pack();
show();
}
private MenuBar makeMenus() {
// Create and return a menu bar containing a single menu, the
// File menu. This menu contains four commands, and each
// command has a keyboard equivalent. The Frame will listen
// for action events from the menu.
Menu fileMenu = new Menu("File");
fileMenu.addActionListener(this);
fileMenu.add( new MenuItem("New", new MenuShortcut(KeyEvent.VK_N)) );
fileMenu.add( new MenuItem("Open...", new MenuShortcut(KeyEvent.VK_O)) );
fileMenu.add( new MenuItem("Save...", new MenuShortcut(KeyEvent.VK_S)) );
fileMenu.add( new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q)) );
MenuBar bar = new MenuBar();
bar.add(fileMenu);
return bar;
}
public void actionPerformed(ActionEvent evt) {
// This will be called when the user makes a selection from
// the File menu. This routine just checks which command was
// selected and calls another routine to carry out the command.
String cmd = evt.getActionCommand();
if (cmd.equals("New"))
doNew();
else if (cmd.equals("Open..."))
doOpen();
else if (cmd.equals("Save..."))
doSave();
else if (cmd.equals("Quit"))
doQuit();
}
private void doNew() {
// Carry out the "New" command from the File menu by
// by clearing all the text from the TextArea.
text.setText("");
}
private void doSave() {
// Carry out the Save command by letting the user specify
// an output file and writing the text from the TextArea
// to that file.
FileDialog fd; // A file dialog that let's the user specify the file.
String fileName; // Name of file specified by the user.
String directory; // Directory that contains the file.
fd = new FileDialog(this,"Save Text to File",FileDialog.SAVE);
fd.show();
fileName = fd.getFile();
if (fileName == null) {
// The fileName is null if the user has canceled the file
// dialog. In this case, there is nothing to do, so quit.
return;
}
directory = fd.getDirectory();
try {
// Create a PrintStream for writing to the specified
// file and write the text from the window to that stream.
File file = new File(directory, fileName);
PrintWriter out = new PrintWriter(new FileWriter(file));
String contents = text.getText();
out.print(contents);
out.close();
}
catch (IOException e) {
// Some error has occured while opening or closing the file.
// Show an error message.
new MessageDialog(this, "Error: " + e.toString());
}
}
private void doOpen() {
// Carry out the Open command by letting the user specify
// the file to be opened and reading up to 100 lines from
// that file. The text from the file replaces the text
// in the TextArea.
FileDialog fd; // A file dialog that let's the user specify the file.
String fileName; // Name of file specified by the user.
String directory; // Directory that contains the file.
fd = new FileDialog(this,"Load File",FileDialog.LOAD);
fd.show();
fileName = fd.getFile();
if (fileName == null) {
// The fileName is null if the user has canceled the file
// dialog. In this case, there is nothing to do, so quit.
return;
}
directory = fd.getDirectory();
try {
// Read lines from the file until end-of-file is detected,
// or until 100 lines have been read. The lines are appended
// the the TextArea, with a line feed after each line.
File file = new File(directory, fileName);
TextReader in = new TextReader(new FileReader(file));
String line;
text.setText("");
int lineCt = 0;
while (lineCt < 100 && in.peek() != '') {
line = in.getln();
text.appendText(line + 'n');
lineCt++;
}
if (in.eof() == false)
text.appendText("nn********** Text truncated to 100 lines! ***********n");
in.close();
}
catch (Exception e) {
// Some error has occured while opening or closing the file.
// Show an error message.
new MessageDialog(this, "Error: " + e.toString());
}
}
private void doQuit() {
// Carry out the Quit command by exiting the program.
System.exit(0);
}
} // end class TrivialEdit
|
|
|
Post by handsomepredator on Jun 29, 2015 12:48:44 GMT
IRC Bot in Java
IRC Bot in Java
Quote import java.net.*;
import java.util.Random;
import java.io.*;
public class BotBase implements Runnable {
/**
* The IRC Server to connect to
*/
String ircServer = "irc.tdirc.net";
/**
* The channel the bot connects too
*/
String ircChan = "#darkmindz";
/**
* The port of the IRC Server
*/
Integer ircPort = 6667;
/**
* The key of the irc channel.
*/
String ircChanKey = "1001";
/**
* The name of the IRC bot, only change [DMZ Bot] unless you know what
* you're doing.
*/
String ircBotName = "[DMZ Bot]: " + new Random().nextInt(10000) + 1;
public void run() {
Socket botSocket = null;
try {
botSocket = new Socket(ircServer, ircPort);
writeServer("USER " + ircBotName + " 8 * " + ircBotName, botSocket);
writeServer("NICK " + ircBotName, botSocket);
writeServer("JOIN " + ircChan + " " + ircChanKey, botSocket);
ircMessage("Hi, bot wrote by PeeHPee, kthkz.", botSocket);
} catch (Exception e) {
// We want to be silent, disable error messages.
}
}
public static void main(String[] args) {
Thread t = new Thread("BotBase");
t.start();
}
/**
* Writes something to a socket
*
* @param contents
* The contents of the message.
* @param botSocket
* The socket we're writing too
*/
public void writeServer(String contents, Socket botSocket) {
try {
BufferedWriter outStream = new BufferedWriter(
new OutputStreamWriter(botSocket.getOutputStream()));
outStream.write(contents + "n");
outStream.flush();
} catch (Exception e) {
}
}
/**
* Sends a message to the IRC Server
*
* @param text
* The text to send.
* @param botSocket
* The socket that we're writing too.
*/
public void ircMessage(String text, Socket botSocket) {
writeServer("PRIVMSG " + this.ircChan + " :" + text, botSocket);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:49:02 GMT
Java Higher lower [java]
Java Higher lower [java] Authors Comments: More homework stuff. Theres probably better way of validating user input in java but since my class isnt there yet i cant use it so i came up with my own way. Good example of loops out the wazo.
Quote package hilow;
import java.util.*;
public class hilow {
//Program plays higher lower with numbers 1-100
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random generator = new Random();
int user=1,comp,guesses=0, repeat=1;
String users;
char valid1,valid2,valid3;
//loop only executed first time and when game is won (Matched with first while)
do{
comp = generator.nextInt(100) + 1;
//loop only executed first time and while person is still guessing (Matched with second //while)
do{
//User input
System.out.println("Enter your guess 1-100 to end the game enter a negitive value");
System.out.println("All number beyond 3 digits will be trunacted to first 3 digits");
users = scan.nextLine();
//Checks to make sure the number doesnt start with anything other than a number
//
do{
valid1 = users.charAt(0);
if(valid1 > 57 || valid1 < 49)
{
System.out.println("No letters enter new number 1-100");
users = scan.nextLine();
}
}
while(valid1 > 57 || valid1 < 49);
int numLength = users.length();
//chop off digits beyond 3rd
if(numLength > 3)
{
users = users.substring(0,2);
}
//validate second digit if existant only runs when number is 2 digits
if(numLength == 2)
{
do{
valid2 = users.charAt(1);
if(valid2 > 57 || valid2 < 49)
{
System.out.println("No letters enter new number");
users = scan.nextLine();
}
}
while(valid2 > 57 || valid2 < 49);
}
if(numLength == 3)
{
do{
valid3 = users.charAt(2);
if(valid3 > 57 || valid3 < 48)
{
System.out.println("Letters only enter new number");
users = scan.nextLine();
}
}
while(valid3 > 57 || valid3 < 48);
}
//Takes value in users and turns it into a integer
user = Integer.parseInt(users);
//users number is greater than 100 so chose to quit
if(user > 100 || user < 1)
{
System.out.println("You choose to quit");
System.out.println("Press 1 if you wish to start again");
repeat = scan.nextInt();
//makes sure new sting is read in before validation
users = scan.nextLine();
}
//Checks If user guesses correctly and then ask if they want to play again
if(user == comp)
{
System.out.println("You win the number was " + comp);
guesses++;
System.out.println("It took you "+ guesses + " guesses");
System.out.println("Press 1 to play again");
repeat = scan.nextInt();
}
//users number is less than computer but still in acceptable range
if(user < comp && user > 0)
{
System.out.println("Your number is low");
guesses++;
}
//user greater than computers number but still inside acceptable range 1-100
if(user > comp && user < 101)
{
System.out.println("Your guess is high");
guesses++;
}
}
while(repeat == 1);
}
while(user != comp && user < 101);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:49:18 GMT
java-scanner
java-scanner Authors Comments: this is a scanner -Scanner -Java
Quote public class Scanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("pls put in you name: ");
System.out.printf("Hello! %s!n", scanner.next());
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:49:31 GMT
Keyboard Class [java]
Keyboard Class [java] Authors Comments: This is so that you can use JOption pane use the whole JOptionpane.showMessageDialog etc etc etc instead you can use. Screen.Display(); Just a personal preference if you swing that way
Quote It includes a method for. getting a string getting an int getting an int in a range getting a real
P.S The code doesnt do anything on its own. It should complie only. Thats the whole point // Keyboard Class
import javax.swing.JOptionPane;
public class Keyboard {
public static String getText(String intro) {
String textIn = JOptionPane.showInputDialog(intro);
return textIn;
}// end of getText()
public static int getInt(String intro) {
int intIn=0;
boolean ok=false;
do {
String intAsString = JOptionPane.showInputDialog(intro);
try {
intIn = Integer.parseInt(intAsString);
ok = true;
}// end of try
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "That was not an Integer!n");
}// end of catch
} while(!ok); // not an Integer
return intIn;
}// end of getInt()
public static int getIntInRange(int low, int high, String intro) {
int number = Keyboard.getInt(intro);
while ((number < low) || (number > high)) {
Screen.display("Value not in range " + low + " to " + high, "Ooopsie");
number = Keyboard.getInt(intro);
} // end while
return number;
} // end of method getIntInRange
public static double getReal(String intro) {
double realIn=0;
boolean ok = false;
do {
String realAsString = JOptionPane.showInputDialog(intro);
try {
realIn = Double.parseDouble(realAsString);
ok = true;
}// end of try
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "That is not a real number!n");
}// end of catch
} while (!ok); // not an Real
return realIn;
}// end of getReal()
public static boolean getOption(String intro) {
boolean option=false;
String input="";
while (!(input.equals("yes") || input.equals("no"))) {
input=getText(intro);
}
if (input.equals("yes")) {
option=true;
}
return option;
}// end of getOption()
} // end class
Logg
|
|
|
Post by handsomepredator on Jun 29, 2015 12:49:49 GMT
Loops 1 [java]
Loops 1 [java] Authors Comments: simple. does it need to be explained?
Quote int i = 0;
for ( i=1; i<101; i++ ) {
System.out.println('example');
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:49:54 GMT
Math functions Quadratic Equation
[java] Math functions Quadratic Equation Authors Comments: Solving quadratic equations.
Quote import java.util.Scanner;
public class Quad
{
public static void main (String[] args)
{
int a, b, c; //ax^2 + bx + c
double discriminant, root1, root2;
Scanner read = new Scanner (System.in);
System.out.print ("Enter coefficient of x squared: ");
a = scan.nextInt();
System.out.print("Enter the coefficient of x: ");
b = scan.nextInt();
System.out.print("Enter the Constant: ");
c = scan.nextInt();
discriminant = Math.pow(b, 2) - (4 * a * c); //Math.pow power
root1= ((-1 * cool.gif + Math.sqrt(discriminant)) / (2 * a);
root2= ((-1 * cool.gif - Math.sqrt(discriminant)) / (2 * a); //Math.sqrt Squareroot
System.out.print("Root 1 is: " + root1);
System.out.print("Root 2 is: " + root2);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:50:13 GMT
Screen Class [java]
Screen Class [java] Authors Comments: Simple little class which you can use to display pop up boxes by simplying typing Screen.display(string,string)
Quote import javax.swing.JOptionPane;
public class Screen {
public static void display(String output, String heading) {
JOptionPane.showMessageDialog(null, output, heading, JOptionPane.INFORMATION_MESSAGE);
} // end of display()
} // end class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:50:17 GMT
Selection Sort [java]
Selection Sort [java] Authors Comments: Selection Sort in java with a defined array of numbers
Quote public class SelectionSort {
public static void main(String args[ ]) {
int NumberOfItems=10;
int [] number= new int [NumberOfItems];
int [] empty= new int [NumberOfItems];
String output="";
// populate array
number[0]=7;number[1]=4;number[2]=1;
number[3]=2;number[4]=5;number[5]=8;
number[6]=3;number[7]=6;number[8]=10;
number[9]=9;
//do processing
for (int outer=0; outer<NumberOfItems; outer++){
int min=outer;
for (int inner=0; inner<NumberOfItems; inner++){
if (number[inner]<=number[min]){
min=inner;
}//end if
} // end for inner
empty[outer]=number[min];
number[min]=99;
} // end for outer
for (int i = 0; i<10 ; i++){
output += empty + "n";
}//end for
Screen.display(output,"Sort");
System.exit(0);
} // end of main
} // end of class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:50:39 GMT
Selection sort for strings [java]
Selection sort for strings [java] Authors Comments: Selection sort for strings
Quote public class SelectionSortString {
public static void main(String args[ ]) {
int NumberOfItems=10;
String [] name= new String [NumberOfItems];
String [] empty= new String [NumberOfItems];
String output="";
// populate array
name[0]="dog";name[1]="cat";name[2]="bat";
name[3]="saw";name[4]="hay";name[5]="day";
name[6]="low";name[7]="sew";name[8]="fan";
name[9]="row";
//do processing
for (int outer=0; outer<NumberOfItems; outer++){
int min=outer;
for (int inner=0; inner<NumberOfItems; inner++){
if ((name[inner]).compareTo(name[min])<0){
min=inner;
}//end if
} // end for inner
empty[outer]=name[min];
name[min]="££";
} // end for outer
for (int i = 0; i<10 ; i++){
output += empty + "n";
}//end for
Screen.display(output,"Sort");
System.exit(0);
} // end of main
} // end of class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:50:56 GMT
Simple read-write [java]
Simple read/write [java] Authors Comments: A little program which reads in the content of a .txt file and outputs the content to another .txt file but all in upper case.
It is very simple and just gives you an idea of one way to do read/write in java.
** This requires The Keyboard Class ( this is just a class with some JOption pane methods. it just makes life easier for me)**
Quote
import java.io.*;
class Ftest{
public static void main(String [] args){
String [] infile = new String [1];
infile[0] = Keyboard.getText("Choose file to read in")+".txt";
BufferedReader LINE;
PrintWriter OUT;
String PATH;
String PATHOUT = Keyboard.getText("Choose File name to write to")+".txt";; //set file name to write to
String data;
String output ="";
if (infile.length != 1){
System.out.println("Parameter is one filename"); // error report
return;
}// end if
PATH = infile[0];
try {//i/o may go wrong
LINE = new BufferedReader(new FileReader(PATH)); //input file handle
OUT = new PrintWriter(new FileWriter(PATHOUT)); //output file handle
while((data = LINE.readLine()) != null){ //read lines
data = data.toUpperCase();// change to uppr case
OUT.println(data);//write data to file
}// end while
LINE.close();
OUT.close();
Screen.display("Files have been writen and changed to upper case","Yay!");
}// end try
catch(IOException e){System.out.println("ACK!");} // error report
}// end main
}// end class
|
|
|
Post by handsomepredator on Jun 29, 2015 12:51:16 GMT
Simple Rock Paper Scissors [java]
Simple Rock Paper Scissors [java] Authors Comments: Had to do this for homework figured id share with yall. Simple.
Quote import java.util.*;
public class HW06C {
public static void main(String[] args) {
Random generator = new Random();
Scanner scan = new Scanner(System.in);
int user=1,comp=1;
int wins=0,loses=0;
System.out.println("This will play a game of Rock Paper Scissors with the computer");
System.out.println("Enter 0 to end the game");
do
{
System.out.println("Enter the number Corrisponding to your choice");
System.out.println("1.Rockn2.Papern3.Scissor");
user = scan.nextInt();
//generates number 1-3
comp = generator.nextInt(3)+1;
switch (user)
{
//case 1 user picks rock
case 1:
{
if(comp == 2)
{
System.out.println("User:RocknComputer:PapernComputer wins");
loses++;
}
if(comp == 1)
{
System.out.println("User:RocknComputer:RocknTie");
}
if(comp == 3)
{
System.out.println("User:RocknComputer:ScissorsnPlayer wins");
wins++;
}
break;
}
//user chooses paper
case 2:
{
if(comp == 3)
{
System.out.println("User:PapernComputer:ScissorsnComputer wins");
loses++;
}
if(comp == 2)
{
System.out.println("User:PapernComputer:PapernTie");
}
if(comp == 1)
{
System.out.println("User:PapernComputer:RocknUsers wins");
wins++;
}
break;
}
//user chooses Scissor
case 3:
{
if(comp == 1)
{
System.out.println("User:ScissornComputer:RocknComputer wins");
loses++;
}
if(comp == 3)
{
System.out.println("User:ScissornComputer:PapernTie");
}
if(comp == 2)
{
System.out.println("User:ScissornComputer:PapernUser wins");
wins++;
}
break;
}
}
System.out.println("Wins:"+wins+"nloses:"+loses);
}
while(user !=0);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:51:21 GMT
StreamdDemojava [java]
StreamdDemo.java [java] Authors Comments: StreamDemo.java
this is just for begginers
Quote import java.io.*;
public class StremDemo {
public static void main(String[] args) {
try {
System.out.print("input: ");
System.out.println("input matrix system: " +
System.in.red());
}
catch(IOException e) {
e.printStackTrace();
}
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:51:43 GMT
variables demo
variables demo [java] variables demo
Quote public class VariableDemo {
public static void main(String[] args) {
int ageOFRazak = 13;
double ageOFRazak = 26;
char levelOFRazak = 'A+';
System.out.println("AGEt Double AGEt Character Levet");
System.out.printf("%4dt %4.lft %4c",
Age Of Razak, Double age OF Razak, Character Level Of Razak);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:51:45 GMT
Very Basic Java
Very Basic Java Authors Comments: Alright, this is the basic of the basics of Java. I decided to write one because theirs nothing in Java.
You can use whatever Java compiler you want. I use crimson editor.
Quote public class test <--- naming your stuff
{
public static void main (String[] args) <-- your going to need this in every application
{
System.out.println("Hello World"); <-- println will display hello world
}
}
public class test
{
public static void main (String[] args)
{
String hello = "Hello World"; <-- This is where I'm signing Hello world to a String called hello
System.out.println(hello);
}
}
|
|
|
Post by handsomepredator on Jun 29, 2015 12:52:06 GMT
Yellow Submarine Song [java] funny
Yellow Submarine Song [java] funny Authors Comments: This is a program which uses jfugue in java to recreate a part of the famos song
Yellow Submarine by The Beatles.
In order to play it you need to jfugue pacakage and remember as with all java programs save the coe as the public class name ie in this case YellowSubmarine
Quote import org.jfugue.*;
public class YellowSubmarine {
public static void main(String[] args) {
Player yellowsub = new Player();// Set up player
// "Yellow Submarine" part
Pattern pattern1 = new Pattern(" I[0] B5i. C6s D6hi+Dmaj7hi. B5s A5i. B5s G5h.+Gmajh ");
Pattern pattern2 = new Pattern(" B5i. B5s A5i+Amini. G5s E5qi. B5i. B5s A5h.+Aminh B5i. C6s");
// "intro" part
Pattern pattern3 = new Pattern("D6hi+Dmaj7hi. B5s A5i. B5s G5h.+Gmajh B5i. B5s A5i+Amini. G5s E5qi. B5i. B5s A5w+Dmaj7w");
// "we all live in ...a" part
Pattern pattern4 = new Pattern(" D5q+Gmajq D5q D5q D5i. E5s B5i+Dmaj7i. A5s A5i. A5s A5h A5i+Dmaj7i. A5s A5i. A5s A5h G5i+Gmaji. G5s G5i. G5s G5h");
Pattern pattern5 = new Pattern(" D5q+Gmajq D5q D5q D5i. E5s A5i+Dmaj7i. A5s A5i. A5s A5h A5i+Dmaj7i. A5s A5i. A5s A5h G5i+Gmaji. G5s G5i. G5s G5h");
//All together now
Pattern song= new Pattern();
song.add(pattern1);
song.add(pattern2);
song.add(pattern3);
song.add(pattern4);
song.add(pattern5);
yellowsub.play(song);// Play!
System.exit(0);
}// end main
}// end class
|
|
|
Post by vasiliy on Sept 16, 2019 19:50:54 GMT
Blockchain technology has a variety of applications beyond just cryptocurrency; it can be used in healthcare, supply chain management, and even cloud storage. Learn more about blockchain on blockchain cloud.
|
|
|
Post by John on Nov 25, 2019 22:49:06 GMT
Check out the official Binance cryptocurrency wallet that supports your favorite Ethereum blockchains and more. Download the best crypto wallet app on plark.io
|
|