The following Java basic programs will be very helpful for the beginners. The java basic programs is an easy to understand copy paste code! Enjoy learning java.
Java Sample Program: 1hello1.java
/* this is a `comment. you can tell because the lines start with `//. these lines are all ignored by the java compiler, so you can write whatever you like. at the very least you should start with a comment saying what the program does
note that this program has far more comments than real program statements; this would probably not be the case in most real programs.
The first real (i.e., non-comment) line of the program is `import this tells the compiler what classes to use apart from any defined in this program. in this case i tell the compiler to consider using any of the classes that are part of the `package java.awt.
and the class `appleti have to do that because the program uses the class `applet to do most of the work, and this class is defined in the java.applet package and the java.awt package. most java programs will start with an `import statement */
import java.applet.applet;
import java.awt.*;
/* so now down to work. define a class. all java programs have at least one class. in this case we are writing an applet, so the new class is a type of applet. `extends means, essentially, `is a type of. note that java rules stipulate that a `public class must be defined in a file of the same name, i.e., hello1 must be defined in `hello1.java */
public class hello1 extends applet
/** the open brace below denotes that all the statements that follow are part of the class hello1, until the matching closing brace at the end of the program **/
{
/**now we define an operation called `paint. providing this operation ensures that when the program starts up, something useful will happen. if we do not provide a `paint operation, then the program will compile and run correctly, but it wont display anything at all. the concept of an `operation will be covered in more detail later in the course**/
public void paint (graphics g)
{
** now the program text is `indented, this is, all the lines start a few spaces from the left margin. doing this makes it easy for the (human) reader to identify all the lines that are part of `paint. the computer does not care about this, but people will find the program easier to understand
`drawstring is an operation in the class called graphics. its job is simply to display text in an area of the screen. in this case the text is positioned 20 pixels across, and 20 pixels down**/
g.drawstring ("welcome to int4120", 20, 20);
}
/** this final brace denotes the end of the class `hello1 and, in this case, the end of the program **/
}
Java Sample Program: 2add1.java/*A program that adds 2 and 2 like most programs that use applets, we need the following two lines to tell the compiler what an applet is and how it works. */
import java.applet.applet;
import java.awt.*;
/* as in all java programs we have to define a class. i am calling this class `add1, so this program must be in a file called `add1.java */
public class add1 extends applet
{
public void paint (graphics g)
{
// ``int is short for `integer, meaning a whole number.
// so this line defines a thing called ``result which stores
// the result of adding 2 and 2.
int result = 2 + 2;
// the next line displays the text ``2+2= on the screen
g.drawstring ("2+2=", 20, 20);
// this line displays the result of the addition on the
// next line.
g.drawstring (integer.tostring(result), 20, 40);
}
}
Java Sample Program: 3add1_1.java// A program that adds 2 and 2
import java.applet.applet;
import java.awt.*;
public class add1_1 extends applet
{
public void paint (graphics g)
{
int fred = 2 + 2;
g.drawstring ("2+2=", 20, 20);
g.drawstring (integer.tostring(fred), 20, 40);
}
}