my JAVA tutorial Please note that coding were created by me, if you want to use them, please state a little credit for me :-)
Most important. please use them for learning purpose only. No cheating and copying code for cut and paste, lots of practice will help to improve your knowledge...remember, no lemons no melons...
Please note: after you copy the codes below, save the file as file_name.JAVA and make sure the CLASS name is EXACTLY the same as the filename that you saved before...
Example: MyFirstJava.JAVA---contains class MyFirstJava{......}; easy peasy...:-)
The 'tab rules' sucked badly when it comes to html, so don't be shock to see every codes are on the same left side..sowie...
basic,
simple thing that would type "HELLO JAVA WORLD".
/*
Author: Russell Havik
PR507 - Lab 1
July 22, 2003. Tuesday
Last modified: July 24, 2003. Thursday
*/
class MyFirstProgram
{
public static void main(String s[]) // change the S
to s
{
System.out.println("Hello JAVA World");
}
};
same
as above, only u need to put it as an argument.
/*
Author: Russell Havik
PR507 - Lab 2
July 24, 2003. Thursday
*/
class MySecondProgram
{
public static void main(String s[])
{
if(s.length < 1)
System.out.println("ERRROR :- Must supply at least
one argument");
else
System.out.println(s[0]);
}
};
// this program will compile but
won't run, unless
// we press tools - run and fill in
// Command: Java
// Parameters: MySecondProgram hello there 1100
// enter!
// 'hello' as argument no.0
// 'there' as argument no.1
// '1100' as argument no.2
converting
String into int and do some calculation to check 'em - note:also
need to put in as an argument.
/*
Author: Russell Havik
PR507 - Lab 2
July 24, 2003. Thursday
*/
class Number
{
public static void main(String s[])
{
int x;
if(s.length==0)
System.out.println("No number supplies - must supply
one argument");
else
{
Integer I = new Integer(s[0]);
x = I.intValue() ; // data conversion from string to
int
System.out.println("twice the value of " +
x + " is " + 2*x);
};
}
};
Shapes.java
contains the main which will add the int into the variable,
and pass the variable into Rectangle.java to calculate its area
and its perimeter, same goes for square.java - received the
variable from shapes.java and calculate its area.
public class Shapes
{
public static void main(String s[])
{
Rectangle R1 = new Rectangle (2.0,4.0);
System.out.println ("The width is " + R1.getWidth(),
toString());
System.out.println ("The height is " + R1.getHeight());
System.out.println ();
System.out.println ("The area is " + R1.Area());
System.out.println ("And the perimeter is "
+ R1.Perimeter());
System.out.println ("For fun " + R1.ToHaveFun());
System.out.println ("For fun v2 " + R1.ToHaveFun2());
System.out.println ();
System.out.println ();
Rectangle R2 = new Rectangle (8.0,14.0);
System.out.println ("The width is " + R1.getWidth());
System.out.println ("The height is " + R1.getHeight());
System.out.println ();
System.out.println ("The area is " + R2.Area());
Square S1 = new Square (4.0);
S1.Area();
//System.out.println ("The sides of this square
is " + S1.getSides());
//System.out.println ("The area of this square
is " + S1.Area());
}
};
------------------------
class Square extends Rectangle
{
Square (double side)
{
super (side, side);
}
Square ()
{
super(); // return value to 0
}
double Area()
{
System.out.println ("The area of this square is
" + width*height);
return -1.0;
}
};
will
receive a string, then convert them into int, using "if"
statement to decide whether the temperature entered is hot or
cold :-p...
/*
Author: Russell Havik
PR507 - Lab 3
July 29, 2003. Tuesday
Program Name Temperature
*/
class Temperature
{
public static void main(String s[])
{
double dblTemp;
if(s.length==0)
System.out.println("No data entered - must supply
at least one argument");
else
{
Double I = new Double(s[0]);
dblTemp = I.doubleValue() ; // data conversion from
string to int
if (dblTemp < 10)
{
System.out.println("You entered " + dblTemp
+ " degress Celcius, must be cold");
}
else
{
System.out.println("Today's temperature is "
+ dblTemp + " Celcius");
}
};
}
};
as what
it said, timetable for number entered as an argument and will
calculate its timestable until 12.
/*
Author: Russell Havik
PR507 - Lab 4
July 31, 2003. Thursday
Program Name TimesTable
*/
class TimesTable
{
public static void main(String s[])
{
int multiplier,value;
String tableName;
if(s.length==0)
{
System.out.println("No data entered - must supply
one argument");
}
else
{
Integer I = new Integer(s[0]);
multiplier = I.intValue() ; // data conversion from
string to int