6.Introduction to Java Programs
Saturday, August 15, 2009
In this module we are going to learn how to write different kinds of java programs and the difference between primitive and reference types.
Statements instruct the computer to perform actions, so java programs all about statements. A sequence of characters in double quotes are called a string, a character string , a message or a string literal.
System.out , the standered output object which allows java programs to display characters in the command window.Every Java statement ends with a semicolon called statement terminator.
To compile a program with the command javac. If the program contains no syntax errors; a class file containing the Java Bytecodes that represent the program is created. These bytecodes are interpreted by the JVM when we execute the program.
So, have a look at the fallowing examples to understand more about Java Language
In the above example the first statement uses System.out’s method print to display a string.
Unlike println, after displaying its argument; print does not position the output cursor at the beginning of the next line in the command window.
ESCAPE SEQUENCES
In order to navigate the cursor on different parts of the output window we can use several escape sequences, some of them are listed below.
\' single quote
\" double quote
\\ backslash character
\b backspace
\f next page
\n start a new line
\r carriage return
\t move to next tab setting
\0 null character marking the end of a string
Using printf in Java
A new feture of J2SE 5.0 provides the System.out.printf method for displaying for displaying formatted output, the f in the name printf stands for “formatted”.
Have a look at the fallowing example to know more about printf.
In the above example printf’s first argument is a format string it consists of text and “format specifiers”.
Here %s is a format specifier which represents a string.
Java supports the fallowing format specifiers.
Have look at the fallowing example, which demonstrates more about format specifiers
The output of the above example is
10
26
1b
1B
12345678.900000
12345678.90
a
JavaCircuit
JAVACIRCUIT
Another Java Program which demonstrates the usage of format specifiers
If you execute the above program, you will get the fallowing output
Student Number : 111
Student Name : Hrithik
Subject I : 45
Subject II : 60
Total Marks : 105
Average Marks : 52.50
Taking Input from the keyboard
Scanner class enables a program to read data for use in a program. The data can come from many sources, such as a file on a disk or the user at the keyboard. Before using Scanner class, the program must create it and specify the source of the data.
The expression new Scanner(System.in) create a Scanner that reads from the keyboard. The standard input object System. in enables Java Programs to read data typed by the user.
In order to use Scanner class we must import java.util package. Scanner class provides various methods to read several kinds of data like character, int, float, long, double, String etc.
Have a look at this fallowing example to know more about Scanner class
When we compile this program, you need to enter the values for a and b at run time then you will get the output for the sum of two numbers.
Enter the value for A :
10
Enter the value for B :
20
The Sum of A and B : 30
Have a look at another program which demonstrates various methods in Scanner class
If you compile this program, you can see the fallowing output.
Enter Student Number :
111
Enter Student Name :
Hrithik
Enter Subject-I Marks :
45
Enter Subject-II Marks :
92
Enter Course Fee :
2500.45
The Student Details are.....
Student No : 111
Student Name : Hrithik
Subject-I : 45
Subject-II :92
Total Marks : 137
Avearge Marks : 68.5
Course Fee :2500.45
In the next chapter we are going to focus more on classes, objects and constructors to write efficient object oriented programs.
Statements instruct the computer to perform actions, so java programs all about statements. A sequence of characters in double quotes are called a string, a character string , a message or a string literal.
System.out , the standered output object which allows java programs to display characters in the command window.Every Java statement ends with a semicolon called statement terminator.
To compile a program with the command javac. If the program contains no syntax errors; a class file containing the Java Bytecodes that represent the program is created. These bytecodes are interpreted by the JVM when we execute the program.
So, have a look at the fallowing examples to understand more about Java Language
// Dispalying a single line of text with multiple statements
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Welcome to");
System.out.println("Java Circuit");
}
}
In the above example the first statement uses System.out’s method print to display a string.
Unlike println, after displaying its argument; print does not position the output cursor at the beginning of the next line in the command window.
ESCAPE SEQUENCES
In order to navigate the cursor on different parts of the output window we can use several escape sequences, some of them are listed below.
\' single quote
\" double quote
\\ backslash character
\b backspace
\f next page
\n start a new line
\r carriage return
\t move to next tab setting
\0 null character marking the end of a string
Using printf in Java
A new feture of J2SE 5.0 provides the System.out.printf method for displaying for displaying formatted output, the f in the name printf stands for “formatted”.
Have a look at the fallowing example to know more about printf.
// Using printf method to diaplay the output
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.printf("%s\t%s\n", "Hai !","Welcome to JavaCircuit");
}
}
In the above example printf’s first argument is a format string it consists of text and “format specifiers”.
Here %s is a format specifier which represents a string.
Java supports the fallowing format specifiers.
Have look at the fallowing example, which demonstrates more about format specifiers
// Using Format-Specifiers in Java
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.printf("%d\n", 10);
System.out.printf("%o\n", 22);
System.out.printf("%x\n", 27);
System.out.printf("%X\n", 27);
System.out.printf("%f\n", 12345678.9);
System.out.printf("%.2f\n", 12345678.9);
char c='a';
System.out.printf("%c\n", c);
String st="JavaCircuit";
System.out.printf("%s\n", st);
System.out.printf("%S\n", st);
}
}
The output of the above example is
10
26
1b
1B
12345678.900000
12345678.90
a
JavaCircuit
JAVACIRCUIT
Another Java Program which demonstrates the usage of format specifiers
// Using Format-Specifiers in Java
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int sno,sub1,sub2,total;
String sname;
float avg;
sno=111;
sub1=45;
sub2=60;
sname="Hrithik";
total=sub1+sub2;
avg=(float) total/2;
System.out.printf("Student Number : %d\n", sno);
System.out.printf("Student Name : %s\n", sname);
System.out.printf("Subject I : %d\n", sub1);
System.out.printf("Subject II : %d\n", sub2);
System.out.printf("Total Marks : %d\n", total);
System.out.printf("Average Marks : %.2f",avg);
}
}
If you execute the above program, you will get the fallowing output
Student Number : 111
Student Name : Hrithik
Subject I : 45
Subject II : 60
Total Marks : 105
Average Marks : 52.50
Taking Input from the keyboard
Scanner class enables a program to read data for use in a program. The data can come from many sources, such as a file on a disk or the user at the keyboard. Before using Scanner class, the program must create it and specify the source of the data.
The expression new Scanner(System.in) create a Scanner that reads from the keyboard. The standard input object System. in enables Java Programs to read data typed by the user.
In order to use Scanner class we must import java.util package. Scanner class provides various methods to read several kinds of data like character, int, float, long, double, String etc.
Have a look at this fallowing example to know more about Scanner class
import java.util.Scanner; // importing Scanner class
// Addition of two numbers using Scanner class
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s=new Scanner(System.in);
int a,b,c;
System.out.print("Enter the value for A :");
a=s.nextInt(); // This method is used to read integer values
System.out.print("Enter the value for B :");
b=s.nextInt();
c=a+b;
System.out.printf("The Sum of A and B : %d",c);
}
}
When we compile this program, you need to enter the values for a and b at run time then you will get the output for the sum of two numbers.
Enter the value for A :
10
Enter the value for B :
20
The Sum of A and B : 30
Have a look at another program which demonstrates various methods in Scanner class
import java.util.Scanner; // importing Scanner class
// Taking different kind of input like String and float
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s=new Scanner(System.in);
int sno,sub1,sub2,total;
String sname;
double avg,fee;
System.out.print("Enter Student Number : ");
sno=s.nextInt();
System.out.print("Enter Student Name : ");
sname=s.next(); // This method is used to read Strings
System.out.print("Enter Subject-I Marks :");
sub1=s.nextInt();
System.out.print("Enter Subject-II Marks :");
sub2=s.nextInt();
System.out.print("Enter Course Fee :");
fee=s.nextDouble(); // This method is used to read double
total=sub1+sub2;
avg=(double)total/2;
System.out.println("The Student Details are.....\n");
System.out.println("Student No : "+sno);
System.out.println("Student Name : "+sname);
System.out.println("Subject-I : "+sub1);
System.out.println("Subject-II :"+sub2);
System.out.println("Total Marks : "+total);
System.out.println("Avearge Marks : "+avg);
System.out.println("Course Fee :"+fee);
}
}
If you compile this program, you can see the fallowing output.
Enter Student Number :
111
Enter Student Name :
Hrithik
Enter Subject-I Marks :
45
Enter Subject-II Marks :
92
Enter Course Fee :
2500.45
The Student Details are.....
Student No : 111
Student Name : Hrithik
Subject-I : 45
Subject-II :92
Total Marks : 137
Avearge Marks : 68.5
Course Fee :2500.45
In the next chapter we are going to focus more on classes, objects and constructors to write efficient object oriented programs.
Labels:
Java Programs