java_program-1_250523_122444
java_program-1_250523_122444
Note: to compile code use javac filename.java after that to execute the code use
java filename
1.Java program to display “Hello World” and display the size of all the data types.
class program1 {
System.out.println("Hello World!");
Output:
2. Java program to implement the usage of static,local and global variables.
class program2 {
example.displayVariables();
Output:
class program3
{
public static void main(String[] args)
Output:
import java.util.Scanner;
class program4 {
int max;
max = num1; // num1 is greater than or equal to both num2 and num3
max = num2; // num2 is greater than or equal to both num1 and num3
} else {
scanner.close();
Output:
import java.util.Scanner;
if (num % 2 == 0) {
} else {
scanner.close();
output:
class MyClass {
int number;
String text;
public MyClass() {
class program6 {
Output:
7.Java program to implement an array of objects.
class Student {
int id;
String name;
this.id = id;
this.name = name;
class program7 {
student.display();
Output:
8. Java program to implement Single Inheritance.
class Animal {
System.out.println("Animal is eating");
System.out.println("Dog is barking");
class program8 {
myDog.eat();
myDog.bark();
Output:
9.Java program to implement Multiple Inheritance using Interface.
interface Walkable{
void walk();
interface Swimmable{
void swim();
System.out.println("Duck is walking.");
System.out.println("Duck is swimming.");
}
}
Output:
Step1: typr the java code and save it as program10.java and comile it
import java.applet.Applet;
import java.awt.Graphics;
@Override
<html>
<body>
height=400>
</applet>
</body>
</html>
Output:
class program11 {
int denominator = 0;
try {
} catch (ArithmeticException e) {
Output:
12. Java program to add two integers and two float numbers. When no arguments are
supplied give a default value to calculate the sum. Use method overloading.
class program12 {
return a + b;
return a + b;
Output:
class Animal {
@Override
System.out.println("Dog barks");
}
@Override
System.out.println("Cat meows");
Animal myAnimal;
Output:
14. Java program to catch negative array size Exception. This exception is caused when the
array is initialized to negative values.
class program14 {
try {
} catch (NegativeArraySizeException e) {
System.out.println("Continuing execution...");
Output:
import java.util.Scanner;
class program15{
int reversedNum = 0;
while (num != 0) {
num /= 10;
if (originalNum == reversedNum) {
} else {
scanner.close();
Output:
@Override
public void run() {
System.out.println("Creating Thread...");
//Runnable instance to it
System.out.println("Starting Thread...");
thread.start();
Output: