Russell Havik's Web, where all things started :)
 
home | for fun | graphics | me | portfolio | links | CELTICS.COM | CELTICS SCHEDULES & RESULTS
home / for fun / my java / basic
 
 
Below links are designed for those who doesn't have Flash Plugins installed :)
 
Articles | My Celtics | My Java | My Prayers | Time to Brag | My win32 Applications
 

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.
class Rectangle
{
double width,
height;

Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}

Rectangle()
{
this (0.0,0.0);
}

void setWidth(double width)
{
this.width = width;
}

void setHeight(double height)
{
this.height = height;
}

double getWidth()
{
return width;
}

double getHeight()
{
return height;
}


double Area()
{
return width * height;
}

double Perimeter()
{
return (2*height) + (2*width);
}

double ToHaveFun()
{
return ((((2*height) + (2*width) / height) * width) - 32);
}

double ToHaveFun2()
{
return ((((2*24) + (2*width) / height) - width) * 32);
}

};

------------------------
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

switch (multiplier)
{
case 1 :
tableName = "one";
break;
case 2 :
tableName = "two";
break;
default :
tableName = "unknown";
break;
}


System.out.println("You entered " + multiplier);
System.out.println("The " + tableName + " times table is ");

for (value=1; value <= 12; value++)
{
System.out.print (value + "\t");
}

System.out.print ("\n");

for (value=1; value <= 12; value++)
{
System.out.print (value * multiplier + "\t");
}

};

}
};

 
 
 
 
+------------+
ICQ-93893008
[View] / [Sign]
G-Book 1
[View] / [Sign]
G-Book 2
 
ChangingLINKS
owned by russ
 
 
all.at/russellhavik
I got it from
v3.com
 
 
hosted for free by Geocities.com
 
link, link, link
 
russ' blog
 
css 2 official!!
home | for fun | graphics | me | portfolio | links © Celticslopedia - Russell Havik, 1998 - 2005
 
my blogs
Hosted by www.Geocities.ws

1