Jump to content


Check out our Community Blogs

Turk4n

Member Since 13 May 2008
Offline Last Active Dec 03 2013 10:12 PM
-----

#414269 What this mean? Why ?

Posted by Turk4n on 09 December 2008 - 02:31 PM

I don't know if my explanation would help you...

Here goes my try.

That code stripe is the object we use to "know/understand" what a "box" is...

class Box {
  private double width;//<-- They are not "accessible" by any other class
  private double height;//<-- Same as the first
  private double depth;//<-- Same as the second

 Box(Box ob) { //Called Constructor,Constructor = template(Not really)*
    width = ob.width;//Making the private width accessible to other classes.
    height = ob.height;//Same as first
    depth = ob.depth;//Same as second
  }
Well, to make it even worse, think like this a "dog" has it's features. This
private double width;
  private double height;
  private double depth;

 Box(Box ob) { 
    width = ob.width;
    height = ob.height;
    depth = ob.depth;
part represents it's features...
  • -1


#393015 Black Jack ;)

Posted by Turk4n on 09 October 2008 - 10:11 AM

Fair enough. :) Nice tutorial.


Thanks, again :D
  • 1


#385335 Black Jack ;)

Posted by Turk4n on 24 September 2008 - 01:07 PM

Okay, today I will show what I have been working on for some days on. Had to learn enums and abstract classes(VERY ANNOYING!!!). So I will do my best to explain and of course share my code :)

So let's get going !

Our cardtypes

public enum CardType {

	TWO("2"),THREE("3"),FOUR("4"),FIVE("5"),SIX("6"),
        SEVEN("7"),EIGHT("8"),NINE("9"),TEN("10"),
	JACK("J"),QUEEN("Q"),KING("K"),ACE("A");
	
	public final int valor = ordinal() + 2;
	public final String symbol;
	
	CardType(String sym) {symbol = sym;}
}
Here we started by creating the cards type, lets proceed with creating the symbols for each card.

public enum CardIcon {
	CLOVER('\u2664'), DIAMONDS('\u2666'),
        HEARTS('\u2665'),SPADES('\u2660');
	
	public final char symbol;
	
	CardIcon(char c) {symbol = c;}
}
Well since I didn't know how to implement icons I made in photoshop. It gave me only one alternative and that was to use ASCII codes.
So let's work on the cards now.

public class Card {

	private CardIcon ci;
	private CardType ct;
	
	public Card(CardIcon icon, CardType type) {
		ci = icon;
		ct = type;
	}
	public CardIcon icon() {
		return ci;
	}
	public CardType type() {
		return ct;
	}
	public String toString() {
		return ci.toString() +" "+ ct.toString();
	}
	public String toSymbol() {
		return ci.symbol + ct.symbol;
	}
}
Binding every symbol and number to them. So we can create a deck.
So let's get on our deck

import java.util.*;

public class Deck {
	private List<Card> theDeck = new LinkedList<Card>();

	public void onTop(Card c) {
		theDeck.add(0,c);
	}
	public Card takeOffTop() {
		Card c = theDeck.get(0);
		theDeck.remove(0);
		return c;
	}
	public int numberDeck() {
		return theDeck.size();
	}
	public void throwCard() {
		theDeck.clear();
	}
	public Card watch(int nr) {
		return theDeck.get(nr);
	}
	public Card takeOff(int nr) {
		Card c = theDeck.get(nr);
		theDeck.remove(nr);
		return c;
	}
	public int search(CardIcon ci, CardType ct) {
		int i = 0;
		for(Card c : theDeck)
			if(c.icon()==ci && k.type()==ct)
				return i;
			else
				i++;
		return -1;
	}
	public void newGame() {
		theDeck.clear();
		for(CardIcon ci : CardIcon.values())
			for(CardType ct : CardType.values())
			theDeck.add(new Card(ci,ct));
	}
	public void shuffle() {
		Collections.shuffle(theDeck);
	}
}
Here are we linking every card and value to each other. So we can remove, select another card or look on. Last code part is to always shuffle the decks for each player(you against the computer).

Let's go on to player(rules 1/3)

public abstract class Player{

	protected Deck game;
	protected Deck hand = new Deck();
	protected int p;
	protected String symbols;
	
	public Player(Deck c) {
		game = c;
	}
	public abstract void play();
	
	public void newGame() {
		hand.throwCard();
		p = 0;
		symbols = "";
	}
	public Card newCard() {
		Card c = game.takeOffTop();
		hand.onTop(c);
		symbols += c.toSymbol()+" ";
		int aces = 0;
		p = 0;
		for(int i = 0; i<hand.numberDeck(); i++) {
			CardType s = hand.watch(i).type();
			p += s.valor;
			if(s == CardType.ACE)
				aces++;
		}
		for(int j = 1; j<=aces && p>=21; j++)
			p -= 13;
		return c;
	}
	public int point() {
		return p;
	}
}
So now we have created the game and it's rules.

Let's go on with the you(the player)

import static javax.swing.JOptionPane.*;

public class Human  extends Player {
	
	public Human(Deck c) {
		super(c);
	}
	public void play() {
		newGame();
		while(p< 21) {
			newCard();
			if(p< 21) {
				int answer = showConfirmDialog(null,
						"You got : "+symbols+"\nand have "+p+" points."+
						"\nDo you wan another card? ");
			
			if(answer == 1)
				break;
			else if(answer == 2)
				System.exit(0);
		}
		else
			showMessageDialog(null,
					"You got : "+symbols+
					"\nand have "+p+" points.");
		}
	}
}
So our game is starting to close up. Right now we did the 2/3 of the rules.
Let's go on with the last part of rules 3/3.

import static javax.swing.JOptionPane.*;

public class Computer extends Player {
	private Player theplayer;
	
	public Computer(Deck c, Player the) {
		super(c);
		theplayer = the;
	}
	public void play() {
		newGame();
		while(p< 21 && p< theplayer.point())
			newCard();
			showMessageDialog(null, "The computer got: "+symbols+
				"\nand has "+p+" points.");
	}
}
Now we have made the rules and the game itself. HOWEVER, this isn't the game itself it's the rules,cards&icons + simple interface for the game. Now for the game.

import static javax.swing.JOptionPane.*;

public class BlackJack {
	public static void main(String[] arg) {
		Deck thegame = new Deck();
		Human you = new Human(thegame);
		Computer it = new Computer(thegame,you);
		
		while(true) {
			thegame.newGame();
			thegame.shuffle();
			you.play();
			String s = "";
			if(you.point() > 21)
				s = "You lost!";
			else if (you.point() == 21)
				s = "You won!";
			else {
			it.play();
			if(it.point() <= 21 && it.point() >= you.point())
				s = "You lost!";
			else
				s = "You won!";
			}
			int answer = showConfirmDialog(null,s+"\nNew game?");
			if (answer !=0)
				break;
		}
	}
}

Okay, now that's the whole game. First of all we created two enums(enums are thing done to count,this is so far I understand which is !=100% accurate answer). Then we started to create the field of the game, the game(rule), then the player(rule) and computer(rule) finally we created the game itself.
Okay, so now I can't explain everything 100% due to it's very new for me enums and abstract classes. I wish I could better English and to express what every part does. Anyways enjoy my little thing :)
  • 6


#381878 capture video to an AVI file

Posted by Turk4n on 14 September 2008 - 03:06 AM

Then why are you posting here? You should go to hardware section if you are in need for help(with the video capturing card)
  • -1


#381439 Tutorial, File Reader

Posted by Turk4n on 12 September 2008 - 08:24 AM

Hey C.C am back again and gonna post a simple tutorial. So today we can start with java's own file reader. We are going today to copy an existing files content to another file(empty). So let's get going.

import java.io.*;//Here we are importing the in & out put streamer(io)
import javax.swing.*;// importing the swing to get a simple user graphics

public class copyfile {
	
	public static void main(String [] ang)throws IOException {// we are throwing away all exceptions
		
	  
		String in = JOptionPane.showInputDialog(
				"What's the name of the file and its direction? ");//NOTE!, people that uses IDEs in windows environment might have problems locating the "path" to the file even if it it in the same folder as the java file. So if using a texteditor you just type in filename.txt and with an IDE path/filename.txt
		
		BufferedReader instrom = new BufferedReader(new FileReader(in));//the input stream reader

		
		String ut = JOptionPane.showInputDialog(
				"What is the name of the file you want to create and where? ");//same as before you type in where if using IDE and name, but if using a texteditor then you need only to type in the name of file.
		
		PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter(ut)));//the function to "understand" and write in the new file the text or numbers
		
		int radNR = 0;//the function of our reader
		while(true) {//while true it will proceed 
			String rad = instrom.readLine();//reading every string in the file
			if(rad == null) {//if there is nothing it will break and print out into the new file
				break;
			}
				radNR++;
				out.println(radNR + ": " + rad);//here is the new file
				
		}
			out.close();//NOTE, this part is important, since you have to close the streams so you do it here.
			System.exit(0);
	}
}
Well that's it, and sorry for my poor description and English. That's all !
  • 1


#376085 HDD errors... help !

Posted by Turk4n on 23 August 2008 - 06:27 AM

Still floppies rulez :)
  • 1


#361585 Tutorial: Simple digital watch...

Posted by Turk4n on 28 June 2008 - 12:25 PM

Hello again !
Today I thought I would share one of my "best" works. It took along while and was quite hard to understand and how to use it; But I have fixed it. So here is my Digital watch.(Sorry I can't explain everything quite good, am right now 50% understanding what I have done to get the watch work lol...)
*Side note* It won't show updating time, only the time you requested for.(I have still along way to go with boring java.)

So lets begin...
public class showtime {

	private float t, m, s;//We setup instructions
	private boolean showsec = true;//allow to show our box of time

	public void set (float h, float min, float sec) {
		
		if (h>=0 && h<24 && min>=0 && min<60 && sec>=0 && sec <60) {//Counter
			t=h;//defenitions
			m=min;//defenitions
			s=sec;//defenitions
		}
		else
			System.out.println ("Wrong hour");//Error message

	}

	public void setshowsec(boolean show) {//function
		showsec = show;
	}

	public float readh() {//function out of our defenition
		return t;
	}

	public float readMin() {//function out of our defenition
		return m;
	}

	public float readSec() {//function out of our defenition
		return s;

	}

	public String toString () {//Print out time in right order,
		String time = t + ":" + m;
		if (showsec)
			time = t + ":" + m + ":" + s;
		return time;
	}

	public void tick() {
		s = s + 1;

		if (s==60){
			s = 0;
			m = m + 1;
		}
		if (m==60){
			m = 0;
			t = t + 1;
		}
		if (t==24){
			t = 0;

		}

	}

}
This code was made logically and took along time to get right construction. Now due to my ability not to be able to explain how this code works; but still I will try and do my best!

Save it on your folder where you keep your java files or so...

Next step lets fix it so it can sync to our local machines time and get it out. This thing we made was kind of a counter, that will check the time and print out it for us(correct me if am wrong!)

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class whatsthetime {
	public static void main (String[] arg) {
	}
}

class thetime extends JFrame implements ActionListener {//Our time catcher

	private showtime tp = new showtime ();//reading our timer we made
	private JLabel l;

	public thetime () {//new function
		javax.swing.Timer h = new javax.swing.Timer (1000, this); h.start ();
		Calendar c = Calendar.getInstance ();//function of our watch
		tp.set (c.get (Calendar.HOUR_OF_DAY),//what to show 
			c.get (Calendar.MINUTE),//what to show
			c.get (Calendar.SECOND));//what to show

		l = new JLabel (tp.toString(), JLabel.CENTER);//Gui menu
		add (l);
		l.setOpaque(true);//so it can be seen

		l.setBackground (Color.black);//background color
		l.setForeground (Color.white);//foreground color
		l.setFont (new Font("SanSerif", Font.BOLD, 24));//text size
		setSize (200,75);//box size
		setVisible (true);//if it should be visible
		setDefaultCloseOperation (EXIT_ON_CLOSE);//exit on our demand
	}

	public void actionPerformed (ActionEvent e) {//The catcher
		tp.tick();
		l.setText (tp.toString());//the outputer
	}
}

Well this code is our main function to get out the time, now we can use it freely.

Last part, to print out the time "we have catched"...
import java.util.*;
import javax.swing.*;

public class time {
	public static void main (String[] arg) {

		showtime tp = new showtime ();//Will look for our main function
		Calendar c = Calendar.getInstance ();//Look up 
		tp.set (c.get (Calendar.HOUR_OF_DAY),//
				c.get (Calendar.MINUTE),//
				c.get (Calendar.SECOND));//
	JOptionPane.showMessageDialog (null, "The time is " + tp.toString());
	}//Printout our time
}
So here is the whole how to printout the time in java. Myself this was kinda fun and challening. I might say this is my last thing for a while with java. So nowdays I will more of my lovely SICP.

P.S, Sorry if I couldn't explain it for you guys as good I have to :(
Anyways enjoy!
  • -1


#360130 Tutorial paint dots(simple version)

Posted by Turk4n on 19 June 2008 - 07:19 AM

After seen Johns how to use the java paint function. I thought I could share my tutorial how to make one.

So lets begin...
import java.awt.*;

  public class prickar extends java.applet.Applet {

private int mouseX, mouseY;
     private boolean mouseclicked = false;//statment of starting the app

 	  public void init()  {
 		setBackground (Color.white);//Our background for the screen
 	  }
So lets fix the false statment(We had to do this due to you starting it with a click it will automatically enter a dot on the screen...
public boolean mouseDown (Event e, int x, int y ) {//applying the mouse when in use
        mouseX=x; mouseY=y;//coordinations
        mouseclicked = true;//Here is our old false statment fixed when we really want to paint.
        repaint();//Allowing use to paint how many times we want
        return true;//return it to the screen "true"
    }
Lets continue...
public void paint ( Graphics g ) {//To enable painting
        g.setColor (Color.black);//paint color
        if (mouseclicked) {//if you click with whatever mouse button
            g.fillOval (mouseX, mouseY, 12, 12);//The screen will be painted with a ovaldot 12x12 in size
            mouseclicked = false;//This means you havn't clicked it will be false and screen won't be "colored"
        }
    }
Last part...
public void  update  ( Graphics g )  {//Updating your screen so it will be active and the painted things won't disappear
		paint (g);
	}
}

Whole code
import java.awt.*;

  public class dots extends java.applet.Applet {

	private int mouseX, mouseY;
     private boolean mouseclicked = false;

 	  public void init()  {
 		setBackground (Color.white);
 	  }

    public boolean mouseDown (Event e, int x, int y ) {
        mouseX=x; mouseY=y;
        mouseclicked = true;
        repaint();
        return true;
    }

     public void paint ( Graphics g ) {
        g.setColor (Color.black);
        if (mouseclicked) {
            g.fillOval (mouseX, mouseY, 12, 12);
            mouseclicked = false;
        }
    }

    public void  update  ( Graphics g )  {
		paint (g);
	}
}
So thats my tutorial, thanks John for learing me :P
  • 3


#360125 Tutorial simple dice game

Posted by Turk4n on 19 June 2008 - 06:29 AM

Yet again my own tutorial. This time its a dice game I made.(Of course text based :x, Improvment will later, java isn't my thing :P)

So lets begin...
import java.util.*;
public class mathrandom {
	protected static Scanner sc = new Scanner(System.in);//The scanner instead of keyboarder
	
	public static void main(String[] arg) {
	
		
	
		System.out.println("Would you like to throw the dice?" +//Input choice
				"\n1.Yes, please\n2.No thanks");
        int	dice = sc.nextInt();//Operation

Our choices of operation...
if(dice == 1) {//if the input was 1 then
	int nr;//Anything in the range of an integer
	nr = (int)(Math.random()*6+1);//The random selection of number range 1-6
	
	System.out.println("You got number "+nr);//Printout the dice number
	}
	
	else if(dice == 2){//else if your choice was 2 then
	
	System.out.println("What ever ");//Printout this message
	
		}
	else {//else if your choices were not 1 or 2 then it will close it self
		System.exit(0);
	}
	}
}

Now the whole code...
import java.util.*;
public class mathrandom {
	protected static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] arg) {
	
		
	
		System.out.println("Would you like to throw the dice?" +
				"\n1.Yes, please\n2.No thanks");
	
	int	dice = sc.nextInt();

	if(dice == 1) {
	int nr;
	nr = (int)(Math.random()*6+1);
	
	System.out.println("You got number "+nr);
	}
	
	else if(dice == 2){
	
	System.out.println("What ever ");
	
		}
	else {
		System.exit(0);
	}
	}
}

Hope this simple dice game will entertain you guys when you are bored lol.
  • 6


#360120 Tutorial an easy ghostwritter

Posted by Turk4n on 19 June 2008 - 05:56 AM

Hey again, I thought I will give a small tutorial how to make a ghostwritter. I have made this with some help of CC, so it turned out to be awsome.
So lets go on.

First lets build the input reader.
import java.util.*;//Using the scanner, instead of Keyboard reader
public class ghostwritter {

	protected static Scanner sc = new Scanner(System.in);//Setting up scanner
	
	public static void main(String[] arg) throws InterruptedException {
		
		System.out.println("Type in something ");//The input reader

Now we would want to use the input data, so...
String x = sc.nextLine();//This will allow the input data to be used(Note input was a string and will be used as a string)
Now on to the function, we want it to type out letter by letter with a delay. What I call a "ghostwritter"
for(int i=0; i<x.length(); i++) {//A loop thru the entire string
			System.out.print(x.charAt(i));//Catching very "x" one by one
			Thread.sleep(1000);//Put every "x" out it with one second delay(1000ms = 1 s)
		}
	}	
}

So here is the whole code
import java.util.*;
public class ghostwritter {

	protected static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] arg) throws InterruptedException {
		
		System.out.println("Type in something ");
		
			String x = sc.nextLine();

		for(int i=0; i<x.length(); i++) {
			System.out.print(x.charAt(i));
			Thread.sleep(1000);
		}
	}	
}
Hope it will be useful, and again sorry for my weak explaination...
  • 1


#359897 Simple JString reader...

Posted by Turk4n on 17 June 2008 - 08:17 AM

I thought that I could show a thing I worked on this morning. Am pretty new on java and very new too the "java swing" thingy...lol

So here is what I done;

First of all we could start of the menu for input data and output...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jhello extends JFrame implements ActionListener {
	
	JTextField answer = new JTextField(25);//The size of inputscreen
	JLabel welcoming = new JLabel();//The size of the outputscreen

Now we can start on the function and GUI size of the program...
public Jhello () {
		setLayout(new FlowLayout());//Declaring it's an active app
		add(new JLabel("What is your name? ", JLabel.CENTER));//Postion of "questioning"(which is the "What is your name? ")
		add(answer);//functions - Input
		add(welcoming);/functions - Output
		welcoming.setHorizontalAlignment(JLabel.CENTER);//Position of output "answer"
		answer.addActionListener(this);//This catches your input
		setSize(350, 100);//Size of the window
		setVisible(true);//If the text should be visible nor not
		setDefaultCloseOperation(EXIT_ON_CLOSE);//When you close it
        }

Now the last part first is to set out the main function of the whole program...
public static void main (String[] arg) {
		new Jhello();
	}

And the last thing our method!(This is the important part, without this the program won't have a meaning...(I guess)
public void actionPerformed(ActionEvent e) {//The catcher
		String namne = answer.getText();//The one that catch'd it
		welcoming.setText("Welcome " + namne + "!");//Output
	}
}

So now here is the whole code...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jhello extends JFrame implements ActionListener {
	
	JTextField answer = new JTextField(25);
	JLabel welcoming = new JLabel();
	
	public Jhello () {
		setLayout(new FlowLayout());
		add(new JLabel("What is your name? ", JLabel.CENTER));
		add(answer);
		add(welcoming);
		welcoming.setHorizontalAlignment(JLabel.CENTER);
		answer.addActionListener(this);
		setSize(350, 100);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public static void main (String[] arg) {
		new Jhello();
	}
	public void actionPerformed(ActionEvent e) {
		String namne = answer.getText();
		welcoming.setText("Welcome " + namne + "!");
	}
}

I hope this will be useful for people and am truly sorry if I couldn't explain in a better way. I understand thing on my own way and normally can't explain it :(
So thats from me, cheers!
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download