Write A Program To Print A Hello World in Java
Write A Program To Print A Hello World in Java
1
21 Write a program to execute the
random file accesses.
2
1. Write a program to print a Hello World in Java.
Code:
public class Main {
public static void main (String[] args){
System.out.println("Hello World");
}
}
OUTPUT
3
2. write a program to execute two numbers of sum in Java.
CODE:
public class Main {
public static void main(String[] args){
int x = 10;
int y = 20;
int s = x + y;
System.out.println(“enter the First Number = "+ x);
System.out.println(“enter the Second Number =” + y);
System.out.println("The sum of two Number =" + s);
}
}
OUTPUT
4
3. write a program to execute an if statement in Java.
CODE:
public class X {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
OUTPUT
5
4. Check whether a number is even or odd using the if...else
statement.
CODE:
OUTPUT
6
5. Write a program to execute an if-nested statement.
CODE:
public class Test {
if( x == 30 ) {
if( y == 10 ) {
System.out.print("X = 30 and Y = 10");
}
}
}
}
OUTPUT
7
6. Write a program to calculate the grade of students.
CODE:
import java.util.Scanner;
public class Grade
{
public static void main(String[] args)
{
int score;
char grade;
Scanner console = new Scanner(System.in);
System.out.print("Enter your numeric test score : ");
score = console.nextInt();
grade = 'F';
8
}
System.out.println("Your grade is " + grade);
}
}
OUTPUT
9
CODE:
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
10
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
OUTPUT
11
8. Write a program to print numbers from 1 to 10 form using for
loop.
CODE:
class PrintNumbers
{
public static void main(String[] args)
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}
OUTPUT
II)This program will try to print “Hello World” 5 times. For using
a while loop.
CODE:
class whileLoopDemo {
public static void main(String args[])
12
{
int i = 1;
while (i < 6) {
System.out.println("Hello World");
i++;
}
}
}
OUTPUT
int i=1;
do{
System.out.println(i);
i++;
}
while(i<=10);
13
}
}
OUTPUT
14
9. write a program to create a half-pyramid pattern using nested
loops
CODE:
class Main {
public static void main(String[] args) {
int rows = 5;
// outer loop
for (int i = 1; i <= rows; ++i) {
OUTPUT
15
10. Write a program to excute ALL Methods of string and all
methods of buffer string.
CODE:
public class string {
public static void main(String[] args) {
String quote = "To be or not to be";
16
System.out.println(quote.indexOf("be")); //index of text
System.out.println(quote.indexOf("r")); //index of character
}
}
OUTPUT
CODE:
public class string {
public static void main(String[] args) {
String quote = "THOR: Love and Thunder";
System.out.println(quote.toLowerCase());
17
System.out.println(quote.toUpperCase());
}
}
OUTPUT
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");
System.out.println(sb);
sb.insert(1,"Java");
System.out.println(sb);
}
}
OUTPUT
18
2.StringBuffer replace() Method AND delete() Method
CODE
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
}
}
OUTPUT
19
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
OUTPUT
20
CODE:
class StringBufferExample7{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34
i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}
OUTPUT
21
11. Write a program to calculate to Add Elements to Vector.
CODE:
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> mammals= new Vector<>();
mammals.add("Dog");
mammals.add("Horse");
mammals.add(2, "Cat");
System.out.println("Vector: " + mammals);
animals.addAll(mammals);
System.out.println("New Vector: " + animals);
}
}
22
OUTPUT
CODE:
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[])
{
Box mybox1 = new Box(Box mybox2 = new Box();
double vol;
variables mybox1.width = 10;
mybox1.height =20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol=mybox1.width*mybox1.height*mybox1.depth;
System.out.println("Volume is " + vol);
23
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}
OUTPUT
24
13. which has two methods that add numbers of different types
using function overloading.
CODE:
public class Main {
static int plusMethodInt(int x, int y) {
return x + y;
}
25
14 write a program, to execute an inheritance and types of
inheritances.
SIMPLE INHERITANCES
CODE:
class FundamentalForce {
void Force() {
System.out.println("There are four fundamental forces.");
}
}
class Gravitational extends FundamentalForce {
void Gravity() {
System.out.println("Fruits fall to the ground due to
gravitational Force.");
}
}
class SingleInheritance {
public static void main(String[] args) {
Gravitational G = new Gravitational();
G.Force();
G.Gravity();
26
}
}
OUTPUT
Multi-level Inheritance
CODE:
class NuclearForce extends FundamentalForce {
void Nuclear() {
System.out.println("Nuclear Forces are of two types;");
System.out.println("Strong Nuclear Force");
System.out.println("Weak Nuclear Force");
}
}
class MultilevelInheritance {
public static void main(String[] args) {
27
StrongNuclearForce st = new StrongNuclearForce();
st.Force();
st.Nuclear();
st.Strong();
}
}
OUTPUT
Hierarchical Inheritance
CODE:
class FundamentalForce {
void Force() {
System.out.println("There are four fundamental forces.");
}
}
28
class Gravitational extends FundamentalForce {
void Gravity() {
System.out.println("Fruits fall to the ground due to
gravitational Force.");
}
}
29
OUTPUT
class StaticImportDemo
{
public static void main(String args[])
{
OUTPUT
USER-DEFINED PACKAGES
30
CODE:
package tulsi;
int data=100/0;
}
catch(ArithmeticException e){System.out.println(e);}
OUTPUT
31
16. i) Write a Java program to sum the values of an array.
CODE:
OUTPUT
32
ii) write a program to execute the student of details using the
interface.
CODE:
interface ShowDetails
{
@Override
OUTPUT
33
17. how a run-time system searches for appropriate exception
handling code on the call stack
CODE:
class GFG {
int i = a / b;
return i;
}
int res = 0;
try {
34
catch (NumberFormatException ex) {
System.out.println(
"NumberFormatException is occurred");
}
return res;
}
int a = 1;
int b = 0;
try {
int i = computeDivision(a, b);
}
System.out.println(ex.getMessage());
}
}
}
OUTPUT
35
18. write a program to create a file by using iostream.
CODE:
import java.io.File;
import java.io.IOException;
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("students.txt");
if (myObj.createNewFile()) {
System.out.println("Created Successfully: " +
myObj.getName());
} else {
System.out.println("Sorry, File Exists.");
}
} catch (IOException e) {
36
System.out.println("Error.....");
e.printStackTrace();
}
}
}
OUTPUT
19. Write a program to create the file and read and write the file.
CODE:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
37
.close();
} catch (FileNotFoundException e) {
System.out.println("Error.....");
e.printStackTrace();
}
}
}
OUTPUT
38
file.write("This file has information of Teachers");
file.write("Name: Rosy Fernandes, Age: 36");
file.write("Name: Isha Mheta, Age: 29");
file.close();
System.out.println("File has been written into.");
} catch (IOException e) {
System.out.println("Error......");
e.printStackTrace();
}
}
}
OUTPUT
39
20. write a Java program to merge the content of two files into a
third file. The user must receive the name of all three files at the
run-time of the program.
CODE:
import java.io.*;
40
String line1 = br1.readLine();
String line2 = br2.readLine();
while (line1 != null || line2 !=null)
{
if(line1 != null)
{
pw.println(line1);
line1 = br1.readLine();
}
if(line2 != null)
{
pw.println(line2);
line2 = br2.readLine();
}
}
pw.flush();
br1.close();
br2.close();
pw.close();
}
}
FILE A.TXT
41
B.TXT
OUTPUT
C.TXT
42
21. Write a program to execute the random file accesses.
import java.io.IOException;
import java.io.RandomAccessFile;
43
}
private static byte[] readFromFile(String filePath, int position, int
size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath,
"r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
}
44
OUTPUT
CODE:
t1.start();
}
}
OUTPUT
45
46