Java Code Examples
BASIC PROGRAM OF JAVA
Demonstrate of Basics of java programming and implementation of Scanner class
1. Introduction to JDK (Java development kit)
Introduction to JDK (Java Development Kit)
The JDK (Java Development Kit) is a software development kit used by Java developers to build, compile, debug, and run Java applications. It includes essential tools and libraries necessary for Java development. Here's a brief overview of its components:
Javac (Java Compiler):
Javac is the Java compiler that translates Java source code (.java files) into bytecode (.class files) that can be executed by the Java Virtual Machine (JVM).
JVM (Java Virtual Machine):
JVM is an essential part of the JDK. It executes Java bytecode on various platforms, providing platform independence for Java applications.
Java Runtime Environment (JRE):
The JRE is included in the JDK and provides the libraries, resources, and environment necessary for running Java applications.
Java API Libraries:
JDK includes a rich set of libraries (such as java.lang, java.util, etc.) that provide predefined functionality to developers, simplifying the development process.
Debugging Tools:
JDK includes debugging tools like jdb (Java Debugger) for debugging Java applications.
Setting Up JDK and Environment Variables
To use the JDK effectively, you need to set up environment variables on your operating system.
2. WAP to print "Welcome to Java".
public class prac_1_2 {
public static void main(String[] args) {
System.out.println("Welcome to Java");
}
}
3. WAP to print your address i) using single print ii) using multiple println.
public class prac_1_3 {
public static void main(String[] args) {
// Using multiple println statements
System.out.println("xyz society");
System.out.println("street no 2");
System.out.println("xyz-360***");
// Using single print statement
// System.out.print("xyz society, ");
// System.out.print("street no 2, ");
// System.out.println("xyz-360***");
}
}
4. WAP that reads a number in meters, converts it to feet, and displays the result.
import java.util.Scanner;
public class prac_1_5 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
double ans, meter;
System.out.println("Enter meter:");
meter = SC.nextDouble();
ans = meter * 3.28;
System.out.println("Feet: " + ans);
}
}
5. WAP to print addition of 2 numbers using command line arguments (with Integer.parseInt()).
public class prac_1_6 {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println(a + b);
}
}
6. WAP to convert temperature from Fahrenheit to Celsius.
import java.util.Scanner;
public class prac_1_7 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
double ans, fah;
System.out.println("Enter temp (Fahrenheit):");
fah = SC.nextDouble();
ans = (fah - 32) * 5 / 9;
System.out.println("Celsius: " + ans);
}
}
7. WAP to calculate the area of a circle.
import java.util.Scanner;
public class prac_1_8 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
double ans, radius;
System.out.println("Enter radius:");
radius = SC.nextDouble();
ans = 3.14 * radius * radius;
System.out.println("Area of circle: " + ans);
}
}
Comments
Post a Comment