Java is the future !: Source code
Showing posts with label Source code. Show all posts
Showing posts with label Source code. Show all posts

Friday, October 8, 2010

Date class in Java

Date is a Java class that can represent a timestamp.

The example shown below will give you a good idea of how it works :



%tc = Complete date and time
%tr = just the time
%tA = Day of the week
%tB = Month
%td = day

< is just another flag in the format specifier that tells the formatter to "use the previous argument again." It saves you from retyping the arguments.

Check out the API for Date class. Click here to see.

Saturday, August 14, 2010

Chapter 2: Positioning text version pt.1

Just as we did with the previous program it displays as label. It is identical to the previous one with a single exception that the TextOne object is displayed instead of the Label object.

What is a TextOne object?

It's not part of the standard library like the label was. This is a class of our own. A Canvas object is a displayable component just like a label except the Canvas object doesn't do anything but provide a blank window and permission to draw graphics of our own. So the TextOne class extends the Canvas class and adds a method to draw a single line of text.

The paint method is called by java whenever TextOne object needs to be displayed. Whenever the TextOne window first appears the paint method is called to draw the content of the window. (Paint method is called again if any changes happens like size change etc to redraw the window)

Graphics object is passed to paint method.

Setsize method specifies the original size of the window.




Chapter 1: Adding exits to Howdy Window

In the previous program the window that we made was not closing, to overcome that problem we have to add some new methods to our class. Here's the source code.




Chapter 1: The source code of a Howdy Window

Monday, August 9, 2010

GuessGame Program

Summary: 

The guessing game involves a 'game' object and three 'player' objects. The game generates a random number between 0 and 9 and the three player objects try to guess it.

Classes:

GuessGame.class   Player.class   GameLauncher.class

Logic:

1) The GameLauncher class is where the application starts; it has the main() method.
2) In the main() method, a GuessGame object is created, and its startGame() method is called.
3) The GuessGame object's startGame() method is where the entire game plays out. It creates three players, then "thinks" of a random number (the target for the players to guess). It then asks each player to guess, checks the result, and either prints out information about the winning player(s) or asks them to guess again.

You can download the source code of the program from the link given below.

Sunday, July 25, 2010

PhraseOMatic (A cool program)

public class PhraseOMatic{
public static void main(String [] args ) {


String[] stat1={"matt","john","harry","mary"};
String[] stat2={"is getting","works like","is"};
String[] stat3={"mad","crazy","smart"};

//size of each array
int len1=stat1.length;
int len2=stat2.length;
int len3=stat3.length;

//pick a random word from each statement

int rand1= (int) (Math.random() * len1);
int rand2= (int) (Math.random() * len2);
int rand3= (int) (Math.random() * len3);

//make a phrase

String phrase = stat1[rand1]+ " " + stat2[rand2] + " "
+ stat3[rand3];

//print
System.out.print("Statement: " + phrase);

}
}

Saturday, July 24, 2010

99 bottles of beer

Again a universal program for looping.

About:

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on Scout and/or Girl Guide outings. The song is derived from the English "Ten Green Bottles".

The lyrics are simple:

Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer.
Take one down, pass it around, Ninety-eight bottles of beer on the wall.
The same verse is repeated, each time with one less bottle. The song is completed when the singer or singers reach zero.

So I made the program for this.

Source code:

public class HelloWorld{
public static void main (String[] args) {

int beerNum =99;
String word = "bottles";
while (beerNum > 0) {
if(beerNum==1) {
word = "bottle";}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println(beerNum + " " + word + " of beer");
System.out.println("Take one down.");
System.out.println( "Pass it around.") ;
beerNum = beerNum - 1;

}if (beerNum == 0) {
System.out.println("No more bottles of beer on the wall");
}
}
}